Thursday, August 5, 2021

How to connect Arduino UNO to a WIFI Network: ESP8266 - AT Commands



ESP8266 is an integrated circuit with a microcontroller allowing connection in WiFi.
In this video, we will study this WIFI module and learn how to use it with the Arduino UNO board.
You would then be able to:
-Connect the ESP-01 to Arduino.
-Send AT commands using Arduino UNO
-Use the Arduino to program the ESP-01 (the SoftwareSerial library)
Finally, You will have your first web server.
Connecting arduino to the internet allows remote control of objects. We're talking about Internet of Things (IOT)



#include <SoftwareSerial.h>                        
SoftwareSerial esp8266(10,11);                   
#define serialCommunicationSpeed 9600               
#define DEBUG true                                 

void setup()

{
  Serial.begin(serialCommunicationSpeed);           
  esp8266.begin(serialCommunicationSpeed);     
  InitWifiModule();                              
}

void loop()                                                         
{

  if(esp8266.available())                                           
 {    
    if(esp8266.find("+IPD,"))
    {
     delay(1000);
 
     int connectionId = esp8266.read()-48;                                                
     String webpage = "<h1>Hello World!</h1>";
     String 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);                                          
    long int time = millis();                                      
    while( (time+timeout) > millis())                                 
    {      
      while(esp8266.available())                                      
      {
        char c = esp8266.read();                                     
        response+=c;                                                  
      }  
    }    
    if(debug)                                                        
    {
      Serial.print(response);
    }    
    return response;                                                  
}

void InitWifiModule()
{
  sendData("AT+RST\r\n", 2000, DEBUG);                                                  
  sendData("AT+CWJAP=\"WIFI_name\",\"pwd\"\r\n", 2000, DEBUG);        
  delay (3000);
  sendData("AT+CWMODE=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIFSR\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPMUX=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPSERVER=1,80\r\n", 1500, DEBUG);                                     

}


5 comments:

  1. can you show me the detailed commands after connecting to firebase by using TCP/IP? I connected successfully but can't get or post to firebase.
    AT+CIPSTART="TCP","XXX.firebase.io.com",80--> CONNECT OK

    ReplyDelete
  2. Hey! If anyone is trying this, replace the line "String webpage = "html code here (it wont let me put html code into the comments)" with this:
    String webpage = "HTTP/1.1 200 OK\r\n\r\n";
    webpage.concat("html code");
    This tells the module to respond with HTTP/1.1 so your server will load on Safari and iOS devices. Other than that, great tutorial

    ReplyDelete
  3. excuse me sir, why in my serial monitor the result just like this "???????????"

    ReplyDelete
  4. I am not getting the ip address of the esp
    I am getting the following output, can you please help me??

    AT+RST


    OK

    ets Jan 8 2013,rst cause:4, boot mode:(3,7)

    wdt reset
    load 0x40100000, len 816, room 16
    tail 0

    OK
    AT+CIFSR

    +CIFSR:APIP,"192.168.4.1"
    +CIFSR:APMAC,"1a:fe:34:0a:ac:4e"
    :9.FC4
    AT+CIPMUX=1


    OK
    AT+CIPSERVER=1,80


    OK

    ReplyDelete
  5. avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xb1

    ReplyDelete