No working separe to class

This commit is contained in:
Ondrej-Trochta
2024-04-22 01:42:36 +02:00
parent bed24da57c
commit 37c0c73337
21 changed files with 411 additions and 241 deletions
+4 -2
View File
@@ -1,5 +1,7 @@
{
"files.associations": {
"pinsdefinitions": "c"
"pinsdefinitions": "c",
}
}
}
+14
View File
@@ -0,0 +1,14 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "PlatformIO",
"task": "Build (uno)",
"problemMatcher": [
"$platformio"
],
"group": "build",
"label": "PlatformIO: Build (uno)"
}
]
}
+2 -1
View File
@@ -15,4 +15,5 @@ framework = arduino
lib_deps =
arduino-libraries/Ethernet@^2.0.2
featherfly/SoftwareSerial@^1.0
build_flags = -DNDEBUG
monitor_speed = 9600
-30
View File
@@ -1,30 +0,0 @@
#include <Arduino.h>
#include <Device/EEPROMUtils.h>
#include <Utills.h>
#include <Data/DeviceBase.h>
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);
}
};
-22
View File
@@ -1,22 +0,0 @@
#ifndef GSMData_H
#define GSMData_H
#include <Arduino.h>
#include <Device/EEPROMUtils.h>
#include <Utills.h>
#include <Data/DeviceBase.h>
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
+43
View File
@@ -0,0 +1,43 @@
#include "Data/DeviceBase.h"
#include "Device/EEPROMUtils.h"
#include <Arduino.h>
class GSMMobileData : public DeviceBase
{
public:
String MobileNumber;
String Sms;
GSMMobileData(String id) : DeviceBase(id)
{
MobileNumber = EEPROMUtils::ReadString(1, 20);
Sms = EEPROMUtils::ReadString(21, 100);
}
void updateMobileNumber(const String &newMobileNumber)
{
if (newMobileNumber.length() <= 0)
{
return;
}
if (newMobileNumber != MobileNumber)
{
MobileNumber = newMobileNumber;
EEPROMUtils::WriteString(1, newMobileNumber, 20);
}
}
void updateSMS(const String &newSMS)
{
if (newSMS.length() <= 0)
{
return;
}
if (newSMS != Sms)
{
Sms = newSMS;
EEPROMUtils::WriteString(21, newSMS, 100);
}
}
};
+20
View File
@@ -0,0 +1,20 @@
#ifndef GSMMobileData_H
#define GSMMobileData_H
#include <Arduino.h>
#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
+1 -13
View File
@@ -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)
+47
View File
@@ -0,0 +1,47 @@
#include <Ethernet.h>
class WebServerData
{
public:
EthernetClient Client;
String Request;
bool IsEmpty;
WebServerData()
{
IsEmpty = true;
}
WebServerData(const EthernetClient &client, const String &request)
{
Client = client;
Request = request;
IsEmpty = false;
}
String getValue(const String &data, const String &key)
{
int keyIndex = data.indexOf(key);
if (keyIndex == -1)
{
return ""; // return empty string if key is not found
}
int separatorIndex = data.indexOf('&', keyIndex);
String value = data.substring(keyIndex + key.length(), separatorIndex != -1 ? separatorIndex : data.length());
return removeAllCharactesrAfterEmpty(value);
}
String removeAllCharactesrAfterEmpty(String &data)
{
int keyIndex = data.indexOf(' ');
if (keyIndex == -1)
{
return data; // return empty string if key is not found
}
data.remove(keyIndex);
data.replace("_", " ");
return data;
}
};
+19
View File
@@ -0,0 +1,19 @@
#ifndef WebServerData_H
#define WebServerData_H
#include <Ethernet.h>
class WebServerData
{
public:
EthernetClient Client;
String Request;
bool IsEmpty;
WebServerData();
WebServerData(EthernetClient &client, String &request);
String getValue(String &data, const String &key);
String removeAllCharactesrAfterEmpty(String &data);
};
#endif // WebServerData_H
+6 -11
View File
@@ -1,16 +1,11 @@
#ifndef _EEPROMUtils_h
#define _EEPROMUtils_h
#ifndef EEPROMUtils_h
#define EEPROMUtils_h
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Data/GSMData.h>
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
#endif
+10 -9
View File
@@ -1,4 +1,5 @@
#include <Data/GSMData.h>
#include "Data/GSMMobileData.h"
#include "Utills.h"
#include <SoftwareSerial.h>
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();
// }
};
+7 -7
View File
@@ -1,9 +1,9 @@
#ifndef GSMDevice_H
#define GSMDevice_H
#include <Data/GSMData.h>
#include <SoftwareSerial.h>
#include <Data/GSMData.h>
#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
+59 -54
View File
@@ -1,63 +1,68 @@
#include <Arduino.h>
#include <Ethernet.h>
#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("<!DOCTYPE HTML>");
client.println("<html>");
// Add your HTML content here
client.println("</html>");
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
}
};
return WebServerData(); // Return a default-constructed WebServerData object if no client is available
}
void WebServer::EndServer()
{
if (server.available())
{
delay(10);
// Close the connection
client.stop();
}
}
};
+33
View File
@@ -0,0 +1,33 @@
#ifndef WebServer_H
#define WebServer_H
#include <Arduino.h>
#include <Ethernet.h>
#include "Data/WebServerData.h"
class WebServer
{
private:
byte mac[6];
IPAddress ip;
EthernetServer server;
// Define the callback function type
public:
EthernetClient client;
// Constructor
WebServer() : server(80) {}
WebServer(byte mac[6], IPAddress ip, uint16_t port) : ip(ip), server(port)
{
memcpy(this->mac, mac, sizeof(this->mac));
}
// Initialize the Ethernet and start the server
void Init();
// Handle client requests
WebServerData GetWebServerData();
void EndServer();
};
#endif // WebServer_H
+14 -10
View File
@@ -1,21 +1,25 @@
#include <Arduino.h>
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 + "</" + tag + ">";
} else {
html_content += "<" + tag + ">" + content + "</" + tag + ">";
if (attributes != "")
{
return "<" + tag + " " + attributes + ">" + content + "</" + tag + ">";
}
else
{
return "<" + tag + ">" + content + "</" + tag + ">";
}
}
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 != "" ? " <a href=\"" + link + "\">" + (isOn ? "OFF" : "ON") + "</a>" : "");
return GetElement(element, content);
return GetElement(element, content);
}
};
-41
View File
@@ -1,41 +0,0 @@
#include <Data/PinTypeOut.h>
#include <Data/PinTypeIn.h>
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;
}
};
-27
View File
@@ -1,27 +0,0 @@
#ifndef PinDefinitions_H
#define PinDefinitions_H
#include <Data/PinTypeOut.h>
#include <Data/PinTypeIn.h>
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
+2 -2
View File
@@ -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);
}
};
+4 -3
View File
@@ -1,11 +1,12 @@
#ifndef Utills_H
#define Utills_H
#include <Arduino.h>
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
+126 -9
View File
@@ -1,23 +1,140 @@
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include "Device/GSMDevice.h"
#include "Device/WebServer.h"
#include "Data/PinTypeIn.h"
#include "Data/PinTypeOut.h"
#include "Utills.h"
#include <Arduino.h>
#include <IPAddress.h>
#include <Ethernet.h>
#include <Utills.h>
#include <Device/GSMDevice.h>
#include <Data/PinTypeOut.h>
#include <Data/PinTypeIn.h>
#include <PinDefinitions.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 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("<html><head><style>");
client.println(" body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0;}");
client.println(".content {text-align: center;}");
client.println("</style>");
client.println("</head><body><div class=\"content\">");
client.println("<H2> Trochta.net:</H2>");
client.println("</div></body></html>");
client.stop();
}
int main()
{
setup();
while (true)
{
loop();
}
return 0;
}