3

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.

sa_leinad
  • 3,188
  • 1
  • 22
  • 51
  • 2
    This site is about electrical engineering. Extra-sensory perception is off topic here. –  Jun 02 '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 02 '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 02 '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. :) –  Jun 02 '16 at 12:27
  • Unless you are pushing the connections on an Arduino to the limit it might be worth looking at a Webbo D1, its an ESP8266 12E with an Arduino form factor. The biggest issue I have had porting is remembering the Pins are addressed by GPIO label not pin number. – Code Gorilla Jun 02 '16 at 13:45
  • 1
    @Matt do you mean WeMos D1? There is no Webbo D1. – gre_gor Jun 02 '16 at 15:42
  • @gre_gor - Yes, that's the one. Sorry its been a long week :( – Code Gorilla Jun 02 '16 at 16:22

1 Answers1

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);
}
sa_leinad
  • 3,188
  • 1
  • 22
  • 51
Dmitry Grigoryev
  • 1,278
  • 10
  • 31
  • Thanx it works fine now! :) –  Jun 02 '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 02 '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 https://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(https://i.stack.imgur.com/3nAC2.png) and i have posted question on this url(https://arduino.stackexchange.com/questions/39729/not-able-to-get-response-from-esp-using-arduino-uno), kindly help me with this...thank u in advance – 3bu1 Jun 20 '17 at 13:19