Previously, we discussed about ESP8266. Working With ESP8266 Not Exactly Just Easy. Here is Circuit Diagram, Code to Setup Arduino WiFi Control of LED From Web Browser to Help New Users. We searched the whole web as much possible to find easiest tutorial which a newbie can follow. Basically, ESP8266 as one component involves complex networking matters. There are development boards for IoT, which are easier for anyone to create things. Connecting ESP8266 with Arduino needs lot of research for your particular model, version. This guide is basic IoT setup.
Arduino WiFi Control of LED From Web Browser
ESP8266 is a serial WiFi module capable of sending and receiving data from the Web over the internet. It is probably better if you setup ESP8266 with Arduino following just basic guide on official Arduino’s site than ours this one for first attempt.
You will need :
---
- Arduino development board
ESP8266
3 LEDs
- 3 resistors – 1K or 2K
- Jumpers or some wires
- Breadboard
- Computer with WiFi
- The HTML webpage (free) we are providing
After copying the below code to Arduino IDE, compile and upload to the UNO. Then run serial monitor. Make sure you have test the NL and CR option and as well as right baud rate on serial monitor on the IDE.
The wiring part is not super difficult :
Here is the HTML page with jQuery to get buttons on web page :
1 | https://github.com/elementzonline/Arduino-Sample-Codes/tree/master/IOT_Kit/iot_control_pins |
Here is the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <SoftwareSerial.h> #define DEBUG true SoftwareSerial esp8266(10,11); // make RX Arduino line is pin 10, make TX Arduino line is pin 11. // This means that you need to connect the TX line from the esp to the Arduino's pin 2 // and the RX line from the esp to the Arduino's pin 3 void setup() { Serial.begin(9600); esp8266.begin(9600); // your esp's baud rate might be different pinMode(2,OUTPUT); digitalWrite(2,LOW); pinMode(3,OUTPUT); digitalWrite(3,LOW); pinMode(4,OUTPUT); digitalWrite(4,LOW); sendData("AT+RST\r\n",2000,DEBUG); // reset module sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80 } void loop() { if(esp8266.available()) // check if the esp is sending a message { if(esp8266.find("+IPD,")) { Serial.println("listening"); delay(1000); // wait for the serial buffer to fill up (read all the serial data) // get the connection id so that we can then disconnect int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns // the ASCII decimal value and 0 (the first decimal number) starts at 48 esp8266.find("pin="); // advance cursor to "pin=" int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10 pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin // make close command String closeCommand = "AT+CIPCLOSE="; closeCommand+=connectionId; // append connection id closeCommand+="\r\n"; sendData(closeCommand,1000,DEBUG); // close connection } } } /* * Name: sendData * Description: Function used to send data to ESP8266. * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no) * Returns: The response from the esp8266 (if there is a reponse) */ String sendData(String command, const int timeout, boolean debug) { String response = ""; esp8266.print(command); // send the read character to the esp8266 long int time = millis(); while( (time+timeout) > millis()) { while(esp8266.available()) { // The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character. response+=c; } } if(debug) { Serial.print(response); } return response; } |