The actual implementation of a web server is much easier than it sounds, because the ESP8266 Arduino Core includes some great libraries that handle pretty much everything for you. Let's look at a basic Hello World! example.
LAN Speed Test is a straightforward Windows software tool designed to test the speed of your Local Area Network, relying on an intuitive user approach and a limited number of configuration options.
Test your Internet connection bandwidth to locations around the world with this interactive broadband speed test from Ookla. Get LAN Speed Test Server 1 Serial Key Updated February 2014.
There's a lot of code that's the same as in the Wi-Fi and mDNS examples.
The actual server code is pretty straightforward. First, we create a server instance that listens for HTTP requests on port 80. This is the default port for web servers. In the setup, we tell the server what to do with certain HTTP requests. If the URI '/' is requested, the server should reply with a HTTP status code of 200 (Ok) and then send a response with the words 'Hello world!'. We put the code for generating a response in a separate function, and the we tell the server to execute it when '/' is requested, using the function.
We haven't specified what the server should do if the client requests any URI other than '/'. It should respond with an HTTP status 404 (Not Found) and a message for the user. We put this in a function as well, and use to tell it that it should execute it when it receives a request for a URI that wasn't specified with .
Then we start listening for HTTP requests by using . During the loop, we constantly check if a new HTTP request is received by running . If handleClient detects new requests, it will automatically execute the right functions that we specified in the setup.
To test it out, upload the sketch, open a new browser tab, and browse to http://esp8266.local. You should get a webpage saying . If you try to go to a different page, http://esp8266.local/test, for instance, you should get a 404 error: .
Turning on and off an LED over Wi-Fi
We can use the web server to serve interactive pages, and to react to certain POST request. In the following example, the ESP8266 hosts a web page with a button. When the button is pressed, the browser sends a POST request to /LED. When the ESP receives such a POST request on the /LED URI, it will turn on or off the LED, and then redirect the browser back to the home page with the button.
In order to perform this redirect, the ESP has to add a Location header to the response, and use a 303 (See Other) HTTP status code.
The button to send the POST request in the browser is part of an HTML form. You have to specify the target URI to send the request to, and the request method, in this case this is '/LED' and POST respectively.
Note that I changed the content type of the response from 'text/plain' to 'text/html'. If you send it as plain text, the browser will display it as text instead of interpreting it as HTML and showing it as a button.
As you can see, the function now takes three parameters: the URI, the request method (GET or POST) and the function to execute.
Connect an LED to GPIO2, and upload the sketch. Then go to http://esp8266.local/ and click the button to turn the LED on or off.
You can open the developer options in Chrome (F12) to check the HTTP request that are made when you click the button: you'll see that it first send a POST request, and then receives a 303 (See Other) HTTP status as a response. The response also has a Location header containing the URI '/', so the browser will send a GET request to the URI of this new location:
If you check the page source (CTRL+U), you can see the simple HTML form that's used:
Sending data to the ESP using HTTP POST
In the previous example, we sent an empty POST request to the ESP8266. In the previous chapter however, I explained that it's possible to send all kinds of data in the body of the POST request.
In this example, I'll show you how to send a username and a password to the ESP. The ESP will then check if they are correct, and respond to the request with the appropriate page.
The HTML in handleRoot is:
Upload the sketch and go to http://esp8266.local/, then type 'John Doe' into the username field, and 'password123' into the password field, and click 'Login'. You should get a welcome screen. If you leave on or both of the fields blank, you should get a 400 (Bad Request) error. If you enter a wrong username or password, you should get a 401 (Unauthorized) error.
The data of the POST body can be accessed using , and you can check if a specific key exists using . The key name on the ESP8266 corresponds to the name argument in the HTML form on the web page.
Lan Speed Test Server Serial Key Euro Truck Simulator 2
When we get a POST request, we first check if the necessary arguments (username and password) are present. If that's not the case, we send a 400 (Invalid Request) status. Then we check if the credentials match 'John Doe' & 'password123'. If that's the case, we respond with a status 200 (Ok) and a welcome page. If the username and/or password doesn't match, we send a 401 (Unauthorized) status.
Inline functions
In the previous examples, we passed and to the function as a parameter (callback function). In some cases however, it's more readable to just write the definition of the function inline, like so: