I am trying to publish JSON
data to mqtt with following code snippet
#include <Arduino_JSON.h>
JSONVar data;
data["temperature"] = temperature;
data["humidity"] = humidity;
data["pressure"] = pressure;
data["chipId"] = chipId;
String jsonString = JSON.stringify(data);
client.publish("esp32/data", jsonString);
But getting following error
/home/ubuntu/Arduino/libraries/PubSubClient/src/PubSubClient.h:144:12: note: candidate expects 4 arguments, 2 provided
Multiple libraries were found for "PubSubClient.h"
Used: /home/ubuntu/Arduino/libraries/PubSubClient
Multiple libraries were found for "Wire.h"
Used: /home/ubuntu/.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/Wire
Multiple libraries were found for "Adafruit_BME280.h"
Used: /home/ubuntu/Arduino/libraries/Adafruit_BME280_Library
Multiple libraries were found for "Adafruit_Sensor.h"
Used: /home/ubuntu/Arduino/libraries/Adafruit_Unified_Sensor
Multiple libraries were found for "SPI.h"
Used: /home/ubuntu/.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/SPI
Multiple libraries were found for "Arduino_JSON.h"
Used: /home/ubuntu/Arduino/libraries/Arduino_JSON
Multiple libraries were found for "WiFi.h"
Used: /home/ubuntu/.arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/WiFi
Not used: /opt/arduino-1.8.10/libraries/WiFi
exit status 1
no matching function for call to 'PubSubClient::publish(const char [11], String&)'
Whats is wrong here ?
1 Answer 1
What's your last error message?
no matching function for call to 'PubSubClient::publish(const char [11], String&)'
Read the documentation for the publish()
method in PubSubClient
:
int publish (topic, payload)
Publishes a string message to the specified topic.
Parameters
topic - the topic to publish to (const char[])
payload - the message to publish (const char[])
Look at the parameters you're using. You're passing a String
as payload
. publish()
expects payload
to be a char[]
(essentially the same thing as a char*
) - a C language string. This is the error you're seeing.
Read the documentation for String
. How do you get a char*
from a String
? The c_str()
method.
You need to change
client.publish("esp32/data", jsonString);
to
client.publish("esp32/data", jsonString.c_str());
As for your other warnings, it looks like your Arduino libraries are a mess with multiple conflicting libraries installed; if you want to get rid of the warnings, clean up your libraries.
-
is it possible to convert
json
tochar*
?rp346– rp3462019年11月16日 18:54:31 +00:00Commented Nov 16, 2019 at 18:54 -
1@roy - I've given you the steps to do that in the answer. If you want to do it more directly, you're using the Arduino_JSON library. The declaration for the
stringily()
method is at github.com/arduino-libraries/Arduino_JSON/blob/master/src/… and it only shows aString
as a return, so you cannot do it more directly using this library.romkey– romkey2019年11月16日 19:41:24 +00:00Commented Nov 16, 2019 at 19:41 -
Arduino_JSON
has this link github.com/arduino-libraries/Arduino_JSON, not much info here. Most of the example are based onArduinoJSON
.rp346– rp3462019年11月16日 19:51:10 +00:00Commented Nov 16, 2019 at 19:51 -
1My comment answered your question - I may have been editing it when you were replying. You can find the answer in the declaration of
stringify()
injson.h
romkey– romkey2019年11月16日 19:59:00 +00:00Commented Nov 16, 2019 at 19:59