Arduino EtherCard Post StaticJsonDocument to a web server

357 Views Asked by At

I am working on a project which I need to post some sensor data to my web service over tcp. I used StaticJsonDocument to hold these sensor data. The problem is that with the code below I can not be able to post any data. Currently I am using ENC28J60 for the ethernet connection.


void sendToApi (StaticJsonDocument<600> root) {

  byte sd = stash.create();

  String json_string;
  serializeJson(root, json_string);

  stash.print(json_string);
  stash.save();



  int stash_size = stash.size();
  Serial.println(stash_size);
  Stash::prepare(PSTR("POST /api/module HTTP/1.1" "\r\n"
    "Host: 192.168.1.5:8181" "\r\n"
    "Content-Type: application/json" "\r\n"
    "Content-Length: $D" "\r\n"
    "Authorization: Basic bWV0Ok1ldEF0czE4Kio=" "\r\n"
    "\r\n"
    "$H"),
   stash_size, sd);


  session = ether.tcpSend();
  //Serial.println(session);


  delay(2000);

}

There is no problem with the ethernet controller (I can ping Google). Also There is no problem with the server side. The postman HTTP request is below.

POST /api/module/ HTTP/1.1
Host: 192.168.1.5:8181
Content-Type: application/json
Authorization: Basic bWV0Ok1ldEF0czE4Kio=
Cache-Control: no-cache
Postman-Token: e160d927-7e05-414f-e0d8-102f3d039ce3

{"id":"0001","module_no":1,"m1":22.5625,"m2":22.5625,"m3":22.5625,"m4":22.5625,"m5":22.5625,"m6":22.5625,"m7":22.5625,"m8":22.5625,"m9":22.5625,"m10":22.5625,"m11":22.5625,"m12":22.5625,"m13":22.5625,"m14":22.5625,"m15":22.5625,"m16":22.5625,"t1":22.5625,"t2":22.5625,"t3":22.5625,"t4":22.5625,"t5":22.5625,"t6":22.5625,"t7":22.5625,"t8":22.5625,"t9":22.5625,"t10":22.5625,"t11":22.5625,"t12":22.5625,"t13":22.5625,"t14":22.5625,"t15":22.5625,"t16":22.5625,"af":22.5625,"uf":22.5625,"sg":22.5625,"sc":22.5625,"a1":946,"a2":946,"a3":946,"a4":946,"a5":946,"a6":32,"a7":32,"a8":32,"a9":946,"a10":946,"a11":946,"a12":946,"a13":32,"a14":32,"a15":32,"a16":32}

So what am I missing here?

0

There are 0 best solutions below