简体   繁体   English

从ESP8266 / Wemos上的传入HTTP Post请求解析数据

[英]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). 我正在Android应用程序上向Wemos D1 mini pro发送HTTP Post请求,并想解析传入的数据(是json)。 My current code just prints out the whole POST request and I need to trim it so I only get the needed data. 我当前的代码只是打印出整个POST请求,我需要对其进行裁剪,以便仅获取所需的数据。 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. 好吧,首先,您似乎正在使用ArduinoJson lib版本5,现在我可以共享使用的代码,而对于版本5则从未失败过。但是我将鼓励您将库更新为版本6,并与您共享我的代码。

I use this normally when I need to get information out of API's 当我需要从API获取信息时,通常会用到它

 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. 现在,正如您所看到的,我正在使用httpClient lib中的getString方法来填充我的char数组,而不是将其解析为json对象(几乎与您尝试的相同,只是区别是内存指针和内存分配。

Hopefully this will work with you. 希望这会与您合作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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