ESP8266 ESP-01 and IoT of LED — Part 2

Mudassar Tamboli
3 min readJul 2, 2018

--

Series : Part 1, Part 2 , Part 3, Part 4

In the Part-1 we exercised the Arduino IDE setup for ESP8266 and programmed firmware in ESP-01 and wrote a simple blinking led program. However the objective is to control the LED from remote application using the Wifi capabilities of ESP-01. We will see in the following article how to do it using client-server model .

ESP-01 As HTTP Client

The idea is to access a php page from a remote website. I am using Amazon’s EC2 instance as php server and will be accessing it from ESP-01.

This can be done quite easily using the ESP8266 library.

ESP-01 as work station

In order to connect to the internet, ESP-01 should act as work station and connect to some Wifi network. It should know the SSID, ie the network name and its password ofcourse (if any).

In the above picture, the AndroidAP is an access point created by my smart phone which is connected to the internet. The Wifi listing on my Windows 10 laptop shows AndroidAP and it is using it to access the internet.

Lets use ESP8266 library to create ESP-01 workstation and connect to the “AndroidAP” access point.

Note:- ESP8266Wifi.h is available only if you install the esp8266 packages discussed in Part-1 of this article

The code above is quite simple. We first set the wifi mode to station mode, connect to wifi access point and check for status

Access a PHP page from a website hosted on Amazon EC2

Now, this is begining of LED to become Thing of Internet. We will access a php page and read the output. The php output is actually a command to ESP8266–01 to switch the “GPIO2 LED” ON or OFF. Lets see the code to achieve this.

ESP8266 as http client

The loop keeps fetching the status.php page from the http web server and reads the http response. If the response is “ON” the Led is set ON and vice versa.

Lets see how the status.php code looks like.

http://xx.xx.xx.xx/status.php?gpio2=ON will write the command ON status of LED in esp8266_gpio2_status.txt

http://xx.xx.xx.xx/status.php?gpio2=OFF will write the command OFF status of LED in esp8266_gpio2_status.txt

When ESP8266 fetches the status.php, it reads the command set in esp8266_gpio2_status.txt and outputs the command as http response.

ESP8266 reads the response and accordingly sets the LED ON or OFF.

Sources

Project : ESP8266-ESP-01-HttpClient

Conclusion

In this article we initialised the ESP8266 module as Wifi Station.The LED is set ON or OFF by polling the php webpage at regular interval. The php page saves the users’ command in a file. The command is issued as HTTP GET. When ESP8266–01 fetches the page, php reads the file and generates HTTP response of command. The ESP8266–01 sets the LED accordingly.

Series : Part 1, Part 2 , Part 3, Part 4

--

--

Mudassar Tamboli

AWS Certified Solutions Architect, IoT enthusiast, Entrepreneurial Thinker, Polyglot programmer