1

I'm sending a GET request from Arduino Uno using ESP8266. The request is sent, but I'm unable to print the received response.

I'm using code from https://elementztechblog.wordpress.com/2015/05/13/esp8266-based-temperature-data-logger-using-arduino/

I have changed the code for connecting to my server and I can see the GET request is received on my server's log.

I tried putting

 while (ser.available())
 {
     Serial.write(ser.read());
 }

after the Serial.println("AT+CIPCLOSE"); statement.

BUT I'm not getting anything on Serial monitor after "AT+CIPCLOSE"

EDIT: Here's my entire code:

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // TX, RX

// this runs once
void setup()
{

    // enable debug serial
    Serial.begin(9600);
    // enable software serial
    ser.begin(9600);

    // reset ESP8266
    ser.println("AT+RST");
}
// the loop
void loop()
{

    // TCP connection
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += "192.168.0.25"; 
    cmd += "\",3000";
    ser.println(cmd);

    if(ser.find("Error"))
    {
        Serial.println("AT+CIPSTART error");
        return;
    }

    // prepare GET string
    String getStr = "GET /api/myservice";

    getStr += "\r\n\r\n";

    // send data length
    cmd = "AT+CIPSEND=";
    cmd += String(getStr.length());
    ser.println(cmd);

    if(ser.find(">")){
    ser.print(getStr);
}
else
{ 
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");

    // CODE I FOUND FOR READING THE REPLY FROM SERVER:
    while (ser.available())
    {
         // char c = ser.read();
         Serial.write(ser.read());
         // if (c == '\r') Serial.print('\n');
    }
}

delay(1000);
}

ESP Details:

ESP-01

AT version: 0.40.0.0

enter image description here

Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
  • I can't help you with your exact question (have been spending many hours trying to connect a ESP8266 to my arduino Nano, with no success), But you could consider using the ESP module standalone, it's not aly a wifi module, but also a microprocessor. You can even program it using the Arduino IDE. Have a look here: http://www.arduinesp.com/wifiwebserver – ErikL Jan 29 '16 at 15:30
  • 1
    Please include your code so we can see what you tried already. – Blurry Sterk Jan 29 '16 at 17:19
  • @ErikL - Off topic: Do you still want to use the ESP8266? I am currently working with it without any problems. – Blurry Sterk Jan 31 '16 at 20:04
  • @blurry, I'm still interested. Working with the ESP8266 standalone is great, but the one thing I do mis is multiple analog inputs – ErikL Jan 31 '16 at 21:17
  • @BlurrySterk, I've added the code, please have a look at it. When I try to read the response, it prints **AT+CIPSEND= ERROR** – Dushyant Bangal Feb 01 '16 at 04:25
  • @ErikL - I don't want to start a subdiscussion on someone else's post so why don't you post a question explaining what you struggle with. Try to add some code to make it a more legit question so nobody will want to try to close it and maybe add an image of the ESP8266 which you struggle with and I will try and help you out and maybe explain the wiring you do to the ESP8266. Also, reply here if you are going to do that so I know. – Blurry Sterk Feb 01 '16 at 04:28
  • @Dushyant - I will give it a check. – Blurry Sterk Feb 01 '16 at 04:29
  • @Dushyant - First thing I need you to do is tell us what version of the firmware you are running. Also if you can, include an image of your ESP8266 into the question. – Blurry Sterk Feb 01 '16 at 07:17
  • @BlurrySterk, updated the question with ESP details. – Dushyant Bangal Feb 01 '16 at 09:20
  • @Dushyant - I have what seems to be the same module. – Blurry Sterk Feb 01 '16 at 09:46
  • I was able to send data to a server using GET and POST. But i'm unable to read response. – Dushyant Bangal Feb 01 '16 at 11:01
  • So your sending is working? – Blurry Sterk Feb 01 '16 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102244/discussion-between-dushyant-bangal-and-blurry-sterk). – Dushyant Bangal Feb 01 '16 at 13:00

1 Answers1

0

If you are only struggling to read a response then the answer is simple;

You are closing the TCP connection before you try to read:

ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");

// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{

Move the reading while into the above block just beneath ser.print(getStr); but add a delay between the two as well.

Blurry Sterk
  • 1,595
  • 2
  • 9
  • 18