Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By igrr
#12783 ESP8266WebServer is an easy-to-use library to set up a web server on ESP8266. It is quite minimal, nowhere near other perfect production-ready web servers like esp-httpd or WebBase. It serves only one client at a time.
It is however very easy to set up and use.

First, include the library into your sketch and create a server object:
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

Next, in your setup function tell the server what URIs it needs to respond to:
Code: Select allserver.on("/", [](){
  server.send(200, "text/plain", "This is an index page.");

or, which is equivalent:
Code: Select allvoid handle_index() {
  server.send(200, "text/plain", "This is an index page.");

// later, in setup() function:
server.on("/", handle_index);

The first argument is the URI and the second is the function that will be called to respond to this URI.
In that function you need to call
Code: Select allserver.send(code, content_type, content)
to actually send contents.

Processing arguments of GET and POST requests is also easy enough. Let's make our sketch turn a led on or off depending on the value of a request argument.
http://<ip address>/led?state=on will turn the led ON
http://<ip address>/led?state=off will turn the led OFF
Code: Select allserver.on("/led", []() {
  String state=server.arg("state");
  if (state == "on") digitalWrite(13, LOW);
  else if (state == "off") digitalWrite(13, HIGH);
  server.send(200, "text/plain", "Led is now " + state);

The complete example which also shows how to do forms and images is available here:
User avatar
By igrr
#12786 @Stevenelson It's here: https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h

Note that the library is bundled with the version of Arduino IDE which is available for download here:
User avatar
By 4refr0nt
#12791 I'm already have serveral arduino version on this computer (Win7 x64)
Download Arduino IDE from you link and select Generic ESP8266 board
Try example httpServer (from Examples menu and from prev post), but can't compile.
I have "Compile error" without any other debug info.
stderr.txt stdout.txt not containg any debug info too.
I'm try to run arduino.exe and arduino_debug.exe, but nothing found too.