当前位置:   article > 正文

ESP8266模块连接OneNet云平台(HTTP协议)方法和步骤_onenet http esp8266

onenet http esp8266

Arduino IDE 代码:

  1. #include <ESP8266WiFi.h> //WiFi驱动库
  2. #include <PubSubClient.h> //提供MQTT协议
  3. //设置OneNet参数
  4. const char* mqttServer = "183.230.40.39";//mqtt服务器
  5. const uint16_t mqttPort = 6002; //端口号
  6. const char *Device_ID = "1060658288"; //设备ID
  7. const char *Product_ID = "584149"; //产品ID
  8. const char *Api_KEY = "LiVJexVOw4FRbSzo=WYozBpbWvM="; //鉴权信息
  9. //设置WiFi参数
  10. const char *WiFi_SSID = ""; //WiFi名称
  11. const char *WiFi_Password = ""; //WiFi密码
  12. float Current = 0;
  13. uint8_t Upload_Package[100];
  14. uint32_t Last_Updata_Time = 0;
  15. //创建对象
  16. WiFiClient espClient;
  17. PubSubClient client(espClient);
  18. //声明下函数
  19. void WiFi_Init(void);
  20. void MQTT_Init(void);
  21. void MQTT_Reconnection(void);
  22. void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len);
  23. void setup()//setup只执行一次,联网放这里面就行
  24. {
  25. Serial.begin(115200);//串口初始化
  26. WiFi_Init(); //WiFi初始化
  27. MQTT_Init(); //MQTT初始化
  28. }
  29. void loop()//loop相当于其他单片机的while(1)
  30. {
  31. //判断MQTT连接了没,没的话重新连接
  32. if (!client.connected())
  33. MQTT_Reconnection();
  34. if (millis() - Last_Updata_Time >= 3000)//3s发送一次数据
  35. {
  36. Last_Updata_Time = millis();
  37. float ADC_Value = analogRead(A0);
  38. Current = 25;
  39. Serial.print("Current: ");
  40. Serial.print(Current);
  41. Serial.println(" A\r\n");
  42. memset(Upload_Package, 0, 100);
  43. String Json_Buffer = ",;Current,";
  44. Json_Buffer += String(Current);
  45. Json_Buffer += ";";
  46. Upload_Package[0] = 5;
  47. Upload_Package[1] = highByte(Json_Buffer.length());
  48. Upload_Package[2] = lowByte(Json_Buffer.length());
  49. for (int i = 0; i < Json_Buffer.length(); i++)
  50. Upload_Package[i + 3] = Json_Buffer.c_str()[i];
  51. //若要上传数据,则使用发布函数发布
  52. //$dp ONENT上传数据固定主题
  53. //Upload_Package 上传的数据数组
  54. //Json_Buffer.length() + 3 上传的数据长度,因为包含0,使用不能使用strlen计算长度
  55. client.publish("$dp", Upload_Package, Json_Buffer.length() + 3);
  56. }
  57. client.loop();
  58. }
  59. //WiFi初始化
  60. void WiFi_Init(void)
  61. {
  62. Serial.print("\r\n\r\nConnecting to ");
  63. Serial.print(WiFi_SSID);
  64. WiFi.mode(WIFI_STA);
  65. WiFi.begin(WiFi_SSID, WiFi_Password);
  66. while (WiFi.status() != WL_CONNECTED)
  67. {
  68. delay(500);
  69. Serial.print(" .");
  70. }
  71. //打印WiFi连接成功提示语和本机IP地址
  72. Serial.print("\r\nWiFi Connection Successful.\r\nIP address: ");
  73. Serial.println(WiFi.localIP());
  74. }
  75. //MQTT初始化
  76. void MQTT_Init(void)
  77. {
  78. client.setServer(mqttServer, mqttPort);
  79. client.setCallback(MQTT_Callback);
  80. }
  81. //收到信息时,会调用这个函数,传入参数含义如下
  82. //MQTT_Topic 收到的消息的主题
  83. //MQTT_Payload 收到的消息的内容
  84. //MQTT_Payload_Len 收到的消息的内容的长度
  85. void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len)
  86. {
  87. //此处只进行了打印,没有其余处理操作
  88. Serial.printf("Topic: %s[%d]:\r\n", MQTT_Topic, MQTT_Payload_Len);
  89. for (uint16_t i = 0; i < MQTT_Payload_Len; i++)
  90. {
  91. Serial.print(MQTT_Payload[i] < 0x10 ? " 0x0" : " 0x");
  92. Serial.print(MQTT_Payload[i]);
  93. }
  94. Serial.println();
  95. }
  96. void MQTT_Reconnection(void)
  97. {
  98. while (!client.connected())
  99. {
  100. Serial.println("MQTT Reconnection.");
  101. if (client.connect(Device_ID, Product_ID, Api_KEY))
  102. Serial.println("MQTT Connection Successful.");
  103. else
  104. {
  105. Serial.print("Connect Failed, Error Code = ");
  106. Serial.print(client.state());
  107. Serial.println(", Try again in 5 seconds.");
  108. delay(5000);
  109. }
  110. }
  111. }

如果报错缺少库,就在这里安装:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/699339
推荐阅读
相关标签
  

闽ICP备14008679号