Created
March 17, 2018 11:04
-
-
Save elktros/7d8caccad46413e930f0663626fd3518 to your computer and use it in GitHub Desktop.
Arduino Code for WiFi Controlled LED using ESP8266 WiFi Module and Arduino UNO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
SoftwareSerial esp8266(2,3); //Pin 2 & 3 of Arduino as RX and TX. Connect TX and RX of ESP8266 respectively. | |
#define DEBUG true | |
#define led_pin 11 //LED is connected to Pin 11 of Arduino | |
void setup() | |
{ | |
pinMode(led_pin, OUTPUT); | |
digitalWrite(led_pin, LOW); | |
Serial.begin(9600); | |
esp8266.begin(115200); //Baud rate for communicating with ESP8266. Your's might be different. | |
esp8266Serial("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266 | |
esp8266Serial("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation | |
esp8266Serial("AT+CWJAP=\"SSID\",\"Password\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password. | |
while(!esp8266.find("OK")) | |
{ | |
} | |
esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command. | |
esp8266Serial("AT+CIPMUX=1\r\n", 5000, DEBUG); | |
esp8266Serial("AT+CIPSERVER=1,80\r\n", 5000, DEBUG); | |
} | |
void loop() | |
{ | |
if (esp8266.available()) | |
{ | |
if (esp8266.find("+IPD,")) | |
{ | |
String msg; | |
esp8266.find("?"); | |
msg = esp8266.readStringUntil(' '); | |
String command1 = msg.substring(0, 3); | |
String command2 = msg.substring(4); | |
if (DEBUG) | |
{ | |
Serial.println(command1);//Must print "led" | |
Serial.println(command2);//Must print "ON" or "OFF" | |
} | |
delay(100); | |
if (command2 == "ON") | |
{ | |
digitalWrite(led_pin, HIGH); | |
} | |
else | |
{ | |
digitalWrite(led_pin, LOW); | |
} | |
} | |
} | |
} | |
String esp8266Serial(String command, const int timeout, boolean debug) | |
{ | |
String response = ""; | |
esp8266.print(command); | |
long int time = millis(); | |
while ( (time + timeout) > millis()) | |
{ | |
while (esp8266.available()) | |
{ | |
char c = esp8266.read(); | |
response += c; | |
} | |
} | |
if (debug) | |
{ | |
Serial.print(response); | |
} | |
return response; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment