#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiMulti.h>
const char *AP_SSID = "Wokwi-GUEST";
const char *AP_PWD = "";
WiFiMulti wifiMulti;
void setup() {
Serial.begin(9600);
//delay(4000);
wifiMulti.addAP(AP_SSID, AP_PWD);
postDataToServer();
}
void loop() {
// Not used in this example
}
void postDataToServer() {
Serial.println("Posting JSON data to server...");
// Block until we are able to connect to the WiFi access point
if (wifiMulti.run() == WL_CONNECTED) {
HTTPClient http;
//http.begin("http://15.207.84.40:4200/api/wastecollecteds");
http.begin("https://wokwidemo.free.beeceptor.com/api/users");
http.addHeader("Content-Type", "application/json");
StaticJsonDocument<200> doc;
// Add values in the document
doc["center"] = "Test Center";
// Add waste array
StaticJsonDocument<200> waste;
waste["recyclable"] = true;
waste["name"] = "Paper Bags";
waste["weight"] = "5kg";
JsonArray data = doc.createNestedArray("waste");
data.add(waste);
doc["centerId"] = "65cef46f0b3aeb76fd38dbfb";
String requestBody;
serializeJson(doc, requestBody);
int httpResponseCode = http.POST(requestBody);
if(httpResponseCode>0){
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
}
else {
Serial.printf("Error occurred while sending HTTP POST: %s\n", http.errorToString(httpResponseCode).c_str());
}
}
}