Introduction: The ESP8266 Part 3 - Triggering Arduino LEDs Remotely
After a long and dark winter, we come to drive LEDs (or anything) remotely via WiFi. By the way, someone once said "If you can blink a LED you can do anything." So, the importance of this 3rd and last tutorial about the ESP8266.
For those who went straight in this post, it is worth taking a look at in my two previous posts.:
The ESP8266 Part 1 - Serial WIFI Module for Arduino
The ESP8266 Part 2 - Arduino Web Server
The basic difference here is that instead of an Arduino creating a web page to send captured information, will now be a page previously created in HTML and hosted on a web site that will send the data to the Arduino. This page will be capturing user commands, sending them via WiFi to the Arduino for controlling "stuff"!.
Step 1: The Circuit
The circuit is very simple. The ESP8266 connections to the Arduino are the same as previous posts, just add 3 LEDs to the GPIOs (This time I am using a UNO instead of of MEGA, only to show that all learned before also applies for both devices):
- GPIO 11: blue LED
- GPIO 12: Red LED
- GPIO 11: Green LED
Step 2: The HTML Code
The main part of the HTML code below, is used for generation of the ”buttons”: "ON" and "OFF". Note that for each button (there are six in total, two for each GPIO), a specific "id" is generated. For example, if the button ”ON”, associated with GPIO 11 is pressed, the generated ID is "111", i.e. the first two digits are for the identification of the GPIO (in this case "11") and third digit is for record the state, in this case ”1” or "ON". If the button is ”OFF", the Id would be 110. In this case, the digital output pin 11, is connected with the Blue LED.
Once generated the "id" a function on page takes care of sending it to the IP address for the ESP8266. Note that the address that appears in the ESP 8266 set-up has to be included in the line ==> $ .get ( " http://10.0.1.14:80/” ; {pin: p});
The first few times that I tested the program, I had many problems and the most important was that I noticed that it was only possible to send 5 commands to the LEDs. That's because to every sending of information, the ESP8255 considered an open connection (CONNECT 0, 1, 2, 3, 4) and the limit is 5.
I also observed that when put in a "refresh" on the page, the “connection count”, returned to zero. The solution was to add a new HTML code, which forced the automatic refresh of the page (code type "META"). I chose to do a refresh every 15 seconds and so not get too annoying for the user. It worked without problems:
Step 3: The Arduino Code
The code is very similar to the previous code:
Initializes, it makes the setup of the GPIOs, "reset" and initializes the ESP8266, etc., using the same functions defined in the previous post.
Reading the "id", for example "111", the code separates the first two digits and build the variable "pinNumber". With the third. digit, you get the "statusLed". The send function digitalWrite LED status in the case of the example "1" for the corresponding GPIO (or "pinNumber"), in Example 11.
In the video you can see an example of the remote activation of the LEDs:
Attachments
Step 4: Conclusion
For more information, please see my Blog: