ESP8266 连接阿里云物联网平台
项目代码
1 #include <ESP8266WiFi.h> 2 #include <PubSubClient.h> 3 #include "DHT.h" 4 5 6 /* 配置WIFI 7 */ 8 #define WIFI_SSID "*****" //WIFI名称 9 #define WIFI_PASSWD "*****" //WIFI 密码 10 11 // GPIO 5 D1 12 #define LED 5 13 14 WiFiClient espClient; 15 PubSubClient client(espClient); 16 // MQTT Broker 17 18 /* 设备证书信息*/ 19 #define PRODUCT_KEY "*****" 20 #define DEVICE_NAME "*****" 21 #define DEVICE_SECRET "*****" 22 #define REGION_ID "cn-shanghai" 23 24 // 25 #define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post" 26 /* 线上环境域名和端口号,不需要改 */ 27 #define MQTT_SERVER "*****" 28 #define MQTT_PORT 1883 29 #define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY 30 31 #define CLIENT_ID PRODUCT_KEY "." DEVICE_NAME "*****" 32 #define MQTT_PASSWD "*****" 33 #define ALINK_BODY_FORMAT "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":{\"CurrentHumidity\":%f,\"CurrentTemperature\":%f}}" 34 35 unsigned long lastMs = 0; 36 37 #define DHTPIN 2 //与ESP8266的D4相连 38 #define DHTTYPE DHT11 // DHT 11 39 40 DHT dht(DHTPIN, DHTTYPE); 41 42 43 /***************************************************** 44 * 函数名称:connectMQTT() 45 * 函数说明:连接MQTT 46 * 参数说明:无 47 ******************************************************/ 48 void connectMQTT(){ 49 client.setServer(MQTT_SERVER, MQTT_PORT); 50 client.setCallback(callback); 51 while (!client.connected()) { 52 Serial.println("Connecting to public emqx mqtt broker....."); 53 if (client.connect(CLIENT_ID,MQTT_USRNAME,MQTT_PASSWD)) { 54 Serial.println("Public emqx mqtt broker connected"); 55 } else { 56 Serial.print("failed with state "); 57 Serial.println(client.state()); 58 delay(2000); 59 } 60 } 61 client.subscribe(ALINK_TOPIC_PROP_POST); 62 } 63 64 65 void callback(char *topic, byte *payload, unsigned int length) { 66 Serial.print("Message arrived in topic: "); 67 Serial.println(topic); 68 Serial.print("Message:"); 69 String message; 70 for (int i = 0; i < length; i++) { 71 message = message + (char) payload[i]; // convert *byte to string 72 } 73 Serial.print(message); 74 if (message == "on") { // LED on 75 digitalWrite(LED, LOW); 76 digitalWrite(LED_BUILTIN, LOW); 77 } 78 if (message == "off") {// LED off 79 digitalWrite(LED, HIGH); 80 digitalWrite(LED_BUILTIN, HIGH); 81 } 82 Serial.println(); 83 Serial.println("-----------------------"); 84 } 85 86 void setup(void) { 87 Serial.begin(115200); 88 dht.begin(); 89 90 WiFi.mode(WIFI_STA); 91 WiFi.begin(WIFI_SSID, WIFI_PASSWD); 92 Serial.print("AutoConfig Waiting......"); 93 while (WiFi.status() != WL_CONNECTED) 94 { 95 delay(1000); 96 Serial.println("WiFi not Connect"); 97 } 98 Serial.println("Connected to AP"); 99 Serial.println("IP address: "); 100 Serial.println(WiFi.localIP()); 101 connectMQTT(); 102 } 103 104 void loop(void) { 105 106 client.loop(); 107 // 读取温度或湿度大约需要250毫秒! 108 // 传感器读数也可能长达2秒“旧”(这是一个非常慢的传感器) 109 float h = dht.readHumidity(); 110 // 读取温度为摄氏温度(默认值) 111 float t = dht.readTemperature(); 112 // 将温度读取为华氏度(isFahrenheit = true) 113 float f = dht.readTemperature(true); 114 Serial.printf("Humidity: %f %\n",h);//湿度 115 Serial.printf("Temperature: %f℃, %f℉ \n", t, f);//温度 116 117 if (millis() - lastMs >= 5000) 118 { 119 lastMs = millis(); 120 121 char msg[200]; 122 snprintf (msg, 200, ALINK_BODY_FORMAT, h,t); 123 client.publish(ALINK_TOPIC_PROP_POST,msg); 124 } 125 126 delay(2000); 127 }
如果连接失败请修改 (MQTT Connect err:-1)
路径:X:\Arduino\libraries\PubSubClient\src\PubSubClient.h
// MQTT_MAX_PACKET_SIZE : Maximum packet size
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 1024 //由原来的128改为1024
#endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 60 //由原来的15改为60
#endif