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

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

 之前带着大家写过使用esp8266接入华为云物联网平台的教程,有小伙伴后台私信,在ESP32接入华为云时遇到了问题,ESP32和ESP8266的代码几乎差不多的,ESP8266代码中用了“ESP8266.h”开发;ESP32可以直接用“WIFI.h”开发,教程基本和esp8266的那期教程一样,有需要的可以直接去看上一篇帖子:教你如何使用esp8266接入华为云物联网平台(IOTDA)(Arduino IDE开发)_MR_J.YW的博客-CSDN博客_怎么接入云平台

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

//ESP32接入华为云物联网平台完成属性定时上报
#include <WiFi.h>
#include <PubSubClient.h>  //建议使用PubSubClient2.7.0,最新的库不太好用
WiFiClient espClient;
PubSubClient client(espClient);
const char* ssid = "xxxxxx";              //wifi名称
const char* password =  "xxxxxx";    //wifi密码
const char* mqttServer = "xxx";       //例如iot-mqtts.cn-north-4.myhuaweicloud.com,详情参考华为云控制台
const int mqttPort = 1883;              //例如1883(华为MQTT地址1883,MQTTS地址8883,详情参考华为云控制台)
//三元组:三元组生成链接:https://iot-tool.obs-website.cn-north-4.myhuaweicloud.com/
const char* ClientId ="xxxxxx";         
const char* mqttUser ="xxxxxx";
const char* mqttPassword = "xxxxxx";
//注册设备的ID和密钥
#define device_id "xxxxxx" 
#define secret "xxxxxx" 
//注意修改自己的服务ID   
#define Iot_link_Body_Format "{\"services\":[{\"service_id\":\"Dev_data\",\"properties\":{%s" 
//参考上报格式:{"services":[{"service_id":"Dev_data","properties":{"temp": 39}}]}
//设备属性上报
#define Iot_link_MQTT_Topic_Report "$oc/devices/"device_id"/sys/properties/report"
int data_temp=1;   //模拟上报的温度值
long lastMsg = 0; 
void setup() {
  //wifi初始化
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  //MQTT初始化
  MQTT_Init();
 //更多资料欢迎关注微信公众号“IOT趣制作”
}
void loop() {
  if (!client.connected()){
    MQTT_Init();
  } 
  else client.loop();
  long now = millis();
  if (now - lastMsg > 5000)//定时上报
  {
    lastMsg = now;
    MQTT_POST();
    data_temp++;
  }
}
void MQTT_Init()
{
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) 
  {
    Serial.println("Connecting to MQTT...");
    if (client.connect(ClientId, mqttUser, mqttPassword )) 
    {
      Serial.println("connected");
    } 
    else 
    {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(3000);
    }
  } 
}
void MQTT_POST()
{
  char properties[32];
  char jsonBuf[128];
  sprintf(properties,"\"temp\":%d}}]}",data_temp);
  sprintf(jsonBuf,Iot_link_Body_Format,properties);
  client.publish(Iot_link_MQTT_Topic_Report, jsonBuf);
  Serial.println(Iot_link_MQTT_Topic_Report);
  Serial.println(jsonBuf);
  Serial.println("MQTT Publish OK!");
}

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IOT趣制作

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值