From e9e2d5e1c31f316a7d6e98f09368710495209398 Mon Sep 17 00:00:00 2001 From: h4km4k <85360886+h4km4k@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:10:22 +0200 Subject: [PATCH] Update RelayBoard.ino --- RelayBoard/RelayBoard.ino | 623 +++++++++++++++++--------------------- 1 file changed, 272 insertions(+), 351 deletions(-) diff --git a/RelayBoard/RelayBoard.ino b/RelayBoard/RelayBoard.ino index 7ced68c..0f16f95 100644 --- a/RelayBoard/RelayBoard.ino +++ b/RelayBoard/RelayBoard.ino @@ -1,7 +1,7 @@ #include #include #include -#include +#include //Relay #define RELAY_PORT 0 @@ -9,12 +9,12 @@ bool relay = false; //WiFi #define MAX_CLIENTS 10 -WiFiServer server(80); +WiFiServer server(80); int wifiScanCount = 0; int32_t ssidVolume[50]; -WiFiClient *clients[MAX_CLIENTS] = { NULL }; -String *clientsCurrentLine[MAX_CLIENTS] = { NULL }; -String *clientsHTTPRequest[MAX_CLIENTS] = { NULL }; +WiFiClient* clients[MAX_CLIENTS] = { NULL }; +char clientsCurrentLine[MAX_CLIENTS][256]; // Puffer für die aktuelle Zeile +char* clientsHTTPRequest[MAX_CLIENTS] = { NULL }; //HTTP Protocol int httpMethod = 0; @@ -23,15 +23,15 @@ int httpMethod = 0; String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete -int ssid_adr = 0; // len = 40 + 5 jeweils Abstand geht gut zum Rechnen -int password_adr = 45; // len = 30 -int ip_adr = 80; // len = 15 -int subnet_adr = 100; // len = 15 -int defaultgw_adr = 120; // len = 15 -int dns_adr = 140; // len = 15 -int ipType_adr = 160; //len = 1 -int powerOn_adr = 165; //len = 1 -int mDNS_adr = 170; //len=20 +int ssid_adr = 0; // len = 40 + 5 jeweils Abstand geht gut zum Rechnen +int password_adr = 45; // len = 30 +int ip_adr = 80; // len = 15 +int subnet_adr = 100; // len = 15 +int defaultgw_adr = 120; // len = 15 +int dns_adr = 140; // len = 15 +int ipType_adr = 160; //len = 1 +int powerOn_adr = 165; //len = 1 +int mDNS_adr = 170; //len=20 //next at 195 String ssid = ""; @@ -45,14 +45,13 @@ String powerOn = ""; String mDNS_name = ""; //EEPROM Struct - struct - { - uint val = 0; - char str[80] = ""; - } data; - -void setup() +struct { + uint val = 0; + char str[80] = ""; +} data; + +void setup() { EEPROM.begin(256); inputString.reserve(200); @@ -63,28 +62,26 @@ void setup() subnet.reserve(15); defaultgw.reserve(15); dns.reserve(15); - powerOn.reserve(1); - + powerOn.reserve(1); + pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(LED_BUILTIN, HIGH); pinMode(RELAY_PORT, OUTPUT); - digitalWrite(RELAY_PORT, HIGH); - + digitalWrite(RELAY_PORT, HIGH); + Serial.begin(115200); readValuesFromEeprom(); - if (powerOn == "1") - { - digitalWrite(RELAY_PORT, LOW); + if (powerOn == "1") { + digitalWrite(RELAY_PORT, LOW); relay = true; } wifiScanCount = scanWiFi(); - if (!startWiFiClient()) - { + if (!startWiFiClient()) { startWifiAccessPoint(); } @@ -92,129 +89,116 @@ void setup() server.begin(); - MDNS.addService("http", "tcp", 80); + MDNS.addService("http", "tcp", 80); } -void loop() -{ - if (WiFi.getMode() == WIFI_AP && WiFi.status() != WL_CONNECTED) - { +void loop() { + if (WiFi.getMode() == WIFI_AP && WiFi.status() != WL_CONNECTED) { startWiFiClient(); } MDNS.update(); - + WiFiClient client = server.available(); checkRS232(); - //Client zwischenspeichern - if (client) - { - for (int i=0 ; iconnected()) - { - if (clients[i]->available()) - { - char c = clients[i]->read(); - - if (c != '\r' && c != '\n') //Zeile lesen - { - *clientsCurrentLine[i] += c; - digitalWrite(LED_BUILTIN, LOW); - } - if (c == '\n') //Zeile abgeschlossen - { - //Serial.println(*clientsCurrentLine[i]); //debug out of http protocol lines - if (clientsCurrentLine[i]->startsWith("GET ")) - { - clientsHTTPRequest[i] = clientsCurrentLine[i]; + // Clients durcharbeiten + for (int i = 0; i < MAX_CLIENTS; ++i) { + if (clients[i] != NULL && clients[i]->connected()) { + if (clients[i]->available()) { + char c = clients[i]->read(); + + if (c != '\r' && c != '\n') { // Zeile lesen + strncat(clientsCurrentLine[i], &c, 1); // Zeichen zur aktuellen Zeile hinzufügen + digitalWrite(LED_BUILTIN, LOW); + } + if (c == '\n') { // Zeile abgeschlossen + if (strncmp(clientsCurrentLine[i], "GET ", 4) == 0) { + // Freigeben des alten HTTP-Requests + if (clientsHTTPRequest[i] != NULL) { + delete[] clientsHTTPRequest[i]; } - answerRequest(*clientsCurrentLine[i], *clients[i], *clientsHTTPRequest[i]); - clientsCurrentLine[i] = new String(); + clientsHTTPRequest[i] = new char[strlen(clientsCurrentLine[i]) + 1]; + strcpy(clientsHTTPRequest[i], clientsCurrentLine[i]); // Kopiere die aktuelle Zeile } + answerRequest(clientsCurrentLine[i], *clients[i], clientsHTTPRequest[i]); + + // Zeilenpuffer zurücksetzen + memset(clientsCurrentLine[i], 0, sizeof(clientsCurrentLine[i])); } } - else if (clients[i] != NULL) - { - clients[i]->flush(); - clients[i]->stop(); - delete clients[i]; - clients[i] = NULL; - delete clientsCurrentLine[i]; - clientsCurrentLine[i] = NULL; - delete clientsHTTPRequest[i]; + } else if (clients[i] != NULL) { + clients[i]->flush(); + clients[i]->stop(); + delete clients[i]; // Speicher freigeben + clients[i] = NULL; + + // Freigabe des HTTP-Requests + if (clientsHTTPRequest[i] != NULL) { + delete[] clientsHTTPRequest[i]; clientsHTTPRequest[i] = NULL; } + } } } -void answerRequest(String currentLine, WiFiClient client, String httpRequest) -{ - if (currentLine.length() == 0) //Ende des HTTP Request +void answerRequest(String currentLine, WiFiClient client, String httpRequest) { + if (currentLine.length() == 0) //Ende des HTTP Request { - if (httpRequest.startsWith("GET / ")) // Startseite - { - sendStartPage(client); - httpMethod = 1; - } - else if (httpRequest.startsWith("GET /ON ")) // Startseite - { - digitalWrite(RELAY_PORT, LOW); - relay = true; - sendStartPage(client); - httpMethod = 1; - } - else if (httpRequest.startsWith("GET /OFF ")) // Startseite - { - digitalWrite(RELAY_PORT, HIGH); - relay = false; - sendStartPage(client); - httpMethod = 1; - } - else if (httpRequest.startsWith("GET /settings ")) // Startseite - { - wifiScanCount = scanWiFi(); - sendSettingsPage(client); - httpMethod = 1; - } - else if (httpRequest.startsWith("GET /save?")) //Nach Save Request - { - saveSettings(httpRequest); - sendSavedPage(client); - httpMethod = 5; - } - - if (httpMethod == 1 || httpMethod == 5) - { - client.flush(); - client.stop(); - if (httpMethod == 5) - { - delay(1000); - ESP.restart(); - } - httpMethod = 0; - digitalWrite(LED_BUILTIN, HIGH); + if (httpRequest.startsWith("GET / ")) // Startseite + { + sendStartPage(client); + httpMethod = 1; + } else if (httpRequest.startsWith("GET /ON ")) // Startseite + { + digitalWrite(RELAY_PORT, LOW); + relay = true; + sendStartPage(client); + httpMethod = 1; + } else if (httpRequest.startsWith("GET /OFF ")) // Startseite + { + digitalWrite(RELAY_PORT, HIGH); + relay = false; + sendStartPage(client); + httpMethod = 1; + } else if (httpRequest.startsWith("GET /settings ")) // Startseite + { + wifiScanCount = scanWiFi(); + sendSettingsPage(client); + httpMethod = 1; + } else if (httpRequest.startsWith("GET /save?")) //Nach Save Request + { + saveSettings(httpRequest); + sendSavedPage(client); + httpMethod = 5; + } + + if (httpMethod == 1 || httpMethod == 5) { + client.flush(); + client.stop(); + if (httpMethod == 5) { + delay(1000); + ESP.restart(); } + httpMethod = 0; + digitalWrite(LED_BUILTIN, HIGH); + } } } -void saveSettings(String inputData) -{ +void saveSettings(String inputData) { bool foundStart = false; String token = ""; @@ -227,72 +211,60 @@ void saveSettings(String inputData) ipType = ""; powerOn = ""; String mdns = ""; - - for(int i = 0 ; i < inputData.length() ; i++) - { + + for (int i = 0; i < inputData.length(); i++) { char c = inputData.charAt(i); token += c; - if (foundStart == false && c == '?') - { + if (foundStart == false && c == '?') { foundStart = true; - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ssid=")) - { - ssid=token.substring(5, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ssid=")) { + ssid = token.substring(5, token.length() - 1); + ; ssid = urldecode(ssid); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("password=")) - { - password=token.substring(9, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("password=")) { + password = token.substring(9, token.length() - 1); + ; password = urldecode(password); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ipType=")) - { - ipType=token.substring(7, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ipType=")) { + ipType = token.substring(7, token.length() - 1); + ; ipType = urldecode(ipType); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ip=")) - { - ip=token.substring(3, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("ip=")) { + ip = token.substring(3, token.length() - 1); + ; ip = urldecode(ip); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("subnet=")) - { - subnet=token.substring(7, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("subnet=")) { + subnet = token.substring(7, token.length() - 1); + ; subnet = urldecode(subnet); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("defaultgw=")) - { - defaultgw=token.substring(10, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("defaultgw=")) { + defaultgw = token.substring(10, token.length() - 1); + ; defaultgw = urldecode(defaultgw); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("dns=")) - { - dns=token.substring(4, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("dns=")) { + dns = token.substring(4, token.length() - 1); + ; dns = urldecode(dns); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("mdns=")) - { - mdns=token.substring(5, token.length()-1); + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("mdns=")) { + mdns = token.substring(5, token.length() - 1); mdns = urldecode(mdns); - token=""; - } - else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("powerOn=")) - { - powerOn=token.substring(8, token.length()-1);; + token = ""; + } else if (foundStart == true && (c == '&' || c == ' ') && token.startsWith("powerOn=")) { + powerOn = token.substring(8, token.length() - 1); + ; powerOn = urldecode(powerOn); - token=""; - } - } + token = ""; + } + } saveEEPROM(ssid, ssid_adr, 40); saveEEPROM(password, password_adr, 30); saveEEPROM(ip, ip_adr, 15); @@ -302,13 +274,12 @@ void saveSettings(String inputData) saveEEPROM(ipType, ipType_adr, 1); saveEEPROM(powerOn, powerOn_adr, 1); saveEEPROM(mdns, mDNS_adr, 20); - EEPROM.commit(); + EEPROM.commit(); readValuesFromEeprom(); } -void readValuesFromEeprom() -{ +void readValuesFromEeprom() { ssid = readEEPROM(ssid_adr, 40); password = readEEPROM(password_adr, 30); ip = readEEPROM(ip_adr, 15); @@ -319,58 +290,47 @@ void readValuesFromEeprom() powerOn = readEEPROM(powerOn_adr, 1); mDNS_name = readEEPROM(mDNS_adr, 20); - if (mDNS_name.length() == 0) - { + if (mDNS_name.length() == 0) { String mdns = "myrelaycard"; saveEEPROM(mdns, mDNS_adr, 20); - EEPROM.commit(); - readValuesFromEeprom(); + EEPROM.commit(); + readValuesFromEeprom(); } } -void saveEEPROM(String in, int adr, int len) -{ +void saveEEPROM(String in, int adr, int len) { clearDataStructure(); - in.toCharArray(data.str, in.length()+1); + in.toCharArray(data.str, in.length() + 1); data.val = len; - EEPROM.put(adr,data); + EEPROM.put(adr, data); } -String readEEPROM(int adr, int len) -{ +String readEEPROM(int adr, int len) { clearDataStructure(); data.val = len; - EEPROM.get(adr,data); - for(int i=0; i < len; i++) - { - if(data.str[i] != 255 && data.str[i] != 63) - { + EEPROM.get(adr, data); + for (int i = 0; i < len; i++) { + if (data.str[i] != 255 && data.str[i] != 63) { return data.str; } } return ""; } -void clearDataStructure() -{ - data.val = 0; - strncpy(data.str,"",80); +void clearDataStructure() { + data.val = 0; + strncpy(data.str, "", 80); } -void checkRS232() -{ +void checkRS232() { ESPserialEvent(); - if (stringComplete) - { - if (inputString.startsWith("setON")) - { + if (stringComplete) { + if (inputString.startsWith("setON")) { digitalWrite(RELAY_PORT, LOW); relay = true; Serial.println("successful set ON"); - } - else if (inputString.startsWith("setOFF")) - { + } else if (inputString.startsWith("setOFF")) { digitalWrite(RELAY_PORT, HIGH); relay = false; Serial.println("successful set OFF"); @@ -381,53 +341,45 @@ void checkRS232() } } -void ESPserialEvent() -{ - while (Serial.available()) - { +void ESPserialEvent() { + while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; - if (inChar == '\n') - { + if (inChar == '\n') { stringComplete = true; } } } -int scanWiFi() -{ +int scanWiFi() { int networksFound = WiFi.scanNetworks(false, true); - for (int i = 0; i < networksFound && i < 50; i++) - { + for (int i = 0; i < networksFound && i < 50; i++) { ssidVolume[i] = WiFi.RSSI(i); } return networksFound; } -void startWifiAccessPoint() -{ +void startWifiAccessPoint() { Serial.println(); Serial.println("Startup as AccessPoint with Name: Relay-Modul"); - - IPAddress local_IP(192,168,1,1); - IPAddress gateway(192,168,1,1); - IPAddress subnet(255,255,255,0); - + + IPAddress local_IP(192, 168, 1, 1); + IPAddress gateway(192, 168, 1, 1); + IPAddress subnet(255, 255, 255, 0); + WiFi.softAP("Relay-Modul"); WiFi.softAPConfig(local_IP, gateway, subnet); } -bool startWiFiClient() -{ +bool startWiFiClient() { // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); - + WiFi.mode(WIFI_STA); - if (ipType.startsWith("1")) - { + if (ipType.startsWith("1")) { IPAddress ipAddress; ipAddress.fromString(ip); IPAddress gatewayAddress; @@ -436,22 +388,20 @@ bool startWiFiClient() dnsAddress.fromString(dns); IPAddress subnetAddress; subnetAddress.fromString(subnet); - - WiFi.config(ipAddress, gatewayAddress, dnsAddress, subnetAddress); + + WiFi.config(ipAddress, gatewayAddress, dnsAddress, subnetAddress); } - + WiFi.begin(ssid, password); int count = 0; - while (WiFi.status() != WL_CONNECTED && count < 20) - { - delay(500); - Serial.print("."); - count ++; + while (WiFi.status() != WL_CONNECTED && count < 20) { + delay(500); + Serial.print("."); + count++; } - if (count >= 20) - { + if (count >= 20) { return false; } @@ -460,8 +410,7 @@ bool startWiFiClient() Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); - if (mDNS_name.length() > 0) - { + if (mDNS_name.length() > 0) { Serial.print("mDNS address: http://"); Serial.print(mDNS_name); Serial.println(".local"); @@ -470,81 +419,65 @@ bool startWiFiClient() return true; } -String urldecode(String str) -{ - - String encodedString=""; - char c; - char code0; - char code1; - for (int i =0; i < str.length(); i++) - { - c=str.charAt(i); - if (c == '+') - { - encodedString+=' '; - } - else if (c == '%') - { - i++; - code0=str.charAt(i); - i++; - code1=str.charAt(i); - c = (h2int(code0) << 4) | h2int(code1); - encodedString+=c; - } - else - { - encodedString+=c; - } - - yield(); +String urldecode(String str) { + + String encodedString = ""; + char c; + char code0; + char code1; + for (int i = 0; i < str.length(); i++) { + c = str.charAt(i); + if (c == '+') { + encodedString += ' '; + } else if (c == '%') { + i++; + code0 = str.charAt(i); + i++; + code1 = str.charAt(i); + c = (h2int(code0) << 4) | h2int(code1); + encodedString += c; + } else { + encodedString += c; } - return encodedString; + + yield(); + } + return encodedString; } -unsigned char h2int(char c) -{ - if (c >= '0' && c <='9') - { - return((unsigned char)c - '0'); - } - if (c >= 'a' && c <='f') - { - return((unsigned char)c - 'a' + 10); - } - if (c >= 'A' && c <='F') - { - return((unsigned char)c - 'A' + 10); - } - return(0); +unsigned char h2int(char c) { + if (c >= '0' && c <= '9') { + return ((unsigned char)c - '0'); + } + if (c >= 'a' && c <= 'f') { + return ((unsigned char)c - 'a' + 10); + } + if (c >= 'A' && c <= 'F') { + return ((unsigned char)c - 'A' + 10); + } + return (0); } -void sendSavedPage(WiFiClient client) -{ +void sendSavedPage(WiFiClient client) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); client.println(""); client.println("NTP Client"); sendFavicon(client); - if (ipType.startsWith("1")) - { + if (ipType.startsWith("1")) { client.print(""); } client.println(""); client.println("

