Hi,
I'm building a simple temperature controller for my home brew beer fermenter and trying to post the temperature to and API in the brewing software I use using json.
The code works fine to post it but only on the first run. It re-posts the latest data every 15 minutes but for some reason, the new data gets appended to the end of the json document rather than overriding it and all subsequent posts fail.
Annoyingly, when I send the json document to the serial monitor, it shows correctly, its only when I send it to a webhook that it grows each time it sends.
I've tried multiple ways to stop this but am yet to figure it out and would love some advice.
How do I create and post a json document every 15 minutes without the document growing each time?
So far I have tried using a staticjsondocument and the clear() function after each post, that didn't work. I've tried a dynamicjsondocument followed by the free() and malloc() functions but that didn't work. And I've just tried converting the object to a new object, theoretically wiping it in the process, but that still doesn't work!
Below is an image of the raw data received when using webhook.site. The code has been run 4 times here.
n
Here's my current attempt at the solution. This function gets called whenever I want to send to the webhook.
void send_json(int CurrentTemp){
if(WiFi.status()== WL_CONNECTED){
//WiFiClient client;
WiFiClientSecure client;
client.setInsecure();
client.connect(serverName, 443);
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
Serial.println("Publishing to: ");
Serial.print(serverName);
Serial.println("");
DynamicJsonDocument doc(1024);
JsonObject obj = doc.to<JsonObject>();
doc["name"] = "ControllerTest";
doc["temp"] = CurrentTemp;
doc["beer"] = "Midnight Spa";
serializeJsonPretty(doc, requestBody);
serializeJsonPretty(doc, Serial);
doc.clear();
//doc.free();
//doc.malloc();
Serial.println("");
int httpResponseCode = http.POST(requestBody);
if(httpResponseCode>0){
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
}
else {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
}```
I'd love some help with this, it doesn't seem logical, especially since printing to the serial monitor looks like this (ignore the timestamps. I post to Brewfather every 15 mins but do it every 5 seconds for testing)
21:44:27.196 -> Publishing to:
21:44:27.196 -> https://webhook.site/79928f66-60d6-449a-9bb5-6b926e8c1956
21:44:27.243 -> {
21:44:27.243 -> "name": "ControllerTest",
21:44:27.243 -> "temp": 24,
21:44:27.243 -> "beer": "Midnight Spa"
21:44:27.243 -> }
21:44:31.843 -> 200
21:44:31.843 ->
21:44:42.408 -> Publishing to:
21:44:42.408 -> https://webhook.site/79928f66-60d6-449a-9bb5-6b926e8c1956
21:44:42.455 -> {
21:44:42.455 -> "name": "ControllerTest",
21:44:42.455 -> "temp": 24,
21:44:42.455 -> "beer": "Midnight Spa"
21:44:42.455 -> }
21:44:47.054 -> 200
21:44:47.054 ->
21:44:57.648 -> Publishing to:
21:44:57.648 -> https://webhook.site/79928f66-60d6-449a-9bb5-6b926e8c1956
21:44:57.648 -> {
21:44:57.648 -> "name": "ControllerTest",
21:44:57.648 -> "temp": 24,
21:44:57.648 -> "beer": "Midnight Spa"
21:44:57.648 -> }
21:45:02.125 -> 200
21:45:02.125 ->
21:45:12.745 -> Publishing to:
21:45:12.745 -> https://webhook.site/79928f66-60d6-449a-9bb5-6b926e8c1956
21:45:12.745 -> {
21:45:12.745 -> "name": "ControllerTest",
21:45:12.745 -> "temp": 24,
21:45:12.745 -> "beer": "Midnight Spa"
21:45:12.745 -> }
21:45:17.566 -> 200
21:45:17.566 ->
Thank you for any help anyone can give me
