Working code - rewrite classes and bussines logic

This commit is contained in:
Ondrej-Trochta
2024-04-24 03:29:50 +02:00
parent 37c0c73337
commit c32905663b
43 changed files with 658 additions and 669 deletions
+1
View File
@@ -1,6 +1,7 @@
{
"files.associations": {
"pinsdefinitions": "c",
"new": "cpp"
}
+3
View File
@@ -0,0 +1,3 @@
#include "DeviceBase.h"
DeviceBase::DeviceBase(const String id): Id(id) {};
+14
View File
@@ -0,0 +1,14 @@
#ifndef _DeviceBase_h
#define _DeviceBase_h
#include <Arduino.h>
class DeviceBase
{
public:
String Id;
DeviceBase(const String id);
};
#endif
+50
View File
@@ -0,0 +1,50 @@
#include <Arduino.h>
#include "GSMMobileData.h"
#include "EEPROMUtils.h"
#include "DeviceBase.h"
#include "DebugLog.h"
GSMMobileData::GSMMobileData(String id) : DeviceBase(id)
{
readAllData(id);
}
void GSMMobileData::readAllData(String id)
{
EEPROMUtils eepromUtils = EEPROMUtils();
MobileNumber = eepromUtils.readString(1, 20);
Sms = eepromUtils.readString(21, 100);
DebugLog::log("Load from EEPROM. Mobile Number: " + MobileNumber + " SMS: " + Sms);
}
void GSMMobileData::updateMobileNumber(const String &newMobileNumber)
{
if (newMobileNumber.length() <= 0)
{
return;
}
if (newMobileNumber != MobileNumber)
{
EEPROMUtils eepromUtils = EEPROMUtils();
MobileNumber = newMobileNumber;
eepromUtils.writeString(1, newMobileNumber, 20);
DebugLog::log("Update Mobile Number: " + newMobileNumber);
}
}
void GSMMobileData::updateSMS(const String &newSMS)
{
if (newSMS.length() <= 0)
{
return;
}
if (newSMS != Sms)
{
EEPROMUtils eepromUtils = EEPROMUtils();
Sms = newSMS;
eepromUtils.writeString(21, newSMS, 100);
DebugLog::log("Update sms: " + newSMS);
}
}
@@ -2,15 +2,19 @@
#define GSMMobileData_H
#include <Arduino.h>
#include "Data/DeviceBase.h"
#include "DeviceBase.h" // Include the missing header file
class GSMMobileData : public DeviceBase
{
private:
void readAllData(String id);
public:
String MobileNumber;
String Sms;
GSMMobileData(String id): DeviceBase(id){};
GSMMobileData(String id);
void updateMobileNumber(const String &newMobileNumber);
+8
View File
@@ -0,0 +1,8 @@
#include <Arduino.h>
#include "PinTypeIn.h"
bool PinTypeIn::IsPinHigh()
{
return digitalRead(_pin) == HIGH ? true : false;
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef PinTypeIn_H
#define PinTypeIn_H
#include <Arduino.h>
#include "DeviceBase.h"
class PinTypeIn : public DeviceBase
{
private:
uint8_t _pin;
unsigned long _debounceDelay;
unsigned long _lastDebounceTime;
int _lastButtonState;
int _buttonState;
public:
const char *description;
PinTypeIn(String id, uint8_t pin, unsigned long debounceDelay, const char *description): DeviceBase(id), _pin(pin), _debounceDelay(debounceDelay)
{
this->description = description;
pinMode(pin, INPUT);
}
/// @brief Get Actual Pin Value if is SET = True, if is NOT SET = False
/// @return
bool IsPinHigh();
};
#endif
+21
View File
@@ -0,0 +1,21 @@
#include "PinTypeOut.h"
#include <Arduino.h>
#include "DebugLog.h"
void PinTypeOut::setOutputSignalToHigh()
{
digitalWrite(_pinNumber, HIGH);
lastState = true;
}
void PinTypeOut::setOutputSignalToLow()
{
digitalWrite(_pinNumber, LOW);
lastState = false;
}
bool PinTypeOut::IsPinOn()
{
return lastState;
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef PINTYPEOUT_H
#define PINTYPEOUT_H
#include <Arduino.h>
#include "DeviceBase.h"
class PinTypeOut : public DeviceBase
{
private:
uint8_t _pinNumber;
bool lastState;
unsigned long _debounceTime;
unsigned long lastStateChangeTime;
public:
const char *description;
PinTypeOut(String id, uint8_t pinNumber, unsigned long debounceTime, const char *description)
: DeviceBase(id), _pinNumber(pinNumber), lastState(false), _debounceTime(debounceTime), lastStateChangeTime(0), description(description)
{
pinMode(pinNumber, OUTPUT);
}
PinTypeOut(String id, uint8_t pinNumber, const char *description)
: DeviceBase(id), _pinNumber(pinNumber), lastState(false), _debounceTime(0), lastStateChangeTime(0), description(description)
{
pinMode(pinNumber, OUTPUT);
}
void setOutputSignalToHigh();
void setOutputSignalToLow();
bool IsPinOn();
};
#endif // PinTypeOut_H
+48
View File
@@ -0,0 +1,48 @@
#include "WebServerData.h"
#include <Ethernet.h>
#include <Arduino.h>
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);
String value = data.substring(keyIndex + key.length(), separatorIndex != -1 ? separatorIndex : data.length());
return removeAllCharactersAfterEmptySpace(value);
}
String WebServerData::getVariableName(const String &data, const String &key)
{
int keyIndex = data.indexOf(key);
String value = data.substring(0, keyIndex != -1 ? keyIndex : keyIndex);
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;
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef WebServerData_H
#define WebServerData_H
#include <Ethernet.h>
#include <Arduino.h>
class WebServerData
{
public:
EthernetClient Client;
String Request;
bool IsEmpty;
WebServerData(EthernetClient client) : Client(client), IsEmpty(true){};
WebServerData(EthernetClient client, String request) : Client(client), 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
+32
View File
@@ -0,0 +1,32 @@
#include "EEPROMUtils.h"
#include <EEPROM.h>
#include <Arduino.h>
// Statická metoda pro čtení řetězce z EEPROM
String EEPROMUtils::readString(int addr, int length)
{
String data;
for (int i = 0; i < length; i++)
{
char onedata = char(EEPROM.read(addr + i)); // read one byte at a time
if (onedata == '\n')
return data;
data += onedata;
}
return data;
}
// Statická metoda pro zápis řetězce do EEPROM
void EEPROMUtils::writeString(int addr, String data, int limit)
{
int numBytes = data.length(); // get length of the string
for (int i = 0; i < numBytes && i < limit; i++)
{
EEPROM.write(addr + i, data[i]); // write one byte at a time
}
if (numBytes < limit)
{
EEPROM.write(addr + numBytes, '\n'); // add newline character at the end
}
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef EEPROMUtils_h
#define EEPROMUtils_h
#include <Arduino.h> // Include the missing header file for the String class
class EEPROMUtils
{
public:
static String readString(int addr, int length);
static void writeString(int addr, const String data, int limit); // Fix the function signature
};
#endif
+82
View File
@@ -0,0 +1,82 @@
#include "GSMMobileData.h"
#include <SoftwareSerial.h>
#include "GSMMobileDevice.h"
#include "DebugLog.h"
void GSMMobileDevice::end()
{
_gsmSerial.end();
DebugLog::log("GSM End ..");
}
void GSMMobileDevice::executeAtCommand(const String &atCommand)
{
DebugLog::log(atCommand);
_gsmSerial.print(atCommand + "\r");
delay(500);
}
String GSMMobileDevice::readStringSerialPort()
{
return _gsmSerial.readString();
}
void GSMMobileDevice::sendSMSToGSM(GSMMobileData gsmData)
{
DebugLog::log("SMS Start SEND SMS Id:" + gsmData.Id + " M:" + gsmData.MobileNumber + " with message " + gsmData.Sms);
/*executeAtCommand("AT+CMGF=1");
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);
}
bool GSMMobileDevice::checkSMSError()
{
return true;
String response = readStringSerialPort();
if (response.indexOf("ERROR") != -1)
{
// If there was an error, try to reconnect and resend
_gsmSerial.println("AT+CFUN=1,1"); // Restart the module
delay(15000); // Wait for the module to restart
return true;
}
return false;
}
GSMMobileDevice::GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): _gsmSerial(pinRx, pinTx)
{
_baudRate = baudRate;
// init software serial
_gsmSerial.begin(baudRate);
DebugLog::log("GSM Init ..");
//delay(5000); // initialization GSM module (give it some time to boot up)
}
void GSMMobileDevice::sendSMS(GSMMobileData gsmData)
{
init();
sendSMSToGSM(gsmData);
if (checkSMSError())
{
sendSMSToGSM(gsmData);
}
end();
}
// GSMMobileDevice::~GSMMobileDevice()
// {
// // // close gsmSerial
// // // close gsmSerial
// _gsmSerial.end();
// }
+31
View File
@@ -0,0 +1,31 @@
#ifndef GSMMobileDevice_H
#define GSMMobileDevice_H
#include "GSMMobileData.h"
#include "SoftwareSerial.h"
class GSMMobileDevice
{
private:
SoftwareSerial _gsmSerial;
// Methods
void end();
void executeAtCommand(const String &atCommand);
String readStringSerialPort();
void sendSMSToGSM(GSMMobileData gsmData);
long _baudRate;
bool checkSMSError();
public:
// Constructor
GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate);
// Destructor
//~GSMDevice();
// Methods
void sendSMS(GSMMobileData gsmData);
};
#endif // GSMDevice_H
+66
View File
@@ -0,0 +1,66 @@
#include <Arduino.h>
#include <Ethernet.h>
#include "WebServerData.h"
#include "WebServer.h"
#include "DebugLog.h"
// Constructor
WebServer::WebServer() : _server(80) {}
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()
{
DebugLog::log("Starting WebServer mac: " + String(_mac[0]) + String(_mac[1]) + ", IP: " + _ip);
Ethernet.begin(_mac, _ip);
DebugLog::log("Ethernet IP: " + Ethernet.localIP());
_server.begin();
DebugLog::log("Server Start");
}
EthernetClient client;
// Handle client requests
WebServerData WebServer::getWebServerData()
{
// Check if a client has connected
client = _server.available();
if (client)
{
if (client.connected())
{
//DebugLog::log("Client connected");
if (client.available())
{
String request = client.readStringUntil('\r');
// 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"))
{
DebugLog::log("GET WEB SERVER DATA: " + request);
WebServerData webServerData = WebServerData(client, request);
return webServerData;
}
}
}
// Give the web browser time to receive the data
}
return WebServerData(client); // Return a default-constructed WebServerData object if no client is available
}
void WebServer::endServer()
{
if (_server.available())
{
delay(10);
// Close the connection
client.stop();
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef WebServer_H
#define WebServer_H
#include <Arduino.h>
#include <Ethernet.h>
#include "WebServerData.h"
class WebServer
{
private:
byte _mac[6];
IPAddress _ip;
EthernetServer _server;
public:
EthernetClient client;
WebServer();
WebServer(byte mac[6], IPAddress ip, uint16_t port);
WebServerData getWebServerData();
void init();
void endServer();
};
#endif // WebServer_H
+12
View File
@@ -0,0 +1,12 @@
#include <Arduino.h>
#include "DebugLog.h"
void DebugLog::log(const String &log)
{
Serial.println(log);
}
void DebugLog::init(const long &bound)
{
Serial.begin(bound);
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef DebugLog_H
#define DebugLog_H
#include <Arduino.h>
class DebugLog
{
public:
static void init(const long &bound);
static void log(const String &log);
};
#endif
+22
View File
@@ -0,0 +1,22 @@
#include <Arduino.h>
#include "HTMLGenerator.h"
const String HTMLGenerator::GetElement(const String &tag, String &content, const String &attributes)
{
if (attributes != "")
{
content += "<" + tag + " " + attributes + ">" + content + "</" + tag + ">";
}
else
{
content+= "<" + tag + ">" + content + "</" + tag + ">";
}
return content;
}
const String HTMLGenerator::DisplayPinStatus(const String &status, const String &link, bool isOn)
{
String element = isOn ? "H4" : "p";
String content = status + (link != "" ? " <a href=\"" + link + "\">" + (isOn ? "OFF" : "ON") + "</a>" : "");
return GetElement(element, content,"");
}
@@ -6,7 +6,7 @@
class HTMLGenerator
{
public:
static const String GetElement(const String &tag, const String &content, const String &attributes = "");
static const String GetElement(const String &tag, String &content, const String &attributes = "");
static const String DisplayPinStatus(const String &status, const String &link, bool isOn);
};
+29
View File
@@ -0,0 +1,29 @@
#include <limits.h>
#include <Arduino.h>
#include "Helper.h"
unsigned long currentTime = millis();
unsigned long lastOverflowTime = 0; // Add missing declaration and initialization
// Function to check for overflow and update lastOverflowTime
bool Helper::hasOverflowed()
{
if (currentTime < lastOverflowTime)
{
lastOverflowTime = currentTime;
return true;
}
return false;
}
// Calculate elapsed time considering overflow
unsigned long Helper::getElapsedTime()
{
unsigned long elapsedTime = currentTime - lastOverflowTime;
if (hasOverflowed())
{
elapsedTime += (ULONG_MAX - lastOverflowTime); // Add the time since last overflow
}
return elapsedTime;
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef HELPER_H
#define HELPER_H
#include <limits.h>
#include <Arduino.h>
class Helper
{
private:
public:
Helper();
static bool hasOverflowed();
static unsigned long getElapsedTime();
};
#endif
-9
View File
@@ -1,9 +0,0 @@
#include <Arduino.h>
class DeviceBase
{
public:
String Id;
DeviceBase(const String id) : Id(id) {}
};
-13
View File
@@ -1,13 +0,0 @@
#ifndef _Device_h
#define _Device_h
#include <Arduino.h>
class DeviceBase
{
public:
String Id;
DeviceBase(const String id) : Id(id) {}
};
#endif
-43
View File
@@ -1,43 +0,0 @@
#include "Data/DeviceBase.h"
#include "Device/EEPROMUtils.h"
#include <Arduino.h>
class GSMMobileData : public DeviceBase
{
public:
String MobileNumber;
String Sms;
GSMMobileData(String id) : DeviceBase(id)
{
MobileNumber = EEPROMUtils::ReadString(1, 20);
Sms = EEPROMUtils::ReadString(21, 100);
}
void updateMobileNumber(const String &newMobileNumber)
{
if (newMobileNumber.length() <= 0)
{
return;
}
if (newMobileNumber != MobileNumber)
{
MobileNumber = newMobileNumber;
EEPROMUtils::WriteString(1, newMobileNumber, 20);
}
}
void updateSMS(const String &newSMS)
{
if (newSMS.length() <= 0)
{
return;
}
if (newSMS != Sms)
{
Sms = newSMS;
EEPROMUtils::WriteString(21, newSMS, 100);
}
}
};
-57
View File
@@ -1,57 +0,0 @@
#include <Arduino.h>
#include "DeviceBase.h"
class PinTypeIn : DeviceBase
{
private:
int pin;
unsigned long debounceDelay;
unsigned long lastDebounceTime;
int lastButtonState;
int buttonState;
const char *description;
bool ReadValue()
{
int reading = digitalRead(pin);
if (reading != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == HIGH)
{
return true;
}
}
}
lastButtonState = reading;
return false;
}
public:
PinTypeIn(String id, int pin, unsigned long debounceDelay, const char *description) : DeviceBase(id)
{
this->pin = pin;
this->debounceDelay = debounceDelay;
this->description = description;
lastDebounceTime = 0;
lastButtonState = LOW;
buttonState = LOW;
pinMode(pin, INPUT);
}
bool IsSetPinValueToHigh()
{
return ReadValue();
}
};
-23
View File
@@ -1,23 +0,0 @@
#ifndef PinTypeIn_H
#define PinTypeIn_H
#include <Arduino.h>
#include <Data/DeviceBase.h>
class PinTypeIn : public DeviceBase
{
private:
uint8_t pin;
unsigned long debounceDelay;
unsigned long lastDebounceTime;
int lastButtonState;
int buttonState;
bool ReadValue();
public:
PinTypeIn(String id, int pin, unsigned long debounceDelay, const char *description): DeviceBase(id){};
};
#endif
-59
View File
@@ -1,59 +0,0 @@
#include <Arduino.h>
#include "DeviceBase.h"
#include "Utills.h"
class PinTypeOut : public DeviceBase
{
private:
uint8_t pinNumber;
bool lastState;
unsigned long debounceTime;
unsigned long lastStateChangeTime;
const char *description;
public:
PinTypeOut(String id, uint8_t pinNumber, bool lastState, unsigned long debounceTime, const char *description)
: DeviceBase(id), pinNumber(pinNumber), lastState(lastState), debounceTime(debounceTime), lastStateChangeTime(0), description(description)
{
pinMode(pinNumber, OUTPUT);
}
bool IsTimeIgnoredPin()
{
bool currentInputState = digitalRead(pinNumber);
if (currentInputState != lastState && millis() - lastStateChangeTime >= debounceTime)
{
lastState = currentInputState;
lastStateChangeTime = millis();
// Add pin state change logic here
return true;
}
return false;
}
void SetOutputSignalToHighWithtimeIgnoring()
{
if (IsTimeIgnoredPin())
{
digitalWrite(pinNumber, HIGH);
}
}
void SetOutputSignalToLowWithtimeIgnoring()
{
if (IsTimeIgnoredPin())
{
digitalWrite(pinNumber, LOW);
}
}
void SetOutputSignalToHigh()
{
digitalWrite(pinNumber, HIGH);
}
void SetOutputSignalToLow()
{
digitalWrite(pinNumber, LOW);
}
};
-32
View File
@@ -1,32 +0,0 @@
#ifndef PinTypeOut_H
#define PinTypeOut_H
#include <Arduino.h>
#include "DeviceBase.h"
#include "Utills.h"
class PinTypeOut : public DeviceBase
{
private:
uint8_t pinNumber;
bool lastState;
unsigned long debounceTime;
unsigned long lastStateChangeTime;
const char *description;
public:
PinTypeOut(String id, uint8_t pinNumber, bool lastState, unsigned long debounceTime, const char *description)
: DeviceBase(id), pinNumber(pinNumber), lastState(lastState), debounceTime(debounceTime), lastStateChangeTime(0), description(description)
{
};
/// @brief Ignoring input signal for debounce time
/// @return true is signal is ignored (debounced)
bool IsTimeIgnoredPin();
void SetOutputSignalToHighWithtimeIgnoring();
void SetOutputSignalToLowWithtimeIgnoring();
void SetOutputSignalToHigh();
void SetOutputSignalToLow();
};
#endif // PinOutData_H
-47
View File
@@ -1,47 +0,0 @@
#include <Ethernet.h>
class WebServerData
{
public:
EthernetClient Client;
String Request;
bool IsEmpty;
WebServerData()
{
IsEmpty = true;
}
WebServerData(const EthernetClient &client, const String &request)
{
Client = client;
Request = request;
IsEmpty = false;
}
String getValue(const String &data, const String &key)
{
int keyIndex = data.indexOf(key);
if (keyIndex == -1)
{
return ""; // return empty string if key is not found
}
int separatorIndex = data.indexOf('&', keyIndex);
String value = data.substring(keyIndex + key.length(), separatorIndex != -1 ? separatorIndex : data.length());
return removeAllCharactesrAfterEmpty(value);
}
String removeAllCharactesrAfterEmpty(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;
}
};
-19
View File
@@ -1,19 +0,0 @@
#ifndef WebServerData_H
#define WebServerData_H
#include <Ethernet.h>
class WebServerData
{
public:
EthernetClient Client;
String Request;
bool IsEmpty;
WebServerData();
WebServerData(EthernetClient &client, String &request);
String getValue(String &data, const String &key);
String removeAllCharactesrAfterEmpty(String &data);
};
#endif // WebServerData_H
-34
View File
@@ -1,34 +0,0 @@
#include <EEPROM.h>
#include <Arduino.h>
class EEPROMUtils
{
public:
// Statická metoda pro čtení řetězce z EEPROM
static String ReadString(int addr, int length)
{
String data;
for (int i = 0; i < length; i++)
{
char onedata = char(EEPROM.read(addr + i)); // read one byte at a time
if (onedata == '\n')
return data;
data += onedata;
}
return data;
}
// Statická metoda pro zápis řetězce do EEPROM
static void WriteString(int addr, const String &data, int limit)
{
int numBytes = data.length(); // get length of the string
for (int i = 0; i < numBytes && i < limit; i++)
{
EEPROM.write(addr + i, data[i]); // write one byte at a time
}
if (numBytes < limit)
{
EEPROM.write(addr + numBytes, '\n'); // add newline character at the end
}
}
};
-11
View File
@@ -1,11 +0,0 @@
#ifndef EEPROMUtils_h
#define EEPROMUtils_h
class EEPROMUtils
{
public:
static String ReadString(int addr, int length);
static void WriteString(int addr, const String &data, int limit);
};
#endif
-88
View File
@@ -1,88 +0,0 @@
#include "Data/GSMMobileData.h"
#include "Utills.h"
#include <SoftwareSerial.h>
class GSMDevice
{
private:
SoftwareSerial gsmSerial;
void end()
{
gsmSerial.end();
Utills::logDebug("GSM End ..");
}
void ExecuteAtCommand(const String &atCommand)
{
Utills::logDebug(atCommand);
gsmSerial.print(atCommand + "\r");
delay(500);
}
String ReadStringSerialPort()
{
return gsmSerial.readString();
}
void sendSMSToGSM(GSMMobileData gsmData)
{
Utills::logDebug("SMS Start SEND SMS Id:" + gsmData.Id + " M:" + gsmData.MobileNumber + " with message " + gsmData.Sms);
ExecuteAtCommand("AT+CMGF=1");
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);
}
bool CheckSMSError()
{
String response = ReadStringSerialPort();
if (response.indexOf("ERROR") != -1)
{
// If there was an error, try to reconnect and resend
gsmSerial.println("AT+CFUN=1,1"); // Restart the module
delay(15000); // Wait for the module to restart
return true;
}
return false;
}
public:
GSMDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): gsmSerial(pinRx, pinTx)
{
//init software serial
gsmSerial.begin(baudRate);
Utills::logDebug("GSM Init ..");
delay(5000); // initialization GSM module (give it some time to boot up)
}
void sendSMS(GSMMobileData gsmData)
{
init();
sendSMSToGSM(gsmData);
if (CheckSMSError())
{
sendSMSToGSM(gsmData);
}
end();
}
// ~GSMDevice()
// {
// // close gsmSerial
// // close gsmSerial
// gsmSerial.end();
// }
};
-29
View File
@@ -1,29 +0,0 @@
#ifndef GSMDevice_H
#define GSMDevice_H
#include "Data/GSMMobileData.h"
#include "SoftwareSerial.h"
class GSMDevice
{
private:
SoftwareSerial gsmSerial;
// Methods
void end();
void ExecuteAtCommand(const String &atCommand);
String ReadStringSerialPort();
void sendSMSToGSM(GSMMobileData gsmData);
public:
// Constructor
GSMDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): gsmSerial(pinRx, pinTx){};
// Destructor
// ~GSMDevice();
// Methods
void sendSMS(GSMMobileData gsmData);
};
#endif // GSMDevice_H
-68
View File
@@ -1,68 +0,0 @@
#include <Arduino.h>
#include <Ethernet.h>
#include "Data/WebServerData.h"
class WebServer
{
private:
byte mac[6];
IPAddress ip;
EthernetServer server;
// Event
// Define the callback function type
// Declare a function pointer
// void (*Callback)(WebServerData) = WebBrowserDataCallback;
public:
EthernetClient client;
// Constructor
WebServer() : server(80) {}
WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port)
{
memcpy(this->mac, mac, sizeof(this->mac));
}
// Initialize the Ethernet and start the server
void WebServer::Init()
{
Ethernet.begin(mac, ip);
server.begin();
}
// Handle client requests
WebServerData GetWebServerData()
{
// Check if a client has connected
client = server.available();
if (client)
{
if (client.connected())
{
while (client.available())
{
String request = client.readStringUntil('\r');
// 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
WebServerData webServerData = WebServerData(client, request);
return webServerData;
}
}
// Give the web browser time to receive the data
}
return WebServerData(); // Return a default-constructed WebServerData object if no client is available
}
void WebServer::EndServer()
{
if (server.available())
{
delay(10);
// Close the connection
client.stop();
}
}
};
-33
View File
@@ -1,33 +0,0 @@
#ifndef WebServer_H
#define WebServer_H
#include <Arduino.h>
#include <Ethernet.h>
#include "Data/WebServerData.h"
class WebServer
{
private:
byte mac[6];
IPAddress ip;
EthernetServer server;
// Define the callback function type
public:
EthernetClient client;
// Constructor
WebServer() : server(80) {}
WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port)
{
memcpy(this->mac, mac, sizeof(this->mac));
}
// Initialize the Ethernet and start the server
void Init();
// Handle client requests
WebServerData GetWebServerData();
void EndServer();
};
#endif // WebServer_H
-25
View File
@@ -1,25 +0,0 @@
#include <Arduino.h>
class HTMLGenerator
{
public:
static const String GetElement(const String &tag, const String &content, const String &attributes = "")
{
String html_content = "";
if (attributes != "")
{
return "<" + tag + " " + attributes + ">" + content + "</" + tag + ">";
}
else
{
return "<" + tag + ">" + content + "</" + tag + ">";
}
}
static const String DisplayPinStatus(const String &status, const String &link, bool isOn)
{
String element = isOn ? "H4" : "p";
String content = status + (link != "" ? " <a href=\"" + link + "\">" + (isOn ? "OFF" : "ON") + "</a>" : "");
return GetElement(element, content);
}
};
-10
View File
@@ -1,10 +0,0 @@
#include <Arduino.h>
class Utills
{
public:
static void logDebug(const String& log)
{
Serial.println(log);
}
};
-12
View File
@@ -1,12 +0,0 @@
#ifndef Utills_H
#define Utills_H
class Utills {
public:
static void logDebug(const String& log) {
// Implementation of the logDebug function goes here
// You can add your desired functionality inside this function
}
};
#endif
+74 -54
View File
@@ -1,16 +1,19 @@
#include "Device/GSMDevice.h"
#include "Device/WebServer.h"
#include "Data/PinTypeIn.h"
#include "Data/PinTypeOut.h"
#include "Utills.h"
#include "GSMMobileDevice.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"
// PIN INPUT Initialization as OUTPUT
static const int COUNT_OUTPUTS_PIN_OUT = 2;
static PinTypeOut PinIn8 = PinTypeOut("PIN8", 8, LOW, 10000, "Electric Alarm");
static PinTypeOut PinIn7 = PinTypeOut("PIN7", 7, LOW, 100, "Default Description");
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};
// PIN INPUT Initialization as INPUT
@@ -19,7 +22,7 @@ static PinTypeIn PinIn2 = PinTypeIn("PIN2", 2, 100, "Input EZS ALARM"); // 100ms
static PinTypeIn PinIn3 = PinTypeIn("PIN3", 3, 100, "Input Contact Box");
static const PinTypeIn INPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN] = {PinIn2, PinIn3};
// GSM Serial Port
// // GSM Serial Port
static const uint8_t GSM_PIN_RX = 10;
static const uint8_t GSM_PIN_TX = 11;
static const long GSM_BAUD_RATE = 115200;
@@ -28,89 +31,86 @@ static const long GSM_BAUD_RATE = 115200;
static byte WebServerMac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
static IPAddress WebServerIP = IPAddress(192, 168, 1, 177);
static const byte WebServerPort = 80;
// Devices
// // Devices
GSMDevice gsmDevice = GSMDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE);
// Pin Input
GSMMobileDevice gsmDevice = GSMMobileDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE);
// // Pin Input
GSMMobileData gsmData = GSMMobileData("GSM");
WebServer webServer;
void SeparateDataFromRequest(WebServerData &request)
{
if (request.Request.indexOf("GET /?id=GSM") != -1)
int indexofHttp = request.Request.indexOf(" HTTP");
if (indexofHttp == -1)
{
Utills::logDebug("it is GSM");
DebugLog::log("ERROR: HTTP not found in GET Header!");
return;
}
DebugLog::log("SeparateDataFromRequest: " + request.Request);
if (request.Request.indexOf("GET /id=GSM") != -1)
{
DebugLog::log("it is GSM");
// int start = request.indexOf("/?id=") + 2;
String data = request.Request.substring(6);
Utills::logDebug("Update Data from GET: " + data);
String data = request.Request.substring(5);
DebugLog::log("Update Data from GET: " + data);
String value = request.getValue(data, "mobileNumber=");
gsmData.updateMobileNumber(value);
value = request.getValue(data, "sms=");
gsmData.updateSMS(value);
}
if (request.Request.indexOf("/PIN") == -1)
int indexPin = request.Request.indexOf("/PIN");
if (indexPin != -1)
{
String pinName = request.Request.substring(5, 4);
Utills::logDebug("Update Data from GET as PIN : " + pinName);
String pinName = request.Request.substring(indexPin + 1, indexofHttp);
DebugLog::log("GET as PIN [" + pinName + "]");
// 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];
if (pinValue.Id == pinName)
{
Utills::logDebug("Update Data from GET as PIN : " + pinName);
DebugLog::log("Compare Pin [" + actualPinName + "] with [" + pinValue.Id + "]");
if (pinValue.Id == actualPinName)
{
DebugLog::log("Update GET as PIN : " + actualPinName);
if (request.Request.indexOf("=ON") != -1)
{
pinValue.SetOutputSignalToHighWithtimeIgnoring();
DebugLog::log("Set " + actualPinName + "=ON");
pinValue.setOutputSignalToHigh();
}
if (request.Request.indexOf("=OFF") != -1)
{
pinValue.SetOutputSignalToLowWithtimeIgnoring();
DebugLog::log("Set " + actualPinName + "=OFF");
pinValue.setOutputSignalToLow();
}
}
}
}
}
void WebServerEvent(WebServerData data)
void WebServerEvent(WebServerData &data)
{
// Your function implementation here
Utills::logDebug("Request: " + data.Request);
SeparateDataFromRequest(data);
}
void setup()
{
DebugLog::init(115200);
DebugLog::log("Called: Setup");
webServer = WebServer(WebServerMac, WebServerIP, WebServerPort);
// serial monitori init and set baud rate
Serial.begin(9600);
webServer.Init();
Utills::logDebug("Called: Setup");
// // serial monitori init and set baud rate
webServer.init();
DebugLog::log("Web Server Initilized");
}
void GenerateHtml(WebServerData data);
void loop()
{
WebServerData data = webServer.GetWebServerData();
if (!data.IsEmpty)
{
WebServerEvent(data);
}
if (data.Client.available())
{
GenerateHtml(data);
}
}
void GenerateHtml(WebServerData data)
void GenerateHtml(WebServerData &data)
{
EthernetClient client = data.Client;
client.println("HTTP/1.1 200 OK");
@@ -121,20 +121,40 @@ void GenerateHtml(WebServerData data)
client.println(" body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0;}");
client.println(".content {text-align: center;}");
for (int i = 0; i < COUNT_OUTPUTS_PIN_OUT; i++)
{
PinTypeOut pinValue = OUPUTS_PINS_ARRAY[i];
HTMLGenerator::DisplayPinStatus(pinValue.Id + "|" + pinValue.description, pinValue.Id, pinValue.IsPinOn());
}
for (int i = 0; i < COUNT_INPUTS_PIN_IN; i++)
{
PinTypeIn pinValue = INPUTS_PINS_ARRAY[i];
HTMLGenerator::DisplayPinStatus(pinValue.Id + "|" + pinValue.description,"", pinValue.IsPinHigh());
}
client.println("</style>");
client.println("</head><body><div class=\"content\">");
client.println("<H2> Trochta.net:</H2>");
client.println("</div></body></html>");
client.stop();
}
int main()
void loop()
{
setup();
while (true)
// DebugLog::log("Called: Loop");
WebServerData data = webServer.getWebServerData();
if (!data.IsEmpty)
{
loop();
WebServerEvent(data);
}
return 0;
}
if (data.Client.available())
{
GenerateHtml(data);
data.Client.stop();
}
}