HiDayurie Dave
HiDayurie Dave

Reputation: 1807

Arduino NodeJS Socket.io send json format data not getting the value of variable

I'm working with Arduino to create JSON.

The code like below

String Temperature = "30";
String macAddressDevice = "ABC";
String Humidity = "56";

char json[] = "{\"mac\":macAddressDevice,\"temperature\":Temperature,\"humidity\":Humidity}";

mqtt.publish("TemperatureHumidity", json);

When I try to console.log in nodejs, it show me not the value but the variable name text:

{"mac":macAddressDevice,"temperature":Temperature,"humidity":Humidity}

Is there any way how to get the value with above json format ?

Upvotes: 0

Views: 166

Answers (1)

hcheung
hcheung

Reputation: 4034

Your code works exactly as you code it, that is, you are sending a string literal instead of concatenating string literal with String variables like Temperature and Humidity.

String concatenation is something like this:

String json= "{\"mac\":macAddressDevice,\"temperature\":" + Temperature + "\"humidity\":" + Humidity + "}";
mqtt.publish("TemperatureHumidity", json.c_str());

See String Addition Operator on how to concatenate variables together to form a String.

The json.c_str() converts a String object to a pointer to a char array. See c_str() for more information.

Upvotes: 1

Related Questions