简介

本文章使用NodeMCU(ESP8266)开发板和SHTC3温湿度传感器接入阿里云物联网(IoT)平台,并上传读取到的温湿度数据。每秒读取一次温湿度数据,每30秒上传一次30秒内的温湿度的平均值。可以自己修改一下程序使用其他温湿度传感器。本文程序使用Arduino IDE开发。

DHT11和DHT22温湿度传感器数据读取的程序:https://blog.zeruns.tech/archives/527.html
ESP8266开发环境搭建教程:https://blog.zeruns.tech/archives/526.html

SHTC3的SDA引脚接NodeMCU的D5引脚,SCL引脚接NodeMCU的D6引脚。

设置物联网平台

首先注册阿里云账号并开通物联网平台:https://www.aliyun.com/product/iot?source=5176.11533457&userCode=jdjc69nf&type=copy

然后打开物联网平台的控制台:https://iot.console.aliyun.com/product

接着按照图片提示创建产品

按照图片提示添加设备

程序源码

需先安装 PubSubClientArduinoJsonSparkFun SHTC3这三个库。

然后按照下面的教程修改PubSubClient.h

按照代码中文字提示修改参数即可。

#include <ESP8266WiFi.h>
/* 依赖 PubSubClient 2.4.0 */
#include <PubSubClient.h>
/* 依赖 ArduinoJson 5.13.4 */
#include <ArduinoJson.h>
#include <SparkFun_SHTC3.h>

/* 连接您的WIFI SSID和密码 */
#define WIFI_SSID         "路由器SSID"
#define WIFI_PASSWD       "密码"


/* 设备的三元组信息*/
#define PRODUCT_KEY       "设备PRODUCT_KEY"
#define DEVICE_NAME       "替换DEVICE_NAME"
#define DEVICE_SECRET     "替换DEVICE_SECRET"
#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         "ESP8266|securemode=3,timestamp=1234567890,signmethod=hmacsha1|"
// 算法工具: http://iot-face.oss-cn-shanghai.aliyuncs.com/tools.htm 进行加密生成password
// password教程 https://www.yuque.com/cloud-dev/iot-tech/mebm5g
#define MQTT_PASSWD       "参考上面password教程,算法工具生成,如果上面的教程看不懂就看文章下面"

#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 = 0;
float RH,T,RH_sum,T_sum;
unsigned char count=0;
WiFiClient espClient;
PubSubClient  client(espClient);
SHTC3 mySHTC3;

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, "{\"CurrentTemperature\":%f}",T_sum/count);
    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"); 
    }
    //https://blog.zeruns.tech   
    sprintf(param, "{\"CurrentHumidity\":%f}",RH_sum/count);
    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 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;
  }
}
//https://blog.zeruns.tech
void setup() 
{
    /* initialize serial for debugging */
    Serial.begin(115200);
    Serial.println("Demo Start");
    wifiInit();
    Wire.begin(D5,D6);           //初始化Wire(IIC)库
    unsigned char i=0;
    errorDecoder(mySHTC3.begin());
}


// the loop function runs over and over again forever
void loop()
{
  delay(1000);                  //延时1000毫秒
  SHTC3_Status_TypeDef result = mySHTC3.update();
  if(mySHTC3.lastStatus == SHTC3_Status_Nominal)   //判断SHTC3状态是否正常
  {
    RH = mySHTC3.toPercent();   //读取湿度数据                   
    T = mySHTC3.toDegC();       //读取温度数据
    RH_sum+=RH;
    T_sum+=T;
    count+=1;                
  }else{
    Serial.print("Update failed, error: ");
    errorDecoder(mySHTC3.lastStatus); //输出错误原因
    Serial.println();
  }
  Serial.print("Humidity:");  //向串口打印 Humidity:
  Serial.print(RH);           //向串口打印湿度数据
  Serial.print("%");
  Serial.print("  Temperature:");
  Serial.print(T);            //向串口打印温度数据
  Serial.println("C"); 
  Serial.println("https://blog.zeruns.tech");
  if (millis() - lastMs >= 30000)
  {
    lastMs = millis();
    mqttCheckConnect(); 

    /* 上报 */
    mqttIntervalPost();
    count=0;
    RH_sum=0;
    T_sum=0;
  }
  client.loop();
}

修改PubSubClient.h

找到PubSubClient.h文件,将 #define MQTT_MAX_PACKET_SIZE后的数字改为1024,将 #define MQTT_KEEPALIVE后的数字改为60

password生成教程

password生成工具:http://iot-face.oss-cn-shanghai.aliyuncs.com/tools.htm

假设你的设备三元组数据如下

设备三元组
ProductKey = a14Xib5kdYd
DeviceName = ESP8266
DeviceSecret = oLyaKqVxtRvjH284LdhqVgVUx1UPy6zq

建立MQTT连接时参数,这个信息是代码中的CLIENT_ID,可以自己设置
clientId = ESP8266
timestamp = 1234567890
signmethod = hmacsha1

然后根据这些信息生成password的content

clientIdESP8266deviceNameESP8266productKeya15ptdU1zhPtimestamp1234567890

明文输入content,秘钥输入deviceSecret,点击hmac-sha1就可以生成password

效果图

推荐阅读:


最后修改:2023 年 08 月 29 日
如果您觉得我的文章有帮助,请随意赞赏,赞赏有助于激发博主的热情,感谢!