0

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.