SETTINGS saved

"); - if (ipType.startsWith("1")) - { + if (ipType.startsWith("1")) { client.println("Please wait, in 10 seconds you will be redirected to the Start Page..."); client.print("

Start Page"); - } - else - { + } else { client.println("Please enter the IP which has set by DHCP from your Router or the mDNS name followed by .local"); } @@ -554,13 +487,11 @@ void sendSavedPage(WiFiClient client) client.flush(); } -void sendFavicon(WiFiClient client) -{ +void sendFavicon(WiFiClient client) { client.print(""); } -void sendStartPage(WiFiClient client) -{ +void sendStartPage(WiFiClient client) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); @@ -569,19 +500,17 @@ void sendStartPage(WiFiClient client) sendFavicon(client); client.println(""); sendStyle(client); - client.println("

WiFi Relay

"); + client.println("

WiFi Relay

"); client.println("
"); - client.println("

WiFi Relay:

"); + client.println("

WiFi Relay:

"); client.print(""); client.print(" "); client.print(""); @@ -592,8 +521,7 @@ void sendStartPage(WiFiClient client) client.flush(); } -void sendSettingsPage(WiFiClient client) -{ +void sendSettingsPage(WiFiClient client) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); @@ -602,19 +530,17 @@ void sendSettingsPage(WiFiClient client) sendFavicon(client); client.println(""); sendStyle(client); - client.println("

