0

I want to retrieve the echo string. Here is my php code

<?php
    echo "<hello word!>";
?>

I am using ESP8266 and Arduino Uno. I have managed to send the request but it's not returning the string from my php file. Any idea where i went wrong with my code? Thanks in advance

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>

SoftwareSerial esp(2, 3);// RX, TX

String ssid ="";  
String password="";     

String server = "http://domain.com/";
String location = "/retrieveInfo.php";

char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup()
{
    esp.begin(9600);
    Serial.begin(9600);
    connectWifi();
}

void loop()
{
  String pageValue = connectAndRead(); //connect to the server and read the output
  Serial.println(pageValue); //print out the findings.
  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead()
{
  //connect to the server
  Serial.println("Connecting...");
    String cmd2 = "AT+CIPSTART=2\"TCP\",\"" + server + "\",80";
    esp.println(cmd2);
    delay(4000);

    if (esp.find("OK"))
      {
        Serial.println("TCP Connection Ready.");
      }
             String postRequest = "GET" + location + "HTTP/1.0\r\n";
             String cmd4 = "AT+CIPSEND=2,80";
             delay(4000);
             esp.println(cmd4);
             esp.println(postRequest.length());
             delay(4000);

             if (esp.find(">"))
                {
                   Serial.println("Sending...");
                   delay(1200);
                   esp.println(postRequest);
                   esp.println();

                  if (esp.find("SEND OK"))
                  {
                    Serial.println("Packet sent");
                    Serial.println();
                    delay(4000);
                    //Connected - Read the page
                    return readPage(); //go and read the output

                 }
                  else
                  {
                    Serial.println("Connection failed");
                  }
            }
    else
      {
        Serial.println("TCP Connection Not Ready.");  
      }
}

String readPage()
{
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true)
  {
      if (esp.available()) 
        {
          char c = esp.read();
          if (c == '<' ) { //'<' is our begining character
            startRead = true; //Ready to start reading the part 
          }else if(startRead){

            if(c != '>'){ //'>' is our ending character
              inString[stringPos] = c;
              stringPos ++;
        }
        else
          {
            //got what we need here! We can disconnect now
            startRead = false;
            return inString;

         }

      }
    }

  }
}

void connectWifi()
  {

    String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"\r\n";
    esp.println(cmd);
    delay(5000);

    if(esp.find("OK"))
      {
        Serial.println("Connected!");
        delay(4000);
        enableconn();
        delay(1000);
      }

    else
      {
        Serial.println("Cannot connect to wifi");
        connectWifi();
      }

  }

void enableconn()
  {
    String cmd1 = "AT+CIPMUX=1";
    esp.println(cmd1);
    delay(4000);
    if (esp.find("OK"))
      {
      Serial.println("Ok");
      delay(4000);
      connectAndRead();
      }
     else
      {
        Serial.println("Cannot enable multiple connections");
      }
  }

for some reason, the line "return readPage();" isn't returning any value. Any heads up would be much appreciated. This is my output in the serial monitor

Output

  • 1
    Possible duplicate of Arduino is not reading data from server using Wifi Shield – Chris Stratton Oct 9 '16 at 22:55
  • You have posted two questions two hours apart both covering essentially the same problem, with only minor differences in the code. This is not permitted by the rules here - you need to delete one of the questions and make sure the remaining one best represents your problem. – Chris Stratton Oct 9 '16 at 22:56
  • Could you show us the serial output you get? – Gerben Oct 10 '16 at 11:22
  • I've edited the post and added the output of the serial monitor @Gerben – zoenightshade Oct 10 '16 at 12:23
  • You can't get PHP from a web server on port 80. It doesn't matter what hardware you use unless the server is compliant it ain't going to happen! The reason is the webserver will convert the PHP into a HTML page so you browser can see it. – Code Gorilla Oct 10 '16 at 13:18
1

It looks like you post request may be missing a CRLF at the end (it needs two) and some spaces.

I think you need this:

 String postRequest = "GET " + location + "\r\nHTTP 1.0\r\n\r\n";

My suggestion would be to get a REST client (like the one you can get for Chrome as an App) and send the message to your server through that and see what the difference is.

  • 1
    The \r\n before the HTTP needs to be a space though. See e.g. GET /questions/30029/arduino-retrieving-php-echo-using-esp8266 HTTP/1.1. But you are correct about the space after the GET and the double newline at the end. – Gerben Oct 10 '16 at 15:30
  • Hello Thank you for your suggestion! I followed it and i used api.thingspeak.com for my server. My arduino can read the data now but Im getting Bad Request Error? – zoenightshade Oct 10 '16 at 18:22
  • Look up the error on Google, but I suspect location is wrong. – Code Gorilla Oct 11 '16 at 8:32

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.