Control of LEDs by WiFi using ESP8266

The example shows how to send data via web page to ESP8266 module using Arduino. The program controls LEDs connected to Arduino board.

In the example the following elements were used:

  • Computer equipped with WiFi card

ESP-01 connection from Arduino

For communication between the WiFi module based on ESP8226 and Arduino board there is serial interface UART. In this case its software version will be used, hardware UART, i.e. pins 1 and 0 will remain at your disposal. The circuits should be connected, e.g. by means of female/male wires in the following way:

Pin ESP8266 Pin Arduino
Tx 2
Rx 3
Vcc 3,3 V
CH_PD

3,3 V

GND GND

Connect the LEDs to pins 11, 12 and 13 of Arduino through a current limiting resistor. The brightness of the LED depends on its value, too little resistance can damage the diode, if it is too big it will not glow. We suggest to choose the resistance value from 220 Ω to 1.5 kΩ.

Program for Arduino

The following code was used in the example:

  1.  
  2. #include ///To connect the library to the UART software interface
  3. SoftwareSerial esp01(2,3); // UART software interface initialization: 2-RX (connect to Tx ESP module); 3-TX (connect to Rx ESP module)
  4. void setup()
  5. {
  6. Serial.begin(9600); // Initialization of the hardware UART interface at 9600 bps - for communication with a computer via USB
  7. esp01.begin(9600); // UART software interface initialization at 9600 bps - for communication with the ESP module
  8. pinMode(11,OUTPUT); // setting pin 11 as input for LED 1
  9. digitalWrite(11,LOW); // low state on pin 11 - LED 1 disabled by default
  10. pinMode(12,OUTPUT); // setting pin 12 as input for LED 2
  11. digitalWrite(12,LOW); // low state on pin 12 - LED 2 off by default
  12. pinMode(13,OUTPUT); // setting pin 13 as input for LED 3
  13. digitalWrite(13,LOW); // low state on pin 13 - LED 3 disabled by default
  14. //Send in the ESP module's initialization commands
  15. sendData("AT+RSTrn",2000); //reset of module
  16. sendData("AT+CWMODE=2rn",1000); // set in Access Point mode
  17. sendData("AT+CIFSRrn",1000); //receive an IP address (default 192.168.4.1)
  18. sendData("AT+CIPMUX=1rn",1000); //enabling multiple connection mode
  19. sendData("AT+CIPSERVER=1,80rn",1000); // setting the server on port 80
  20. }
  21. void loop()
  22. {
  23. if(esp01.available()) // check if the module has received the data
  24. {
  25. if(esp01.find("+IPD,"))
  26. {
  27. delay(1000); // waiting for the data buffer to fill
  28. // reading the connection ID
  29. int connectionId = esp01.read()-48; //read ASCI value: subtracted 48 because read() returns the decimal value of ASCII at item 48
  30. esp01.find("pin="); // find the phrase "pin=" and set the "cursor" there
  31. int pinNumber = (esp01.read()-48)*10; // download the first digit, e.g. for pin 13, the first digit is 1, multiply it by 10
  32. pinNumber += (esp01.read()-48); //pick the second digit, e.g. for pin 13, the second digit is 3, it should be added to the previous result
  33. digitalWrite(pinNumber, !digitalRead(pinNumber)); // change of state of selected pin
  34. // closing the connection
  35. String closeCommand = "AT+CIPCLOSE=";
  36. closeCommand+=connectionId; // Connection ID read at start
  37. closeCommand+="rn";
  38. sendData(closeCommand,1000); // send commands to terminate the connection
  39. }
  40. }
  41. }
  42. /*
  43. * Function to send commands to ESP01
  44. * Parameters:
  45. * command - command to be sent
  46. * timeout - time to answer
  47. */
  48. String sendData(String command, const int timeout)
  49. {
  50. String response = ""
  51. esp01.print(command); // send command to ESP01
  52. long int time = millis();
  53. while( (time+timeout) > millis())
  54. {
  55. while(esp01.available()) //If there's any data from the module, then it's read
  56. {
  57. char c = esp01.read(); // reading the next character
  58. response += c;
  59. }
  60. }
  61. Serial.print(response);
  62. return response;
  63. }

Get the code for Arduino

HTML code for sending data

Copy the code below and, using e.g. the system Notepad, save it with the .html extension.


LED Control via ESP8266










Download the HTML code

The program also uses the JQuery library. It should be downloaded and saved in the directory where the above .html file is located.

Starting the program

At the beginning, it is necessary to connect with the module as an access point (similarly to e.g. a wireless router). After establishing the connection, open the created website using any browser. Using one of the three buttons you can light or turn off the diodes.

Botland.store - shop for makers!