diff --git a/Misc/VeryBasicWebserver.ino b/Misc/VeryBasicWebserver.ino index cff831f..ffe09ec 100644 --- a/Misc/VeryBasicWebserver.ino +++ b/Misc/VeryBasicWebserver.ino @@ -1,7 +1,7 @@ // ---------------------------------------------------------------- // // -// ESP32 very basic web server demo - 02Jan21 +// ESP32 / ESp8266 very basic web server demo - 02Jan21 // // Starting point to experiment with // @@ -20,18 +20,26 @@ bool serialDebug = 1; // if to enable debugging info on serial port // ---------------------------------------------------------------- -#if !defined ESP32 - #error You need to select ESP32 in the Arduino IDE +//#include // required by platformio? + +#if defined ESP32 + // esp32 + byte LEDpin = 2; + #include + #include + #include // Web server running on port 80 + WebServer server(80); +#elif defined ESP8266 + //Esp8266 + byte LEDpin = D4; + #include + #include + #include "ESP8266HTTPClient.h" + ESP8266WebServer server(80); +#else + #error "This sketch only works with the ESP8266 or ESP32" #endif -//#include // required by platformio -#include -#include -#include - -// Web server running on port 80 - WebServer server(80); - // ---------------------------------------------------------------- @@ -44,6 +52,10 @@ void setup() { Serial.begin(115200); // start serial comms at speed 115200 Serial.println("\n\nWebserver demo sketch"); + // onboard LEDs + pinMode(LEDpin, OUTPUT); + digitalWrite(LEDpin, LOW); + // Connect to Wifi Serial.print("Connecting to "); Serial.println(SSID); @@ -63,8 +75,13 @@ void setup() { // start web server server.begin(); - WiFi.setSleep(false); // stop the wifi being turned off if not used for a while - + #if defined ESP32 + WiFi.setSleep(false); // stop the wifi being turned off if not used for a while (esp32) + #else + WiFi.setSleepMode(WIFI_NONE_SLEEP); // stop the wifi being turned off if not used for a while (esp8266) + #endif + WiFi.mode(WIFI_STA); // turn off access point - options are WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF + } // setup @@ -77,9 +94,10 @@ void setup() { void loop() { - server.handleClient(); // service any web page requests + server.handleClient(); // service any web page requests - delay(100); // wait 100 milliseconds + digitalWrite(LEDpin, !digitalRead(LEDpin)); // invert onboard LED status + delay(200); // wait 200 milliseconds } // loop @@ -114,13 +132,13 @@ void handleTest(){ WiFiClient client = server.client(); // open link with client // html header - client.write(" Web Demo \n"); // basic html header + client.print(" Web Demo \n"); // basic html header // html body client.print("

Test Page

\n"); // end html - client.write("\n"); + client.print("\n"); delay(3); client.stop(); @@ -153,18 +171,18 @@ void handleButton(){ WiFiClient client = server.client(); // open link with client // html header - client.write(" Web Demo \n"); // basic html header - client.write("
\n"); // used by the buttons in the html (action = the web page to send it to + client.print(" Web Demo \n"); // basic html header + client.print("\n"); // used by the buttons in the html (action = the web page to send it to // html body client.print("

Button demo page

\n"); if (server.hasArg("button1")) client.print("Button 1 has been pressed!"); if (server.hasArg("button2")) client.print("Button 2 has been pressed!"); - client.write("

\n"); - client.write("

\n"); + client.print("

\n"); + client.print("

\n"); // end html - client.write("\n"); + client.print("\n"); delay(3); client.stop();