如何将esp_mqtt库连接到thingsboard.io?

时间:2017-10-24 08:24:28

标签: arduino mqtt esp8266 arduino-esp8266 thingsboard

我正在尝试使用esp_mqtt库将传感器数据发布到thingsboard.io仪表板。但是图书馆和事物板之间的联系还没有建立起来。我使用的是esp8266 nodeMCU v2和platformIO

以下是esp_mqtt库文档的链接。 https://github.com/tuanpmt/esp_mqtt/blob/master/README.md

以下是代码段:

    #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);
       }

1 个答案:

答案 0 :(得分:1)

您正试图连接到localhost,除非您在ESP8266上运行经纪人,否则这将无法正常工作。

您需要知道thingsboard.io代理的主机名是什么,并用该值替换localhost。端口号也可能需要更改。

我也猜测clientid可能需要比client1更独特的东西