How to make AT commands work programatically in arduino for ESP8266 wifi module

10,684

Solution 1

I have come across the same problem and yet not have found a solution. But your connections are a bit of, you have to connect the TX pin of your ESP8266 module to the RX pin of your arduino and the RX pin of your ESP8266 module to the TX pin. Hope this helps you on your way

Solution 2

This might be a bit late, but I got stuck with a similar problem fairly recently. If it's sorted then feel free to ignore this.

Depending on firmware version of your ESP8266 module the baud rate of 9600 may not work, try out 115200 instead - it may prove to be more reliable?

I think the main reason your code above isn't working is because of the face that the ESP needs both newline and carriage returns at the end of the AT command. The serial monitor adds these on for you. Rather than sending AT try sending AT\r\n. This should encourage the ESP to reply with OK, or if the echo is turned on AT\r\nOK.

Serial.available() also checks that there is content in a receive buffer - this takes time unfortunately so I had to put a delay(10) in there to get it to register a character in the buffer.

#include <SoftwareSerial.h>

//i find that putting them here makes it easier to 
//edit it when trying out new things

#define RX_PIN 2
#define TX_PIN 3
#define ESP_BRATE 115200

SoftwareSerial esp8266(RX_PIN, TX_PIN);

void setup()
{
  Serial.begin(9600);
  esp8266.begin(ESP_BRATE); // I changed this
}

void loop()
{
 esp8266.println("AT\r\n"); //the newline and CR added
 delay(10);  //arbitrary value

 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);
  }  
 }
}

My next problem is that the0 replies for my ESP are unreliable - sometimes they are read as OK but sometime they are garbage values. I suspect it's a matter of not enough power to the module.

Share:
10,684
lost Coder
Author by

lost Coder

lOVES CODING AT TIMES. mOSTLY LOst in the code sea.

Updated on June 05, 2022

Comments

  • lost Coder
    lost Coder almost 2 years

    I am doing a simple tcp communication from an arduino to raspberry-pi wirelessly with an ESP8266 wifi module on arduino uno.The tcp server is running on the raspberry-pi.I am able to do TCP communication with the following AT commands in arduino serial monitor at a baudrate of 9600.

    AT+CIPMUX=1
    AT+CIPSTART=4,"TCP","192.168.43.150",7777
    AT+CIPSEND=4,5
    >hai
    

    How to do this programatically in an arduino sketch.I used the following code on my arduino uno,but still without any success.The baudrate is 9600 only since it is working directly in serial monitor.

    #include <SoftwareSerial.h>
    SoftwareSerial esp8266(2,3);
    
    void setup()
    {
      Serial.begin(9600);
      esp8266.begin(9600); // your esp's baud rate might be different
    }
    
    void loop()
    {
     esp8266.println("AT");
     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);
      }  
     }
    }
    

    The connections are as follows

      ESP8266  Arduino Uno
    
      Vcc       3.3V
      CH_PD     3.3V
      RX        RX(PIN 2) 
      TX        TX(PIN 3)
      GND       GND