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