ESP8266接入阿里云物联网平台上传温湿度数据-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4196|回复: 1

ESP8266接入阿里云物联网平台上传温湿度数据

[复制链接]
发表于 2020-5-19 13:25 | 显示全部楼层 |阅读模式

[md]## 简介

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

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

SHTC3的SDA引脚接NodeMCU的D5引脚,SCL引脚接NodeMCU的D6引脚。
![](https://tc.zeruns.tech/images/2020/05/15/1.jpg)

## 设置物联网平台

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

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

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

![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_16-25-54.png)
![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_16-28-46.png)

按照图片提示添加设备

![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_16-32-45.png)

## 程序源码

需先安装`PubSubClient`、`ArduinoJson`、`SparkFun SHTC3`这三个库。

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

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

```c
#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

![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_16-49-36.png)

## 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

![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_19-23-11.png)

## 效果图

![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_19-28-53.png)
![](https://tc.zeruns.tech/images/2020/05/15/2020-05-15_19-29-28.png)

## 推荐阅读:

- **高性价比和便宜的VPS/云服务器推荐:**[https://blog.zeruns.tech/archives/383.html](https://blog.zeruns.tech/archives/383.html)
- 搭建内网穿透服务器,带Web面板:https://blog.zeruns.tech/archives/397.html
- 使用Cloudreve自建网盘:https://blog.zeruns.tech/archives/515.html
- 怎样搭建个人博客:https://blog.zeruns.tech/archives/218.html
- 学生优惠权益大全:https://blog.zeruns.tech/archives/321.html
- 分享个能赚钱的小游戏给大家:[https://blog.zeruns.tech/archives/472.html](https://blog.zeruns.tech/archives/472.html)[/md]
发表于 2022-1-27 18:01 | 显示全部楼层
老兄,如何从云获取数据呀,就像你最后的图一样,获取数据可以进行分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:26 , Processed in 0.172736 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表