简体   繁体   中英

Parsing data from incoming HTTP Post request on ESP8266/Wemos

I'm sending an HTTP Post request on my Android App to my Wemos D1 mini pro and want to parse the incoming data (which is a json). My current code just prints out the whole POST request and I need to trim it so I only get the needed data. There are several examples out there but nothing matched my needs or worked at all.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>




const char* ssid = "myssid";
const char* password = "mypassword";
char c;
String readString = String(100);
WiFiServer wifiServer(80);


void setup() {

  Serial.begin(9600);
  delay(1000);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }

  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());
  wifiServer.begin();

}


//for parsing the actual JSON later  
//you can ignore this at this moment because I don't even get the needed string to parse it from JSON
void handleReceivedMessage(String message){

  StaticJsonBuffer<500> JSONBuffer;                     //Memory pool
  JsonObject& parsed = JSONBuffer.parseObject(message); //Parse message

  if (!parsed.success()) {   //Check for errors in parsing

    Serial.println("Parsing failed");
    return;

  }

  const char * name3 = parsed["name"];           //Get name from HTTP
  Serial.println("name3");
}


void loop() {
  WiFiClient client = wifiServer.available();


  if (client) {
  Serial.println("Client connected");


    while (client.connected()) {

      while (client.available()>0) {
        //instream from mobile device

        char c = client.read();
        if (readString.length() < 100) {

         //store characters to string
         readString.concat(c);
         //Serial.print(c);
}
      Serial.print(c);
//if HTTP request has ended
       if (c == '\n') {
          //Serial.println(readString);   
          delay(50);  
          //handleReceivedMessage(readString);         
          readString = "";
          client.stop();
   }
 }}}}

Well first of all you seem to be using ArduinoJson lib version 5, now I could share the code I worked with and never failed me with version 5. But i'm going to encourage you to update the library to version 6 and share with you my piece of code.

I use this normally when I need to get information out of API's

 DynamicJsonDocument doc(1024);

char* payload1 = (char*)malloc(http.getSize() + 1);
http.getString().toCharArray(payload1, http.getSize() + 1);
Serial.println(payload1);
http.end();

auto error = deserializeJson(doc, payload1);
free(payload1);
if (error) {
  Serial.print(F("deserializeJson() failed with code "));
  Serial.println(error.c_str());
  return;
}
serializeJsonPretty(doc, Serial);

now as you can see, I'm using a getString method from httpClient lib in order to fill my char array and than parse it into json object (pretty much the same thing you was attempting, only difference is the memory pointers and Memory allocations.

Hopefully this will work with you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM