I am doing a project with Arduino.
Here is the piece of the code of one method I used
void HttpUpstreamClient::sendAlarmDeviceId(char* alarmType, char* alarmText, char* severity, String timestamp,char* URL, String deviceID ){
Serial.print("The alarm is sent to the device: ");
Serial.println(deviceID);
Serial.println(timestamp);
StaticJsonDocument<100> root;
JsonObject source = root.createNestedObject("source");
source["id"] = deviceID;
root["type"] = alarmType;
root["text"] = alarmText;
root["severity"] = severity;
root["time"] = timestamp;
String body2send= "";
serializeJsonPretty(root, body2send);
Serial.println(body2send);
if(_networkClient->connect(URL,443)){
Serial.println("Connected to server");
_networkClient->println("POST /alarm/alarms HTTP/1.1");
_networkClient->print("Host: ");
_networkClient->println(URL);
_networkClient->print("Authorization: Basic ");
_networkClient->println(_base64);
_networkClient->println("Content-Type: application/json");
_networkClient->print("Content-Length: ");
_networkClient->println(body2send.length());
_networkClient->println("Accept: application/json");
_networkClient->println();
//client.println("Connection: close");
_networkClient->println(body2send);
}
}
The most important point I want to mention here is the parameter String timestamp.
In the main.ino, the part loop() is as following:
void loop() {
// put your main code here, to run repeatedly:
// from the server, read them and print them:
while (nwc.available()) {
char c = nwc.read();
//Serial.print(c);
}
timeClient.update();
delay(1000);
clienttest.sendAlarmDeviceId(alarm_Type, alarm_Text,Severity,timeClient.getFormattedDate(),URL,"2556130");
}
That means the method "Serial.println(timestamp);" can work. However, it doesn't work when it's value passes to the StaticJsonDocument<100> root.
