简体   繁体   中英

Sending POST Json to API using Arduino Mega and Ethernet shield W5100

Having this as my sketch, the serial monitor saying that the JSON is successfully sent. But it does not reflect to my Cloud DB.

I changed my HTTPS to HTTP but no luck. Where could this go wrong?

My objective is all input in my Arduino will be sent to my server and store to my Cloud DB.

EDIT: After replacing all suggested Edits, I am getting 400 Bad request. In Postman, the request is working so i know that my request is valid. But I can't make it work in arduino using ethernet shield

void setup() {
  Serial.begin(9600);
  
  // initialize the Ethernet shield using DHCP:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to obtaining an IP address using DHCP");
      while(true);
    }
    
  delay(1000);

  Serial.println("connecting...");

  if (client.connect(HOST_NAME, HTTP_PORT)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
  }

   //Create JSON doc and write a "name" attribute
  const size_t capacity = JSON_OBJECT_SIZE(3);
  DynamicJsonDocument doc(capacity);
    doc["tank_id"] = "2a";
    doc["branch_name"] = "aurora";
    doc["water_level"] = "high level";


  //POST request
  Serial.println("Begin POST Request");

  client.println("POST /myURL HTTP/1.1");
    Serial.println("POST /myURL HTTP/1.1");

  client.println("Host: host.net");
    Serial.println("Host: host.net");
  client.println("User-Agent: Arduino/1.0");
    Serial.println("User-Agent: Arduino/1.0");

  client.println("Content-Type: application/json");
    Serial.println("Content-Type: application/json");

  client.println("Connection: keep-alive");
    Serial.println("Connection: keep-alive");

  client.print("Content-Length: ");
    Serial.print("Content-Length: ");

  client.println(measureJson(doc));
      Serial.println(measureJson(doc));

  client.println();
  Serial.print(F("Sending: "));
  serializeJson(doc, Serial);
  Serial.println();


  //This works like client.println, but prints doc to client
  serializeJsonPretty(doc, client);

  //To let me know that request has been completed
  Serial.println("Sent POST Request");

  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

void loop() {
 while(client.connected()) {
      if(client.available()){
        // read an incoming byte from the server and print it to serial monitor:
        char c = client.read();
        Serial.print(c);
      }
    }
}

I had exactly the same issue... after hours and hours of trying different codes, I found the problem:

serializeJsonPretty(doc, client);

This code, prints this:

POST /api/ HTTP/1.1
Host: api.xxxxxxx.com
Content-Type: application/json
Connection: close
Content-Length: 63
-> 
-> {
->   "data": "2a",
->   "branch_name": "aurora",
->   "water_level": "high level"
-> }

But, if you change to serializeJson(doc, client); you'll send an HTTP like this:

POST /api/ HTTP/1.1
Host: api.xxxxxxx.com
Content-Type: application/json
Connection: close
Content-Length: 63
-> 
-> {"data":"2a","branch_name":"aurora","water_level":"high level"}

I understood that the problem is to send the data in each line.. for me, changing data for only one line, the problem is gone.

I hope to help!

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