Reputation: 21
I am trying to publish sensor data using esp_mqtt library to thingsboard.io dashboard. But the connection between the library and thingsboard is not getting established. I am using esp8266 nodeMCU v2 and platformIO
Here is the link to the esp_mqtt library documentation. https://github.com/tuanpmt/esp_mqtt/blob/master/README.md
Here is the code snippet :
#include <ESP8266WiFi.h>
#include <MQTT.h>
#define CLIENT_ID "client1"
#define TOPIC "v1/devices/me/telemetry"
#define TOKEN "access token"
MQTT myMqtt(CLIENT_ID,"localhost",1883);
const char* ssid = "ssid";
const char* password = "password";
void setup() {
Serial.begin(115200);
delay(1000);
setup_wifi();
Serial.println("Connecting to MQTT server");
// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);
Serial.println("connect mqtt...");
myMqtt.setClientId(CLIENT_ID);
myMqtt.setUserPwd(TOKEN,"");
myMqtt.connect();
Serial.println("subscribe to topic...");
myMqtt.subscribe(TOPIC);
delay(10);
}
void loop() {
float value = analogRead(A0);
String temp=String(value);
// publish value to topic
String payload = "{";
//payload += "\"temperature\":"; payload += temperature; payload += ",";
payload += "\"temperature\":"; payload += temp;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
//String(attributes);
boolean result = myMqtt.publish("v1/devices
/me/telemetry",attributes,100,1,0);
delay(1000);
}
Upvotes: 0
Views: 934
Reputation: 59791
You are trying to connect to localhost
unless you are running a broker on the ESP8266 this is never going to work.
You need to know what the host name of thingsboard.io's broker is and replace localhost
with that value. The port number may also need changing.
I'm also guessing the clientid may need to be something a little more unique than client1
Upvotes: 1