From ec1658ef58b194c6a2fe5f791fcfe0e7dc7af894 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Sun, 21 Apr 2024 15:28:23 +0200 Subject: [PATCH 1/8] Separed to classes (not working yet) --- src/Device/EEPROMUtils.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/Device/EEPROMUtils.h diff --git a/src/Device/EEPROMUtils.h b/src/Device/EEPROMUtils.h new file mode 100644 index 0000000..e69de29 From bed24da57cf283e3906d8c2f51d60aed20b83c28 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Sun, 21 Apr 2024 15:28:31 +0200 Subject: [PATCH 2/8] new file: .idea/.idea.GSM-Chata.dir/.idea/.gitignore new file: .idea/.idea.GSM-Chata.dir/.idea/encodings.xml new file: .idea/.idea.GSM-Chata.dir/.idea/indexLayout.xml new file: .idea/.idea.GSM-Chata.dir/.idea/vcs.xml new file: .vscode/settings.json modified: platformio.ini new file: src/Data/DeviceBase.cpp new file: src/Data/DeviceBase.h new file: src/Data/GSMData.cpp new file: src/Data/GSMData.h new file: src/Data/PinTypeIn.cpp new file: src/Data/PinTypeIn.h new file: src/Data/PinTypeOut.cpp new file: src/Data/PinTypeOut.h new file: src/Device/EEPROMUtils.cpp modified: src/Device/EEPROMUtils.h new file: src/Device/GSMDevice.cpp new file: src/Device/GSMDevice.h new file: src/Device/WebServer.cpp new file: src/Helper/HTMLGenerator.cpp new file: src/Helper/HTMLGenerator.h new file: src/PinDefinitions.cpp new file: src/PinDefinitions.h new file: src/Utills.cpp new file: src/Utills.h deleted: src/Zabezpecovacka-GSM.cpp new file: src/main.cpp --- .idea/.idea.GSM-Chata.dir/.idea/.gitignore | 13 + .idea/.idea.GSM-Chata.dir/.idea/encodings.xml | 4 + .../.idea.GSM-Chata.dir/.idea/indexLayout.xml | 8 + .idea/.idea.GSM-Chata.dir/.idea/vcs.xml | 6 + .vscode/settings.json | 5 + platformio.ini | 5 +- src/Data/DeviceBase.cpp | 9 + src/Data/DeviceBase.h | 13 + src/Data/GSMData.cpp | 30 ++ src/Data/GSMData.h | 22 ++ src/Data/PinTypeIn.cpp | 57 +++ src/Data/PinTypeIn.h | 23 ++ src/Data/PinTypeOut.cpp | 71 ++++ src/Data/PinTypeOut.h | 32 ++ src/Device/EEPROMUtils.cpp | 34 ++ src/Device/EEPROMUtils.h | 16 + src/Device/GSMDevice.cpp | 87 +++++ src/Device/GSMDevice.h | 29 ++ src/Device/WebServer.cpp | 63 ++++ src/Helper/HTMLGenerator.cpp | 21 ++ src/Helper/HTMLGenerator.h | 13 + src/PinDefinitions.cpp | 41 +++ src/PinDefinitions.h | 27 ++ src/Utills.cpp | 10 + src/Utills.h | 11 + src/Zabezpecovacka-GSM.cpp | 337 ------------------ src/main.cpp | 23 ++ 27 files changed, 672 insertions(+), 338 deletions(-) create mode 100644 .idea/.idea.GSM-Chata.dir/.idea/.gitignore create mode 100644 .idea/.idea.GSM-Chata.dir/.idea/encodings.xml create mode 100644 .idea/.idea.GSM-Chata.dir/.idea/indexLayout.xml create mode 100644 .idea/.idea.GSM-Chata.dir/.idea/vcs.xml create mode 100644 .vscode/settings.json create mode 100644 src/Data/DeviceBase.cpp create mode 100644 src/Data/DeviceBase.h create mode 100644 src/Data/GSMData.cpp create mode 100644 src/Data/GSMData.h create mode 100644 src/Data/PinTypeIn.cpp create mode 100644 src/Data/PinTypeIn.h create mode 100644 src/Data/PinTypeOut.cpp create mode 100644 src/Data/PinTypeOut.h create mode 100644 src/Device/EEPROMUtils.cpp create mode 100644 src/Device/GSMDevice.cpp create mode 100644 src/Device/GSMDevice.h create mode 100644 src/Device/WebServer.cpp create mode 100644 src/Helper/HTMLGenerator.cpp create mode 100644 src/Helper/HTMLGenerator.h create mode 100644 src/PinDefinitions.cpp create mode 100644 src/PinDefinitions.h create mode 100644 src/Utills.cpp create mode 100644 src/Utills.h delete mode 100644 src/Zabezpecovacka-GSM.cpp create mode 100644 src/main.cpp diff --git a/.idea/.idea.GSM-Chata.dir/.idea/.gitignore b/.idea/.idea.GSM-Chata.dir/.idea/.gitignore new file mode 100644 index 0000000..8790f46 --- /dev/null +++ b/.idea/.idea.GSM-Chata.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/modules.xml +/projectSettingsUpdater.xml +/.idea.GSM-Chata.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.GSM-Chata.dir/.idea/encodings.xml b/.idea/.idea.GSM-Chata.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.GSM-Chata.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.GSM-Chata.dir/.idea/indexLayout.xml b/.idea/.idea.GSM-Chata.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.GSM-Chata.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.GSM-Chata.dir/.idea/vcs.xml b/.idea/.idea.GSM-Chata.dir/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.GSM-Chata.dir/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..76ca929 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "pinsdefinitions": "c" + } +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 1ee07a7..bc9bc01 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,4 +12,7 @@ platform = atmelavr board = uno framework = arduino -lib_deps = arduino-libraries/Ethernet@^2.0.2 +lib_deps = + arduino-libraries/Ethernet@^2.0.2 + featherfly/SoftwareSerial@^1.0 +build_flags = -DNDEBUG diff --git a/src/Data/DeviceBase.cpp b/src/Data/DeviceBase.cpp new file mode 100644 index 0000000..fca2f66 --- /dev/null +++ b/src/Data/DeviceBase.cpp @@ -0,0 +1,9 @@ +#include + +class DeviceBase +{ + +public: + String Id; + DeviceBase(const String id) : Id(id) {} +}; \ No newline at end of file diff --git a/src/Data/DeviceBase.h b/src/Data/DeviceBase.h new file mode 100644 index 0000000..0492d24 --- /dev/null +++ b/src/Data/DeviceBase.h @@ -0,0 +1,13 @@ +#ifndef _Device_h +#define _Device_h + +#include + +class DeviceBase +{ +public: + String Id; + DeviceBase(const String id) : Id(id) {} +}; + +#endif \ No newline at end of file diff --git a/src/Data/GSMData.cpp b/src/Data/GSMData.cpp new file mode 100644 index 0000000..3344b1c --- /dev/null +++ b/src/Data/GSMData.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +class GSMData : public DeviceBase +{ + +public: + String MobileNumber; + String Sms; + + GSMData(String id): DeviceBase(id) + { + MobileNumber = EEPROMUtils::ReadString(1, 20); + Sms = EEPROMUtils::ReadString(21, 100); + } + + void updateMobileNumber(const String &newMobileNumber) + { + MobileNumber = newMobileNumber; + EEPROMUtils::WriteString(1, newMobileNumber, 20); + } + + void updateSMS(const String &newSMS) + { + Sms = newSMS; + EEPROMUtils::WriteString(21, newSMS, 100); + } +}; diff --git a/src/Data/GSMData.h b/src/Data/GSMData.h new file mode 100644 index 0000000..a8f3bf9 --- /dev/null +++ b/src/Data/GSMData.h @@ -0,0 +1,22 @@ +#ifndef GSMData_H +#define GSMData_H + +#include +#include +#include +#include + +class GSMData : public DeviceBase +{ +public: + String MobileNumber; + String Sms; + + GSMData(String id): DeviceBase(id){}; + + void updateMobileNumber(const String &newMobileNumber); + + void updateSMS(const String &newSMS); +}; + +#endif // GSMData_H \ No newline at end of file diff --git a/src/Data/PinTypeIn.cpp b/src/Data/PinTypeIn.cpp new file mode 100644 index 0000000..ed3307f --- /dev/null +++ b/src/Data/PinTypeIn.cpp @@ -0,0 +1,57 @@ +#include +#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(); + } +}; diff --git a/src/Data/PinTypeIn.h b/src/Data/PinTypeIn.h new file mode 100644 index 0000000..39994ce --- /dev/null +++ b/src/Data/PinTypeIn.h @@ -0,0 +1,23 @@ +#ifndef PinTypeIn_H +#define PinTypeIn_H + +#include +#include + +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 \ No newline at end of file diff --git a/src/Data/PinTypeOut.cpp b/src/Data/PinTypeOut.cpp new file mode 100644 index 0000000..e322466 --- /dev/null +++ b/src/Data/PinTypeOut.cpp @@ -0,0 +1,71 @@ +#include +#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); + } + + void checkPin() + { + bool currentInputState = digitalRead(pinNumber); + if (currentInputState != lastState && millis() - lastStateChangeTime >= debounceTime) + { + lastState = currentInputState; + lastStateChangeTime = millis(); + Utills::logDebug("Pin " + String(pinNumber) + " changed to " + String(lastState)); + // Add pin state change logic here + } + } + + 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); + } +}; diff --git a/src/Data/PinTypeOut.h b/src/Data/PinTypeOut.h new file mode 100644 index 0000000..a70acfb --- /dev/null +++ b/src/Data/PinTypeOut.h @@ -0,0 +1,32 @@ +#ifndef PinTypeOut_H +#define PinTypeOut_H + +#include +#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 diff --git a/src/Device/EEPROMUtils.cpp b/src/Device/EEPROMUtils.cpp new file mode 100644 index 0000000..5f1d649 --- /dev/null +++ b/src/Device/EEPROMUtils.cpp @@ -0,0 +1,34 @@ +#include +#include + +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 + } + } +}; \ No newline at end of file diff --git a/src/Device/EEPROMUtils.h b/src/Device/EEPROMUtils.h index e69de29..de1ce2c 100644 --- a/src/Device/EEPROMUtils.h +++ b/src/Device/EEPROMUtils.h @@ -0,0 +1,16 @@ +#ifndef _EEPROMUtils_h +#define _EEPROMUtils_h + +#include +#include +#include + + + class EEPROMUtils +{ +public: + static String ReadString(int addr, int length); + static void WriteString(int addr, const String &data, int limit); +}; + + #endif \ No newline at end of file diff --git a/src/Device/GSMDevice.cpp b/src/Device/GSMDevice.cpp new file mode 100644 index 0000000..90d988d --- /dev/null +++ b/src/Device/GSMDevice.cpp @@ -0,0 +1,87 @@ +#include +#include + +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(GSMData 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(GSMData gsmData) + { + init(); + sendSMSToGSM(gsmData); + + if (CheckSMSError()) + { + sendSMSToGSM(gsmData); + } + + end(); + } + + ~GSMDevice() + { + // close gsmSerial + // close gsmSerial + gsmSerial.end(); + } +}; \ No newline at end of file diff --git a/src/Device/GSMDevice.h b/src/Device/GSMDevice.h new file mode 100644 index 0000000..0bb1171 --- /dev/null +++ b/src/Device/GSMDevice.h @@ -0,0 +1,29 @@ +#ifndef GSMDevice_H +#define GSMDevice_H + +#include +#include +#include + +class GSMDevice +{ +private: + SoftwareSerial gsmSerial; + // Methods + void end(); + void ExecuteAtCommand(const String &atCommand); + String ReadStringSerialPort(); + void sendSMSToGSM(GSMData gsmData); + +public: + // Constructor + GSMDevice(uint8_t pinRx, uint8_t pinTx, long baudRate); + + // Destructor + ~GSMDevice(); + + // Methods + void sendSMS(GSMData gsmData); +}; + +#endif // GSMDevice_H \ No newline at end of file diff --git a/src/Device/WebServer.cpp b/src/Device/WebServer.cpp new file mode 100644 index 0000000..bcae5bf --- /dev/null +++ b/src/Device/WebServer.cpp @@ -0,0 +1,63 @@ +#include +#include + +class WebServer { + private: + byte mac[6]; + IPAddress ip; + EthernetServer server; + + public: + // Constructor + WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port) { + // Initialization code here + memcpy(this->mac, mac, sizeof(this->mac)); + } + + // Initialize the Ethernet and start the server + void begin() { + Ethernet.begin(mac, ip); + server.begin(); + } + + // Handle client requests + void handleClient() { + // Check if a client has connected + EthernetClient client = server.available(); + if (client) { + // An http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // 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 (c == '\n' && currentLineIsBlank) { + // Send a standard http response header + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println("Connection: close"); // The connection will be closed after completion of the response + client.println(); + client.println(""); + client.println(""); + // Add your HTML content here + client.println(""); + break; + } + if (c == '\n') { + // You're starting a new line + currentLineIsBlank = true; + } else if (c != '\r') { + // You've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + // Give the web browser time to receive the data + delay(1); + // Close the connection + client.stop(); + } + } +}; \ No newline at end of file diff --git a/src/Helper/HTMLGenerator.cpp b/src/Helper/HTMLGenerator.cpp new file mode 100644 index 0000000..6d0cea4 --- /dev/null +++ b/src/Helper/HTMLGenerator.cpp @@ -0,0 +1,21 @@ +#include + +class HTMLGenerator { +public: + + static const String GetElement(const String& tag, const String& content, const String& attributes = "") { + String html_content = ""; + if (attributes != "") { + html_content += "<" + tag + " " + attributes + ">" + content + ""; + } else { + html_content += "<" + tag + ">" + content + ""; + } + } + + static const String DisplayPinStatus(const String& status, const String& link, bool isOn) { + String element = isOn ? "H4" : "p"; + String content = status + (link != "" ? " " + (isOn ? "OFF" : "ON") + "" : ""); + return GetElement(element, content); + } + +}; \ No newline at end of file diff --git a/src/Helper/HTMLGenerator.h b/src/Helper/HTMLGenerator.h new file mode 100644 index 0000000..621b028 --- /dev/null +++ b/src/Helper/HTMLGenerator.h @@ -0,0 +1,13 @@ +#ifndef HTMLGENERATOR_H +#define HTMLGENERATOR_H + +#include + +class HTMLGenerator +{ +public: + static const String GetElement(const String &tag, const String &content, const String &attributes = ""); + static const String DisplayPinStatus(const String &status, const String &link, bool isOn); +}; + +#endif // HTMLGENERATOR_H \ No newline at end of file diff --git a/src/PinDefinitions.cpp b/src/PinDefinitions.cpp new file mode 100644 index 0000000..b939d68 --- /dev/null +++ b/src/PinDefinitions.cpp @@ -0,0 +1,41 @@ +#include +#include + +class PinDefinitions +{ +public: + + // PIN OUTPUT + static const int COUNT_OUTPUTS_PIN_OUT = 2; + static PinTypeOut PinIn8; + static PinTypeOut PinIn7; + static const PinTypeOut *INPUT_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT]; + + // PIN INPUT + static const int COUNT_INPUTS_PIN_IN = 2; + static PinTypeIn PinIn2; + static PinTypeIn PinIn3; + static const PinTypeIn *OUPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN]; + + static const uint8_t GSM_PIN_RX = 10; + static const uint8_t GSM_PIN_TX = 11; + static const long GSM_BAUD_RATE = 115200; + + PinDefinitions() + { + // PIN INPUT Inicialization + PinDefinitions::PinIn7 = PinTypeOut("PIN7", 7, LOW, 100, "Default Description"); + PinDefinitions::PinIn8 = PinTypeOut("PIN8", 8, LOW, 10000, "Electric Alarm"); + + INPUT_PINS_ARRAY[0] = &PinDefinitions::PinIn7; + INPUT_PINS_ARRAY[1] = &PinDefinitions::PinIn8; + + // PIN INPUT Inicialization + PinDefinitions::PinIn2 = PinTypeIn("PIN2",2, 100, "Input EZS ALARM"); // 100ms debounce + PinDefinitions::PinIn3 = PinTypeIn("PIN3" ,3, 100, "Input Contact Box"); + + OUPUTS_PINS_ARRAY[0] = &PinDefinitions::PinIn2; + OUPUTS_PINS_ARRAY[1] = &PinDefinitions::PinIn3; + + } +}; diff --git a/src/PinDefinitions.h b/src/PinDefinitions.h new file mode 100644 index 0000000..a755021 --- /dev/null +++ b/src/PinDefinitions.h @@ -0,0 +1,27 @@ +#ifndef PinDefinitions_H +#define PinDefinitions_H + +#include +#include + +class PinDefinitions +{ +public: + static const int COUNT_OUTPUTS_PIN_OUT = 2; + static PinTypeOut PinIn8; + static PinTypeOut PinIn7; + static const PinTypeOut *INPUT_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT]; + + static const int COUNT_INPUTS_PIN_IN = 2; + static PinTypeIn PinIn2; + static PinTypeIn PinIn3; + static const PinTypeIn *OUPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN]; + + static const uint8_t GSM_PIN_RX = 10; + static const uint8_t GSM_PIN_TX = 11; + static const long GSM_BAUD_RATE = 115200; + + PinDefinitions(); +}; + +#endif // PinDefinition_H \ No newline at end of file diff --git a/src/Utills.cpp b/src/Utills.cpp new file mode 100644 index 0000000..2b0046c --- /dev/null +++ b/src/Utills.cpp @@ -0,0 +1,10 @@ +#include + +class Utills +{ + public: + static void logDebug(const String &log) + { + // Serial.println(log); + } +}; \ No newline at end of file diff --git a/src/Utills.h b/src/Utills.h new file mode 100644 index 0000000..39aa952 --- /dev/null +++ b/src/Utills.h @@ -0,0 +1,11 @@ +#ifndef Utills_H +#define Utills_H + +#include + +class Utills { +public: + static void logDebug(const String &log); +}; + +#endif \ No newline at end of file diff --git a/src/Zabezpecovacka-GSM.cpp b/src/Zabezpecovacka-GSM.cpp deleted file mode 100644 index 7c3d3b8..0000000 --- a/src/Zabezpecovacka-GSM.cpp +++ /dev/null @@ -1,337 +0,0 @@ -#include -#include -#include -#include - -#define DEBUG - -// MAC address and IP address for the Ethernet module -byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -IPAddress ip(192, 168, 1, 177); - -// Initialize the Ethernet server (port 80 is default for HTTP) -EthernetServer server(80); - -// Initialize software serial for RS232 communication (RX, TX) -SoftwareSerial gsmSerial(5, 6); // RX, TX (this is using for communicate with GSM module as AT commands) - -uint8_t PIN_OUT_2 = 2; -uint8_t PIN_OUT_3 = 3; - -String ElmP = "p"; -String ElmH2 = "H2"; -EthernetClient client; - -struct BaseTypeScript { - String id; -}; - -// Definice struktury pro ukládání parametrů GET požadavku -struct GsmPostParams : public BaseTypeScript { - String mobileNumber; - String sms; -}; - -//Pin (Vstupni piny) -struct pinState { - uint8_t pinNumber; - volatile bool lastState; - unsigned long debounceTime; - unsigned long lastStateChangeTime; - const char *description; -}; - -const uint8_t numInputPins = 2; // Replace with the actual number of input pins - -// definice ignorace vstupnich pinu -pinState inputStates[numInputPins] = { - // { 4, LOW, 60000, 0, "Enter Door" }, // Pin 4, debounce time 1 minute - { 7, LOW, 30000, 0, "Alarm Alert" }, // Pin 8, debounce time 30 seconds - { 8, LOW, 10000, 0, "Electric Alarm" } // Pin 9, debounce time 10 seconds -}; -GsmPostParams gsmPostParams; - -String readStringFromEEPROM(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; -} - -void logDebug(const String &log) { - // Serial.println(log); -} - -void setup() { - - // if (Serial.available() > 0) { - // Serial.begin(115200); - // } - - gsmSerial.begin(115200); - - // Start the Ethernet connection and the server - Ethernet.begin(mac, ip); - server.begin(); - - gsmPostParams.mobileNumber = readStringFromEEPROM(1, 20); - gsmPostParams.sms = readStringFromEEPROM(21, 200); - gsmPostParams.id = "GSM"; - - logDebug("Init .."); - // Set all input pins as inputs - for (int i = 0; i < numInputPins; i++) { - pinMode(inputStates[i].pinNumber, INPUT); - } - - // Set pin 2 and 3 as OUTPUT - pinMode(PIN_OUT_2, OUTPUT); - pinMode(PIN_OUT_3, OUTPUT); - - logDebug("Init I/O is OK"); - // Open serial communications -} - - -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; -} - -// Metoda pro parsování parametrů - -String getValue(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()); - logDebug("Only value:" + value); - return removeAllCharactesrAfterEmpty(value); -} - -void writeToLogAndIntoWebAsP(const String &msg, const String &element) { - String lineElement = "<" + element + ">" + msg + ""; - // logDebug(msg); - //gsmlogDebug("GSM:" + msg); - client.println(lineElement); -} - -// pin = definice pinu Arduino, string je post hodnota, ktera se porovnava -void checkValue(String &request, uint8_t pin, const String &postValue) { - if (request.indexOf("/PIN") == -1) return; - - if (request.indexOf("/" + postValue + "=ON") != -1) { - digitalWrite(pin, HIGH); - String msgOn = "PIN ON: " + String(pin) + " PostVale: " + postValue; - writeToLogAndIntoWebAsP(msgOn, ElmH2); // Send state to debug console - } - if (request.indexOf("/" + postValue + "=OFF") != -1) { - digitalWrite(pin, LOW); - String msgOff = "PIN OFF: " + String(pin) + " PostVale: " + postValue; - writeToLogAndIntoWebAsP(msgOff, ElmH2); // Send state to debug console - } -} - - - -String ConvertStatusToString(uint8_t &pin, bool showLink) { - String pinAsStr = String(pin); - String elm = ""; - if (digitalRead(pin) == HIGH) { - elm = "PIN" + pinAsStr + " is ON "; - if (showLink) elm += "OFF"; - return elm; - } - - elm = "PIN" + pinAsStr + " is OFF "; - if (showLink) elm += "ON"; - return elm; -} - -void changeDataIfNotNull(String &variable, String &data) { - if (data == "") return; - variable = data; -} - -void SednAtCommandWithLog(String msg) { - logDebug(msg); - gsmSerial.print(msg + "\r"); - delay(400); - logDebug(gsmSerial.readString()); -} - -void SendSMS(String pin) { - // Set the SIM900A to text mode - SednAtCommandWithLog("AT+CMGF=1"); - delay(400); - - SednAtCommandWithLog("AT + CMGS =\"" + gsmPostParams.mobileNumber + "\""); - delay(400); - - // Specify the SMS message - SednAtCommandWithLog(pin + " " + gsmPostParams.sms); - delay(1000); - - // Send a Ctrl+Z / ASCII SUB - gsmSerial.print((char)26); - delay(1000); - // Close the connection - gsmSerial.print((char)27); - delay(1000); - logDebug("Close communication GSM"); - // String msg = gsmSerial.readStringUntil("/n"); - // gsmlogDebug("Msg:"+msg); -} - -void writeStringToEEPROM(int addr, const String &data, int limit) { - uint8_t endIndex =0; - int numBytes = data.length(); // get length of the string - for (uint8_t i = 0; i < numBytes && i < limit; i++) { - EEPROM.write(addr + i, data[i]); // write one byte at a time - endIndex = i; - } - if (endIndex < limit) { - EEPROM.write(addr + endIndex + 1, '\n'); - } -} - -void SetPin(uint8_t pin, const char *description) { - SendSMS("PIN:" + String(pin) + "|" + description); -} - - -void checkInputPins() { - unsigned long currentTime = millis(); - - for (int i = 0; i < numInputPins; i++) { - pinState *currentState = &inputStates[i]; // Pointer to current pin state - - bool currentInputState = digitalRead(currentState->pinNumber); - if (currentInputState != currentState->lastState && currentTime - currentState->lastStateChangeTime >= currentState->debounceTime) { - // Input state change confirmed after debounce time - currentState->lastState = currentInputState; - - if (currentInputState) { - - // Call SetPin function (modify for specific pin control) - SetPin(currentState->pinNumber, currentState->description); // Example usage with pin number passed - } - currentState->lastStateChangeTime = currentTime; - } - } -} - -int iterationCounter = 0; // Counter for loop iterations - - - -void loop() { - // Listen for incoming clients - String msg = ""; - client = server.available(); - - if (client) { - String request = client.readStringUntil('\r'); - - logDebug("request:" + request); - - if (request.indexOf("GET /?id=GSM") != -1) { - logDebug("it is GSM"); - //int start = request.indexOf("/?id=") + 2; - String data = request.substring(6); - logDebug("data:" + data); - - String id = getValue(data, "id="); - logDebug("id data:" + id); - - String value = getValue(data, "mobileNumber="); - changeDataIfNotNull(gsmPostParams.mobileNumber, value); - - value = getValue(data, "sms="); - changeDataIfNotNull(gsmPostParams.sms, value); - msg += "PARSING GMS DATA SUCESSFULLY"; - } - - logDebug("struc GSM:" + gsmPostParams.mobileNumber + " sms:" + gsmPostParams.sms); - - if (request.indexOf("GET /SEND-SMS") != -1) { - msg += "SEND SMS TROUGHT GSM. "; - } - - if (request.indexOf("GET /SAVE-EEPROM") != -1) { - writeStringToEEPROM(1, gsmPostParams.mobileNumber, 20); - writeStringToEEPROM(21, gsmPostParams.sms, 200); - msg += "Update to EEPROM is sucessfully. "; - } - - logDebug("Request:" + request); - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connection: close"); - client.println(); - client.println(""); - client.println("
"); - - client.println("

Trochta.net:

"); - - writeToLogAndIntoWebAsP(msg, "H3"); - //nastav jestli je post - checkValue(request, PIN_OUT_2, "PIN2"); - checkValue(request, PIN_OUT_3, "PIN3"); - - // Vypis aktualni hodnoty - writeToLogAndIntoWebAsP("Input:", ElmP); - - for (int i = 0; i < numInputPins; i++) { - pinState *currentState = &inputStates[i]; - writeToLogAndIntoWebAsP(ConvertStatusToString(currentState->pinNumber, false), ElmP); - } - - writeToLogAndIntoWebAsP("Output:", ElmP); - writeToLogAndIntoWebAsP(ConvertStatusToString(PIN_OUT_2, true), ElmP); - writeToLogAndIntoWebAsP(ConvertStatusToString(PIN_OUT_3, true), ElmP); - - writeToLogAndIntoWebAsP("GSM Number:" + gsmPostParams.mobileNumber, ElmP); - writeToLogAndIntoWebAsP("SMS:" + gsmPostParams.sms, ElmP); - - writeToLogAndIntoWebAsP("Send SMS", "H4"); - writeToLogAndIntoWebAsP("Save to EEPROM", ElmP); - writeToLogAndIntoWebAsP("Refresh", ElmP); - // Send the time and the message that the page is loaded - client.println("
"); - // Give the web browser time to receive the data - client.stop(); - - logDebug("Time: " + String(millis() / 1000)); - - if (request.indexOf("GET /SEND-SMS") != -1) { - SendSMS("Web Link (Test)"); - } - // Zjkontroluje vstupni piny - - delay(5); - } - - iterationCounter++; - if (iterationCounter == INT_FAST16_MAX) iterationCounter = 0; - if (iterationCounter % 5 == 0) { // Check if 10 iterations have passed - checkInputPins(); // Call checkInputPins() every 10 iterations - } - - delay(100); -} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0e04f3a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Devices + +GSMDevice gsmDevice = GSMDevice(PinDefinitions::GSM_PIN_RX, PinDefinitions::GSM_PIN_TX, PinDefinitions::GSM_BAUD_RATE); +// Pin Input +GSMData gsmData = GSMData("GSM"); + +void setup() +{ +} + +void loop() +{ +} From 37c0c733376364227ba7d08ce6844c48740098a4 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Mon, 22 Apr 2024 01:42:36 +0200 Subject: [PATCH 3/8] No working separe to class --- .vscode/settings.json | 6 +- .vscode/tasks.json | 14 ++++ platformio.ini | 3 +- src/Data/GSMData.cpp | 30 -------- src/Data/GSMData.h | 22 ------ src/Data/GSMMobileData.cpp | 43 +++++++++++ src/Data/GSMMobileData.h | 20 ++++++ src/Data/PinTypeOut.cpp | 14 +--- src/Data/WebServerData.cpp | 47 ++++++++++++ src/Data/WebServerData.h | 19 +++++ src/Device/EEPROMUtils.h | 17 ++--- src/Device/GSMDevice.cpp | 19 ++--- src/Device/GSMDevice.h | 14 ++-- src/Device/WebServer.cpp | 113 +++++++++++++++-------------- src/Device/WebServer.h | 33 +++++++++ src/Helper/HTMLGenerator.cpp | 24 ++++--- src/PinDefinitions.cpp | 41 ----------- src/PinDefinitions.h | 27 ------- src/Utills.cpp | 4 +- src/Utills.h | 7 +- src/main.cpp | 135 ++++++++++++++++++++++++++++++++--- 21 files changed, 411 insertions(+), 241 deletions(-) create mode 100644 .vscode/tasks.json delete mode 100644 src/Data/GSMData.cpp delete mode 100644 src/Data/GSMData.h create mode 100644 src/Data/GSMMobileData.cpp create mode 100644 src/Data/GSMMobileData.h create mode 100644 src/Data/WebServerData.cpp create mode 100644 src/Data/WebServerData.h create mode 100644 src/Device/WebServer.h delete mode 100644 src/PinDefinitions.cpp delete mode 100644 src/PinDefinitions.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 76ca929..55532de 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,7 @@ { "files.associations": { - "pinsdefinitions": "c" + "pinsdefinitions": "c", } -} \ No newline at end of file + + +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ada4221 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,14 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "PlatformIO", + "task": "Build (uno)", + "problemMatcher": [ + "$platformio" + ], + "group": "build", + "label": "PlatformIO: Build (uno)" + } + ] +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index bc9bc01..1069e24 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,4 +15,5 @@ framework = arduino lib_deps = arduino-libraries/Ethernet@^2.0.2 featherfly/SoftwareSerial@^1.0 -build_flags = -DNDEBUG +monitor_speed = 9600 + diff --git a/src/Data/GSMData.cpp b/src/Data/GSMData.cpp deleted file mode 100644 index 3344b1c..0000000 --- a/src/Data/GSMData.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include - -class GSMData : public DeviceBase -{ - -public: - String MobileNumber; - String Sms; - - GSMData(String id): DeviceBase(id) - { - MobileNumber = EEPROMUtils::ReadString(1, 20); - Sms = EEPROMUtils::ReadString(21, 100); - } - - void updateMobileNumber(const String &newMobileNumber) - { - MobileNumber = newMobileNumber; - EEPROMUtils::WriteString(1, newMobileNumber, 20); - } - - void updateSMS(const String &newSMS) - { - Sms = newSMS; - EEPROMUtils::WriteString(21, newSMS, 100); - } -}; diff --git a/src/Data/GSMData.h b/src/Data/GSMData.h deleted file mode 100644 index a8f3bf9..0000000 --- a/src/Data/GSMData.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef GSMData_H -#define GSMData_H - -#include -#include -#include -#include - -class GSMData : public DeviceBase -{ -public: - String MobileNumber; - String Sms; - - GSMData(String id): DeviceBase(id){}; - - void updateMobileNumber(const String &newMobileNumber); - - void updateSMS(const String &newSMS); -}; - -#endif // GSMData_H \ No newline at end of file diff --git a/src/Data/GSMMobileData.cpp b/src/Data/GSMMobileData.cpp new file mode 100644 index 0000000..589887a --- /dev/null +++ b/src/Data/GSMMobileData.cpp @@ -0,0 +1,43 @@ +#include "Data/DeviceBase.h" +#include "Device/EEPROMUtils.h" +#include + +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); + } + } +}; diff --git a/src/Data/GSMMobileData.h b/src/Data/GSMMobileData.h new file mode 100644 index 0000000..6147c68 --- /dev/null +++ b/src/Data/GSMMobileData.h @@ -0,0 +1,20 @@ +#ifndef GSMMobileData_H +#define GSMMobileData_H + +#include +#include "Data/DeviceBase.h" + +class GSMMobileData : public DeviceBase +{ +public: + String MobileNumber; + String Sms; + + GSMMobileData(String id): DeviceBase(id){}; + + void updateMobileNumber(const String &newMobileNumber); + + void updateSMS(const String &newSMS); +}; + +#endif // GSMMobileData_H \ No newline at end of file diff --git a/src/Data/PinTypeOut.cpp b/src/Data/PinTypeOut.cpp index e322466..9bd5955 100644 --- a/src/Data/PinTypeOut.cpp +++ b/src/Data/PinTypeOut.cpp @@ -18,19 +18,7 @@ public: pinMode(pinNumber, OUTPUT); } - void checkPin() - { - bool currentInputState = digitalRead(pinNumber); - if (currentInputState != lastState && millis() - lastStateChangeTime >= debounceTime) - { - lastState = currentInputState; - lastStateChangeTime = millis(); - Utills::logDebug("Pin " + String(pinNumber) + " changed to " + String(lastState)); - // Add pin state change logic here - } - } - - bool IsTimeIgnoredPin() + bool IsTimeIgnoredPin() { bool currentInputState = digitalRead(pinNumber); if (currentInputState != lastState && millis() - lastStateChangeTime >= debounceTime) diff --git a/src/Data/WebServerData.cpp b/src/Data/WebServerData.cpp new file mode 100644 index 0000000..a7e41e4 --- /dev/null +++ b/src/Data/WebServerData.cpp @@ -0,0 +1,47 @@ +#include + +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; + } +}; diff --git a/src/Data/WebServerData.h b/src/Data/WebServerData.h new file mode 100644 index 0000000..5d337c7 --- /dev/null +++ b/src/Data/WebServerData.h @@ -0,0 +1,19 @@ +#ifndef WebServerData_H +#define WebServerData_H + +#include + +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 \ No newline at end of file diff --git a/src/Device/EEPROMUtils.h b/src/Device/EEPROMUtils.h index de1ce2c..b3c4d8b 100644 --- a/src/Device/EEPROMUtils.h +++ b/src/Device/EEPROMUtils.h @@ -1,16 +1,11 @@ -#ifndef _EEPROMUtils_h -#define _EEPROMUtils_h +#ifndef EEPROMUtils_h +#define EEPROMUtils_h -#include -#include -#include - - - class EEPROMUtils +class EEPROMUtils { public: - static String ReadString(int addr, int length); - static void WriteString(int addr, const String &data, int limit); + static String ReadString(int addr, int length); + static void WriteString(int addr, const String &data, int limit); }; - #endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/src/Device/GSMDevice.cpp b/src/Device/GSMDevice.cpp index 90d988d..2c6a9ca 100644 --- a/src/Device/GSMDevice.cpp +++ b/src/Device/GSMDevice.cpp @@ -1,4 +1,5 @@ -#include +#include "Data/GSMMobileData.h" +#include "Utills.h" #include class GSMDevice @@ -24,7 +25,7 @@ private: return gsmSerial.readString(); } - void sendSMSToGSM(GSMData gsmData) + void sendSMSToGSM(GSMMobileData gsmData) { Utills::logDebug("SMS Start SEND SMS Id:" + gsmData.Id + " M:" + gsmData.MobileNumber + " with message " + gsmData.Sms); @@ -65,7 +66,7 @@ public: } - void sendSMS(GSMData gsmData) + void sendSMS(GSMMobileData gsmData) { init(); sendSMSToGSM(gsmData); @@ -78,10 +79,10 @@ public: end(); } - ~GSMDevice() - { - // close gsmSerial - // close gsmSerial - gsmSerial.end(); - } + // ~GSMDevice() + // { + // // close gsmSerial + // // close gsmSerial + // gsmSerial.end(); + // } }; \ No newline at end of file diff --git a/src/Device/GSMDevice.h b/src/Device/GSMDevice.h index 0bb1171..b7b7bd4 100644 --- a/src/Device/GSMDevice.h +++ b/src/Device/GSMDevice.h @@ -1,9 +1,9 @@ #ifndef GSMDevice_H #define GSMDevice_H -#include -#include -#include +#include "Data/GSMMobileData.h" +#include "SoftwareSerial.h" + class GSMDevice { @@ -13,17 +13,17 @@ private: void end(); void ExecuteAtCommand(const String &atCommand); String ReadStringSerialPort(); - void sendSMSToGSM(GSMData gsmData); + void sendSMSToGSM(GSMMobileData gsmData); public: // Constructor - GSMDevice(uint8_t pinRx, uint8_t pinTx, long baudRate); + GSMDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): gsmSerial(pinRx, pinTx){}; // Destructor - ~GSMDevice(); + // ~GSMDevice(); // Methods - void sendSMS(GSMData gsmData); + void sendSMS(GSMMobileData gsmData); }; #endif // GSMDevice_H \ No newline at end of file diff --git a/src/Device/WebServer.cpp b/src/Device/WebServer.cpp index bcae5bf..5af0050 100644 --- a/src/Device/WebServer.cpp +++ b/src/Device/WebServer.cpp @@ -1,63 +1,68 @@ #include #include +#include "Data/WebServerData.h" -class WebServer { - private: - byte mac[6]; - IPAddress ip; - EthernetServer server; +class WebServer +{ +private: + byte mac[6]; + IPAddress ip; + EthernetServer server; + // Event + // Define the callback function type - public: - // Constructor - WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port) { - // Initialization code here - memcpy(this->mac, mac, sizeof(this->mac)); - } + // Declare a function pointer + // void (*Callback)(WebServerData) = WebBrowserDataCallback; +public: + EthernetClient client; + // Constructor + WebServer() : server(80) {} - // Initialize the Ethernet and start the server - void begin() { - Ethernet.begin(mac, ip); - server.begin(); - } + WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port) + { + memcpy(this->mac, mac, sizeof(this->mac)); + } - // Handle client requests - void handleClient() { - // Check if a client has connected - EthernetClient client = server.available(); - if (client) { - // An http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - // 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 (c == '\n' && currentLineIsBlank) { - // Send a standard http response header - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connection: close"); // The connection will be closed after completion of the response - client.println(); - client.println(""); - client.println(""); - // Add your HTML content here - client.println(""); - break; - } - if (c == '\n') { - // You're starting a new line - currentLineIsBlank = true; - } else if (c != '\r') { - // You've gotten a character on the current line - currentLineIsBlank = false; - } - } + // 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 - delay(1); - // Close the connection - client.stop(); } + // Give the web browser time to receive the data } -}; \ No newline at end of file + + 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(); + } + } +}; diff --git a/src/Device/WebServer.h b/src/Device/WebServer.h new file mode 100644 index 0000000..fe7668a --- /dev/null +++ b/src/Device/WebServer.h @@ -0,0 +1,33 @@ +#ifndef WebServer_H +#define WebServer_H + +#include +#include +#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 \ No newline at end of file diff --git a/src/Helper/HTMLGenerator.cpp b/src/Helper/HTMLGenerator.cpp index 6d0cea4..3a8c007 100644 --- a/src/Helper/HTMLGenerator.cpp +++ b/src/Helper/HTMLGenerator.cpp @@ -1,21 +1,25 @@ #include -class HTMLGenerator { +class HTMLGenerator +{ public: - - static const String GetElement(const String& tag, const String& content, const String& attributes = "") { + static const String GetElement(const String &tag, const String &content, const String &attributes = "") + { String html_content = ""; - if (attributes != "") { - html_content += "<" + tag + " " + attributes + ">" + content + ""; - } else { - html_content += "<" + tag + ">" + content + ""; + if (attributes != "") + { + return "<" + tag + " " + attributes + ">" + content + ""; } + else + { + return "<" + tag + ">" + content + ""; + } } - static const String DisplayPinStatus(const String& status, const String& link, bool isOn) { + static const String DisplayPinStatus(const String &status, const String &link, bool isOn) + { String element = isOn ? "H4" : "p"; String content = status + (link != "" ? " " + (isOn ? "OFF" : "ON") + "" : ""); - return GetElement(element, content); + return GetElement(element, content); } - }; \ No newline at end of file diff --git a/src/PinDefinitions.cpp b/src/PinDefinitions.cpp deleted file mode 100644 index b939d68..0000000 --- a/src/PinDefinitions.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -class PinDefinitions -{ -public: - - // PIN OUTPUT - static const int COUNT_OUTPUTS_PIN_OUT = 2; - static PinTypeOut PinIn8; - static PinTypeOut PinIn7; - static const PinTypeOut *INPUT_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT]; - - // PIN INPUT - static const int COUNT_INPUTS_PIN_IN = 2; - static PinTypeIn PinIn2; - static PinTypeIn PinIn3; - static const PinTypeIn *OUPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN]; - - static const uint8_t GSM_PIN_RX = 10; - static const uint8_t GSM_PIN_TX = 11; - static const long GSM_BAUD_RATE = 115200; - - PinDefinitions() - { - // PIN INPUT Inicialization - PinDefinitions::PinIn7 = PinTypeOut("PIN7", 7, LOW, 100, "Default Description"); - PinDefinitions::PinIn8 = PinTypeOut("PIN8", 8, LOW, 10000, "Electric Alarm"); - - INPUT_PINS_ARRAY[0] = &PinDefinitions::PinIn7; - INPUT_PINS_ARRAY[1] = &PinDefinitions::PinIn8; - - // PIN INPUT Inicialization - PinDefinitions::PinIn2 = PinTypeIn("PIN2",2, 100, "Input EZS ALARM"); // 100ms debounce - PinDefinitions::PinIn3 = PinTypeIn("PIN3" ,3, 100, "Input Contact Box"); - - OUPUTS_PINS_ARRAY[0] = &PinDefinitions::PinIn2; - OUPUTS_PINS_ARRAY[1] = &PinDefinitions::PinIn3; - - } -}; diff --git a/src/PinDefinitions.h b/src/PinDefinitions.h deleted file mode 100644 index a755021..0000000 --- a/src/PinDefinitions.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef PinDefinitions_H -#define PinDefinitions_H - -#include -#include - -class PinDefinitions -{ -public: - static const int COUNT_OUTPUTS_PIN_OUT = 2; - static PinTypeOut PinIn8; - static PinTypeOut PinIn7; - static const PinTypeOut *INPUT_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT]; - - static const int COUNT_INPUTS_PIN_IN = 2; - static PinTypeIn PinIn2; - static PinTypeIn PinIn3; - static const PinTypeIn *OUPUTS_PINS_ARRAY[COUNT_INPUTS_PIN_IN]; - - static const uint8_t GSM_PIN_RX = 10; - static const uint8_t GSM_PIN_TX = 11; - static const long GSM_BAUD_RATE = 115200; - - PinDefinitions(); -}; - -#endif // PinDefinition_H \ No newline at end of file diff --git a/src/Utills.cpp b/src/Utills.cpp index 2b0046c..05219bc 100644 --- a/src/Utills.cpp +++ b/src/Utills.cpp @@ -3,8 +3,8 @@ class Utills { public: - static void logDebug(const String &log) + static void logDebug(const String& log) { - // Serial.println(log); + Serial.println(log); } }; \ No newline at end of file diff --git a/src/Utills.h b/src/Utills.h index 39aa952..b9d25f2 100644 --- a/src/Utills.h +++ b/src/Utills.h @@ -1,11 +1,12 @@ #ifndef Utills_H #define Utills_H -#include - class Utills { public: - static void logDebug(const String &log); +static void logDebug(const String& log) { + // Implementation of the logDebug function goes here + // You can add your desired functionality inside this function +} }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0e04f3a..b09afd5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,140 @@ -#include -#include +#include "Device/GSMDevice.h" +#include "Device/WebServer.h" +#include "Data/PinTypeIn.h" +#include "Data/PinTypeOut.h" +#include "Utills.h" #include +#include #include -#include -#include -#include -#include -#include +// 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 const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {PinIn8, PinIn7}; + +// 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}; + +// 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; + +// WebServer Data +static byte WebServerMac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; +static IPAddress WebServerIP = IPAddress(192, 168, 1, 177); +static const byte WebServerPort = 80; // Devices -GSMDevice gsmDevice = GSMDevice(PinDefinitions::GSM_PIN_RX, PinDefinitions::GSM_PIN_TX, PinDefinitions::GSM_BAUD_RATE); +GSMDevice gsmDevice = GSMDevice(GSM_PIN_RX, GSM_PIN_TX, GSM_BAUD_RATE); // Pin Input -GSMData gsmData = GSMData("GSM"); +GSMMobileData gsmData = GSMMobileData("GSM"); +WebServer webServer; + + +void SeparateDataFromRequest(WebServerData &request) +{ + if (request.Request.indexOf("GET /?id=GSM") != -1) + { + Utills::logDebug("it is GSM"); + // int start = request.indexOf("/?id=") + 2; + String data = request.Request.substring(6); + Utills::logDebug("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) + { + String pinName = request.Request.substring(5, 4); + Utills::logDebug("Update Data from GET as PIN : " + pinName); + + // foreach OUPUTS_PINS_ARRAY + for (int i = 0; i < COUNT_OUTPUTS_PIN_OUT; i++) + { + PinTypeOut pinValue = OUPUTS_PINS_ARRAY[i]; + if (pinValue.Id == pinName) + { + + Utills::logDebug("Update Data from GET as PIN : " + pinName); + if (request.Request.indexOf("=ON") != -1) + { + pinValue.SetOutputSignalToHighWithtimeIgnoring(); + } + + if (request.Request.indexOf("=OFF") != -1) + { + pinValue.SetOutputSignalToLowWithtimeIgnoring(); + } + } + } + } +} + +void WebServerEvent(WebServerData data) +{ + // Your function implementation here + Utills::logDebug("Request: " + data.Request); + SeparateDataFromRequest(data); +} void setup() { + webServer = WebServer(WebServerMac, WebServerIP, WebServerPort); + // serial monitori init and set baud rate + Serial.begin(9600); + webServer.Init(); + Utills::logDebug("Called: Setup"); } +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) +{ + EthernetClient client = data.Client; + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println("Connection: close"); + client.println(); + client.println(""); + client.println("
"); + + client.println("

Trochta.net:

"); + client.println("
"); + client.stop(); +} + +int main() +{ + setup(); + while (true) + { + loop(); + } + return 0; +} \ No newline at end of file From c32905663b96d4971a8c0ca3c31938a8be17ae6b Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Wed, 24 Apr 2024 03:29:50 +0200 Subject: [PATCH 4/8] Working code - rewrite classes and bussines logic --- .vscode/settings.json | 1 + {src => lib}/.theia/launch.json | 0 lib/Data/DeviceBase.cpp | 3 + lib/Data/DeviceBase.h | 14 +++ lib/Data/GSMMobileData.cpp | 50 +++++++++++ {src => lib}/Data/GSMMobileData.h | 8 +- lib/Data/PinTypeIn.cpp | 8 ++ lib/Data/PinTypeIn.h | 31 +++++++ lib/Data/PinTypeOut.cpp | 21 +++++ lib/Data/PinTypeOut.h | 37 ++++++++ lib/Data/WebServerData.cpp | 48 +++++++++++ lib/Data/WebServerData.h | 22 +++++ lib/Device/EEPROMUtils.cpp | 32 +++++++ lib/Device/EEPROMUtils.h | 13 +++ lib/Device/GSMMobileDevice.cpp | 82 ++++++++++++++++++ lib/Device/GSMMobileDevice.h | 31 +++++++ lib/Device/WebServer.cpp | 66 ++++++++++++++ lib/Device/WebServer.h | 25 ++++++ lib/Helper/DebugLog.cpp | 12 +++ lib/Helper/DebugLog.h | 13 +++ lib/Helper/HTMLGenerator.cpp | 22 +++++ {src => lib}/Helper/HTMLGenerator.h | 2 +- lib/Helper/Helper.cpp | 29 +++++++ lib/Helper/Helper.h | 17 ++++ src/Data/DeviceBase.cpp | 9 -- src/Data/DeviceBase.h | 13 --- src/Data/GSMMobileData.cpp | 43 ---------- src/Data/PinTypeIn.cpp | 57 ------------- src/Data/PinTypeIn.h | 23 ----- src/Data/PinTypeOut.cpp | 59 ------------- src/Data/PinTypeOut.h | 32 ------- src/Data/WebServerData.cpp | 47 ---------- src/Data/WebServerData.h | 19 ----- src/Device/EEPROMUtils.cpp | 34 -------- src/Device/EEPROMUtils.h | 11 --- src/Device/GSMDevice.cpp | 88 ------------------- src/Device/GSMDevice.h | 29 ------- src/Device/WebServer.cpp | 68 --------------- src/Device/WebServer.h | 33 ------- src/Helper/HTMLGenerator.cpp | 25 ------ src/Utills.cpp | 10 --- src/Utills.h | 12 --- src/main.cpp | 128 ++++++++++++++++------------ 43 files changed, 658 insertions(+), 669 deletions(-) rename {src => lib}/.theia/launch.json (100%) create mode 100644 lib/Data/DeviceBase.cpp create mode 100644 lib/Data/DeviceBase.h create mode 100644 lib/Data/GSMMobileData.cpp rename {src => lib}/Data/GSMMobileData.h (68%) create mode 100644 lib/Data/PinTypeIn.cpp create mode 100644 lib/Data/PinTypeIn.h create mode 100644 lib/Data/PinTypeOut.cpp create mode 100644 lib/Data/PinTypeOut.h create mode 100644 lib/Data/WebServerData.cpp create mode 100644 lib/Data/WebServerData.h create mode 100644 lib/Device/EEPROMUtils.cpp create mode 100644 lib/Device/EEPROMUtils.h create mode 100644 lib/Device/GSMMobileDevice.cpp create mode 100644 lib/Device/GSMMobileDevice.h create mode 100644 lib/Device/WebServer.cpp create mode 100644 lib/Device/WebServer.h create mode 100644 lib/Helper/DebugLog.cpp create mode 100644 lib/Helper/DebugLog.h create mode 100644 lib/Helper/HTMLGenerator.cpp rename {src => lib}/Helper/HTMLGenerator.h (67%) create mode 100644 lib/Helper/Helper.cpp create mode 100644 lib/Helper/Helper.h delete mode 100644 src/Data/DeviceBase.cpp delete mode 100644 src/Data/DeviceBase.h delete mode 100644 src/Data/GSMMobileData.cpp delete mode 100644 src/Data/PinTypeIn.cpp delete mode 100644 src/Data/PinTypeIn.h delete mode 100644 src/Data/PinTypeOut.cpp delete mode 100644 src/Data/PinTypeOut.h delete mode 100644 src/Data/WebServerData.cpp delete mode 100644 src/Data/WebServerData.h delete mode 100644 src/Device/EEPROMUtils.cpp delete mode 100644 src/Device/EEPROMUtils.h delete mode 100644 src/Device/GSMDevice.cpp delete mode 100644 src/Device/GSMDevice.h delete mode 100644 src/Device/WebServer.cpp delete mode 100644 src/Device/WebServer.h delete mode 100644 src/Helper/HTMLGenerator.cpp delete mode 100644 src/Utills.cpp delete mode 100644 src/Utills.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 55532de..30f1af8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { "pinsdefinitions": "c", + "new": "cpp" } diff --git a/src/.theia/launch.json b/lib/.theia/launch.json similarity index 100% rename from src/.theia/launch.json rename to lib/.theia/launch.json diff --git a/lib/Data/DeviceBase.cpp b/lib/Data/DeviceBase.cpp new file mode 100644 index 0000000..d3b8c10 --- /dev/null +++ b/lib/Data/DeviceBase.cpp @@ -0,0 +1,3 @@ +#include "DeviceBase.h" + + DeviceBase::DeviceBase(const String id): Id(id) {}; \ No newline at end of file diff --git a/lib/Data/DeviceBase.h b/lib/Data/DeviceBase.h new file mode 100644 index 0000000..f6e336f --- /dev/null +++ b/lib/Data/DeviceBase.h @@ -0,0 +1,14 @@ +#ifndef _DeviceBase_h +#define _DeviceBase_h + +#include + +class DeviceBase +{ +public: + String Id; + DeviceBase(const String id); + +}; + +#endif \ No newline at end of file diff --git a/lib/Data/GSMMobileData.cpp b/lib/Data/GSMMobileData.cpp new file mode 100644 index 0000000..dc330b9 --- /dev/null +++ b/lib/Data/GSMMobileData.cpp @@ -0,0 +1,50 @@ + +#include +#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); + } +} diff --git a/src/Data/GSMMobileData.h b/lib/Data/GSMMobileData.h similarity index 68% rename from src/Data/GSMMobileData.h rename to lib/Data/GSMMobileData.h index 6147c68..db64a58 100644 --- a/src/Data/GSMMobileData.h +++ b/lib/Data/GSMMobileData.h @@ -2,15 +2,19 @@ #define GSMMobileData_H #include -#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); diff --git a/lib/Data/PinTypeIn.cpp b/lib/Data/PinTypeIn.cpp new file mode 100644 index 0000000..17170a2 --- /dev/null +++ b/lib/Data/PinTypeIn.cpp @@ -0,0 +1,8 @@ +#include +#include "PinTypeIn.h" + + +bool PinTypeIn::IsPinHigh() +{ + return digitalRead(_pin) == HIGH ? true : false; +} diff --git a/lib/Data/PinTypeIn.h b/lib/Data/PinTypeIn.h new file mode 100644 index 0000000..33709eb --- /dev/null +++ b/lib/Data/PinTypeIn.h @@ -0,0 +1,31 @@ +#ifndef PinTypeIn_H +#define PinTypeIn_H + +#include +#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 \ No newline at end of file diff --git a/lib/Data/PinTypeOut.cpp b/lib/Data/PinTypeOut.cpp new file mode 100644 index 0000000..824dc50 --- /dev/null +++ b/lib/Data/PinTypeOut.cpp @@ -0,0 +1,21 @@ +#include "PinTypeOut.h" +#include +#include "DebugLog.h" + + +void PinTypeOut::setOutputSignalToHigh() +{ + digitalWrite(_pinNumber, HIGH); + lastState = true; +} + +void PinTypeOut::setOutputSignalToLow() +{ + digitalWrite(_pinNumber, LOW); + lastState = false; +} + +bool PinTypeOut::IsPinOn() +{ + return lastState; +} \ No newline at end of file diff --git a/lib/Data/PinTypeOut.h b/lib/Data/PinTypeOut.h new file mode 100644 index 0000000..7346bbb --- /dev/null +++ b/lib/Data/PinTypeOut.h @@ -0,0 +1,37 @@ +#ifndef PINTYPEOUT_H +#define PINTYPEOUT_H + +#include +#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 \ No newline at end of file diff --git a/lib/Data/WebServerData.cpp b/lib/Data/WebServerData.cpp new file mode 100644 index 0000000..e4c53bf --- /dev/null +++ b/lib/Data/WebServerData.cpp @@ -0,0 +1,48 @@ +#include "WebServerData.h" +#include +#include + +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; +} diff --git a/lib/Data/WebServerData.h b/lib/Data/WebServerData.h new file mode 100644 index 0000000..8f6038e --- /dev/null +++ b/lib/Data/WebServerData.h @@ -0,0 +1,22 @@ +#ifndef WebServerData_H +#define WebServerData_H + +#include +#include + +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 \ No newline at end of file diff --git a/lib/Device/EEPROMUtils.cpp b/lib/Device/EEPROMUtils.cpp new file mode 100644 index 0000000..cdeaae7 --- /dev/null +++ b/lib/Device/EEPROMUtils.cpp @@ -0,0 +1,32 @@ +#include "EEPROMUtils.h" +#include +#include + + +// 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 + } +} diff --git a/lib/Device/EEPROMUtils.h b/lib/Device/EEPROMUtils.h new file mode 100644 index 0000000..f7aded6 --- /dev/null +++ b/lib/Device/EEPROMUtils.h @@ -0,0 +1,13 @@ +#ifndef EEPROMUtils_h +#define EEPROMUtils_h + +#include // 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 \ No newline at end of file diff --git a/lib/Device/GSMMobileDevice.cpp b/lib/Device/GSMMobileDevice.cpp new file mode 100644 index 0000000..54f0578 --- /dev/null +++ b/lib/Device/GSMMobileDevice.cpp @@ -0,0 +1,82 @@ +#include "GSMMobileData.h" +#include +#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(); +// } \ No newline at end of file diff --git a/lib/Device/GSMMobileDevice.h b/lib/Device/GSMMobileDevice.h new file mode 100644 index 0000000..0f34415 --- /dev/null +++ b/lib/Device/GSMMobileDevice.h @@ -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 \ No newline at end of file diff --git a/lib/Device/WebServer.cpp b/lib/Device/WebServer.cpp new file mode 100644 index 0000000..b0856ba --- /dev/null +++ b/lib/Device/WebServer.cpp @@ -0,0 +1,66 @@ +#include +#include +#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(); + } +} diff --git a/lib/Device/WebServer.h b/lib/Device/WebServer.h new file mode 100644 index 0000000..bc1d738 --- /dev/null +++ b/lib/Device/WebServer.h @@ -0,0 +1,25 @@ +#ifndef WebServer_H +#define WebServer_H + +#include +#include +#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 \ No newline at end of file diff --git a/lib/Helper/DebugLog.cpp b/lib/Helper/DebugLog.cpp new file mode 100644 index 0000000..73058cb --- /dev/null +++ b/lib/Helper/DebugLog.cpp @@ -0,0 +1,12 @@ +#include +#include "DebugLog.h" + +void DebugLog::log(const String &log) +{ + Serial.println(log); +} + +void DebugLog::init(const long &bound) +{ + Serial.begin(bound); +} diff --git a/lib/Helper/DebugLog.h b/lib/Helper/DebugLog.h new file mode 100644 index 0000000..0cce7aa --- /dev/null +++ b/lib/Helper/DebugLog.h @@ -0,0 +1,13 @@ +#ifndef DebugLog_H +#define DebugLog_H + +#include + +class DebugLog +{ +public: + static void init(const long &bound); + static void log(const String &log); +}; + +#endif \ No newline at end of file diff --git a/lib/Helper/HTMLGenerator.cpp b/lib/Helper/HTMLGenerator.cpp new file mode 100644 index 0000000..16b150f --- /dev/null +++ b/lib/Helper/HTMLGenerator.cpp @@ -0,0 +1,22 @@ +#include +#include "HTMLGenerator.h" + +const String HTMLGenerator::GetElement(const String &tag, String &content, const String &attributes) +{ + if (attributes != "") + { + content += "<" + tag + " " + attributes + ">" + content + ""; + } + else + { + content+= "<" + tag + ">" + content + ""; + } + return content; +} + +const String HTMLGenerator::DisplayPinStatus(const String &status, const String &link, bool isOn) +{ + String element = isOn ? "H4" : "p"; + String content = status + (link != "" ? " " + (isOn ? "OFF" : "ON") + "" : ""); + return GetElement(element, content,""); +} diff --git a/src/Helper/HTMLGenerator.h b/lib/Helper/HTMLGenerator.h similarity index 67% rename from src/Helper/HTMLGenerator.h rename to lib/Helper/HTMLGenerator.h index 621b028..36f9d58 100644 --- a/src/Helper/HTMLGenerator.h +++ b/lib/Helper/HTMLGenerator.h @@ -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); }; diff --git a/lib/Helper/Helper.cpp b/lib/Helper/Helper.cpp new file mode 100644 index 0000000..a5bc317 --- /dev/null +++ b/lib/Helper/Helper.cpp @@ -0,0 +1,29 @@ + +#include +#include +#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; +} diff --git a/lib/Helper/Helper.h b/lib/Helper/Helper.h new file mode 100644 index 0000000..a027dcb --- /dev/null +++ b/lib/Helper/Helper.h @@ -0,0 +1,17 @@ +#ifndef HELPER_H +#define HELPER_H + +#include +#include + +class Helper +{ +private: + +public: + Helper(); + static bool hasOverflowed(); + static unsigned long getElapsedTime(); +}; + +#endif \ No newline at end of file diff --git a/src/Data/DeviceBase.cpp b/src/Data/DeviceBase.cpp deleted file mode 100644 index fca2f66..0000000 --- a/src/Data/DeviceBase.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -class DeviceBase -{ - -public: - String Id; - DeviceBase(const String id) : Id(id) {} -}; \ No newline at end of file diff --git a/src/Data/DeviceBase.h b/src/Data/DeviceBase.h deleted file mode 100644 index 0492d24..0000000 --- a/src/Data/DeviceBase.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef _Device_h -#define _Device_h - -#include - -class DeviceBase -{ -public: - String Id; - DeviceBase(const String id) : Id(id) {} -}; - -#endif \ No newline at end of file diff --git a/src/Data/GSMMobileData.cpp b/src/Data/GSMMobileData.cpp deleted file mode 100644 index 589887a..0000000 --- a/src/Data/GSMMobileData.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "Data/DeviceBase.h" -#include "Device/EEPROMUtils.h" -#include - -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); - } - } -}; diff --git a/src/Data/PinTypeIn.cpp b/src/Data/PinTypeIn.cpp deleted file mode 100644 index ed3307f..0000000 --- a/src/Data/PinTypeIn.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#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(); - } -}; diff --git a/src/Data/PinTypeIn.h b/src/Data/PinTypeIn.h deleted file mode 100644 index 39994ce..0000000 --- a/src/Data/PinTypeIn.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef PinTypeIn_H -#define PinTypeIn_H - -#include -#include - -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 \ No newline at end of file diff --git a/src/Data/PinTypeOut.cpp b/src/Data/PinTypeOut.cpp deleted file mode 100644 index 9bd5955..0000000 --- a/src/Data/PinTypeOut.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#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); - } -}; diff --git a/src/Data/PinTypeOut.h b/src/Data/PinTypeOut.h deleted file mode 100644 index a70acfb..0000000 --- a/src/Data/PinTypeOut.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef PinTypeOut_H -#define PinTypeOut_H - -#include -#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 diff --git a/src/Data/WebServerData.cpp b/src/Data/WebServerData.cpp deleted file mode 100644 index a7e41e4..0000000 --- a/src/Data/WebServerData.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include - -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; - } -}; diff --git a/src/Data/WebServerData.h b/src/Data/WebServerData.h deleted file mode 100644 index 5d337c7..0000000 --- a/src/Data/WebServerData.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef WebServerData_H -#define WebServerData_H - -#include - -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 \ No newline at end of file diff --git a/src/Device/EEPROMUtils.cpp b/src/Device/EEPROMUtils.cpp deleted file mode 100644 index 5f1d649..0000000 --- a/src/Device/EEPROMUtils.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -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 - } - } -}; \ No newline at end of file diff --git a/src/Device/EEPROMUtils.h b/src/Device/EEPROMUtils.h deleted file mode 100644 index b3c4d8b..0000000 --- a/src/Device/EEPROMUtils.h +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/Device/GSMDevice.cpp b/src/Device/GSMDevice.cpp deleted file mode 100644 index 2c6a9ca..0000000 --- a/src/Device/GSMDevice.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "Data/GSMMobileData.h" -#include "Utills.h" -#include - -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(); - // } -}; \ No newline at end of file diff --git a/src/Device/GSMDevice.h b/src/Device/GSMDevice.h deleted file mode 100644 index b7b7bd4..0000000 --- a/src/Device/GSMDevice.h +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/Device/WebServer.cpp b/src/Device/WebServer.cpp deleted file mode 100644 index 5af0050..0000000 --- a/src/Device/WebServer.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#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(); - } - } -}; diff --git a/src/Device/WebServer.h b/src/Device/WebServer.h deleted file mode 100644 index fe7668a..0000000 --- a/src/Device/WebServer.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef WebServer_H -#define WebServer_H - -#include -#include -#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 \ No newline at end of file diff --git a/src/Helper/HTMLGenerator.cpp b/src/Helper/HTMLGenerator.cpp deleted file mode 100644 index 3a8c007..0000000 --- a/src/Helper/HTMLGenerator.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -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 + ""; - } - else - { - return "<" + tag + ">" + content + ""; - } - } - - static const String DisplayPinStatus(const String &status, const String &link, bool isOn) - { - String element = isOn ? "H4" : "p"; - String content = status + (link != "" ? " " + (isOn ? "OFF" : "ON") + "" : ""); - return GetElement(element, content); - } -}; \ No newline at end of file diff --git a/src/Utills.cpp b/src/Utills.cpp deleted file mode 100644 index 05219bc..0000000 --- a/src/Utills.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -class Utills -{ - public: - static void logDebug(const String& log) - { - Serial.println(log); - } -}; \ No newline at end of file diff --git a/src/Utills.h b/src/Utills.h deleted file mode 100644 index b9d25f2..0000000 --- a/src/Utills.h +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b09afd5..fd524c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 #include #include +#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(""); client.println("
"); client.println("

Trochta.net:

"); + client.println("
"); + 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; -} \ No newline at end of file + + if (data.Client.available()) + { + GenerateHtml(data); + data.Client.stop(); + } +} From 481aaf9ad32552017131719f5b3721d3b9a101a0 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Wed, 1 May 2024 16:29:23 +0200 Subject: [PATCH 5/8] Update WebServerData class; move EthernetClient inside WebServer class; modify WebServer init, getWebServerData, and SendToBrowser methods; update DebugLog init method; modify HTMLGenerator methods to use DebugLog; update platformio --- lib/Data/GSMMobileData.cpp | 7 ++ lib/Data/PinTypeIn.cpp | 2 +- lib/Data/PinTypeIn.h | 2 +- lib/Data/PinTypeOut.cpp | 6 +- lib/Data/PinTypeOut.h | 6 +- lib/Data/WebServerData.cpp | 2 +- lib/Data/WebServerData.h | 6 +- lib/Device/GSMMobileDevice.cpp | 32 +++++---- lib/Device/WebServer.cpp | 106 +++++++++++++++++++++++----- lib/Device/WebServer.h | 5 +- lib/Helper/DebugLog.cpp | 21 +++++- lib/Helper/DebugLog.h | 5 ++ lib/Helper/HTMLGenerator.cpp | 27 ++++--- lib/Helper/HTMLGenerator.h | 5 +- platformio.ini | 2 +- src/main.cpp | 124 ++++++++++++++++++++------------- 16 files changed, 249 insertions(+), 109 deletions(-) diff --git a/lib/Data/GSMMobileData.cpp b/lib/Data/GSMMobileData.cpp index dc330b9..7a572ad 100644 --- a/lib/Data/GSMMobileData.cpp +++ b/lib/Data/GSMMobileData.cpp @@ -15,7 +15,10 @@ 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) @@ -30,7 +33,9 @@ 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 } } @@ -45,6 +50,8 @@ void GSMMobileData::updateSMS(const String &newSMS) EEPROMUtils eepromUtils = EEPROMUtils(); Sms = newSMS; eepromUtils.writeString(21, newSMS, 100); +#ifdef DEBUG DebugLog::log("Update sms: " + newSMS); +#endif } } diff --git a/lib/Data/PinTypeIn.cpp b/lib/Data/PinTypeIn.cpp index 17170a2..16ec314 100644 --- a/lib/Data/PinTypeIn.cpp +++ b/lib/Data/PinTypeIn.cpp @@ -2,7 +2,7 @@ #include "PinTypeIn.h" -bool PinTypeIn::IsPinHigh() +const bool PinTypeIn::IsPinHigh() { return digitalRead(_pin) == HIGH ? true : false; } diff --git a/lib/Data/PinTypeIn.h b/lib/Data/PinTypeIn.h index 33709eb..8aabbf7 100644 --- a/lib/Data/PinTypeIn.h +++ b/lib/Data/PinTypeIn.h @@ -24,7 +24,7 @@ public: /// @brief Get Actual Pin Value if is SET = True, if is NOT SET = False /// @return - bool IsPinHigh(); + const bool IsPinHigh(); }; diff --git a/lib/Data/PinTypeOut.cpp b/lib/Data/PinTypeOut.cpp index 824dc50..e238d15 100644 --- a/lib/Data/PinTypeOut.cpp +++ b/lib/Data/PinTypeOut.cpp @@ -6,16 +6,16 @@ void PinTypeOut::setOutputSignalToHigh() { digitalWrite(_pinNumber, HIGH); - lastState = true; + } void PinTypeOut::setOutputSignalToLow() { digitalWrite(_pinNumber, LOW); - lastState = false; + } bool PinTypeOut::IsPinOn() { - return lastState; + return digitalRead(_pinNumber) == HIGH; } \ No newline at end of file diff --git a/lib/Data/PinTypeOut.h b/lib/Data/PinTypeOut.h index 7346bbb..9a355ae 100644 --- a/lib/Data/PinTypeOut.h +++ b/lib/Data/PinTypeOut.h @@ -8,7 +8,7 @@ class PinTypeOut : public DeviceBase { private: uint8_t _pinNumber; - bool lastState; + unsigned long _debounceTime; unsigned long lastStateChangeTime; @@ -17,13 +17,13 @@ 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) + : DeviceBase(id), _pinNumber(pinNumber), _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) + : DeviceBase(id), _pinNumber(pinNumber),_debounceTime(0), lastStateChangeTime(0), description(description) { pinMode(pinNumber, OUTPUT); } diff --git a/lib/Data/WebServerData.cpp b/lib/Data/WebServerData.cpp index e4c53bf..063bf28 100644 --- a/lib/Data/WebServerData.cpp +++ b/lib/Data/WebServerData.cpp @@ -1,4 +1,4 @@ -#include "WebServerData.h" +#include #include #include diff --git a/lib/Data/WebServerData.h b/lib/Data/WebServerData.h index 8f6038e..24a047c 100644 --- a/lib/Data/WebServerData.h +++ b/lib/Data/WebServerData.h @@ -7,16 +7,16 @@ 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){}; + 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 \ No newline at end of file diff --git a/lib/Device/GSMMobileDevice.cpp b/lib/Device/GSMMobileDevice.cpp index 54f0578..2aa1aab 100644 --- a/lib/Device/GSMMobileDevice.cpp +++ b/lib/Device/GSMMobileDevice.cpp @@ -6,12 +6,17 @@ void GSMMobileDevice::end() { _gsmSerial.end(); +#ifdef DEBUG DebugLog::log("GSM End .."); +#endif } void GSMMobileDevice::executeAtCommand(const String &atCommand) { +#ifdef DEBUG DebugLog::log(atCommand); +#endif + _gsmSerial.print(atCommand + "\r"); delay(500); } @@ -23,18 +28,19 @@ String GSMMobileDevice::readStringSerialPort() 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"); - 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(1000); + // Send a Ctrl+Z / ASCII SUB + _gsmSerial.print((char)26); + delay(500); + // Close the connection + _gsmSerial.print((char)27);*/ delay(500); } @@ -52,13 +58,15 @@ bool GSMMobileDevice::checkSMSError() return false; } -GSMMobileDevice::GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): _gsmSerial(pinRx, pinTx) +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 .."); - //delay(5000); // initialization GSM module (give it some time to boot up) +#endif + // delay(5000); // initialization GSM module (give it some time to boot up) } void GSMMobileDevice::sendSMS(GSMMobileData gsmData) diff --git a/lib/Device/WebServer.cpp b/lib/Device/WebServer.cpp index b0856ba..53ae6fa 100644 --- a/lib/Device/WebServer.cpp +++ b/lib/Device/WebServer.cpp @@ -1,10 +1,10 @@ #include #include -#include "WebServerData.h" #include "WebServer.h" +#include "WebServerData.h" #include "DebugLog.h" -// Constructor +// Constructor WebServer::WebServer() : _server(80) {} WebServer::WebServer(byte mac[6], IPAddress ip, uint16_t port) : _ip(ip), _server(port) @@ -15,46 +15,83 @@ WebServer::WebServer(byte mac[6], IPAddress ip, uint16_t port) : _ip(ip), _serve // 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); + +#ifdef DEBUG DebugLog::log("Ethernet IP: " + Ethernet.localIP()); +#endif + _server.begin(); + +#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 + client = _server.available(); if (client) { if (client.connected()) { - //DebugLog::log("Client connected"); - if (client.available()) +#ifdef DEBUG + DebugLog::log("Client connected"); +#endif + + unsigned long startTime = millis(); + while (client.available() == 0) { - - 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")) + if (millis() - startTime >= 10000) { - DebugLog::log("GET WEB SERVER DATA: " + request); - WebServerData webServerData = WebServerData(client, request); - return webServerData; + 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(); + + String request = client.readStringUntil('\r'); + + if (request.startsWith("GET / HTTP/1.1")) + { + +#ifdef DEBUG + DebugLog::log("EMPTY DATA: " + request); +#endif + + 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 + + if (request.startsWith("GET /")) + { +#ifdef DEBUG + DebugLog::log("GET WEB SERVER DATA: " + request); +#endif + WebServerData webServerData = WebServerData(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 + return WebServerData(); + // Give the web browser time to receive the data } +// Return a default-constructed WebServerData object if no client is available + void WebServer::endServer() { if (_server.available()) @@ -64,3 +101,36 @@ void WebServer::endServer() client.stop(); } } + +void WebServer::SendToBrowser(const String &dataString) +{ + const byte chunkSize = 50; + + // 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); + + // 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(""); +} diff --git a/lib/Device/WebServer.h b/lib/Device/WebServer.h index bc1d738..46f3882 100644 --- a/lib/Device/WebServer.h +++ b/lib/Device/WebServer.h @@ -1,9 +1,9 @@ #ifndef WebServer_H #define WebServer_H +#include #include #include -#include "WebServerData.h" class WebServer { @@ -12,7 +12,7 @@ private: IPAddress _ip; EthernetServer _server; -public: + public: EthernetClient client; WebServer(); @@ -20,6 +20,7 @@ public: WebServerData getWebServerData(); void init(); void endServer(); + void SendToBrowser(const String &dataString); }; #endif // WebServer_H \ No newline at end of file diff --git a/lib/Helper/DebugLog.cpp b/lib/Helper/DebugLog.cpp index 73058cb..94de7f0 100644 --- a/lib/Helper/DebugLog.cpp +++ b/lib/Helper/DebugLog.cpp @@ -1,12 +1,31 @@ + + #include #include "DebugLog.h" void DebugLog::log(const String &log) { - Serial.println(log); + WriteToSerial(log); } void DebugLog::init(const long &bound) { Serial.begin(bound); + Serial.println("Serial Port Initialized"); +} + +void DebugLog::WriteToSerial(const String &dataString) +{ + const byte chunkSize = 60; + unsigned int stringIndex = 0; + while (stringIndex < dataString.length()) + { + int chunkLength = min(chunkSize, dataString.length() - stringIndex); + String chunk = dataString.substring(stringIndex, stringIndex + chunkLength); + + Serial.print(chunk); + stringIndex += chunkLength; + delay(4); // Optional delay between chunks + } + Serial.println(""); } diff --git a/lib/Helper/DebugLog.h b/lib/Helper/DebugLog.h index 0cce7aa..792d258 100644 --- a/lib/Helper/DebugLog.h +++ b/lib/Helper/DebugLog.h @@ -3,11 +3,16 @@ #include + class DebugLog { +private: + static void WriteToSerial(const String &dataString); + public: static void init(const long &bound); static void log(const String &log); }; + #endif \ No newline at end of file diff --git a/lib/Helper/HTMLGenerator.cpp b/lib/Helper/HTMLGenerator.cpp index 16b150f..248c11d 100644 --- a/lib/Helper/HTMLGenerator.cpp +++ b/lib/Helper/HTMLGenerator.cpp @@ -1,22 +1,21 @@ #include #include "HTMLGenerator.h" +#include "DebugLog.h" -const String HTMLGenerator::GetElement(const String &tag, String &content, const String &attributes) +const String HTMLGenerator::GetElement(const String &tag, const String &content) { - if (attributes != "") - { - content += "<" + tag + " " + attributes + ">" + content + ""; - } - else - { - content+= "<" + tag + ">" + content + ""; - } - return content; + return "<" + tag + ">" + content + ""; } -const String HTMLGenerator::DisplayPinStatus(const String &status, const String &link, bool isOn) +const String HTMLGenerator::DisplayOuputPinStatus(const String &pinName, const bool &isOn, const String &description) { - String element = isOn ? "H4" : "p"; - String content = status + (link != "" ? " " + (isOn ? "OFF" : "ON") + "" : ""); - return GetElement(element, content,""); + const String pinvariable = pinName + (isOn ? "=OFF" : "=ON"); + String content = pinName + " is " + (isOn ? "ON" : "OFF") + " | " + pinvariable + " "; + return GetElement("p", content); +} + +const String HTMLGenerator::DisplayInputPinStatus(const String &pinName, const bool &isOn, const String &description) +{ + const String content = pinName + " is " + (isOn ? "ON" : "OFF"); + return GetElement("H4", content); } diff --git a/lib/Helper/HTMLGenerator.h b/lib/Helper/HTMLGenerator.h index 36f9d58..f14ed4e 100644 --- a/lib/Helper/HTMLGenerator.h +++ b/lib/Helper/HTMLGenerator.h @@ -6,8 +6,9 @@ class HTMLGenerator { public: - static const String GetElement(const String &tag, String &content, const String &attributes = ""); - static const String DisplayPinStatus(const String &status, const String &link, bool isOn); + static const String GetElement(const String &tag,const String &content ); + static const String DisplayOuputPinStatus( const String &pinName,const bool &isOn, const String &description); + static const String DisplayInputPinStatus( const String &pinName,const bool &isOn,const String &description); }; #endif // HTMLGENERATOR_H \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 1069e24..e79bd8e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,5 +15,5 @@ framework = arduino lib_deps = arduino-libraries/Ethernet@^2.0.2 featherfly/SoftwareSerial@^1.0 -monitor_speed = 9600 +monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp index fd524c4..f0c484e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,8 @@ #include "DebugLog.h" #include "HTMLGenerator.h" + #define DEBUG + // PIN INPUT Initialization as OUTPUT static const int COUNT_OUTPUTS_PIN_OUT = 2; static PinTypeOut PinIn8 = PinTypeOut("PIN8", 8, "Electric Alarm"); @@ -23,69 +25,100 @@ 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 -static const uint8_t GSM_PIN_RX = 10; -static const uint8_t GSM_PIN_TX = 11; -static const long GSM_BAUD_RATE = 115200; +/*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, 177); +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); -// // Pin Input -GSMMobileData gsmData = GSMMobileData("GSM"); -WebServer webServer; +// 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); + // gsmData.updateMobileNumber(value); value = request.getValue(data, "sms="); - gsmData.updateSMS(value); + // 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(); } } @@ -93,68 +126,65 @@ void SeparateDataFromRequest(WebServerData &request) } } -void WebServerEvent(WebServerData &data) -{ - // Your function implementation here - SeparateDataFromRequest(data); -} - void setup() { +#ifdef DEBUG DebugLog::init(115200); DebugLog::log("Called: Setup"); - webServer = WebServer(WebServerMac, WebServerIP, WebServerPort); - // // serial monitori init and set baud rate +#endif - webServer.init(); + _webServer = WebServer(WebServerMac, WebServerIP, WebServerPort); + delay(1000); + _webServer.init(); + delay(1000); +#ifdef DEBUG DebugLog::log("Web Server Initilized"); +#endif } -void GenerateHtml(WebServerData &data) +void GenerateHtml() { - EthernetClient client = data.Client; - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connection: close"); - client.println(); - client.println(""); + _webServer.SendToBrowser("
"); + _webServer.SendToBrowser("

