Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 88b878b4ff | |||
| 0caeec996e | |||
| e319e2db05 | |||
| 9c2d569308 |
@@ -3,3 +3,7 @@
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
.vscode/
|
||||
.vs/
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"name": "Arduino-GSM-Chata",
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"settings": {
|
||||
"files.associations": {
|
||||
"pinsdefinitions": "c",
|
||||
"new": "cpp"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "GSMMobileData.h"
|
||||
#include "EEPROMUtils.h"
|
||||
#include "DeviceBase.h"
|
||||
#include "DebugLog.h"
|
||||
|
||||
|
||||
GSMMobileData::GSMMobileData(String id) : DeviceBase(id)
|
||||
{
|
||||
@@ -15,10 +15,6 @@ void GSMMobileData::readAllData(String id)
|
||||
EEPROMUtils eepromUtils = EEPROMUtils();
|
||||
MobileNumber = eepromUtils.readString(1, 20);
|
||||
Sms = eepromUtils.readString(21, 100);
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Load from EEPROM. Mobile Number: " + MobileNumber + " SMS: " + Sms);
|
||||
#endif
|
||||
}
|
||||
|
||||
void GSMMobileData::updateMobileNumber(const String &newMobileNumber)
|
||||
@@ -33,9 +29,6 @@ void GSMMobileData::updateMobileNumber(const String &newMobileNumber)
|
||||
EEPROMUtils eepromUtils = EEPROMUtils();
|
||||
MobileNumber = newMobileNumber;
|
||||
eepromUtils.writeString(1, newMobileNumber, 20);
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Update Mobile Number: " + newMobileNumber);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,8 +43,5 @@ void GSMMobileData::updateSMS(const String &newSMS)
|
||||
EEPROMUtils eepromUtils = EEPROMUtils();
|
||||
Sms = newSMS;
|
||||
eepromUtils.writeString(21, newSMS, 100);
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Update sms: " + newSMS);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
#include "PinTypeIn.h"
|
||||
|
||||
|
||||
const bool PinTypeIn::IsPinHigh()
|
||||
|
||||
bool PinTypeIn::IsPinHigh()
|
||||
{
|
||||
return digitalRead(_pin) == HIGH ? true : false;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
|
||||
/// @brief Get Actual Pin Value if is SET = True, if is NOT SET = False
|
||||
/// @return
|
||||
const bool IsPinHigh();
|
||||
bool IsPinHigh();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "PinTypeOut.h"
|
||||
#include <Arduino.h>
|
||||
#include "DebugLog.h"
|
||||
|
||||
|
||||
void PinTypeOut::setOutputSignalToHigh()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <WebServerData.h>
|
||||
/*#include <WebServerData.h>
|
||||
#include <Ethernet.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
@@ -46,3 +46,4 @@ String WebServerData::removeAllCharactersAfterEmptySpace(String &data)
|
||||
data.replace("_", " ");
|
||||
return data;
|
||||
}
|
||||
*/
|
||||
@@ -1,4 +1,4 @@
|
||||
#ifndef WebServerData_H
|
||||
/*ifndef WebServerData_H
|
||||
#define WebServerData_H
|
||||
|
||||
#include <Ethernet.h>
|
||||
@@ -19,4 +19,5 @@ public:
|
||||
|
||||
};
|
||||
|
||||
#endif // WebServerData_H
|
||||
#endif // WebServerData_H
|
||||
*/
|
||||
@@ -2,6 +2,16 @@
|
||||
#include <SoftwareSerial.h>
|
||||
#include "GSMMobileDevice.h"
|
||||
#include "DebugLog.h"
|
||||
#include "Helper.h"
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
void GSMMobileDevice::init()
|
||||
{
|
||||
// init software serial
|
||||
_gsmSerial.begin(_baudRate);
|
||||
delay(5000); // initialization GSM module (give it some time to boot up)
|
||||
}
|
||||
|
||||
void GSMMobileDevice::end()
|
||||
{
|
||||
@@ -19,6 +29,10 @@ void GSMMobileDevice::executeAtCommand(const String &atCommand)
|
||||
|
||||
_gsmSerial.print(atCommand + "\r");
|
||||
delay(500);
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("read fromGMS:" + readStringSerialPort());
|
||||
delay(100);
|
||||
#endif
|
||||
}
|
||||
|
||||
String GSMMobileDevice::readStringSerialPort()
|
||||
@@ -31,16 +45,17 @@ void GSMMobileDevice::sendSMSToGSM(GSMMobileData gsmData)
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("SMS Start SEND SMS Id:" + gsmData.Id + " M:" + gsmData.MobileNumber + " with message " + gsmData.Sms);
|
||||
#endif
|
||||
/*executeAtCommand("AT+CMGF=1");
|
||||
executeAtCommand("AT + CMGS =\"" + gsmData.MobileNumber + "\"");
|
||||
executeAtCommand("ID:" + gsmData.Id + " Msg:" + gsmData.Sms);
|
||||
executeAtCommand("AT+CMGF=1");
|
||||
|
||||
delay(1000);
|
||||
// Send a Ctrl+Z / ASCII SUB
|
||||
_gsmSerial.print((char)26);
|
||||
delay(500);
|
||||
// Close the connection
|
||||
_gsmSerial.print((char)27);*/
|
||||
executeAtCommand("AT + CMGS =\"" + gsmData.MobileNumber + "\"");
|
||||
executeAtCommand("ID:" + gsmData.Id + " Msg:" + gsmData.Sms);
|
||||
|
||||
delay(1000);
|
||||
// Send a Ctrl+Z / ASCII SUB
|
||||
_gsmSerial.print((char)26);
|
||||
delay(500);
|
||||
// Close the connection
|
||||
_gsmSerial.print((char)27);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
@@ -58,28 +73,35 @@ bool GSMMobileDevice::checkSMSError()
|
||||
return false;
|
||||
}
|
||||
|
||||
GSMMobileDevice::GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate) : _gsmSerial(pinRx, pinTx)
|
||||
{
|
||||
_baudRate = baudRate;
|
||||
// init software serial
|
||||
_gsmSerial.begin(baudRate);
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("GSM Init ..");
|
||||
#endif
|
||||
// delay(5000); // initialization GSM module (give it some time to boot up)
|
||||
}
|
||||
unsigned long _lastSMSTime = 0;
|
||||
|
||||
void GSMMobileDevice::sendSMS(GSMMobileData gsmData)
|
||||
{
|
||||
init();
|
||||
sendSMSToGSM(gsmData);
|
||||
|
||||
if (checkSMSError())
|
||||
// Check if enough time has passed since the last SMS was sent
|
||||
if (millis() - _lastSMSTime >= 60000)
|
||||
{
|
||||
// Send the SMS
|
||||
init();
|
||||
sendSMSToGSM(gsmData);
|
||||
if (checkSMSError())
|
||||
{
|
||||
// Log that the SMS was sent
|
||||
DebugLog::log("SMS sent successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Log that the SMS was not sent
|
||||
DebugLog::log("SMS not sent. Error occurred.");
|
||||
}
|
||||
// Update the last SMS time
|
||||
_lastSMSTime = millis();
|
||||
end();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Log that the SMS was not sent
|
||||
DebugLog::log("SMS not sent. Not enough time has passed since the last SMS was sent.");
|
||||
}
|
||||
|
||||
end();
|
||||
}
|
||||
|
||||
// GSMMobileDevice::~GSMMobileDevice()
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
class GSMMobileDevice
|
||||
{
|
||||
private:
|
||||
|
||||
SoftwareSerial _gsmSerial;
|
||||
|
||||
// Methods
|
||||
void end();
|
||||
|
||||
void executeAtCommand(const String &atCommand);
|
||||
String readStringSerialPort();
|
||||
void sendSMSToGSM(GSMMobileData gsmData);
|
||||
@@ -18,14 +18,14 @@ private:
|
||||
bool checkSMSError();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate);
|
||||
|
||||
// Destructor
|
||||
//~GSMDevice();
|
||||
// Constructor with no parameters
|
||||
GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate) : _gsmSerial(pinRx, pinTx), _baudRate(baudRate)
|
||||
{
|
||||
}
|
||||
|
||||
// Methods
|
||||
void init();
|
||||
void sendSMS(GSMMobileData gsmData);
|
||||
void end();
|
||||
};
|
||||
|
||||
#endif // GSMDevice_H
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <Arduino.h>
|
||||
/*#include <Arduino.h>
|
||||
#include <Ethernet.h>
|
||||
#include "WebServer.h"
|
||||
#include "WebServerData.h"
|
||||
@@ -134,3 +134,4 @@ void WebServer::SendToBrowser(const String &dataString)
|
||||
}
|
||||
client.println("");
|
||||
}
|
||||
*/
|
||||
@@ -1,4 +1,4 @@
|
||||
#ifndef WebServer_H
|
||||
/*#ifndef WebServer_H
|
||||
#define WebServer_H
|
||||
|
||||
#include <WebServerData.h>
|
||||
@@ -23,4 +23,5 @@ private:
|
||||
void SendToBrowser(const String &dataString);
|
||||
};
|
||||
|
||||
#endif // WebServer_H
|
||||
#endif // WebServer_H
|
||||
*/
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
void DebugLog::log(const String &log)
|
||||
{
|
||||
WriteToSerial(log);
|
||||
// WriteToSerial(log);
|
||||
}
|
||||
|
||||
void DebugLog::init(const long &bound)
|
||||
|
||||
@@ -18,8 +18,9 @@ bool Helper::hasOverflowed()
|
||||
}
|
||||
|
||||
// Calculate elapsed time considering overflow
|
||||
unsigned long Helper::getElapsedTime()
|
||||
unsigned long Helper::getElapsedTime(bool resetTime)
|
||||
{
|
||||
|
||||
unsigned long elapsedTime = currentTime - lastOverflowTime;
|
||||
if (hasOverflowed())
|
||||
{
|
||||
|
||||
+3
-2
@@ -10,8 +10,9 @@ private:
|
||||
|
||||
public:
|
||||
Helper();
|
||||
static bool hasOverflowed();
|
||||
static unsigned long getElapsedTime();
|
||||
|
||||
bool hasOverflowed();
|
||||
unsigned long getElapsedTime(bool reset);
|
||||
};
|
||||
|
||||
#endif
|
||||
+5
-4
@@ -12,8 +12,9 @@
|
||||
platform = atmelavr
|
||||
board = uno
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
arduino-libraries/Ethernet@^2.0.2
|
||||
featherfly/SoftwareSerial@^1.0
|
||||
debug_tool = avr-stub
|
||||
debug_port = COM8
|
||||
monitor_port = COM8
|
||||
monitor_speed = 115200
|
||||
|
||||
lib_deps =
|
||||
jdolinay/avr-debugger@^1.5
|
||||
|
||||
+34
-163
@@ -1,190 +1,61 @@
|
||||
#include "GSMMobileDevice.h"
|
||||
#include "WebServer.h"
|
||||
//#include "WebServer.h"
|
||||
#include "PinTypeIn.h"
|
||||
#include "PinTypeOut.h"
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.h>
|
||||
#include <Ethernet.h>
|
||||
#include "WebServer.h"
|
||||
#include "WebServerData.h"
|
||||
#include "DebugLog.h"
|
||||
#include "HTMLGenerator.h"
|
||||
//#include <IPAddress.h>
|
||||
// #include <Ethernet.h>
|
||||
// #include "WebServer.h"
|
||||
// #include "WebServerData.h"
|
||||
|
||||
#define DEBUG
|
||||
//#include "HTMLGenerator.h"
|
||||
#include "avr8-stub.h"
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
// PIN INPUT Initialization as OUTPUT
|
||||
static const int COUNT_OUTPUTS_PIN_OUT = 2;
|
||||
static PinTypeOut PinIn8 = PinTypeOut("PIN8", 8, "Electric Alarm");
|
||||
static PinTypeOut PinIn7 = PinTypeOut("PIN7", 7, "Default Description");
|
||||
static const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {PinIn8, PinIn7};
|
||||
static PinTypeOut PinOutAlarm8 = PinTypeOut("PIN8", 8, "Electric Alarm");
|
||||
static PinTypeOut PinOutCase7 = PinTypeOut("PIN7", 7, "Default Description");
|
||||
static const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {PinOutAlarm8, PinOutCase7};
|
||||
|
||||
// PIN INPUT Initialization as INPUT
|
||||
static const int COUNT_INPUTS_PIN_IN = 2;
|
||||
static PinTypeIn PinIn2 = PinTypeIn("PIN2", 2, 100, "Input EZS ALARM"); // 100ms debounce;
|
||||
static PinTypeIn PinIn3 = PinTypeIn("PIN3", 3, 100, "Input Contact Box");
|
||||
static const PinTypeIn INPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN] = {PinIn2, PinIn3};
|
||||
static PinTypeIn PinInEZSAlarm2 = PinTypeIn("PIN4", 4, 100, "Input EZS ALARM"); // 100ms debounce;
|
||||
static PinTypeIn PinInCaseOpen3 = PinTypeIn("PIN3", 3, 100, "Input Contact Box");
|
||||
static const PinTypeIn INPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN] = {PinInEZSAlarm2, PinInCaseOpen3};
|
||||
|
||||
// // GSM Serial Port
|
||||
/*static const uint8_t GSM_PIN_RX = 5;
|
||||
//https://arduino.stackexchange.com/questions/44599/sim900a-mini-modem-imei-0-help-with-tx-rx-pins
|
||||
static const uint8_t GSM_PIN_RX = 5;
|
||||
static const uint8_t GSM_PIN_TX = 6;
|
||||
static const long GSM_BAUD_RATE = 115200;*/
|
||||
|
||||
// WebServer Data
|
||||
static byte WebServerMac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||
static IPAddress WebServerIP = IPAddress(192, 168, 1, 99);
|
||||
static const byte WebServerPort = 80;
|
||||
byte gateway[] = {192, 168, 1, 1}; // Replace with your gateway IP
|
||||
byte subnetMask[] = {255, 255, 255, 0}; // Replace if needed
|
||||
|
||||
// // Devices
|
||||
|
||||
// GSMMobileDevice gsmDevice = GSMMobileDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE);
|
||||
static const long GSM_BAUD_RATE = 115200;
|
||||
GSMMobileDevice gsmDevice = GSMMobileDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE);
|
||||
// // Pin Input
|
||||
// GSMMobileData gsmData = GSMMobileData("GSM");
|
||||
WebServer _webServer;
|
||||
|
||||
void SeparateDataFromRequest(WebServerData &request)
|
||||
{
|
||||
int indexofHttp = request.Request.indexOf(" HTTP");
|
||||
if (indexofHttp == -1)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("ERROR: HTTP not found in GET Header!");
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("SeparateDataFromRequest: " + request.Request);
|
||||
#endif
|
||||
|
||||
if (request.Request.indexOf("GET /id=GSM") != -1)
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("it is GSM");
|
||||
#endif
|
||||
// int start = request.indexOf("/?id=") + 2;
|
||||
String data = request.Request.substring(5);
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Update Data from GET: " + data);
|
||||
#endif
|
||||
String value = request.getValue(data, "mobileNumber=");
|
||||
// gsmData.updateMobileNumber(value);
|
||||
|
||||
value = request.getValue(data, "sms=");
|
||||
// gsmData.updateSMS(value);
|
||||
}
|
||||
|
||||
int indexPin = request.Request.indexOf("/PIN");
|
||||
if (indexPin != -1)
|
||||
{
|
||||
String pinName = request.Request.substring(indexPin + 1, indexofHttp);
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("GET as PIN [" + pinName + "]");
|
||||
#endif
|
||||
|
||||
// foreach OUPUTS_PINS_ARRAY
|
||||
for (int i = 0; i < COUNT_OUTPUTS_PIN_OUT; i++)
|
||||
{
|
||||
String actualPinName = request.getVariableName(pinName, "=");
|
||||
PinTypeOut pinValue = OUPUTS_PINS_ARRAY[i];
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Compare Pin [" + actualPinName + "] with [" + pinValue.Id + "]");
|
||||
#endif
|
||||
|
||||
if (pinValue.Id == actualPinName)
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Update GET as PIN : " + actualPinName);
|
||||
#endif
|
||||
|
||||
if (request.Request.indexOf("=ON") != -1)
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Set " + actualPinName + "=ON");
|
||||
#endif
|
||||
|
||||
pinValue.setOutputSignalToHigh();
|
||||
}
|
||||
|
||||
if (request.Request.indexOf("=OFF") != -1)
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Set " + actualPinName + "=OFF");
|
||||
#endif
|
||||
|
||||
pinValue.setOutputSignalToLow();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GSMMobileData gsmData = GSMMobileData("GSM");
|
||||
|
||||
void setup()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
DebugLog::init(115200);
|
||||
DebugLog::log("Called: Setup");
|
||||
#endif
|
||||
debug_init();
|
||||
|
||||
_webServer = WebServer(WebServerMac, WebServerIP, WebServerPort);
|
||||
delay(1000);
|
||||
_webServer.init();
|
||||
delay(1000);
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("Web Server Initilized");
|
||||
#endif
|
||||
}
|
||||
|
||||
void GenerateHtml()
|
||||
{
|
||||
_webServer.SendToBrowser("<html><head><style>");
|
||||
_webServer.SendToBrowser(" body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0;}");
|
||||
_webServer.SendToBrowser(".content {text-align: center;}");
|
||||
_webServer.SendToBrowser("</style>");
|
||||
_webServer.SendToBrowser("</head><body><div class=\"content\">");
|
||||
_webServer.SendToBrowser("<H2> Trochta.net:</H2>");
|
||||
|
||||
for (int i = 0; i < COUNT_OUTPUTS_PIN_OUT; i++)
|
||||
{
|
||||
PinTypeOut pinValue = OUPUTS_PINS_ARRAY[i];
|
||||
String line = HTMLGenerator::DisplayOuputPinStatus(pinValue.Id, pinValue.IsPinOn(), pinValue.description);
|
||||
_webServer.SendToBrowser(line);
|
||||
}
|
||||
|
||||
for (int i = 0; i < COUNT_INPUTS_PIN_IN; i++)
|
||||
{
|
||||
PinTypeIn pinValue = INPUTS_PINS_ARRAY[i];
|
||||
String line = HTMLGenerator::DisplayInputPinStatus(pinValue.Id, pinValue.IsPinHigh(), pinValue.description);
|
||||
_webServer.SendToBrowser(line);
|
||||
}
|
||||
|
||||
_webServer.SendToBrowser("</div></body></html>");
|
||||
gsmData.MobileNumber = "+420608126674";
|
||||
gsmData.Sms = "Alarm EZS CHATA";
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// DebugLog::log("Called: Loop");
|
||||
WebServerData data = _webServer.getWebServerData();
|
||||
if (!data.IsEmpty)
|
||||
;
|
||||
if (PinInEZSAlarm2.IsPinHigh())
|
||||
{
|
||||
SeparateDataFromRequest(data);
|
||||
PinOutAlarm8.setOutputSignalToHigh();
|
||||
gsmDevice.sendSMS(gsmData);
|
||||
}
|
||||
else
|
||||
{
|
||||
PinOutAlarm8.setOutputSignalToLow();
|
||||
}
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
DebugLog::log("client avaible [" + String (_webServer.client.available()) + "]");
|
||||
#endif
|
||||
|
||||
if (_webServer.client.available())
|
||||
{
|
||||
GenerateHtml();
|
||||
_webServer.endServer();
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user