• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

send data in Json format in MQTT

Q&A Forum › Category: ESP32 › send data in Json format in MQTT
0 Vote Up Vote Down
ramatito asked 6 years ago

// New temperature readings – 0 ~ 100C
temp_value = analogRead(ANALOG_PIN_0);
temp_value = (temp_value/4096) * 100;
// New Pressure readings – 0 ~ 300 PSI
press_value = analogRead(ANALOG_PIN_1);
press_value = (press_value/4096) * 300;
data_send = “{\”temp_value\”:”&temp_value&”,\”press_value\”:”&press_value&”}”;

I am using Arduino IDE.
error: invalid operands of types ‘const char [15]’ and ‘float’ to binary ‘operator&’

data_send = “{\”temp_value\”:”&temp_value&”,\”press_value\”:”&press_value&”}”;

3 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello,
Is that your exact code?

data_send = “{\”temp_value\”:”&temp_value&”,\”press_value\”:”&press_value&”}”;

What’s the data_send variable type? How are you trying to concatenate the values in that variable using a &?
Thanks for your patience, but I need a few more details to help you with this question..
 

0 Vote Up Vote Down
ramatito answered 6 years ago

Hello, good day,
data_send = string data type;
temp_value = float data type;
press_value =  float data type;

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

I haven’t tested it, but I think I didn’t miss any characters if you do the following:

data_send = "{\"temp_value\":\"" + String(temp_value) + "\",\"press_value\":\"" + String(press_value) + "\"}";

Then, you print that String data_send in the serial monitor for debugging purposes:

Serial.println(data_send);

It should print something like this:

{"temp_value":"YOUR_VALUE","press_value:"YOUR_VALUE"}";

Then, you can send that String via MQTT. Does it work for you?

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.