Trochta.net:

"); 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()); + String line = HTMLGenerator::DisplayOuputPinStatus(pinValue.Id, pinValue.IsPinOn(), pinValue.description); + _webServer.SendToBrowser(line); } - for (int i = 0; i < COUNT_INPUTS_PIN_IN; i++) + for (int i = 0; i < COUNT_INPUTS_PIN_IN; i++) { PinTypeIn pinValue = INPUTS_PINS_ARRAY[i]; - HTMLGenerator::DisplayPinStatus(pinValue.Id + "|" + pinValue.description,"", pinValue.IsPinHigh()); + String line = HTMLGenerator::DisplayInputPinStatus(pinValue.Id, pinValue.IsPinHigh(), pinValue.description); + _webServer.SendToBrowser(line); } - client.println(""); - client.println("
"); - - client.println("

Trochta.net:

"); - - client.println("
"); - - client.stop(); + _webServer.SendToBrowser("
"); } void loop() { // DebugLog::log("Called: Loop"); - WebServerData data = webServer.getWebServerData(); + WebServerData data = _webServer.getWebServerData(); if (!data.IsEmpty) { - WebServerEvent(data); + SeparateDataFromRequest(data); } - if (data.Client.available()) + +#ifdef DEBUG + DebugLog::log("client avaible [" + String (_webServer.client.available()) + "]"); +#endif + + if (_webServer.client.available()) { - GenerateHtml(data); - data.Client.stop(); + GenerateHtml(); + _webServer.endServer(); } } From 9c2d5693080b7133d1b8d6e4a5c4e3051699859a Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Wed, 1 May 2024 18:30:46 +0200 Subject: [PATCH 6/8] modified: .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 89cc49c..ffe74bd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +.vscode/ +.vs/ + + From e319e2db059cbe40ca122c7bc491ca669dff4a30 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Thu, 2 May 2024 18:35:38 +0200 Subject: [PATCH 7/8] send sms working --- lib/Data/PinTypeIn.cpp | 3 +- lib/Data/PinTypeIn.h | 2 +- lib/Device/GSMMobileDevice.cpp | 72 ++++++++----- lib/Device/GSMMobileDevice.h | 16 +-- lib/Helper/DebugLog.cpp | 2 +- lib/Helper/Helper.cpp | 3 +- lib/Helper/Helper.h | 5 +- src/main.cpp | 182 +++++---------------------------- 8 files changed, 92 insertions(+), 193 deletions(-) diff --git a/lib/Data/PinTypeIn.cpp b/lib/Data/PinTypeIn.cpp index 16ec314..163fb13 100644 --- a/lib/Data/PinTypeIn.cpp +++ b/lib/Data/PinTypeIn.cpp @@ -2,7 +2,8 @@ #include "PinTypeIn.h" -const bool PinTypeIn::IsPinHigh() + +bool PinTypeIn::IsPinHigh() { return digitalRead(_pin) == HIGH ? true : false; } diff --git a/lib/Data/PinTypeIn.h b/lib/Data/PinTypeIn.h index 8aabbf7..33709eb 100644 --- a/lib/Data/PinTypeIn.h +++ b/lib/Data/PinTypeIn.h @@ -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(); }; diff --git a/lib/Device/GSMMobileDevice.cpp b/lib/Device/GSMMobileDevice.cpp index 2aa1aab..b57e38b 100644 --- a/lib/Device/GSMMobileDevice.cpp +++ b/lib/Device/GSMMobileDevice.cpp @@ -2,6 +2,16 @@ #include #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() diff --git a/lib/Device/GSMMobileDevice.h b/lib/Device/GSMMobileDevice.h index 0f34415..303ae11 100644 --- a/lib/Device/GSMMobileDevice.h +++ b/lib/Device/GSMMobileDevice.h @@ -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 \ No newline at end of file diff --git a/lib/Helper/DebugLog.cpp b/lib/Helper/DebugLog.cpp index 94de7f0..b12b64a 100644 --- a/lib/Helper/DebugLog.cpp +++ b/lib/Helper/DebugLog.cpp @@ -5,7 +5,7 @@ void DebugLog::log(const String &log) { - WriteToSerial(log); + // WriteToSerial(log); } void DebugLog::init(const long &bound) diff --git a/lib/Helper/Helper.cpp b/lib/Helper/Helper.cpp index a5bc317..66d3f7b 100644 --- a/lib/Helper/Helper.cpp +++ b/lib/Helper/Helper.cpp @@ -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()) { diff --git a/lib/Helper/Helper.h b/lib/Helper/Helper.h index a027dcb..84e3550 100644 --- a/lib/Helper/Helper.h +++ b/lib/Helper/Helper.h @@ -10,8 +10,9 @@ private: public: Helper(); - static bool hasOverflowed(); - static unsigned long getElapsedTime(); + + bool hasOverflowed(); + unsigned long getElapsedTime(bool reset); }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f0c484e..4027f7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(""); - _webServer.SendToBrowser("
"); - _webServer.SendToBrowser("

