Hello Community,
I am facing some issue when I publish multiple strings to the bucket using MQTT.
I get this error from my instance logs : error while processing protocol: [json.exception.type_error.302] type must be string, but is binary
. I have 5 temperature sensing devices located at different places which send data using MQTT protocol to thinger. I have then created a bucket which stores this data. I want to send different parameters of the device like its MAC, its ID, location and finally temperature. How can I combine all these parameters into one mqttPublish command?
thing["DHT11"] >> [](pson& out)
{
out ["City"] = "Pune";
out ["Center"] = "Ruby";
out ["Room"] = "300";
out ["Sensor_No"] = "S1";
out["Temperature"]= DHT.getTemperature();
out["MAC Address"]= "9E:95:37:53:7A:66";
};
Can we do something like above in mqttPublish command?
Here’s my code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include<DHTesp.h>
#include<OneWire.h>
#include<DallasTemperature.h>
//#include "Thinger.h"
#define One_Wire_Bus 5
const char* ssid = ""; // Enter your WiFi name
const char* password = ""; // Enter WiFi password
const char* mqtt_server = "";//MQTT Broker
const int mqttPort = 1883;
const char* mqttUser = "";//Thinger Username
const char* mqttPassword = "";//Device Credentials
long lastReconnectAttempt = 0;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
DHTesp DHT;
OneWire oneWire(One_Wire_Bus);
DallasTemperature sensors(&oneWire);
char msg[200];
void setup_wifi() {
delay(10);
sensors.begin();
DHT.setup(16,DHTesp::DHT11);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.setSleepMode(WIFI_NONE_SLEEP); // Suggested to help with led flickering issue
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
boolean reconnect() {
Serial.println("Reconnecting to mqtt");
if (mqttClient.connect("",mqttUser, mqttPassword)) {
// Once connected, publish an announcement...
mqttClient.publish("mqtt/state","\"online\"");
}
return mqttClient.connected();
}
void setup() {
Serial.begin(9600);
Serial.println("Starting up");
/* ESP8266 Stuff */
setup_wifi();
Serial.println("Connect to mqtt server");
mqttClient.setServer(mqtt_server, 1883);
// mqttClient.setCallback(mqttCallback);
lastReconnectAttempt = 0;
/* End ESP8266 Stuff */
Serial.println("Setup done");
}
void loop() {
if (!mqttClient.connected()) {
Serial.println("mqtt NOT connected");
long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
mqttClient.loop();
}
float dtemp = DHT.getTemperature();
float dhumi = DHT.getHumidity();
float ctemp = sensors.getTempCByIndex(0);
Serial.println(ctemp);
//Serial.println(dtemp);
//Serial.println(dhumi);
// mqttClient.publish("mqtt/test",String(dhumi).c_str());
snprintf(msg,200,"");
Serial.println(msg);
mqttClient.publish("mqtt/test", msg);
// mqttClient.publish("mqtt/test", String(dhumi).c_str());
// mqttClient.publish("mqtt/test", String(ctemp).c_str());
//Serial.println("Temp: " + (String) sensors.getTempCByIndex(0));
delay(60000);
}