• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

代码ESP32接入华为云物联网平台完成属性定时上报Arduino IDE开发

武飞扬头像
IOT趣制作
帮助1

更多相关:华为云IOT物联网 论坛

教你如何使用esp8266接入华为云物联网平台(IOTDA)(Arduino IDE开发)-云社区-华为云 (huaweicloud.com)

  1.  
    //ESP32接入华为云物联网平台完成属性定时上报
  2.  
    #include <WiFi.h>
  3.  
    #include <PubSubClient.h> //建议使用PubSubClient2.7.0,最新的库不太好用
  4.  
    WiFiClient espClient;
  5.  
    PubSubClient client(espClient);
  6.  
    const char* ssid = "xxxxxx"; //wifi名称
  7.  
    const char* password = "xxxxxx"; //wifi密码
  8.  
    const char* mqttServer = "xxx"; //例如iot-mqtts.cn-north-4.myhuaweicloud.com,详情参考华为云控制台
  9.  
    const int mqttPort = 1883; //例如1883(华为MQTT地址1883,MQTTS地址8883,详情参考华为云控制台)
  10.  
    //三元组:三元组生成链接:https://iot-tool.obs-website.cn-north-4.myhuaweicloud.com/
  11.  
    const char* ClientId ="xxxxxx";
  12.  
    const char* mqttUser ="xxxxxx";
  13.  
    const char* mqttPassword = "xxxxxx";
  14.  
    //注册设备的ID和密钥
  15.  
    #define device_id "xxxxxx"
  16.  
    #define secret "xxxxxx"
  17.  
    //注意修改自己的服务ID
  18.  
    #define Iot_link_Body_Format "{\"services\":[{\"service_id\":\"Dev_data\",\"properties\":{%s"
  19.  
    //参考上报格式:{"services":[{"service_id":"Dev_data","properties":{"temp": 39}}]}
  20.  
    //设备属性上报
  21.  
    #define Iot_link_MQTT_Topic_Report "$oc/devices/"device_id"/sys/properties/report"
  22.  
    int data_temp=1; //模拟上报的温度值
  23.  
    long lastMsg = 0;
  24.  
    void setup() {
  25.  
    //wifi初始化
  26.  
    Serial.begin(115200);
  27.  
    WiFi.begin(ssid, password);
  28.  
    while (WiFi.status() != WL_CONNECTED) {
  29.  
    delay(500);
  30.  
    Serial.println("Connecting to WiFi..");
  31.  
    }
  32.  
    Serial.println("Connected to the WiFi network");
  33.  
    //MQTT初始化
  34.  
    MQTT_Init();
  35.  
    //更多资料欢迎关注微信公众号“IOT趣制作”
  36.  
    }
  37.  
    void loop() {
  38.  
    if (!client.connected()){
  39.  
    MQTT_Init();
  40.  
    }
  41.  
    else client.loop();
  42.  
    long now = millis();
  43.  
    if (now - lastMsg > 5000)//定时上报
  44.  
    {
  45.  
    lastMsg = now;
  46.  
    MQTT_POST();
  47.  
    data_temp ;
  48.  
    }
  49.  
    }
  50.  
    void MQTT_Init()
  51.  
    {
  52.  
    client.setServer(mqttServer, mqttPort);
  53.  
    while (!client.connected())
  54.  
    {
  55.  
    Serial.println("Connecting to MQTT...");
  56.  
    if (client.connect(ClientId, mqttUser, mqttPassword ))
  57.  
    {
  58.  
    Serial.println("connected");
  59.  
    }
  60.  
    else
  61.  
    {
  62.  
    Serial.print("failed with state ");
  63.  
    Serial.print(client.state());
  64.  
    delay(3000);
  65.  
    }
  66.  
    }
  67.  
    }
  68.  
    void MQTT_POST()
  69.  
    {
  70.  
    char properties[32];
  71.  
    char jsonBuf[128];
  72.  
    sprintf(properties,"\"temp\":%d}}]}",data_temp);
  73.  
    sprintf(jsonBuf,Iot_link_Body_Format,properties);
  74.  
    client.publish(Iot_link_MQTT_Topic_Report, jsonBuf);
  75.  
    Serial.println(Iot_link_MQTT_Topic_Report);
  76.  
    Serial.println(jsonBuf);
  77.  
    Serial.println("MQTT Publish OK!");
  78.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfgafb
系列文章
更多 icon
同类精品
更多 icon
继续加载