Hello guys,
I am running into an odd problem which I can't solve on my own. Maybe you guys can and will help me out
The main thing I am trying to achieve is to receive some data by HTTP POST request, and parsing it into some variables which can be used later.
Currently I have set up the following test sketch:
//===================================== Libraries toevoegen =============================================================================================================================================================================
#include <Ethernet.h>
#include <SPI.h>
#include <ArduinoJson.h>
//===================================== Globale Variabelen===============================================================================================================================================================================
String json = String(300);Â Â Â
EthernetServer server(80);     Â
//===================================== Ethernet var ====================================================================================================================================================================================
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
      };
IPAddress ip(192, 168, 1, 177);Â Â Â Â
IPAddress myDns(192,168,1, 1);Â
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
//===================================== Eenmalige setup==================================================================================================================================================================================
 void setup() {
//=====================================Pinmodes en overige inits=========================================================================================================================================================================
Â
 Serial.begin(9600);              Â
Â
 Serial.println("IP Autodiscover");
 if (Ethernet.begin(mac) == 0) {
                Â
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
 }
 Serial.print("Connect to me at: ");
 Serial.println(Ethernet.localIP());
 }
//===================================== Einde eenmalige setup==========================================================================================================================================================================
//
//===================================== Main Loop Begin ===============================================================================================================================================================================
void loop(){
EthernetClient client = server.available();Â
 if (client) {
  while (client.connected()) {
   if (client.available()) {
    char c = client.read();
   if (json.length() < 300) {
    json.concat(c); }
   Â
   Serial.print(c);
  Â
   if (c == '\n') {Â
    Â
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: application/json");
 client.println("Connection: close");
 client.println();
Â
StaticJsonBuffer<400> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
//Â Test if parsing succeeds.
 if (!root.success()) {
 Serial.println("parseObject() failed");
 }
 else {
 Serial.println("parseObject() success!");
}
//clearing string for next read
json="";
//stopping client
client.stop();
Serial.println("Client stopped");
}}}}}
The function for receiving data in HTTP-requests works. I have recyled this code from earlier projects. The main difference is: i never used it in combination with JSON-data.
I am using the Chrome REST plugin to do some testing posts. The test JSON-payload i am using is:
{
"hqid": "1",
}
As a result on the serial monitor (after posting) i get the following:
IP Autodiscover
Connect to me at: 192.168.1.121
POST /?%7B%0A%09%22hqid%22%3A+%221%22%2C%09%0A%7D HTTP/1.1
parseObject() failed
Client stopped
Obviously, the JSON- parsing fails because of the data not being in the right format. I can't get this to work. Am I overlooking something?
Thank you!
Leon.