I read data from sensor and show to serial monitor like this.
int humidity = dht.readHumidity();
int temperature = dht.readTemperature();
String place = "My Home";
if (online) {
if (!client.connected()) {
reconnect();
}
client.loop();
String data = "{\"data\": {\"humidity\":" + String(humidity) + ", \"temperature\":" + String(temperature) + ", \"place\": " + place + "}}";
data.toCharArray(msg, (data.length() + 1));
Serial.println(msg);
The output is
{"data": {"humidity":60, "temperature":27, "place": My Home}}
{"data": {"humidity":2147483647, "temperature":2147483647, "place": My Home}}
After I run code it have no error but the output is not correct and the program will stop after show this output.
If I comment at line data.toCharArray(msg, (data.length() + 1));
. Then, the program will show the correct output of temperature and humidity value. How to fix it?
Use the basic
itoa
function. It is included instdlib.h
Source : Arduino Playground