2

I am working on an Arduino-based project. When I send AT commands manually through the serial monitor, I get the correct response, but when I try the same commands through code, the ESP8266 returns garbage values. I've attached both the responses images and also uploaded the program used.

enter image description here

enter image description here

#include <SoftwareSerial.h>
// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (2,3);
//rx=2 connected to 3 of arduino. tx=3 connected to 2 of arduino
const char SSID_ESP[]="xxxxxxxx";
const char SSID_KEY[]="xxxxxxxx";

void setup() {
  Serial.begin(115200);
  ESP8266.begin(115200);
  // Change this to the baudrate used by ESP8266
  delay(1000); // Let the module self-initialize
  ESP8266.println("AT");
  delay(1000);
  while (ESP8266.available()) Serial.write(ESP8266.read());
  delay(1000);
  ESP8266.println("AT+CWJAP");
  ESP8266.println(SSID_ESP);
  ESP8266.println("\",\"");
  ESP8266.println(SSID_KEY);
  ESP8266.println("\"\r\n");
  delay(1000);
  while(ESP8266.available()) Serial.write(ESP8266.read());
  delay(2000);
  ESP8266.println("AT+CWMODE=3");
  delay(1000);
  while(ESP8266.available()) Serial.write(ESP8266.read());
  delay(1000);
  ESP8266.println("AT+CIPMUX=0");
  delay(1000);
  while(ESP8266.available()) Serial.write(ESP8266.read());
  delay(1000);
  ESP8266.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");
  delay(4000);
  while (ESP8266.available()) {
    Serial.write(ESP8266.read());
  }
}

void loop() {}
cagdas
  • 1,634
  • 2
  • 14
  • 27
  • If my answer helped fix your issue, could you make it as the accepted answer @Rohit Rajapure – L Bahr Apr 28 '16 at 16:32

4 Answers4

3

The Fix

1. AT commands expect a \r\n at the end of commands. Here you are sending a new line after every part of the command.

ESP8266.println("AT+CWJAP");
ESP8266.println(SSID_ESP);
ESP8266.println("\",\"");
ESP8266.println(SSID_KEY);
ESP8266.println("\"\r\n");

The simple fix would be to change all the ESP8266.println() to ESP8266.write()

2. Also the syntax for this command has a =" after AT+CWJAP https://github.com/espressif/ESP8266_AT/wiki/CWJAP

So ESP8266.println("AT+CWJAP"); should be ESP8266.println("AT+CWJAP=\"");

Other Solution

These types of problems can be hard to debug. For this reason I try to avoid sending parts of a command. It would be easier to debug if you use a string.

This also has the benefit of being able to send the command to both serial ports so you can see exactly what gets sent.

String ConnectAPCmd = "AT+CWJAP=\"";
ConnectAPCmd += SSID_ESP;
ConnectAPCmd += "\",\"";
ConnectAPCmd += SSID_KEY;
ConnectAPCmd += "\"";
Serial.println("Sent: " + ConnectAPCmd);
ESP8266.println(ConnectAPCmd);
L Bahr
  • 2,723
  • 3
  • 22
  • 25
  • 1
    By the way, terminating a command line with `\r\n` is not correct, you should only use `\r`. See [this answer](http://stackoverflow.com/a/21503919/23118) for details. So never use `writeln` or `println`, use `write` or `print` and supply the `\r` explicitly to make sure the command line is terminated correctly. – hlovdal Aug 07 '16 at 10:50
1

If you haven't changed it yourself, the ESP8266 dosen't run at baud 115200. The default is 9600. Even if the ESP8266 runs at baud 115200, the Arduino dosen't handle 115200 with software serial very well. You might want to change to a lower baud.

That aside I agree with @hlovdal. Use write or print and supply the \r\n to the end of each command e.g.

  ESP8266.write("AT+CWJAP=");
  ESP8266.write(SSID_ESP);
  ESP8266.write(",");
  ESP8266.write(SSID_KEY);
  ESP8266.write("\r\n");
XerXeX
  • 784
  • 4
  • 16
0

I think it might be because of low power. Try parallel connecting the power source, for example the Arduino UNO boards 3.3v with a couple of AA batteries to power up the ESP.

This made my ESP8266-01 stop returning garbage characters and also stop disconnecting every now and then.

Bill
  • 176
  • 2
  • 5
0

The problem you are facing is because of baud rate - 115200. Change the baud rate to 9600 ,it will solve your problem.

vallabh
  • 140
  • 1
  • 10