WiFi Relay Settings:

"); + client.println("

WiFi Relay Settings:

"); client.println("
"); - - client.println("

WiFi:

"); + + client.println("

WiFi:

"); client.println("
"); client.println(""); client.println("
"); client.println("SSID:"); client.println("
"); client.println("

"); - - client.println("

IP:

"); - client.println("
"); + + client.println("

IP:

"); + client.println("
"); client.println("Type:"); client.println(""); client.print(""); - client.println("
"); + client.println("\">"); + client.println("
"); client.println("Subnet:"); client.print(""); - client.println("
"); + client.println("
"); client.println("Default Gateway:"); client.print(""); - client.println("
"); + client.println("\"/>"); + client.println("
"); client.println("DNS Server:"); client.print(""); + client.println("\"/>"); - client.println("
"); - client.println("mDNS Name:"); + client.println("
"); + client.println("mDNS Name:"); client.print(".local"); @@ -683,31 +607,29 @@ void sendSettingsPage(WiFiClient client) client.println("

"); - client.println("

Relay:

"); - client.println("
"); + client.println("

Relay:

"); + client.println("
"); client.println("Relay on Power on:"); client.println(""); client.println("


"); - - client.println("
"); + + client.println("
"); client.println(" "); client.println(""); client.println(""); - - + + client.println("