send sms working

This commit is contained in:
Ondrej-Trochta
2024-05-02 18:35:38 +02:00
parent 9c2d569308
commit e319e2db05
8 changed files with 92 additions and 193 deletions
+2 -1
View File
@@ -2,7 +2,8 @@
#include "PinTypeIn.h"
const bool PinTypeIn::IsPinHigh()
bool PinTypeIn::IsPinHigh()
{
return digitalRead(_pin) == HIGH ? true : false;
}
+1 -1
View File
@@ -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();
};
+47 -25
View File
@@ -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()
+8 -8
View File
@@ -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 -1
View File
@@ -5,7 +5,7 @@
void DebugLog::log(const String &log)
{
WriteToSerial(log);
// WriteToSerial(log);
}
void DebugLog::init(const long &bound)
+2 -1
View File
@@ -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
View File
@@ -10,8 +10,9 @@ private:
public:
Helper();
static bool hasOverflowed();
static unsigned long getElapsedTime();
bool hasOverflowed();
unsigned long getElapsedTime(bool reset);
};
#endif
+28 -154
View File
@@ -10,181 +10,55 @@
#include "DebugLog.h"
#include "HTMLGenerator.h"
#define DEBUG
//#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("PIN2", 2, 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");
DebugLog::log("Called: Setup and init GSM");
DebugLog::log("Init GSM is OK");
#endif
_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>");
// gsmDevice.init();
gsmData.MobileNumber = "+420608126674";
gsmData.Sms = "Alarm EZS CHATA";
}
void loop()
{
// DebugLog::log("Called: Loop");
WebServerData data = _webServer.getWebServerData();
if (!data.IsEmpty)
DebugLog::log("PIN IN 2: " + String(PinInEZSAlarm2.IsPinHigh()));
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);
}