From bed24da57cf283e3906d8c2f51d60aed20b83c28 Mon Sep 17 00:00:00 2001 From: Ondrej-Trochta Date: Sun, 21 Apr 2024 15:28:31 +0200 Subject: [PATCH] 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() +{ +}