This is the code I uploaded in Arduino UNO:
#include<SoftwareSerial.h>
SoftwareSerial ESP(2,3); // RX, TX
//ESP RX-->3(UNO) TX-->2(UNO)
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
ESP.begin(9600);
}
void loop() {
if (ESP.available()) {
char inData = ESP.read();
Serial.println("Got reponse from ESP8266: ");
Serial.write(inData);
}
ESP.write("AT\r\n"); //Normally ESP responds to AT command with "OK"
}
But ESP is not able to receive the AT command properly...sometimes it recieves A, sometime T. It then responds with garbage value. Though we are able to give command to ESP through Serial Monitor and it responds pretty well. But when trying to communicate through code, the communication is not OK.
-
2This site is about electrical engineering. Extra-sensory perception is off topic here.Olin Lathrop– Olin Lathrop2016年06月02日 11:05:58 +00:00Commented Jun 2, 2016 at 11:05
-
4Why is this question downvoted and voted to close? MCVE code is provided, sympthoms of failure are described. What is the problem?Dmitry Grigoryev– Dmitry Grigoryev2016年06月02日 12:00:34 +00:00Commented Jun 2, 2016 at 12:00
-
Are you aware that you can program the ESP8266 directly from the Arduino IDE using proper Arduino C rather than AT commands. This means that it might be possible to eliminate the need for a 32MHz Arduino and just rely on the 160MHz ESP82366 instead. Also you need to have a look at the SO Arduino forum.Code Gorilla– Code Gorilla2016年06月02日 12:04:34 +00:00Commented Jun 2, 2016 at 12:04
-
1@Matt yeah it can be done...but am using arduino in my project to control servos and all, so I am relying on arduino for the processing work. :)abhimanyu bishnoi– abhimanyu bishnoi2016年06月02日 12:27:12 +00:00Commented Jun 2, 2016 at 12:27
-
1@Matt do you mean WeMos D1? There is no Webbo D1.gre_gor– gre_gor2016年06月02日 15:42:47 +00:00Commented Jun 2, 2016 at 15:42
1 Answer 1
The problem is in your loop()
function. On each iteration, you will receive at most one character from the SoftwareSerial, while writing the same AT command again and again. As a result, your slave device (ie. ESP8266) will overflow its UART buffers, resulting in garbage being sent and received.
You should keep receiving data from the slave for as long as it has data:
while (ESP.available()) {
char inData = ESP.read();
Serial.println("Got response from ESP8266: ");
Serial.write(inData);
}
-
Thanx it works fine now! :)abhimanyu bishnoi– abhimanyu bishnoi2016年06月02日 11:41:34 +00:00Commented Jun 2, 2016 at 11:41
-
@abhimanyubishnoi Note that you can click o that "check" mark to accept my answer if it really solved your problem. That way, other people will see your problem was solved, and move on to answer unsolved questions.Dmitry Grigoryev– Dmitry Grigoryev2016年06月02日 11:50:50 +00:00Commented Jun 2, 2016 at 11:50
-
Dmitry Grigoryev - i have tried u r solution, but still i am not able to get response 'OK' from ESP 01(my device)....here is the screen shot with response i.sstatic.net/mOklg.png....3bu1– 3bu12017年06月20日 12:54:25 +00:00Commented Jun 20, 2017 at 12:54
-
@3bu1 Judging by your screenshot, you still have failed to replace
if
withwhile
.Dmitry Grigoryev– Dmitry Grigoryev2017年06月20日 13:13:27 +00:00Commented Jun 20, 2017 at 13:13 -
still the same, here is the screen shot(i.sstatic.net/3nAC2.png) and i have posted question on this url(arduino.stackexchange.com/questions/39729/…), kindly help me with this...thank u in advance3bu1– 3bu12017年06月20日 13:19:14 +00:00Commented Jun 20, 2017 at 13:19