赞
踩
Arduino IDE 代码:
- #include <ESP8266WiFi.h> //WiFi驱动库
- #include <PubSubClient.h> //提供MQTT协议
-
- //设置OneNet参数
- const char* mqttServer = "183.230.40.39";//mqtt服务器
- const uint16_t mqttPort = 6002; //端口号
- const char *Device_ID = "1060658288"; //设备ID
- const char *Product_ID = "584149"; //产品ID
- const char *Api_KEY = "LiVJexVOw4FRbSzo=WYozBpbWvM="; //鉴权信息
- //设置WiFi参数
- const char *WiFi_SSID = ""; //WiFi名称
- const char *WiFi_Password = ""; //WiFi密码
-
- float Current = 0;
- uint8_t Upload_Package[100];
- uint32_t Last_Updata_Time = 0;
- //创建对象
- WiFiClient espClient;
- PubSubClient client(espClient);
-
- //声明下函数
- void WiFi_Init(void);
- void MQTT_Init(void);
- void MQTT_Reconnection(void);
- void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len);
-
- void setup()//setup只执行一次,联网放这里面就行
- {
- Serial.begin(115200);//串口初始化
- WiFi_Init(); //WiFi初始化
- MQTT_Init(); //MQTT初始化
- }
-
- void loop()//loop相当于其他单片机的while(1)
- {
- //判断MQTT连接了没,没的话重新连接
- if (!client.connected())
- MQTT_Reconnection();
- if (millis() - Last_Updata_Time >= 3000)//3s发送一次数据
- {
- Last_Updata_Time = millis();
- float ADC_Value = analogRead(A0);
- Current = 25;
- Serial.print("Current: ");
- Serial.print(Current);
- Serial.println(" A\r\n");
- memset(Upload_Package, 0, 100);
- String Json_Buffer = ",;Current,";
- Json_Buffer += String(Current);
- Json_Buffer += ";";
- Upload_Package[0] = 5;
- Upload_Package[1] = highByte(Json_Buffer.length());
- Upload_Package[2] = lowByte(Json_Buffer.length());
- for (int i = 0; i < Json_Buffer.length(); i++)
- Upload_Package[i + 3] = Json_Buffer.c_str()[i];
-
- //若要上传数据,则使用发布函数发布
- //$dp ONENT上传数据固定主题
- //Upload_Package 上传的数据数组
- //Json_Buffer.length() + 3 上传的数据长度,因为包含0,使用不能使用strlen计算长度
- client.publish("$dp", Upload_Package, Json_Buffer.length() + 3);
- }
- client.loop();
- }
-
- //WiFi初始化
- void WiFi_Init(void)
- {
- Serial.print("\r\n\r\nConnecting to ");
- Serial.print(WiFi_SSID);
- WiFi.mode(WIFI_STA);
- WiFi.begin(WiFi_SSID, WiFi_Password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(" .");
- }
- //打印WiFi连接成功提示语和本机IP地址
- Serial.print("\r\nWiFi Connection Successful.\r\nIP address: ");
- Serial.println(WiFi.localIP());
- }
-
- //MQTT初始化
- void MQTT_Init(void)
- {
- client.setServer(mqttServer, mqttPort);
- client.setCallback(MQTT_Callback);
- }
-
- //收到信息时,会调用这个函数,传入参数含义如下
- //MQTT_Topic 收到的消息的主题
- //MQTT_Payload 收到的消息的内容
- //MQTT_Payload_Len 收到的消息的内容的长度
- void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len)
- {
- //此处只进行了打印,没有其余处理操作
- Serial.printf("Topic: %s[%d]:\r\n", MQTT_Topic, MQTT_Payload_Len);
- for (uint16_t i = 0; i < MQTT_Payload_Len; i++)
- {
- Serial.print(MQTT_Payload[i] < 0x10 ? " 0x0" : " 0x");
- Serial.print(MQTT_Payload[i]);
- }
- Serial.println();
- }
-
- void MQTT_Reconnection(void)
- {
- while (!client.connected())
- {
- Serial.println("MQTT Reconnection.");
- if (client.connect(Device_ID, Product_ID, Api_KEY))
- Serial.println("MQTT Connection Successful.");
- else
- {
- Serial.print("Connect Failed, Error Code = ");
- Serial.print(client.state());
- Serial.println(", Try again in 5 seconds.");
- delay(5000);
- }
- }
- }
如果报错缺少库,就在这里安装:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。