|
本帖最后由 Chengo_97 于 2020-7-13 19:50 编辑
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <DHT.h>//依赖 DHT sensor lib
/* 连接您的WIFI SSID和密码 */
#define WIFI_SSID "dd-wrt"
#define WIFI_PASSWD "1234012340"
/* 设备的三元组信息*/
#define PRODUCT_KEY "xxxxxx"
#define DEVICE_NAME "xxxx"
#define DEVICE_SECRET "zxxxx"
#define REGION_ID "cn-shanghai"
/* 线上环境域名和端口号,不需要改 */
#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 "xxxxxxxxx"
#define MQTT_PASSWD "xxxxxxxxxxxxx"
#define ALINK_BODY_FORMAT "{\"id\":\"1247126\",\"version\":\"1.0\",\"method\":\"/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/"PRODUCT_KEY"/"DEVICE_NAME"/thing/event/property/post"
unsigned long lastMs = 0;
float RH, T, RH_sum, T_sum;
unsigned char count = 0;
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht( 2, DHT11); //定义DHT的引脚号
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
Serial.println((char *)payload);
}
void wifiInit()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWD); //连接WiFi
while (WiFi.status() != 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之后,连接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, "{\"Temp\":%0.1f}", T);
sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
Serial.println(jsonBuf);
boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
if (d) {
Serial.println("publish Temperature success");
} else {
Serial.println("publish Temperature fail");
}
sprintf(param, "{\"Humi\":%0.1f}", RH);
sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
Serial.println(jsonBuf);
d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
if (d) {
Serial.println("publish Humidity success");
} else {
Serial.println("publish Humidity fail");
}
}
void setup()
{
/* initialize serial for debugging */
Serial.begin(115200);
Serial.println("Demo Start");
wifiInit();
unsigned char i = 0;
dht.begin();
}
void loop()
{
delay(8000); //延时1000毫秒
RH = dht.readHumidity(); //读取湿度数据
T = dht.readTemperature();//读取温度数据
Serial.print("Humidity:"); //向串口打印 Humidity:
Serial.print(RH); //向串口打印湿度数据
Serial.print("%");
Serial.print(" Temperature:");
Serial.print(T); //向串口打印温度数据
Serial.println("C");
mqttCheckConnect();
mqttIntervalPost();
client.loop();
} |
|