Reputation: 21
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.
#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() {}
Upvotes: 2
Views: 4544
Reputation: 142
The problem you are facing is because of baud rate - 115200. Change the baud rate to 9600 ,it will solve your problem.
Upvotes: 0
Reputation: 792
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");
Upvotes: 1
Reputation: 176
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.
Upvotes: 0
Reputation: 2911
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=\"");
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);
Upvotes: 3