2

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.

migrated from electronics.stackexchange.com Jun 2 '16 at 13:19

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 2
    This site is about electrical engineering. Extra-sensory perception is off topic here. – Olin Lathrop Jun 2 '16 at 11:05
  • 4
    Why is this question downvoted and voted to close? MCVE code is provided, sympthoms of failure are described. What is the problem? – Dmitry Grigoryev Jun 2 '16 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 Jun 2 '16 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 Jun 2 '16 at 12:27
  • 1
    @Matt do you mean WeMos D1? There is no Webbo D1. – gre_gor Jun 2 '16 at 15:42
4

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 Jun 2 '16 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 Jun 2 '16 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.stack.imgur.com/mOklg.png.... – 3bu1 Jun 20 '17 at 12:54
  • @3bu1 Judging by your screenshot, you still have failed to replace if with while. – Dmitry Grigoryev Jun 20 '17 at 13:13
  • still the same, here is the screen shot(i.stack.imgur.com/3nAC2.png) and i have posted question on this url(arduino.stackexchange.com/questions/39729/…), kindly help me with this...thank u in advance – 3bu1 Jun 20 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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