Hello,
As a rather new developer in Arduino and it must be +25 year since I lasted used C++ I do have sort of frustration if I choose the right WEB server for my ESP32C6 solution.
Here is the challenge I seem to have: I have 8 curtain motors (Somfy Glydea 35 RTS) which I control from an ESP32-C6 board (Sparkfun Quick). Of the 8 ESP32-C6 cards, 5 works perfect after 3 years or something. So that leaves the 3 that still kind of works, however the response from them is always delayed, between 1 to 3 seconds, and sometimes it just fails. It is not often it fails, that is maybe once every 6-9 months, however the delay is a bit more frustrating. Now all 8 cards run the exact same compiled software. Well except one of the 3 above which I kind of hammering with different examples of web server to see if that changes the challenge.
Se it like this: Either one has direct response (the 5 working ESP32-C6) or the command sent is delayed with 1-3 seconds. For me there is no reason for this strange behavior, the same kind of ESP-card and compiled software (with the exception that one of the remaining 3 is some sort of development work on - it does however never change this delay behavior). But I still wonder if my very old knowledge in C++ is the reason this happens? Or if for some reason I somehow managed to set the ESP32-C6 into some sort of low power CPU setting (no need, it is powered over 230v outlets into the Glydea motor, which converts it to 3.3v which I feed direct into ESP32-C6 pin for 3.3v - and it has more than enough to drive the card, I even tried a stabilizing solution, no change).
So what I would love is for someone to check my code, and tell me what I might improve - or if I should change web server (webserver.h - but I have tested the Async version also - no difference)? Any ideas or comments are welcome - and please do remember it is a looooong time since I used C++, consider me a beginner (heck I have used IBM Mainframe Assembler for just a few years ago so I seem to know that better, although I am actually more of a SQL person nowdays).
#include <WiFi.h>
#include <WebServer.h>
#include <EEPROM.h>
#define EEPROM_SIZE 1
#define GPIO_OPEN 2
#define GPIO_CLOSE 3
#define GPIO_MYPOSITION 18
#define CURTAIN_UNKNOWN 0
#define CURTAIN_CLOSE 1
#define CURTAIN_OPEN 2
#define CURTAIN_MYPOSITION 3
byte curtain_status = 0;
// -------------------------------------------------------------------
const char* WIFI_NAME = "xxxx";
const char* WIFI_PASSWORD = "xxx";
#define SERVERPORT 80
WebServer server(SERVERPORT);
#define WEBPAGESIZE 500
char webpage[WEBPAGESIZE];
// -------------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(2000);
EEPROM.begin(EEPROM_SIZE);
curtain_status = EEPROM.read(0);
pinMode(GPIO_CLOSE, INPUT);
pinMode(GPIO_OPEN, INPUT);
pinMode(GPIO_MYPOSITION, INPUT);
Serial.print("Connecting to ");
Serial.println(WIFI_NAME);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print(" done!\nSignal strength: ");
Serial.println(String(WiFi.RSSI()));
Serial.print("Webserver started at http://");
Serial.println(WiFi.localIP());
server.on("/", handlewebpage_root);
server.on("/status", handlewebpage_status);
server.on("/restart", handlewebpage_restart);
server.on("/open", handlewebpage_open);
server.on("/close", handlewebpage_close);
server.on("/myposition", handlewebpage_myposition);
server.on("/changedirection", handlewebpage_changedirection);
server.on("/storemyposition", handlewebpage_storemyposition);
server.begin();
}
// -------------------------------------------------------------------
void loop() {
server.handleClient();
// delay(1);
}
// -------------------------------------------------------------------
void handlewebpage_root() {
// Serial.println("Called root");
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_open() {
// Serial.println("Called open");
curtain_status = CURTAIN_OPEN;
EEPROM.write(0, curtain_status);
EEPROM.commit();
pinMode(GPIO_OPEN, OUTPUT);
pinMode(GPIO_MYPOSITION, INPUT);
pinMode(GPIO_CLOSE, INPUT);
digitalWrite(GPIO_OPEN, LOW);
delay(200);
pinMode(GPIO_OPEN, INPUT);
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_close() {
// Serial.println("Called close");
curtain_status = CURTAIN_CLOSE;
EEPROM.write(0, curtain_status);
EEPROM.commit();
pinMode(GPIO_OPEN, INPUT);
pinMode(GPIO_MYPOSITION, INPUT);
pinMode(GPIO_CLOSE, OUTPUT);
digitalWrite(GPIO_CLOSE, LOW);
delay(200);
pinMode(GPIO_CLOSE, INPUT);
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_myposition() {
// Serial.println("Called myposition");
curtain_status = CURTAIN_MYPOSITION;
EEPROM.write(0, curtain_status);
EEPROM.commit();
pinMode(GPIO_OPEN, INPUT);
pinMode(GPIO_MYPOSITION, OUTPUT);
pinMode(GPIO_CLOSE, INPUT);
digitalWrite(GPIO_MYPOSITION, LOW);
delay(200);
pinMode(GPIO_MYPOSITION, INPUT);
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_changedirection() {
// Serial.println("Called changedirection");
pinMode(GPIO_OPEN, OUTPUT);
pinMode(GPIO_MYPOSITION, OUTPUT);
pinMode(GPIO_CLOSE, OUTPUT);
digitalWrite(GPIO_OPEN, LOW);
digitalWrite(GPIO_MYPOSITION, LOW);
digitalWrite(GPIO_CLOSE, LOW);
delay(4200);
pinMode(GPIO_OPEN, INPUT);
pinMode(GPIO_MYPOSITION, INPUT);
pinMode(GPIO_CLOSE, INPUT);
delay(500);
pinMode(GPIO_MYPOSITION, OUTPUT);
digitalWrite(GPIO_MYPOSITION, LOW);
delay(2500);
pinMode(GPIO_MYPOSITION, INPUT);
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_storemyposition() {
// Serial.println("Called storemyposition");
pinMode(GPIO_OPEN, INPUT);
pinMode(GPIO_MYPOSITION, OUTPUT);
pinMode(GPIO_CLOSE, INPUT);
digitalWrite(GPIO_MYPOSITION, LOW);
delay(4500);
pinMode(GPIO_MYPOSITION, INPUT);
SendHTML();
}
// -------------------------------------------------------------------
void handlewebpage_restart() {
// Serial.println("Called restart");
snprintf(webpage, WEBPAGESIZE, "<!DOCTYPE HTML><html>Restart</html>");
server.send(200, "text/html", FPSTR(webpage));
esp_restart();
}
// -------------------------------------------------------------------
void handlewebpage_status() {
// Serial.println("Called status");
if (curtain_status == CURTAIN_OPEN) { snprintf(webpage, WEBPAGESIZE, "<!DOCTYPE HTML><html>Open</html>"); }
if (curtain_status == CURTAIN_CLOSE) { snprintf(webpage, WEBPAGESIZE, "<!DOCTYPE HTML><html>Close</html>"); }
if (curtain_status == CURTAIN_MYPOSITION) { snprintf(webpage, WEBPAGESIZE, "<!DOCTYPE HTML><html>MyPosition</html>"); }
server.send(200, "text/html", FPSTR(webpage));
}
// -------------------------------------------------------------------
void SendHTML() {
// Serial.println("Called SendHTML");
snprintf(webpage, WEBPAGESIZE, " \
<!DOCTYPE html> <html> \
<head><title>Curtain Control</title></head> \
<body> \
<h1><a href=""/"">Curtain Web Server v5.1</a></h1> \
<a href=\"/open\">Open</a><br> \
<a href=\"/close\">Close</a><br> \
<a href=\"/myposition\">My Position</a><br> \
<a href=\"/changedirection\">Change Direction</a><br> \
<a href=\"/storemyposition\">Store My Position</a><br> \
<a href=\"/restart\">Restart</a><br> \
<a href=\"/status\">Status\n</a> \
</body></html>");
server.send(200, "text/html", FPSTR(webpage));
}
And for reference I have updated Arduino to Arduino IDE 2.3.8 - no errors/warnings when compiling.