Trochta.net:

"); - - 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("
"); + // 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); + } + From 0caeec996e91c197b7bfc02bd223afbaab5f1cb9 Mon Sep 17 00:00:00 2001 From: Ondrej Trochta Date: Thu, 26 Sep 2024 20:58:31 +0200 Subject: [PATCH 8/8] test chata --- Arduino-GSM-Chata.code-workspace | 14 ++++++++++++++ lib/Data/GSMMobileData.cpp | 12 +----------- lib/Data/PinTypeOut.cpp | 1 - lib/Data/WebServerData.cpp | 3 ++- lib/Data/WebServerData.h | 5 +++-- lib/Device/WebServer.cpp | 3 ++- lib/Device/WebServer.h | 5 +++-- platformio.ini | 9 +++++---- src/main.cpp | 27 ++++++++++++--------------- 9 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 Arduino-GSM-Chata.code-workspace diff --git a/Arduino-GSM-Chata.code-workspace b/Arduino-GSM-Chata.code-workspace new file mode 100644 index 0000000..91479a6 --- /dev/null +++ b/Arduino-GSM-Chata.code-workspace @@ -0,0 +1,14 @@ +{ + "folders": [ + { + "name": "Arduino-GSM-Chata", + "path": "." + } + ], + "settings": { + "files.associations": { + "pinsdefinitions": "c", + "new": "cpp" + } + } +} \ No newline at end of file diff --git a/lib/Data/GSMMobileData.cpp b/lib/Data/GSMMobileData.cpp index 7a572ad..ba6b5a0 100644 --- a/lib/Data/GSMMobileData.cpp +++ b/lib/Data/GSMMobileData.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 } } diff --git a/lib/Data/PinTypeOut.cpp b/lib/Data/PinTypeOut.cpp index e238d15..51e05ce 100644 --- a/lib/Data/PinTypeOut.cpp +++ b/lib/Data/PinTypeOut.cpp @@ -1,6 +1,5 @@ #include "PinTypeOut.h" #include -#include "DebugLog.h" void PinTypeOut::setOutputSignalToHigh() diff --git a/lib/Data/WebServerData.cpp b/lib/Data/WebServerData.cpp index 063bf28..4e4fb77 100644 --- a/lib/Data/WebServerData.cpp +++ b/lib/Data/WebServerData.cpp @@ -1,4 +1,4 @@ -#include +/*#include #include #include @@ -46,3 +46,4 @@ String WebServerData::removeAllCharactersAfterEmptySpace(String &data) data.replace("_", " "); return data; } +*/ \ No newline at end of file diff --git a/lib/Data/WebServerData.h b/lib/Data/WebServerData.h index 24a047c..273ebd8 100644 --- a/lib/Data/WebServerData.h +++ b/lib/Data/WebServerData.h @@ -1,4 +1,4 @@ -#ifndef WebServerData_H +/*ifndef WebServerData_H #define WebServerData_H #include @@ -19,4 +19,5 @@ public: }; -#endif // WebServerData_H \ No newline at end of file +#endif // WebServerData_H +*/ \ No newline at end of file diff --git a/lib/Device/WebServer.cpp b/lib/Device/WebServer.cpp index 53ae6fa..7d1dcbc 100644 --- a/lib/Device/WebServer.cpp +++ b/lib/Device/WebServer.cpp @@ -1,4 +1,4 @@ -#include +/*#include #include #include "WebServer.h" #include "WebServerData.h" @@ -134,3 +134,4 @@ void WebServer::SendToBrowser(const String &dataString) } client.println(""); } +*/ \ No newline at end of file diff --git a/lib/Device/WebServer.h b/lib/Device/WebServer.h index 46f3882..d9f8f53 100644 --- a/lib/Device/WebServer.h +++ b/lib/Device/WebServer.h @@ -1,4 +1,4 @@ -#ifndef WebServer_H +/*#ifndef WebServer_H #define WebServer_H #include @@ -23,4 +23,5 @@ private: void SendToBrowser(const String &dataString); }; -#endif // WebServer_H \ No newline at end of file +#endif // WebServer_H +*/ \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index e79bd8e..bea0a82 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 4027f7b..56fbf77 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,15 @@ #include "GSMMobileDevice.h" -#include "WebServer.h" +//#include "WebServer.h" #include "PinTypeIn.h" #include "PinTypeOut.h" #include -#include -#include -#include "WebServer.h" -#include "WebServerData.h" -#include "DebugLog.h" -#include "HTMLGenerator.h" +//#include +// #include +// #include "WebServer.h" +// #include "WebServerData.h" + +//#include "HTMLGenerator.h" +#include "avr8-stub.h" //#define DEBUG @@ -20,7 +21,7 @@ static const PinTypeOut OUPUTS_PINS_ARRAY[COUNT_OUTPUTS_PIN_OUT] = {PinOutAlarm8 // PIN INPUT Initialization as INPUT static const int COUNT_INPUTS_PIN_IN = 2; -static PinTypeIn PinInEZSAlarm2 = PinTypeIn("PIN2", 2, 100, "Input EZS ALARM"); // 100ms debounce; +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}; @@ -35,19 +36,15 @@ GSMMobileData gsmData = GSMMobileData("GSM"); void setup() { -#ifdef DEBUG - DebugLog::init(115200); - DebugLog::log("Called: Setup and init GSM"); - DebugLog::log("Init GSM is OK"); -#endif - // gsmDevice.init(); + debug_init(); + gsmData.MobileNumber = "+420608126674"; gsmData.Sms = "Alarm EZS CHATA"; } void loop() { - DebugLog::log("PIN IN 2: " + String(PinInEZSAlarm2.IsPinHigh())); + ; if (PinInEZSAlarm2.IsPinHigh()) { PinOutAlarm8.setOutputSignalToHigh();