0

I tried getting a HTTP response from an ESP8266 which is in HTTP server mode, I am getting a response but not the latest one, response is always one command previous to that of my request to the ESP. Also first time I don't get any response, from the nd request response start coming but previous.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>   //ESP Web Server Library to host a web page

const char* ssid = "SpruceRND";
const char* password = "sprucernd1234";

ESP8266WebServer server(80);   // server at port 80 (http port)

//Variable to store the HTTP request
String inputString ="";
boolean stringComplete = false;
String Clientquery ="";  //for store all query params
char query_array[30];    //for store all query character
int LEDPin = 4;

void ESPserialEvent()
{
  boolean x=0;
  Serial.println("Working ESPserialevent....");
  while (Serial.available())
  {
    char inChar = (char)Serial.read();    // get the new byte:
    if (inChar == 2)   //check if 1st byte is 2 
    {
      x=1;
    }
    if (x == 1);
    {
    inputString += inChar;   // add it to the inputString:
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    }
    if (inChar == 3) //check if 1ast byte is 3 
    {
      stringComplete = true;
      x=0;
    }
  }
}

void  handleNew()
{
  server.send(200, "text/plain", "Hello! SPRUCE"); //200=http status code,text/plain=content type,message
}

void handleMstring()
{
  Clientquery = server.arg("tx");    //Gets the value of the query parameter
//  Clientquery.toCharArray(query_array, sizeof(Clientquery)); // convert to chararry (buffer,length)
//  if (query_array[0] == 2 )
//  {
    Serial.println(Clientquery);
//  }
//  else
//  {
//     Serial.println("Incorrect string....");
//  }
  Clientquery="";
  ESPserialEvent();
  if (stringComplete == true)
  {  
    server.send(200, "text/plain",inputString);
    //Serial.println("Response from Mcu=" +inputString);
    //Serial.println("");
    inputString ="";
    stringComplete = false;
    //Serial.println("Buffer clear...");
  }
}

void setup(void)
{
  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, LOW);
  delay(1000);
  Serial.begin(9600);
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("Connecting to....");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/test", handleNew);
  server.on("/senddata", handleMstring);
  server.begin();
  Serial.println("Web server started!");
}

void loop(void){
  server.handleClient();
}
  • 1
    AT commands or esp8266 sketch? show the sketch – Juraj Jun 4 at 12:11
  • i don't know.How to post code – SPRUCE AUTOMATION Jun 4 at 12:20
  • the cause of your problem is if (stringComplete == true) in handleMstring – Juraj Jun 4 at 13:28
  • I don't what is the problem can you please help me. I am beginner. – SPRUCE AUTOMATION Jun 5 at 4:52
  • It is a coding logic problem, not an Arduino problem. – Juraj Jun 5 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.