2 Commits

Author SHA1 Message Date
Ondrej-Trochta 0e4692a3ca fix first sms - power on 2024-10-01 23:40:45 +02:00
Ondrej-Trochta 7e6cb50e92 Add power message, blinky diod every 5 sec, Live message per day 2024-10-01 22:35:27 +02:00
13 changed files with 260 additions and 230 deletions
+6 -1
View File
@@ -1,3 +1,8 @@
#include "DeviceBase.h"
DeviceBase::DeviceBase(const String id): Id(id) {};
DeviceBase::DeviceBase(const String id): Id(id) {};
String DeviceBase::GetNamePin()
{
return Id;
}
+2 -2
View File
@@ -7,8 +7,8 @@ class DeviceBase
{
public:
String Id;
DeviceBase(const String id);
DeviceBase(const String id);
virtual String GetNamePin();
};
#endif
+6 -3
View File
@@ -1,9 +1,12 @@
#include <Arduino.h>
#include "PinTypeIn.h"
bool PinTypeIn::IsPinHigh()
bool PinTypeIn::ReadPinValue()
{
return digitalRead(_pin) == HIGH ? true : false;
}
String PinTypeIn::GetNamePin()
{
return Id + " " + description;
}
+3 -2
View File
@@ -24,8 +24,9 @@ public:
/// @brief Get Actual Pin Value if is SET = True, if is NOT SET = False
/// @return
bool IsPinHigh();
bool ReadPinValue();
String GetNamePin() override;
};
#endif
+40 -40
View File
@@ -1,48 +1,48 @@
#include <WebServerData.h>
#include <Ethernet.h>
#include <Arduino.h>
// #include <WebServerData.h>
// #include <Ethernet.h>
// #include <Arduino.h>
String WebServerData::getValue(const String &data, const String &key)
{
return getValue(data, key, '&');
}
// String WebServerData::getValue(const String &data, const String &key)
// {
// return getValue(data, key, '&');
// }
/// @brief Separe data PIN1=ON to PIN1 and ON
/// @param data PIN1=ON
/// @param key =
/// @param endchar end of string & or space or any other character
/// @return
String WebServerData::getValue(const String &data, const String &key, char endchar)
{
int keyIndex = data.indexOf(key);
if (keyIndex == -1)
{
return ""; // return empty string if key is not found
}
int separatorIndex = data.indexOf(endchar, keyIndex);
// /// @brief Separe data PIN1=ON to PIN1 and ON
// /// @param data PIN1=ON
// /// @param key =
// /// @param endchar end of string & or space or any other character
// /// @return
// String WebServerData::getValue(const String &data, const String &key, char endchar)
// {
// int keyIndex = data.indexOf(key);
// if (keyIndex == -1)
// {
// return ""; // return empty string if key is not found
// }
// int separatorIndex = data.indexOf(endchar, keyIndex);
String value = data.substring(keyIndex + key.length(), separatorIndex != -1 ? separatorIndex : data.length());
// String value = data.substring(keyIndex + key.length(), separatorIndex != -1 ? separatorIndex : data.length());
return removeAllCharactersAfterEmptySpace(value);
}
// return removeAllCharactersAfterEmptySpace(value);
// }
String WebServerData::getVariableName(const String &data, const String &key)
{
int keyIndex = data.indexOf(key);
// String WebServerData::getVariableName(const String &data, const String &key)
// {
// int keyIndex = data.indexOf(key);
String value = data.substring(0, keyIndex != -1 ? keyIndex : keyIndex);
// String value = data.substring(0, keyIndex != -1 ? keyIndex : keyIndex);
return removeAllCharactersAfterEmptySpace(value);
}
// return removeAllCharactersAfterEmptySpace(value);
// }
String WebServerData::removeAllCharactersAfterEmptySpace(String &data)
{
int keyIndex = data.indexOf(' ');
if (keyIndex == -1)
{
return data; // return empty string if key is not found
}
data.remove(keyIndex);
data.replace("_", " ");
return data;
}
// String WebServerData::removeAllCharactersAfterEmptySpace(String &data)
// {
// int keyIndex = data.indexOf(' ');
// if (keyIndex == -1)
// {
// return data; // return empty string if key is not found
// }
// data.remove(keyIndex);
// data.replace("_", " ");
// return data;
// }
+17 -17
View File
@@ -1,22 +1,22 @@
#ifndef WebServerData_H
#define WebServerData_H
// #ifndef WebServerData_H
// #define WebServerData_H
#include <Ethernet.h>
#include <Arduino.h>
// #include <Ethernet.h>
// #include <Arduino.h>
class WebServerData
{
public:
String Request;
bool IsEmpty;
// class WebServerData
// {
// public:
// String Request;
// bool IsEmpty;
WebServerData() : IsEmpty(true){};
WebServerData(String request) : Request(request), IsEmpty(false){};
String getValue(const String &data, const String &key, char endchar);
String getValue(const String &data, const String &key);
String removeAllCharactersAfterEmptySpace(String &data);
String getVariableName(const String &data, const String &key);
// WebServerData() : IsEmpty(true){};
// WebServerData(String request) : Request(request), IsEmpty(false){};
// String getValue(const String &data, const String &key, char endchar);
// String getValue(const String &data, const String &key);
// String removeAllCharactersAfterEmptySpace(String &data);
// String getVariableName(const String &data, const String &key);
};
// };
#endif // WebServerData_H
// #endif // WebServerData_H
+3 -3
View File
@@ -4,7 +4,7 @@
#include "DebugLog.h"
#include "Helper.h"
//#define DEBUG
// #define DEBUG
void GSMMobileDevice::init()
{
@@ -75,10 +75,10 @@ bool GSMMobileDevice::checkSMSError()
unsigned long _lastSMSTime = 0;
void GSMMobileDevice::sendSMS(GSMMobileData gsmData)
void GSMMobileDevice::sendSMS(GSMMobileData gsmData, bool ignoreWaiting = false)
{
// Check if enough time has passed since the last SMS was sent
if (millis() - _lastSMSTime >= 60000)
if (millis() - _lastSMSTime >= 60000 || ignoreWaiting)
{
// Send the SMS
init();
+2 -2
View File
@@ -13,7 +13,7 @@ private:
void executeAtCommand(const String &atCommand);
String readStringSerialPort();
void sendSMSToGSM(GSMMobileData gsmData);
void sendSMSToGSM(GSMMobileData gsmData );
long _baudRate;
bool checkSMSError();
@@ -25,7 +25,7 @@ public:
// Methods
void init();
void sendSMS(GSMMobileData gsmData);
void sendSMS(GSMMobileData gsmData,bool ignoreWaiting = false);
void end();
};
#endif // GSMDevice_H
+113 -113
View File
@@ -1,136 +1,136 @@
#include <Arduino.h>
#include <Ethernet.h>
#include "WebServer.h"
#include "WebServerData.h"
#include "DebugLog.h"
// #include <Arduino.h>
// #include <Ethernet.h>
// #include "WebServer.h"
// #include "WebServerData.h"
// #include "DebugLog.h"
// Constructor
WebServer::WebServer() : _server(80) {}
// // Constructor
// WebServer::WebServer() : _server(80) {}
WebServer::WebServer(byte mac[6], IPAddress ip, uint16_t port) : _ip(ip), _server(port)
{
memcpy(_mac, mac, 6);
}
// WebServer::WebServer(byte mac[6], IPAddress ip, uint16_t port) : _ip(ip), _server(port)
// {
// memcpy(_mac, mac, 6);
// }
// Initialize the Ethernet and start the server
void WebServer::init()
{
Ethernet.begin(_mac, _ip);
// // Initialize the Ethernet and start the server
// void WebServer::init()
// {
// Ethernet.begin(_mac, _ip);
#ifdef DEBUG
DebugLog::log("Ethernet IP: " + Ethernet.localIP());
#endif
// #ifdef DEBUG
// DebugLog::log("Ethernet IP: " + Ethernet.localIP());
// #endif
_server.begin();
// _server.begin();
#ifdef DEBUG
DebugLog::log("Server Start");
#endif
}
// #ifdef DEBUG
// DebugLog::log("Server Start");
// #endif
// }
// Move the declaration of EthernetClient inside the WebServer class
EthernetClient client;
// Handle client requests
WebServerData WebServer::getWebServerData()
{
// Check if a client has connected
// // Move the declaration of EthernetClient inside the WebServer class
// EthernetClient client;
// // Handle client requests
// WebServerData WebServer::getWebServerData()
// {
// // Check if a client has connected
client = _server.available();
if (client)
{
if (client.connected())
{
#ifdef DEBUG
DebugLog::log("Client connected");
#endif
// client = _server.available();
// if (client)
// {
// if (client.connected())
// {
// #ifdef DEBUG
// DebugLog::log("Client connected");
// #endif
unsigned long startTime = millis();
while (client.available() == 0)
{
if (millis() - startTime >= 10000)
{
break;
}
delay(10);
}
// unsigned long startTime = millis();
// while (client.available() == 0)
// {
// if (millis() - startTime >= 10000)
// {
// break;
// }
// delay(10);
// }
client.println(F("HTTP/1.1 200 OK")); // Respond together with the Web page in the HTML
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println();
// client.println(F("HTTP/1.1 200 OK")); // Respond together with the Web page in the HTML
// client.println(F("Content-Type: text/html"));
// client.println(F("Connection: close"));
// client.println();
String request = client.readStringUntil('\r');
// String request = client.readStringUntil('\r');
if (request.startsWith("GET / HTTP/1.1"))
{
// if (request.startsWith("GET / HTTP/1.1"))
// {
#ifdef DEBUG
DebugLog::log("EMPTY DATA: " + request);
#endif
// #ifdef DEBUG
// DebugLog::log("EMPTY DATA: " + request);
// #endif
return WebServerData();
}
// return WebServerData();
// }
// client.flush(); // Clear the incoming buffer after reading the GET request
// If you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
// // client.flush(); // Clear the incoming buffer after reading the GET request
// // If you've gotten to the end of the line (received a newline
// // character) and the line is blank, the http request has ended,
// // so you can send a reply
if (request.startsWith("GET /"))
{
#ifdef DEBUG
DebugLog::log("GET WEB SERVER DATA: " + request);
#endif
WebServerData webServerData = WebServerData(request);
return webServerData;
}
}
}
return WebServerData();
// Give the web browser time to receive the data
}
// if (request.startsWith("GET /"))
// {
// #ifdef DEBUG
// DebugLog::log("GET WEB SERVER DATA: " + request);
// #endif
// WebServerData webServerData = WebServerData(request);
// return webServerData;
// }
// }
// }
// return WebServerData();
// // Give the web browser time to receive the data
// }
// Return a default-constructed WebServerData object if no client is available
// // Return a default-constructed WebServerData object if no client is available
void WebServer::endServer()
{
if (_server.available())
{
delay(10);
// Close the connection
client.stop();
}
}
// void WebServer::endServer()
// {
// if (_server.available())
// {
// delay(10);
// // Close the connection
// client.stop();
// }
// }
void WebServer::SendToBrowser(const String &dataString)
{
const byte chunkSize = 50;
// void WebServer::SendToBrowser(const String &dataString)
// {
// const byte chunkSize = 50;
// Send data in chunks
unsigned int stringIndex = 0;
unsigned long startTime = millis();
// // Send data in chunks
// unsigned int stringIndex = 0;
// unsigned long startTime = millis();
while (stringIndex < dataString.length())
{
if (client.availableForWrite() > 0)
{
int chunkLength = min(chunkSize, dataString.length() - stringIndex);
const String chunk = dataString.substring(stringIndex, stringIndex + chunkLength);
// while (stringIndex < dataString.length())
// {
// if (client.availableForWrite() > 0)
// {
// int chunkLength = min(chunkSize, dataString.length() - stringIndex);
// const String chunk = dataString.substring(stringIndex, stringIndex + chunkLength);
// Send the chunk (consider error handling)
#ifdef DEBUG
DebugLog::log("StBC: " + chunk);
#endif
client.print(chunk);
stringIndex += chunkLength;
delay(5); // Optional delay between chunks
}
else if (millis() - startTime >= 10000)
{
return; // Timeout
}
// // Send the chunk (consider error handling)
// #ifdef DEBUG
// DebugLog::log("StBC: " + chunk);
// #endif
// client.print(chunk);
// stringIndex += chunkLength;
// delay(5); // Optional delay between chunks
// }
// else if (millis() - startTime >= 10000)
// {
// return; // Timeout
// }
delay(5); // Optional delay between chunks
}
client.println("");
}
// delay(5); // Optional delay between chunks
// }
// client.println("");
// }
+21 -21
View File
@@ -1,26 +1,26 @@
#ifndef WebServer_H
#define WebServer_H
// #ifndef WebServer_H
// #define WebServer_H
#include <WebServerData.h>
#include <Arduino.h>
#include <Ethernet.h>
// #include <WebServerData.h>
// #include <Arduino.h>
// #include <Ethernet.h>
class WebServer
{
private:
byte _mac[6];
IPAddress _ip;
EthernetServer _server;
// class WebServer
// {
// private:
// byte _mac[6];
// IPAddress _ip;
// EthernetServer _server;
public:
EthernetClient client;
// public:
// EthernetClient client;
WebServer();
WebServer(byte mac[6], IPAddress ip, uint16_t port);
WebServerData getWebServerData();
void init();
void endServer();
void SendToBrowser(const String &dataString);
};
// WebServer();
// WebServer(byte mac[6], IPAddress ip, uint16_t port);
// WebServerData getWebServerData();
// void init();
// void endServer();
// void SendToBrowser(const String &dataString);
// };
#endif // WebServer_H
// #endif // WebServer_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)
+1 -4
View File
@@ -12,8 +12,5 @@
platform = atmelavr
board = uno
framework = arduino
lib_deps =
arduino-libraries/Ethernet@^2.0.2
featherfly/SoftwareSerial@^1.0
lib_deps = featherfly/SoftwareSerial@^1.0
monitor_speed = 115200
+45 -21
View File
@@ -1,22 +1,19 @@
#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"
//#define DEBUG
//#include "HTMLGenerator.h"
#define DEBUG
// Fill PIN 2,3,4,5,6,7,8,
// PIN INPUT Initialization as OUTPUT
static const int COUNT_OUTPUTS_PIN_OUT = 2;
static PinTypeOut PinOutAlarm8 = PinTypeOut("PIN8", 8, "Electric Alarm");
static const int COUNT_OUTPUTS_PIN_OUT = 3;
static PinTypeOut LivePinOut = PinTypeOut("PIN4", 4, "Live Pin");
static PinTypeOut PinOutCase7 = PinTypeOut("PIN7", 7, "Default Description");
static const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {PinOutAlarm8, PinOutCase7};
static PinTypeOut PinOutAlarm8 = PinTypeOut("PIN8", 8, "Electric Alarm");
static const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {LivePinOut, PinOutAlarm8, PinOutCase7};
// PIN INPUT Initialization as INPUT
static const int COUNT_INPUTS_PIN_IN = 2;
@@ -25,13 +22,14 @@ 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
//https://arduino.stackexchange.com/questions/44599/sim900a-mini-modem-imei-0-help-with-tx-rx-pins
// 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;
GSMMobileDevice gsmDevice = GSMMobileDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE);
// // Pin Input
GSMMobileData gsmData = GSMMobileData("GSM");
static int counter = 0;
void setup()
{
@@ -40,25 +38,51 @@ void setup()
DebugLog::log("Called: Setup and init GSM");
DebugLog::log("Init GSM is OK");
#endif
// gsmDevice.init();
gsmDevice.init();
gsmData.MobileNumber = "+420608126674";
gsmData.Sms = "Alarm EZS CHATA";
#ifdef DEBUG
DebugLog::log("Delay 30 sec");
#endif
delay(30000);
gsmData.Sms = "Arduino EZS Chata is Power ON";
gsmDevice.sendSMS(gsmData, true);
#ifdef DEBUG
DebugLog::log("Send first SMS - Power On");
#endif
}
void loop()
{
DebugLog::log("PIN IN 2: " + String(PinInEZSAlarm2.IsPinHigh()));
if (PinInEZSAlarm2.IsPinHigh())
DebugLog::log("PIN IN 2: " + String(PinInEZSAlarm2.description) + " " + String(PinInEZSAlarm2.ReadPinValue()));
if (PinInEZSAlarm2.ReadPinValue())
{
PinOutAlarm8.setOutputSignalToHigh();
gsmDevice.sendSMS(gsmData);
PinOutAlarm8.setOutputSignalToHigh();
gsmData.Sms = "--== Alarm EZS CHATA ==--";
gsmDevice.sendSMS(gsmData);
}
else
{
PinOutAlarm8.setOutputSignalToLow();
}
delay(1000);
}
delay(500);
counter++;
if (counter >= 86400) // one day
{
gsmData.Sms = "EZS -> I'm Live :)";
gsmDevice.sendSMS(gsmData, true);
counter = 0; // Reset counter to prevent overflow
DebugLog::log("One Day timmer");
}
if (counter % 20 == 0)
{
LivePinOut.setOutputSignalToHigh();
DebugLog::log("Counter: " + String(counter));
}
else
{
LivePinOut.setOutputSignalToLow();
}
}