HaWe
21.06.2020, 12:55
hallo,
nachdem prinzipiell der ESP32 webserver funktioniert und man mit ihm über eine von ihm aufgebaute website zwei eigene onboard-LEDs schalten kann, soll nun der nächste Schritt folgen:
die 2. LED soll nicht auf dem ESP32 liegen, sondern per Wifi/Website auf einem zusätzlichen ESP8266 geschaltet werden(LED_BUILTIN), der mit dem ESP32 und seiner Website verbunden werden soll.
Was muss man wie tun, um diesen ESP8266 mit einzubinden?
(meinen bisherigen Code mit esp8266 webserver/wifiserver für einen zweiten esp8266 als remote client kann ich leider nicht verwenden, weil dieser nicht esp32-kompatibel ist)
Hier ist der bisherige ESP32 Code (nur lokale ESP32-LED-Steuerung, kein weiterer esp client):
// Create A Simple ESP32 Web Server In Arduino IDE
// https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide/
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#include <WebServer.h>
// #include <WiFiServer.h> // <<<< funktioniert bei ESP32 nicht wie bei ESP8266!
/*Put your SSID & Password*/
const char* ssid = "WLAN"; // Enter SSID here
const char* password = "1865844"; //Enter Password here
char www_username[64] = "admin";
char www_password[64] = "esp32";
#define this_ip_4 202 // <<< static webserver ip (4th byte)
IPAddress this_ip(192, 168, 2, this_ip_4); // <<< static local IP of this ESP-webserver
IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP
IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask
#define wwwport 8008
#define wifiport 8081
WebServer webserver(wwwport); // html website: http port www
// WiFiServer server(wifiport); // client comm: wifi port lan <<<< evtl auch webserer verwenden?
// allows you to set the realm of authentication Default:"Login Required"
const char* www_realm = "Custom Auth Realm";
// the Content of the HTML response in case of Unautherized Access Default:empty
String authFailResponse = "Authentication Failed";
uint8_t LED1pin = LED_BUILTIN;
bool LED1status = LOW;
uint8_t LED2pin = 12;
bool LED2status = LOW;
//----------------------------------------------------------
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += webserver.uri();
message += "\nMethod: ";
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += webserver.args();
message += "\n";
for (uint8_t i = 0; i < webserver.args(); i++) {
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
}
webserver.send(404, "text/plain", message);
}
//----------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
WiFi.config(this_ip, gateway, subnet); // static IP
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
ArduinoOTA.begin();
webserver.on("/", []() {
if (!webserver.authenticate(www_username, www_password))
//Basic Auth Method with Custom realm and Failure Response
//return webserver.requestAuthentication(BASIC_AUTH, www_realm, authFailResponse);
//Digest Auth Method with realm="Login Required" and empty Failure Response
//return webserver.requestAuthentication(DIGEST_AUTH);
//Digest Auth Method with Custom realm and empty Failure Response
//return webserver.requestAuthentication(DIGEST_AUTH, www_realm);
//Digest Auth Method with Custom realm and Failure Response
{
return webserver.requestAuthentication(DIGEST_AUTH, www_realm, authFailResponse);
}
handle_OnConnect();
});
/*
webserver.on("/inline", []() {
webserver.send(200, "text/plain", "this works as well");
});
*/
webserver.on("/led1on", handle_led1on);
webserver.on("/led1off", handle_led1off);
webserver.on("/led2on", handle_led2on);
webserver.on("/led2off", handle_led2off);
webserver.onNotFound(handle_NotFound);
webserver.begin();
Serial.println("HTTP webserver started");
}
//----------------------------------------------------------
void loop() {
ArduinoOTA.handle();
webserver.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
//----------------------------------------------------------
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println("GPIO13 Status: OFF | GPIO12 Status: OFF");
webserver.send(200, "text/html", SendHTML(LED1status,LED2status));
}
//----------------------------------------------------------
void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO13 Status: ON");
webserver.send(200, "text/html", SendHTML(true,LED2status));
}
//----------------------------------------------------------
void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO13 Status: OFF");
webserver.send(200, "text/html", SendHTML(false,LED2status));
}
//----------------------------------------------------------
void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO12 Status: ON");
webserver.send(200, "text/html", SendHTML(LED1status,true));
}
//----------------------------------------------------------
void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO12 Status: OFF");
webserver.send(200, "text/html", SendHTML(LED1status,false));
}
//----------------------------------------------------------
void handle_NotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += webserver.uri();
message += "\nMethod: ";
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += webserver.args();
message += "\n";
for (uint8_t i = 0; i < webserver.args(); i++) {
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
}
webserver.send(404, "text/plain", message);
//webserver.send(404, "text/plain", "Not found");
}
//----------------------------------------------------------
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; /*text-align: center;*/}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none; color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP32 Web Server</h1>\n";
ptr +="<h3>Using Station(STA) Mode</h3>\n";
ptr+="<p>";
if(led1stat) {ptr +="LED1 Status: ON ";}
else {ptr +=" LED1 Status: OFF ";}
{ptr +="<a class=\"button button-off\" href=\"/led1off\">OFF</a>";}
{ptr +="<a class=\"button button-on\" href=\"/led1on\">ON</a>";}
ptr+="</p>";
ptr+="<p>";
if(led2stat) {ptr +="LED2 Status: ON ";}
else {ptr +=" LED2 Status: OFF";}
{ptr +="<a class=\"button button-off\" href=\"/led2off\">OFF</a>";}
{ptr +="<a class=\"button button-on\" href=\"/led2on\">ON</a>";}
ptr+="</p>";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
https://lastminuteengineers.com/wp-content/uploads/2019/12/Creating-Simple-ESP32-Webserver-in-Arduino-IDE-using-Access-Point-Station-mode.jpg
https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide/
nachdem prinzipiell der ESP32 webserver funktioniert und man mit ihm über eine von ihm aufgebaute website zwei eigene onboard-LEDs schalten kann, soll nun der nächste Schritt folgen:
die 2. LED soll nicht auf dem ESP32 liegen, sondern per Wifi/Website auf einem zusätzlichen ESP8266 geschaltet werden(LED_BUILTIN), der mit dem ESP32 und seiner Website verbunden werden soll.
Was muss man wie tun, um diesen ESP8266 mit einzubinden?
(meinen bisherigen Code mit esp8266 webserver/wifiserver für einen zweiten esp8266 als remote client kann ich leider nicht verwenden, weil dieser nicht esp32-kompatibel ist)
Hier ist der bisherige ESP32 Code (nur lokale ESP32-LED-Steuerung, kein weiterer esp client):
// Create A Simple ESP32 Web Server In Arduino IDE
// https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide/
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#include <WebServer.h>
// #include <WiFiServer.h> // <<<< funktioniert bei ESP32 nicht wie bei ESP8266!
/*Put your SSID & Password*/
const char* ssid = "WLAN"; // Enter SSID here
const char* password = "1865844"; //Enter Password here
char www_username[64] = "admin";
char www_password[64] = "esp32";
#define this_ip_4 202 // <<< static webserver ip (4th byte)
IPAddress this_ip(192, 168, 2, this_ip_4); // <<< static local IP of this ESP-webserver
IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP
IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask
#define wwwport 8008
#define wifiport 8081
WebServer webserver(wwwport); // html website: http port www
// WiFiServer server(wifiport); // client comm: wifi port lan <<<< evtl auch webserer verwenden?
// allows you to set the realm of authentication Default:"Login Required"
const char* www_realm = "Custom Auth Realm";
// the Content of the HTML response in case of Unautherized Access Default:empty
String authFailResponse = "Authentication Failed";
uint8_t LED1pin = LED_BUILTIN;
bool LED1status = LOW;
uint8_t LED2pin = 12;
bool LED2status = LOW;
//----------------------------------------------------------
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += webserver.uri();
message += "\nMethod: ";
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += webserver.args();
message += "\n";
for (uint8_t i = 0; i < webserver.args(); i++) {
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
}
webserver.send(404, "text/plain", message);
}
//----------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
WiFi.config(this_ip, gateway, subnet); // static IP
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
ArduinoOTA.begin();
webserver.on("/", []() {
if (!webserver.authenticate(www_username, www_password))
//Basic Auth Method with Custom realm and Failure Response
//return webserver.requestAuthentication(BASIC_AUTH, www_realm, authFailResponse);
//Digest Auth Method with realm="Login Required" and empty Failure Response
//return webserver.requestAuthentication(DIGEST_AUTH);
//Digest Auth Method with Custom realm and empty Failure Response
//return webserver.requestAuthentication(DIGEST_AUTH, www_realm);
//Digest Auth Method with Custom realm and Failure Response
{
return webserver.requestAuthentication(DIGEST_AUTH, www_realm, authFailResponse);
}
handle_OnConnect();
});
/*
webserver.on("/inline", []() {
webserver.send(200, "text/plain", "this works as well");
});
*/
webserver.on("/led1on", handle_led1on);
webserver.on("/led1off", handle_led1off);
webserver.on("/led2on", handle_led2on);
webserver.on("/led2off", handle_led2off);
webserver.onNotFound(handle_NotFound);
webserver.begin();
Serial.println("HTTP webserver started");
}
//----------------------------------------------------------
void loop() {
ArduinoOTA.handle();
webserver.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
//----------------------------------------------------------
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println("GPIO13 Status: OFF | GPIO12 Status: OFF");
webserver.send(200, "text/html", SendHTML(LED1status,LED2status));
}
//----------------------------------------------------------
void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO13 Status: ON");
webserver.send(200, "text/html", SendHTML(true,LED2status));
}
//----------------------------------------------------------
void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO13 Status: OFF");
webserver.send(200, "text/html", SendHTML(false,LED2status));
}
//----------------------------------------------------------
void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO12 Status: ON");
webserver.send(200, "text/html", SendHTML(LED1status,true));
}
//----------------------------------------------------------
void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO12 Status: OFF");
webserver.send(200, "text/html", SendHTML(LED1status,false));
}
//----------------------------------------------------------
void handle_NotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += webserver.uri();
message += "\nMethod: ";
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += webserver.args();
message += "\n";
for (uint8_t i = 0; i < webserver.args(); i++) {
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
}
webserver.send(404, "text/plain", message);
//webserver.send(404, "text/plain", "Not found");
}
//----------------------------------------------------------
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; /*text-align: center;*/}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none; color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP32 Web Server</h1>\n";
ptr +="<h3>Using Station(STA) Mode</h3>\n";
ptr+="<p>";
if(led1stat) {ptr +="LED1 Status: ON ";}
else {ptr +=" LED1 Status: OFF ";}
{ptr +="<a class=\"button button-off\" href=\"/led1off\">OFF</a>";}
{ptr +="<a class=\"button button-on\" href=\"/led1on\">ON</a>";}
ptr+="</p>";
ptr+="<p>";
if(led2stat) {ptr +="LED2 Status: ON ";}
else {ptr +=" LED2 Status: OFF";}
{ptr +="<a class=\"button button-off\" href=\"/led2off\">OFF</a>";}
{ptr +="<a class=\"button button-on\" href=\"/led2on\">ON</a>";}
ptr+="</p>";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
https://lastminuteengineers.com/wp-content/uploads/2019/12/Creating-Simple-ESP32-Webserver-in-Arduino-IDE-using-Access-Point-Station-mode.jpg
https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide/