Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By ectoplasma
#41308 Hey all,

I am looking for a good example of an esp standalone arduino webserver with html and http requests.
Something simple like switching an led on and off based on a http request.

Side note: I manage to make html webservers on my wemos d1 board and find them locally. I manage to make ethernet webservers locally on my uno with http requests. When i try to include http requests like "ProcessCheckbox()" in a wifi.client example it refuses to compile for some reason. Ideally I'd like to get the following example working on my Wemos board: https://startingelectronics.org/tutoria ... D-control/

I can do html , css and some JS , I can do basic ethernet sketches and arduino... But for this I really just need a push in the right direction. All help is appreciated!!
User avatar
By volthaus
#41332 This sketch results in super stable server and you can turn off and on two LEDs.

Code: Select all/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com 
*********/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

ESP8266WebServer server(80);

String webPage = "";
 
void setup(void){
  webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a>&nbsp;<a href=\"socket1Off\"><button>OFF</button></a></p>";
  webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";
 
  // preparing GPIOs
//  pinMode(gpio0_pin, OUTPUT);
  digitalWrite(gpio0_pin, LOW);
  pinMode(gpio2_pin, OUTPUT);
  digitalWrite(gpio2_pin, LOW);
 
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
 
  server.on("/", [](){
    server.send(200, "text/html", webPage);
  });
  server.on("/socket1On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio0_pin, HIGH);
    delay(1000);
  });
  server.on("/socket1Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio0_pin, LOW);
    delay(1000);
  });
  server.on("/socket2On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio2_pin, HIGH);
    delay(1000);
  });
  server.on("/socket2Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio2_pin, LOW);
    delay(1000);
  });
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void){
  server.handleClient();
}
User avatar
By ba_us
#41340 Hi volthaus,

I expereince some problems with the webserver, sometimes a request from the browser is intterupted during send out data with :er -8

I tried your sketch, modified it to stream out a table with data and was very happy to see that about 200 requests were processed correctly. BUT at about 350 request the same error appeared :(

here is the modified code:

Code: Select all/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com
*********/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// needed to avoid link error on ram check
extern "C"

  #include "user_interface.h"
}

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";

unsigned long ulReqcount;       // how often has a valid page been requested


ESP8266WebServer server(80);

String webPage = "";

const char* createTable()

  ulReqcount++;
  webPage  = F("<!DOCTYPE HTML>");
  webPage += F("\r\n<html>\r\n<head>\r\n<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
  webPage += F("\r\n\t<meta http-equiv=\"refresh\" content=\"2; url=/\">");
  webPage += F("\r\n\t<style>");
  webPage += F("\r\n\t\tbody {background-color: #dfdfdf;}");
  webPage += F("\r\n\t\tfont {color: #000000;}");
  webPage += F("\r\n\t\ttable {font-size: large; width:100%;}");
  webPage += F("\r\n\t\ttable, th, td {border: 2px solid black; border-collapse: collapse;}");
  webPage += F("\r\n\t\tth, td {padding: 5px;}");
  webPage += F("\r\n\t\tth {text-align: left;}");
  webPage += F("\r\n\t</style>\r\n<title>Test</title>\r\n</head>\r\n<body>");
  webPage += F("\r\n<h1>Testdata</h1>");
  webPage += F("\r\n<table>");
  webPage += F("\r\n\t<tbody><tr><th>#</th><th>Time</th><th>Calibration used</th><th>Sesnor 1</th><th>Sensor2</th></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:12</td><td>Standard</td><td>25,12</td><td>10,60</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:14</td><td>Standard</td><td>25,14</td><td>10,67</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:16</td><td>Standard</td><td>25,09</td><td>10,58</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:18</td><td>Standard</td><td>25,11</td><td>10,62</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:20</td><td>Standard</td><td>25,12</td><td>10,61</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:22</td><td>Standard</td><td>25,15</td><td>10,70</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:24</td><td>Standard</td><td>25,10</td><td>10,63</td></tr>");
  webPage += F("\r\n\t<tr><td>1</td><td>04.02.2016 09:40:26</td><td>Standard</td><td>25,13</td><td>10,67</td></tr>");
  webPage += F("\r\n</tbody></table>");
  webPage += F("\r\n<div style=\"font-size:x-small\">");
  webPage += F("\r\n\t<BR>requests: ");
  webPage += ulReqcount;
  webPage += F("\r\n\t<BR>free RAM: ");
  webPage += (uint32_t)system_get_free_heap_size();
  webPage += F("\r\n</div>\r\n</body>\r\n</html>");
 
  return webPage.c_str();
}

 
void setup(void)
{
  ulReqcount = 0;
  delay(1000);
  Serial.begin(38400);
  Serial1.begin(38400);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
 
  server.on("/", [](){
    server.send(200, "text/html", createTable());
  });
 
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void){
  server.handleClient();
}


Any ideas what is wrong here?
I have asked a question here regarding the same issue, but that it is not reviewed by mod yet.

Greetz,
Bob