Any aspiring IOT device needs to be connected to the internet. When a WiFi connection is nearby (say home, hotspot etc) it makes sense to use it. So the ESP8266 is probably the cheapest way to get your device to the IOT world using WiFi technology. Let’s see how to interface it with first ATmega and then with Uno
Pin Diagram of ESP8266
AT command Sheet for ESP8266
Code for Mega
//#include <SoftwareSerial.h> //use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). //#define SSID "SSID" //#define PASS "Password" #include <dht.h> dht DHT; #define DHT11_PIN 5 #define SSID "SKS Cottage" #define PASS "kayar123" #define DST_IP "data.sparkfun.com" //baidu.com float temp; int pintemp = A0; //SoftwareSerial dbgSerial(10, 11); // RX, TX // Use DHT11 sensor void setup() { // Open serial communications and wait for port to open: //serial 2 is to esp8266 Serial2.begin(115200);//9600 (mine), 57600, 115200 Serial2.setTimeout(1000); //serial 0 is to usb Serial.begin(115200); while(!Serial); while(!Serial2); //dbgSerial.begin(9600); //can't be faster than 19200 for softserial //dbgSerial.println("ESP8266 Demo"); Serial.println("ESP8266 Demo on Mega2560"); while(Serial2.available()>0) Serial2.read(); Serial2.println("AT"); delay(1000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(1000); Serial2.flush(); Serial2.println("AT+RST"); delay(1000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(1000); Serial2.flush(); Serial2.println("AT+GMR"); delay(1000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(1000); Serial2.flush(); Serial2.println("AT+CIFSR"); delay(1000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(1000); Serial2.flush(); Serial2.println("AT+CWMODE=3"); delay(1000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(2000); Serial2.flush(); Serial2.println("AT+CWLAP"); delay(2000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } Serial.print('\n'); delay(2000); Serial2.flush(); String cmd="AT+CWJAP=\""; cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; Serial2.println(cmd); Serial.println(cmd); delay(2000); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } delay(2000); Serial.print('\n'); Serial2.flush(); /* //test if the module is ready Serial2.println("AT+RST"); //delay(1000); //delay(1000); Serial.println("Resetting module"); Serial2.flush(); //if(Serial2.find("ready")) if(Serial2.find("Ready")||Serial2.find("ready")) { //dbgSerial.println("Module is ready"); Serial.println("Module is ready"); } else { //dbgSerial.println("Module have no response."); Serial.println("Module have no response."); while(1); } delay(1000); //connect to the wifi boolean connected=false; for(int i=0;i<5;i++) { if(connectWiFi()) { connected = true; break; } } if (!connected){while(1);} delay(5000); //print the ip addr /* Serial2.println("AT+CIFSR"); Serial.println("ip address:"); while (Serial2.available()) Serial.write(Serial2.read()); //set the single connection mode Serial2.println("AT+CIPMUX=0"); */ } void loop() { // Read temperature as Celsius Serial.print("DHT11, \t"); int chk = DHT.read11(DHT11_PIN); switch (chk) { case DHTLIB_OK: Serial.print("OK,\t"); break; case DHTLIB_ERROR_CHECKSUM: Serial.print("Checksum error,\t"); break; case DHTLIB_ERROR_TIMEOUT: Serial.print("Time out error,\t"); break; case DHTLIB_ERROR_CONNECT: Serial.print("Connect error,\t"); break; case DHTLIB_ERROR_ACK_L: Serial.print("Ack Low error,\t"); break; case DHTLIB_ERROR_ACK_H: Serial.print("Ack High error,\t"); break; default: Serial.print("Unknown error,\t"); break; } int temp = DHT.temperature; #if 1 String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += DST_IP; cmd += "\",80"; Serial2.println(cmd); //dbgSerial.println(cmd); Serial.println(cmd); if(Serial2.find("Error")) return; //cmd = "GET / HTTP/1.0\r\n\r\n"; //cmd = "POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=40 HTTP/1.0\r\n\r\n Host: data.sparkfun.com\r\n\r\n"; char s[200]; Serial.println(temp); int t1 = temp; float f2 = temp-t1; int t2 = trunc(f2 * 10000); sprintf (s, "POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=%d.%04d HTTP/1.0\r\n\r\n Host: data.sparkfun.com\r\n\r\n", t1,t2); Serial.println(s); cmd = s; Serial.println(cmd); Serial2.print("AT+CIPSEND="); Serial2.println(cmd.length()); if(Serial2.find(">")) { //dbgSerial.print(">"); Serial.print(">"); }else { Serial2.println("AT+CIPCLOSE"); //dbgSerial.println("connect timeout"); Serial.println("connect timeout"); delay(1000); return; } Serial2.print(cmd); delay(2000); //Serial.find("+IPD"); while (Serial2.available()) { char c = Serial2.read(); //dbgSerial.write(c); Serial.write(c); //if(c=='\r') dbgSerial.print('\n'); if(c=='\r') Serial.print('\n'); } //dbgSerial.println("===="); Serial.println("===="); delay(1000); #endif } boolean connectWiFi() { Serial2.println("AT+CWMODE=1"); String cmd="AT+CWJAP=\""; cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; //dbgSerial.println(cmd); Serial2.println(cmd); Serial.println(cmd); delay(2000); if(Serial2.find("OK")) { //dbgSerial.println("OK, Connected to WiFi."); Serial.println("OK, Connected to WiFi."); return true; }else { //dbgSerial.println("Can not connect to the WiFi."); Serial.println("Can not connect to the WiFi."); return false; } }
Arduino Code to send AT command to ESP8266.
Download the following file to get some AT command ESP8266_WiFi_Module_Quick_Start_Guide_v_1.0.4
#include <SoftwareSerial.h> SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3. // 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(115200); // your esp's baud rate might be different } void loop() { if(esp8266.available()) // check if the esp is sending a message { while(esp8266.available()) { // The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character. Serial.write(c); } } if(Serial.available()) { // the following delay is required because otherwise the arduino will read the first letter of the command but not the rest // In other words without the delay if you use AT+RST, for example, the Arduino will read the letter A send it, then read the rest and send it // but we want to send everything at the same time. delay(1000); String command=""; while(Serial.available()) // read the command character by character { // read one character command+=(char)Serial.read(); } esp8266.println(command); // send the read character to the esp8266 } }
Creating Arduino as Web Server
- Connect to your Esp8266 wifi
- Find your Router address (Esp8266 wifi ) by ipconfig – Gateway default address
- Browser type the Router address You will get the Page served by your Router
#include <SoftwareSerial.h> #define DEBUG true SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3. // 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(115200); // your esp's baud rate might be different 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 { /* while(esp8266.available()) { // The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character. Serial.write(c); } */ if(esp8266.find("+IPD,")) { delay(1000); 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 String webpage = "<h1>Hello</h1><h2>World!</h2><button>LED1</button>"; String cipSend = "AT+CIPSEND="; cipSend += connectionId; cipSend += ","; cipSend +=webpage.length(); cipSend +="\r\n"; sendData(cipSend,1000,DEBUG); sendData(webpage,1000,DEBUG); webpage="<button>LED2</button>"; cipSend = "AT+CIPSEND="; cipSend += connectionId; cipSend += ","; cipSend +=webpage.length(); cipSend +="\r\n"; sendData(cipSend,1000,DEBUG); sendData(webpage,1000,DEBUG); String closeCommand = "AT+CIPCLOSE="; closeCommand+=connectionId; // append connection id closeCommand+="\r\n"; sendData(closeCommand,3000,DEBUG); } } } 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; }
Arduino Uno + ESP8266 + Temperature Sensor + Sparkfun
The Uno can also be used with ESP8266 using SoftwareSerial library. Here is the code and connections for using Uno for sparkfun temperature sensor monitoring
#include <SoftwareSerial.h>
//use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).
//#define SSID “SSID”
//#define PASS “Password”
#include <dht.h>dht DHT;
#define DHT11_PIN 5
#define SSID “SKS Cottage”
#define PASS “kayar123”
#define DST_IP “data.sparkfun.com” //baidu.com
SoftwareSerial Serial2(2,3);
float temp;
int pintemp = A0;
//SoftwareSerial dbgSerial(10, 11); // RX, TX// Use DHT11 sensor
void setup()
{
// Open serial communications and wait for port to open:
//serial 2 is to esp8266
Serial2.begin(115200);//9600 (mine), 57600, 115200
Serial2.setTimeout(1000);//serial 0 is to usb
Serial.begin(9600);while(!Serial);
while(!Serial2);//dbgSerial.begin(9600); //can’t be faster than 19200 for softserial
//dbgSerial.println(“ESP8266 Demo”);
Serial.println(“ESP8266 Demo on Mega2560”);while(Serial2.available()>0)
Serial2.read();Serial2.println(“AT”);
delay(1000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(1000);
Serial2.flush();Serial2.println(“AT+RST”);
delay(1000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(1000);
Serial2.flush();Serial2.println(“AT+GMR”);
delay(1000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(1000);
Serial2.flush();Serial2.println(“AT+CIFSR”);
delay(1000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(1000);
Serial2.flush();
Serial2.println(“AT+CWMODE=3”);
delay(1000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(2000);
Serial2.flush();
Serial2.println(“AT+CWLAP”);
delay(2000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
Serial.print(‘\n’);
delay(2000);
Serial2.flush();
String cmd=”AT+CWJAP=\””;
cmd+=SSID;
cmd+=”\”,\””;
cmd+=PASS;
cmd+=”\””;
Serial2.println(cmd);
Serial.println(cmd);
delay(2000);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
delay(2000);
Serial.print(‘\n’);
Serial2.flush();/*
//test if the module is ready
Serial2.println(“AT+RST”);
//delay(1000);
//delay(1000);
Serial.println(“Resetting module”);
Serial2.flush();//if(Serial2.find(“ready”))
if(Serial2.find(“Ready”)||Serial2.find(“ready”))
{
//dbgSerial.println(“Module is ready”);
Serial.println(“Module is ready”);
}
else
{
//dbgSerial.println(“Module have no response.”);
Serial.println(“Module have no response.”);
while(1);
}
delay(1000);
//connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){while(1);}
delay(5000);
//print the ip addr
/*
Serial2.println(“AT+CIFSR”);
Serial.println(“ip address:”);
while (Serial2.available())
Serial.write(Serial2.read());//set the single connection mode
Serial2.println(“AT+CIPMUX=0”);
*/}
void loop()
{// Read temperature as Celsius
Serial.print(“DHT11, \t”);
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print(“OK,\t”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error,\t”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Time out error,\t”);
break;
case DHTLIB_ERROR_CONNECT:
Serial.print(“Connect error,\t”);
break;
case DHTLIB_ERROR_ACK_L:
Serial.print(“Ack Low error,\t”);
break;
case DHTLIB_ERROR_ACK_H:
Serial.print(“Ack High error,\t”);
break;
default:
Serial.print(“Unknown error,\t”);
break;
}
int temp = DHT.temperature;
#if 1
String cmd = “AT+CIPSTART=\”TCP\”,\””;
cmd += DST_IP;
cmd += “\”,80″;
Serial2.println(cmd);
//dbgSerial.println(cmd);
Serial.println(cmd);
if(Serial2.find(“Error”)) return;
//cmd = “GET / HTTP/1.0\r\n\r\n”;
//cmd = “POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=40 HTTP/1.0\r\n\r\n Host: data.sparkfun.com\r\n\r\n”;
char s[200];
Serial.println(temp);
int t1 = temp;
float f2 = temp-t1;
int t2 = trunc(f2 * 10000);
sprintf (s, “POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=%d.%04d HTTP/1.0\r\n\r\n Host: data.sparkfun.com\r\n\r\n”, t1,t2);
Serial.println(s);
cmd = s;
Serial.println(cmd);
Serial2.print(“AT+CIPSEND=”);
Serial2.println(cmd.length());
if(Serial2.find(“>”))
{
//dbgSerial.print(“>”);
Serial.print(“>”);
}else
{
Serial2.println(“AT+CIPCLOSE”);
//dbgSerial.println(“connect timeout”);
Serial.println(“connect timeout”);
delay(1000);
return;
}
Serial2.print(cmd);
delay(2000);
//Serial.find(“+IPD”);
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c==’\r’) dbgSerial.print(‘\n’);
if(c==’\r’) Serial.print(‘\n’);
}
//dbgSerial.println(“====”);
Serial.println(“====”);
delay(1000);
#endif
}
boolean connectWiFi()
{
Serial2.println(“AT+CWMODE=1”);
String cmd=”AT+CWJAP=\””;
cmd+=SSID;
cmd+=”\”,\””;
cmd+=PASS;
cmd+=”\””;
//dbgSerial.println(cmd);
Serial2.println(cmd);
Serial.println(cmd);
delay(2000);
if(Serial2.find(“OK”))
{
//dbgSerial.println(“OK, Connected to WiFi.”);
Serial.println(“OK, Connected to WiFi.”);
return true;
}else
{
//dbgSerial.println(“Can not connect to the WiFi.”);
Serial.println(“Can not connect to the WiFi.”);
return false;
}
}