[Solved]: ESP-01 read data from web-server

I’m trying to read data sent from a web server. For this purpose I wrote a simple php script on the web erver:

<?php
echo "123456@\n"

I’m able to connect to the web server, but when trying to read the response, I always get ascii values 13, 10, 62, 32 (4 bytes) regardless of what the php script echo.

I googled a lot but cannot find any good example of how to read data from a web server on the arduino.

The Serial3 interface on a Mega2560 is connected to the ESP unit.

void loop()
{
  String url = "GET /foo/bar/test.php";

  sendATcmd(url, 10, "OK");

  char buffer[50];
  int rc = Serial3.readBytesUntil('@', buffer, 49);
  Serial3.flush();
  buffer[rc] = 0;

  for (int i = 0; rc > i; i++) {
    Serial.print(buffer[i], DEC);
    Serial.print(F(", "));
  }
  Serial.println('.');
  while (true);
}


bool sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) 
{
  int   trycnt = 0;
  bool  status = false;
  
  while (trycnt < AT_cmd_maxTime) {
    Serial3.println(AT_cmd);
    if (Serial3.find(readReplay)) {
      AT_cmd_result = true;
      break;
    }
  
    ++trycnt;
  }

  if (true == AT_cmd_result) {
    status = true;
  } 

  AT_cmd_result = false;

  return status;
}

What’s the magic for reading the response that I have missed?

The codes returned are CR+LF and then a > followed by space, in other words, the AT command is returning a prompt. Since your url is not a valid AT command, there is no OK because the command did not succeed. All AT commands are prefixed with AT+ and the AT command set can be found here:

https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf

However, you will find no AT command that connects to a URL, performs a GET and the returns some data.

For that you will need to write a sketch for the ESP-01 to set up a HTTP client, connect to the server, perform the GET and then return the results to the Mega. You can upload sketches to the ESP just as you can on any Arduino. You can find an example of how to do that in the IDE, but you will first have to install the ESP library. Then head to Files -> Examples.

The ESP has its own GPIO pins and can run sketches so depending on what you are trying to accomplish, you may not even need the Mega. Be aware that the AT commands are provided by a sketch that is factory installed. Uploading your own sketch will overwrite that although it can be restored later from a binary file if required. Howverer, once you start writing your own sketches for the ESP you will likely not go back to using AT commands!

What do you mean by not a valid url? The AT command is added by the sendATcmd function.

I have already tested code to send data to the web-server and it works fine:

        String url = "GET /dahls/ard/uno.php";
        url += "?v1=";
        url += random(10, 100);
        url += "&v2=";
        url += random(10, 100);
        
        status = sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
          
        if (status) {
          sendATcmd(url, 10, "OK");
          //delay(100);
          status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
        }

The PHP script report.php data received this.

Output from the sketch with full debug code enabled:

15:06:34.357 -> ********** Open TCP connection 
15:06:34.397 -> ---> AT+CIPSTART=0,"TCP","192.168.2.22",80
15:06:34.517 -> <--- DONE
15:06:34.517 -> ---> AT+CIPSEND=0,38
15:06:34.557 -> <--- DONE
15:06:34.557 -> ---> GET /dahls/ard/uno.php?v1=33&v2=48
15:06:35.677 -> <--- DONE
15:06:35.677 -> ---> AT+CIPCLOSE=0
15:06:35.677 -> <--- DONE
15:06:35.717 -> ********** Close TCP Connection

For that you will need to write a sketch for the ESP-01 to set up a HTTP client, connect to the server, perform the GET and then return the results to the Mega.

So you cannot get data back to the Mega without writing a sketch for the ESP-01?

I managed to fix it. It works now.