Categories

LEDs control via WiFi using ESP8266

The LEDs via wifi using the ESP8266

Example shows how to send data over the WWW for module ESP8266 with Arduino. The program controls the LEDs connected to the Arduino Board.

 

In the example, we used the following elements:

  • The computer is equipped with a wi-fi card

 

To connect ESP-01 with Arduino

For communication between the wi-fi module based on the system ESP8226 and the Arduino uses a serial UART interface. In this case will be used, its software version, hardware UART, that is, contacts 1 and 0 remain at the disposal of the user. Models should be combined, for example, using wires żeńsko-men as follows:

 

Pin ESP8266Pin Arduino
Tx2
Rx3
Vcc3.3 V
CH_PD

3.3 V

GNDGND

 

Contacts 11, 12 and 13 of the Arduino you should connect the LEDs through a resistor that limits the led current. From its value depends on the brightness of the LEDs, too little resistance will damage the diode with too high it will not burn. We offer you to choose the resistance value in the range from 220 Ohm to 1.5 kOhm.

 

Program for Arduino

In the example, we used the following code:

 

#include  //dolaczenie library to support software interface UART 

Esp01 SoftwareSerial(2,3); // Initialize software UART interface: 2-RX (connect to Tx of the module ESP); 3-TX (compatible with Rx module ESP) 

void setup() 
{ 
 Serial.begin(9600); // Initialize sprzetowego UART interface with speed of 9600 bps to communicate with the computer via USB 
 esp01.begin(9600); // Initialize software UART interface with speed of 9600 bps to communicate with the ESP module 

 pinMode(11,OUTPUT); //set pin 11 as an input for led № 1 
 digitalWrite(11,LOW); //enter low position on the pin 11 the led 1 is disabled by default 

 pinMode(12,OUTPUT); //setting pin 12 as input for the LEDs number 2 
 digitalWrite(12,LOW); //enter low position on the pin 12, the indicator 2 is disabled by default 

 pinMode(13,OUTPUT); //setting pin 13 as input for the LEDs No. 3 
 digitalWrite(13,LOW); //enter low position on the pin 13, the led 3 is disabled by default 

 //Send string incjalizacyjnych to ESP module 
 sendData("AT+RSTrn",2000); //reset the module 
 sendData("AT+CWMODE=2rn",1000); //setting in Access Point mode 
 sendData("AT+CIFSRrn",1000); //get IP address (default 192.168.4.1) 
 sendData("AT+CIPMUX=1rn",1000); //enabling multiple invocations 
 sendData("AT+CIPSERVER=1,80 BP",1000); //setting the server to port 80 
} 

void loop() 
{ 
 if(esp01.available()) // check whether the module has received data 
 { 
 if(esp01.find("+IPD,")) 
 { 
 delay(1000); // waiting for the buffer to fill with data 

 // read the connection ID 
 int connectionId = esp01.read()-48; //read the value ASCI: 48 because the read() function returns the decimal ASCII value at position 48 

 esp01.find("pin="); // search for the phrase "pin=" and set there "cursor" 

 int pinNumber = (esp01.read()-48)*10; // load first digit, for example, for the contact 13, the first digit is 1, multiply by 10 
 pinNumber += (esp01.read()-48); //load the other numbers, for example, for the contact 13, the second digit is a 3, you should add it to the previous result 

 digitalWrite(pinNumber, !digitalRead(pinNumber)); // change the status of the selected contact 

 // close the connection 
 String closeCommand = "AT+CIPCLOSE="; 
 closeCommand+=connectionId; // read the connection ID in the beginning 
 closeCommand+="rn"; 

 sendData(closeCommand,1000); // send commands zakoczenia connection 
 } 
 } 
} 

/* 
* Function send commands to the module ESP01 
* Parameters: 
* command - the command to send 
* timeout - the waiting time for a response 
*/ 
String sendData(String command, const int timeout) 
{ 
 String response = ""; 

 esp01.print ("command"); / / send string for ESP01 

 long int time = millis(); 

 while( (time+timeout) > millis()) 
 { 
 while(esp01.available()) //if there is any data module, then is reading them 
 { 
 char c = esp01.read(); // read next character 
 response += c; 
 } 
 } 
 Serial.print(response); 
 return response; 
} 

 

 

Download code for Arduino

 

HTML code to send data

Copy the following code and using, for example, system Notepad, save it with the extension .html.

 

the
the control of the INDICATOR using ESP8266
the
    

    



        


 

Download HTML code

 

 

The program also uses the JQuery library. It should download and save in the folder where the above file .html.

 

Run the program

First we need to associate with the module as access point (e.g. wireless router). After the connection is established, you need to open a crafted web page using any browser. Using one of the three buttons we can light or extinguish the LEDs.