Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By martinayotte
#45281 Hi Richard,

If you look at ESP8266WebServer.cpp, you will see that server.send() is actually using client.write() under the hood.

@ceyhunkeklik, what do you means by saying that digitalWrite() is not working ?
User avatar
By observatus
#55332 Hello all, I know this is an old thread, but I figured I'd give it a try asking a question. I have been able to modify the sketch provided by igrr to control a light strip. I built a simple web page and opened a port on my router to allow the control from a cell phone (or any internet connection). I am confused about the image file, and why that is needed. I am not an expert at C++ or html, so my code might be pretty bad. My question is whether the image file can be eliminated. Below is the section of code where I create the web page.

Any assistance would be appreciated.

And THANK YOU igrr for the sketch example and library!!

String form = "<body><form action='led'><H1><font color=Black>ARDUINO LIGHT CONTROL</h1><button style=height:50px;width:100px name=state value=1>BLUE</button><br><button style=height:50px;width:100px name=state value=2>GREEN</button><br><button style=height:50px;width:100px name=state value=3>RED</button><br><button style=height:50px;width:100px name=state value=4>BLUE/GREEN</button><br><button style=height:50px;width:100px name=state value=5>PINK</button><br><button style=height:50px;width:100px name=state value=0>OFF</button><br></form><p><h1>Click the button for your selection.</h1></p></body><br></html>";
User avatar
By WisterDesigns
#56624
igrr wrote: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:
https://gist.github.com/igrr/3b66292cf8708770f76b.

Can you please link me the ESP8266Websocket library? The links don't work anymore!
User avatar
By rlivio
#75155 Hi Igrr
ESP8266WebServer is really an easy-to-use library, I can be able to have a working Web Server in a very short time.
But now I would like to explore more the powerful of this library, do you know where I can found the documentation about this library?

My code has been copied from some example found on the web, but, for example, now I would like to know the IP of the client connected to my server, I think I must add some item to this code:

Code: Select all  const char * headerkeys[] = {"User-Agent", "Cookie"} ;
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  server.collectHeaders(headerkeys, headerkeyssize );         //ask server to track these headers (nel file ESP8266WebServer.cpp)


to collect also the IP, but I don't know how to ask, I try to add "IP" but doesn't work.
I'd rather find the documentation rather than asking every thing in the forum every time.

I found somethink at: https://links2004.github.io/Arduino/d3/ ... erver.html
but isn't explained the parameters of the functions.

Thank you
Livio