Hello! I have managed to send the packet to my server but it’s not returning the string. Any heads up where i went wrong with my code? Thanks in advance
#include <SoftwareSerial.h>
String ssid ="asdasd";
String password="asdasd";
SoftwareSerial esp(2, 3);// RX, TX
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
String server = "pmsfeeder.com";
String location = "\retrieveInfo.php";
String data;
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
enableconn();
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.1\r\n" +
"Host:" + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" + data;
String cmd4 = "AT+CIPSEND=2,18";
esp.println(cmd4);
esp.println(postRequest.length());
delay(4000);
if (esp.find(">"))
{
Serial.println("Sending...");
delay(1200);
esp.println(postRequest);
if (esp.find("SEND OK"))
{
Serial.println("Packet sent");
return readPage();
}
else
{
Serial.println("Connection failed");
}
}
}
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!");
connectAndRead();
}
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");
}
else
{
Serial.println("Cannot enable multiple connections");
}
}