ESP8266 ESP-01 and IoT of LED — Part 3
Series : Part 1, Part 2 , Part 3, Part 4
In Part 2 we implemented ESP-01 as a simple HTTP client and controlled the LED through it. In this article i will show how to implement ESP-01 as HTTP Webserver and get commands from the client directly. We are relieved off polling for commands as was the case in the client implementation in Part 2.
ESP-01 As Station and HTTP Web Server
Here the idea is to implement ESP-01 as a HTTP Web server and take LED commands via URL paths.
Lets go over the Arduino code.
The above code implements the following
- ESP8266–01 acts as station and is connected to an access point. See initWifiStation();
- ESP8266WebServer “server” C++ object is created to listen on port 80. The Callback functions are registered against the URL paths.
handlePlainRoot() is called on “/plain”. It provides LED status.
handlePlainGpio2On is called to turn LED ON on “/plain/GPIO2/ON”
handlePlainGpio2Off is called to turn LED OFF on “/plain/GPIO2/OFF”
3. WiFi.localIP() is the IP provided by the DHCP server. We can get this ip address by running “arp -a” in the command prompt
Web browser client accessing ESP8266 http server
Fancy HTML output
Instead of typing the URL, its easier to click on link and set the LED on and off. In the code below, “page not found” error is handled by server.onNotFound;
ESP-01 As Access Point and HTTP Web Server
A few additional code is required to make ESP as access point and still act as a web server
Sources
Project: ESP8266-ESP-01-WebServer
Conclusion
This article demonstrated the working of ESP8266 in Station and AP mode. LED is turned on and off using different URL paths. A fancy html is generated by the ESP web server with HREF links to ease the user’s interface with the LED