先感谢:
太极创客DHT11 温度湿度传感器 ESP8266 开发资料
ESP8266接入阿里云物联网平台上传温湿度数据
再上代码
#include <ESP8266WiFi.h>
/* 依赖 PubSubClient 2.4.0 */
#include <PubSubClient.h>
/* 依赖 ArduinoJson 5.13.4 */
#include <ArduinoJson.h>
#include <SparkFun_SHTC3.h>
#include <dht11.h>
dht11 DHT11;//定义传感器类型
#define DHT11PIN 2//定义传感器连接引脚。此处的PIN2在NodeMcu8266开发板上对应的引脚是D4
/* 连接您的WIFI SSID和密码 */
#define WIFI_SSID "wifi name"
#define WIFI_PASSWD "wifi password"/* 设备的三元组信息*/
#define PRODUCT_KEY "阿里平台获取"
#define DEVICE_NAME "阿里平台获取"
#define DEVICE_SECRET "阿里平台获取"
#define REGION_ID "cn-阿里平台获取"/* 线上环境域名和端口号&#xff0c;不需要改 */
#define MQTT_SERVER PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
#define MQTT_PORT 1883
#define MQTT_USRNAME DEVICE_NAME "&" PRODUCT_KEY#define CLIENT_ID "wushuo123|securemode&#61;3,signmethod&#61;hmacsha1,timestamp&#61;132323232|"
// 算法工具: http://iot-face.oss-cn-shanghai.aliyuncs.com/tools.htm 进行加密生成password
// password教程 https://www.yuque.com/cloud-dev/iot-tech/mebm5g
#define MQTT_PASSWD "算法生成的密码"#define ALINK_BODY_FORMAT "{\"id\":\"ESP8266\",\"version\":\"1.0\",\"method\":\"thing.event.property.post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"unsigned long lastMs &#61; 0;
float RH,T,RH_sum,T_sum;
unsigned char count&#61;0;
WiFiClient espClient;
PubSubClient client(espClient);
SHTC3 mySHTC3;/********************###子函数###********************/
double Fahrenheit(double celsius)
{return 1.8 * celsius &#43; 32; //摄氏温度度转化为华氏温度
}double Kelvin(double celsius)
{return celsius &#43; 273.15; //摄氏温度转化为开氏温度
}void callback(char *topic, byte *payload, unsigned int length)
{Serial.print("Message arrived [");Serial.print(topic);Serial.print("] ");payload[length] &#61; &#39;\0&#39;;Serial.println((char *)payload);}void wifiInit()
{WiFi.mode(WIFI_STA);WiFi.begin(WIFI_SSID, WIFI_PASSWD); //连接WiFiwhile (WiFi.status() !&#61; WL_CONNECTED){delay(1000);Serial.println("WiFi not Connect");}Serial.println("Connected to AP");Serial.println("IP address: ");Serial.println(WiFi.localIP()); Serial.print("espClient [");client.setServer(MQTT_SERVER, MQTT_PORT); /* 连接WiFi之后&#xff0c;连接MQTT服务器 */client.setCallback(callback);
}void mqttCheckConnect()
{while (!client.connected()){Serial.println("Connecting to MQTT Server ...");if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD)){Serial.println("MQTT Connected!");}else{Serial.print("MQTT Connect err:");Serial.println(client.state());delay(5000);}}
}void mqttIntervalPost()
{char param[32];char jsonBuf[128];sprintf(param, "{\"CurrentTemperature\":%f}",T_sum/count);sprintf(jsonBuf, ALINK_BODY_FORMAT, param);Serial.println(jsonBuf);boolean d &#61; client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);if(d){Serial.println("publish Temperature success"); }else{Serial.println("publish Temperature fail"); }sprintf(param, "{\"CurrentHumidity\":%f}",RH_sum/count);sprintf(jsonBuf, ALINK_BODY_FORMAT, param);Serial.println(jsonBuf);d &#61; client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);if(d){Serial.println("publish Humidity success"); }else{Serial.println("publish Humidity fail"); }
}void errorDecoder(SHTC3_Status_TypeDef message) // The errorDecoder function prints "SHTC3_Status_TypeDef" resultsin a human-friendly way
{switch(message){case SHTC3_Status_Nominal : Serial.print("Nominal"); break;case SHTC3_Status_Error : Serial.print("Error"); break;case SHTC3_Status_CRC_Fail : Serial.print("CRC Fail"); break;default : Serial.print("Unknown return code"); break;}
}void setup()
{/* initialize serial for debugging */Serial.begin(115200);Serial.println("\n");Serial.println("esp8266读取DHT11传感器数值 ");Serial.println("串口会分别打印当前湿度&#xff08;%&#xff09;&#xff0c;当前摄氏度温度&#xff0c;当前华氏度温度和当前开式温度 ");Serial.print("DHT11库文件版本: ");Serial.println(DHT11LIB_VERSION);Serial.println();wifiInit();Wire.begin(D5,D6); //初始化Wire&#xff08;IIC&#xff09;库unsigned char i&#61;0;errorDecoder(mySHTC3.begin());
}// the loop function runs over and over again forever
void loop()
{RH&#61;DHT11.humidity;T&#61;DHT11.temperature;RH_sum&#43;&#61;RH;T_sum&#43;&#61;T;count&#43;&#61;1; Serial.println(RH&#43;T&#43;RH_sum&#43;T_sum);DHT11.read(DHT11PIN); //更新传感器所有信息Serial.print("当前湿度 (%): ");Serial.println((float)DHT11.humidity, 2);Serial.print("当前温度 (℃): ");Serial.println((float)DHT11.temperature, 2);Serial.print("当前温度 (℉): ");Serial.println(Fahrenheit(DHT11.temperature), 2);Serial.print("当前温度 (K): ");Serial.println(Kelvin(DHT11.temperature), 2);delay(2000);//每两秒打印一次if (millis() - lastMs >&#61; 1000){lastMs &#61; millis();mqttCheckConnect(); /* 上报 */mqttIntervalPost();count&#61;0;RH_sum&#61;0;T_sum&#61;0;}client.loop();
}
今天买的板子刚到货&#xff0c;之前没了解过物联网&#xff0c;就百度着简单做了做&#xff0c;代码大部分为拼凑&#xff0c;还在研究学习。期望更高的提升~
底下是效果图。