Hello, I am trying to use the arduino json on an esp32, with publishes requests.
The problem is that you can only make the primary post, you cannot make any more posts. Some allocation problem, strategy change, my code :
void curlRequest() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
const int capacity = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2)
StaticJsonDocument doc;
doc["name"] = "name";
doc["sensor_1"] = sensor_1;
doc["sensor_2"] = sensor_2;
doc["sensor_3"] = sensor_3;
doc["sensor_4"] = sensor_4;
serializeJsonPretty(doc, postMessage);
serializeJsonPretty(doc, Serial);
http.begin("https://xxxxxxxxxxxxxxxxxxxxxx "); //Specify destination for HTTP request
http.addHeader("Content-Type", "application/json");
http.addHeader("api_token", "xxxxxxxxxxxxxxxx");
int httpCode = http.POST(postMessage);
if (httpCode > 0) {
Serial.println();
Serial.println(httpCode);
if (httpCode == 200) {
Serial.println("Hooray!");
}
}
httpCode.end.
}
}
void loop() {
curlRequest();
}
silvaa:
Hello, I am trying to use the arduino json on an esp32, with publishes requests.
The problem is that you can only make the primary post, you cannot make any more posts. Some allocation problem, strategy change, my code :
I was unable to compile your sketch. It appears to be incomplete.
This line seems to be missing a ';':
const int capacity = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2)
These four 'sensor' objects aren't declared:
doc["sensor_1"] = sensor_1;
doc["sensor_2"] = sensor_2;
doc["sensor_3"] = sensor_3;
doc["sensor_4"] = sensor_4;
This line is confusing since 'httpCode' is an 'int' and not a class:
httpCode.end.
Sorry, it was an approximate sketch; and http.end();
it hear:
void curlRequest() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
const int capacity = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2);
StaticJsonDocument doc;
doc["name"] = "name";
doc["sensor_1"] = sensor_1;
doc["sensor_2"] = sensor_2;
doc["sensor_3"] = sensor_3;
doc["sensor_4"] = sensor_4;
serializeJsonPretty(doc, postMessage);
serializeJsonPretty(doc, Serial);
http.begin("https://xxxxxxxxxxxxxxxxxxxxxx "); //Specify destination for HTTP request
http.addHeader("Content-Type", "application/json");
http.addHeader("api_token", "xxxxxxxxxxxxxxxx");
int httpCode = http.POST(postMessage);
if (httpCode > 0) {
Serial.println();
Serial.println(httpCode);
if (httpCode == 200) {
Serial.println("Hooray!");
}
}
http.end.
}
}
void loop() {
curlRequest();
}
The problem is that make the first post ...
That sketch is incomplete and won't compile.
//Change the code below by your sketch
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v6
const char* ssid = "";
const char* password = "";
int sensor_0 = 1234;
int sensor_1 = 5678;
String postMessage;
void setup() {
Serial.begin(115200);
delay(500);
WiFi.begin(ssid, password);
Serial.print("Connecting to WPAPersonal Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(200);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
curlRequest();
}
//void setup() {
// WPAConnect();
// curlRequest();
//}
void curlRequest() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
const int capacity = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2);
StaticJsonDocument doc;
doc["name"] = "name";
doc["sensor_1"] = 1;
doc["sensor_2"] = 2;
doc["sensor_3"] = 3;
doc["sensor_4"] = 4;
serializeJsonPretty(doc, postMessage);
serializeJsonPretty(doc, Serial);
http.begin("https://xxxxxxxxxxxxxxxxxxxxxx ");
http.addHeader("Content-Type", "application/json");
http.addHeader("", "xxxxxxxxxxxxxxxx");
int httpCode = http.POST(postMessage);
if (httpCode > 0) {
Serial.println();
Serial.println(httpCode);
if (httpCode == 200) {
Serial.println("Hooray!");
}
}
http.end();
}
}
void loop() {
curlRequest();
delay(10000);
}
The problem and approach is this :
approach:
const int capacity = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2);
StaticJsonDocument doc;
i think.
silvaa
June 3, 2020, 10:15pm
6
Only first input is valid :
{
"id_produto": 1,
"name": "teste",
"board": 1,
"gpio": 1,
"state": 1
}
200 OK
{
"id_produto": 1,
"name": "teste",
"board": 1,
"gpio": 1,
"state": 1
}
500 Not OK
{
"id_produto": 1,
"name": "teste",
"board": 1,
"gpio": 1,
"state": 1
}
500 Not OK
serializeJsonPretty(doc, postMessage);
I'm not sure but I think this may be APPENDING the document to postMessage.
One way to find out: Replace "serializeJsonPretty(doc, Serial);" with "Serial.print(postMessage)";
Hello i try but i have same, one 200 and otheres 500
Use postman to validate api and all it's ok, insert as many as I want.
some clue to continue was good, I am stopped here for 2 days ...