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

Time:12-08

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 ?

CodePudding user response:

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.

  • Related