2

I'm trying to make my esp8266 work with my arduino.

But when I send AT commands I get some weird symbols and some functions return "ERROR"

Here's the code I'm using.

#include "SoftwareSerial.h"

SoftwareSerial esp8266(6, 7); // RX, TX

void setup()
{
  Serial.begin(9600); // serial port used for debugging
  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())
    {
      char c = esp8266.read();  // read the next character.

      Serial.write(c);  // writes data to the serial monitor
    }
  }

  if(Serial.available())
  {
    delay(10);  // wait to let all the input command in the serial buffer

    // read the input command in a string
    String cmd = "";
    while(Serial.available())
    {
      cmd += (char)Serial.read();
    }
    // send to the esp8266
    esp8266.println(cmd); 
  }
}

Here's the Serial Monitor:

AT


OK
AT+RST

OK


 ets Jan  8 2013,rst catse:2, boot lode:(3,7)

loab 0x40100000& len 1396, qoom 16 
tail 4C�,kk�������e
load 0x3fge8000, len 776, room 4 tak0  t0�v�
�!jH+��
����(j5�!
,�2%1�

j
�sl��o�
Ai,Thinker Technology Co. Ltd.

invalid
AT+CWMODE?

+CWMODE:1

OK

AT+GMR

AT version;0.25.0.0(Juo  5 2015 17:27:16)
SDK version:1.0
ryJ 0

AT+CWLAP

+CWLAP:(4,"�P��R�*bj��b���*��2��*����Ҋ�b�Jj
��*A�B�br������Յź�aj��b����2j
":6�*AՉ����J
"�́������

The commands where, AT, AT+RST, AT+CWMODE?, AT+GMR, AT+CWLAP

Why am I getting this weird symbols and etc? I haven't upgraded the firmware, should I? There other commands I tried return "ERROR" or even "ERROQ"

I don't have a USB to TTL or any other board. I connected my board in the arduino with this schematic:

esp8266 connection to arduino using voltage regulator

Cap = 10uF VolReg = LM1117 3.3v

3
  • ESP8266 is a 3.3V device that means it's logic HIGH is not 5V. That means you need to convert ESP's serial output from 3.3V to 5V logic and serial input must be converted from 5V to 3.3V to avoid damaging the ESP. It is not meant to be fed 5V signals!
    – Avamander
    Commented Feb 28, 2016 at 10:12
  • I'm not feeding 5v to my ESP. But do I have to step up the output signal from 3.3v to 5v? How can I do this? Commented Feb 28, 2016 at 19:33
  • Yes you are. Look at digital pin 7 and 6 they operate at 5V, ESP8266 is not a 5V device. So signals going from Arduino to ESP must be converted (5V->3.3V) and signals coming from ESP to Arduino can be converted, but Arduino should handle 3.3V HIGH just fine (to minimize possible issues you might want to do this). TisteAndii has mentioned it in his answer too.
    – Avamander
    Commented Feb 28, 2016 at 22:20

5 Answers 5

1

The problem is mainly in using Serial and SoftwareSerial together. Or it can be due the esp getting less current than it requires from the Arduino(you are also damaging your Arduino!), use an external 5V source (I use my computer USB)

Why dont you directly use Serial for communication with pins 0 and 1. Just upload a blank sketch, connect Arduino Tx, Rx to esp's Tx, Rx(reverse Tx, Rx if not working). What you type into Serial will then be sent to the esp. Later if you wish to communicate to esp in sketches, use only SoftwareSerial.

Also, the esp is not 5V tolerant so you need to use a level shifter(like the cd4050 ic) or a voltage divider(less efficient) for signals from the Arduino to the esp. 3.3V is considered as a logical HIGH so you dont need to do any changes for signals from esp to the Arduino.

I highly recommend getting a USB TTL converter, this will save you a lot of headaches and debugging will be easy. I did things without it for 1-2months and now I can totally feel the difference.

1

SoftwareSerial is reliable only up to 9600 baud or so. You can either swap ports and use Software serial as your debug output (but requires disconnecting/reconnecting the ESP8266 chip each time you upload to the UNO); or write a short sketch to set the device's baud rate to 9600 baud (and convince yourself that at least that much ran OK!) and set your SoftwareSerial to that rate.

0
  • The Arduino pin 7 (TX) signal must be level-shifted from 5V to 3.3V using a voltage divider, because the ESP cant handle 5V signals.
  • The Serial baud is a lot lower than that of SoftwareSerial plus you are spending a lot of time waiting for the whole command to come in. SoftwareSerial is also unreliable at 115200 baud. Use 57600 baud for both serial ports.

Use this code:

SoftwareSerial esp8266(6, 7); // RX, TX

void setup()
{
  Serial.begin(57600); 
  esp8266.begin(57600);
}

void loop()
{
  while (Serial.available())
    esp8266.write(Serial.read());
  while (esp8266.available())
    Serial.write(esp8266.read());
}

It will do the exact same thing you want to do. Good luck.

0

I had the same issue before and it is a little bug in your code - while reading all the data from the serial port, you also read the CR & NL and then you send that as a line (with probably another NL). I solved it as such:

  if(Serial.available())
  {
    String command="";
    uint8_t c=0;    
    while(c!=10) // read the command character by character
    {
      if (Serial.available()) {
        c=Serial.read(); 
        command+=(char)c;
      }
    }
    //Serial.print("Sending: ");
    //Serial.println(command);
    esp8266.print(command); // send the read character to the esp8266
  }

As you can tell, I was suspicious of the code and echoed back to make sure the reading from the Serial function. Also I went back to straight USB->Uno->ESP mode to confirm all was working, even the firmware with the 9600 Baud uploaded and then it clicked.

1
  • When I tried one simple scetch which just communicates between Serial monitor and ESP I got "ERROR" when typed AT in the monitor. Then I switched from "Newline" to "Both NL & CR" in Serial monitor and after that I got OK when typed AT.
    – vilpe89
    Commented Mar 4, 2020 at 20:25
0

These values mostly come when you are unable to communicate with any UART device on proper baud rate. Best is to change the baud rate of ESP8266 from dafault 115200 to 9600 using

AT+UART_DEF=9600,8,1,0,3

And then use the regular program. And you will get what you want.

#include <SoftwareSerial.h>
#include<stdio.h>
SoftwareSerial mySerial(2,3);
void get_wifi_list();
uint8_t flag = 0;

void setup() {                

    Serial.begin(9600);
    Serial.println("Starting the code, initial set-up");   
    mySerial.begin(9600);
    delay(1000);
    Serial.println("Inside the main loop, going to start communicating with ESP8266");
    delay(1000);

}

void loop(){

  char data[1024];
  uint8_t ch = -1;
  uint8_t index = 0;
  memset(&data,0,sizeof(data));
  char data1[200];
  uint8_t index1 = 0;
  memset(&data1,0,sizeof(data1));


  if(mySerial.available())  // check if the ESP is sending a message
    {
    while(mySerial.available())
        {
            char c = mySerial.read();  // read the next character.
            data[index] = c; 
            index++;
            if(index >= 2000){
                Serial.print("Data received exceed allowed length \n");
                return;
                }
                if (c == '+' ){
                    Serial.print("got start char");
                    //data1[index1] = c;
                    //index1++;
                    //if ( c== '\n');{
                    //    Serial.print("First data1 is :  ");
                    //    Serial.println(data1);
                    //    return;
                    //    }



      if( strstr(data,"OK")!=NULL || strstr(data,"CWLAP") !=NULL){
      Serial.print ("Get reply as OK \n");
      Serial.print (data);

        }
      }  
    }
  }
  if (flag == 0){
      get_wifi_list();
      Serial.println("Getting the wifi list");
      flag = 1;
      return;
  }

  if(Serial.available()){
    delay(10);  
    String cmd = "";
    while(Serial.available()){
        cmd += (char)Serial.read();
        }
    mySerial.println(cmd); 
      }

}

void get_wifi_list(){
  String cmd = "AT+CWLAP";
  mySerial.println(cmd);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.