DFSyBK4KmYN 发表于 2019-5-13 00:52:41

IoT套件测评:【二】温湿度传感器模块测试

本帖最后由 DFSyBK4KmYN 于 2019-5-13 00:56 编辑

温湿度传感器测试:
按照示例教程进行配置,温湿度传感器按照教程配置也很顺利,产品创建步骤可以用之前led模块测评是创建的产品上添加属性来代替。这边做了一点小偷懒,把灯光操作和温湿度传感器配置在同一个产品和app中进行控制。另外代码中的传感器上报是半分钟一次,我将其改为了5s一次。本来希望能将两段代码合并一下,做个根据温湿度数据进行报警的小示例,但是在连接温湿度传感器之后,灯的控制似乎也出现了问题;串口没法进行输出的情况下也不是很好进行调试,因此暂时还未成功。(在没有上云的情况下本地调试,灯也没能亮起。由于开发经验较少,暂时还没能搞懂是哪里出了问题….)
代码如下:#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "DFRobot_Aliyun.h"
#include "DFRobot_DHT11.h"

#define DHT11_PIND2
#define BEDROOD_LIGHTD7

/*配置WIFI名和密码*/
const char * WIFI_SSID   = "11111111";
const char * WIFI_PASSWORD = "11111111";

/*配置设备证书信息*/
String ProductKey = "a168bbrZwdr";
String ClientId = "12345";/*自定义ID*/
String DeviceName = "nnEZ5evyhV39V6TnnfxF";
String DeviceSecret = "sIlZ7d0fSqK3XrLDdpF3ffgC44Wvlu39";

/*配置域名和端口号*/
String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
uint16_t PORT = 1883;

/*需要操作的产品标识符(温度和湿度两个标识符)*/
String TempIdentifier = "IndoorTemperature";
String HumiIdentifier = "Humidity";
String LightIdentifier = "light_stat";

/*需要上报和订阅的两个TOPIC*/
const char * subTopic = "/sys/a168bbrZwdr/nnEZ5evyhV39V6TnnfxF/thing/service/property/set";//****set
const char * pubTopic = "/sys/a168bbrZwdr/nnEZ5evyhV39V6TnnfxF/thing/event/property/post";//******post

DFRobot_Aliyun myAliyun;
WiFiClient espClient;
PubSubClient client(espClient);
DFRobot_DHT11 DHT;

static void openLight(){
digitalWrite(BEDROOD_LIGHT, HIGH);
}

static void closeLight(){
digitalWrite(BEDROOD_LIGHT, LOW);
}

void connectWiFi(){
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP Adderss: ");
Serial.println(WiFi.localIP());
}

void callback(char * topic, byte * payload, unsigned int len){
Serial.print("Recevice [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < len; i++){
    Serial.print((char)payload);
}
Serial.println();
}

void ConnectAliyun(){
while(!client.connected()){
    Serial.print("Attempting MQTT connection...");
    /*根据自动计算的用户名和密码连接到Alinyun的设备,不需要更改*/
    if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){
      Serial.println("connected");
      client.subscribe(subTopic);
    }else{
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
}
}

void setup(){
Serial.begin(115200);

/*连接WIFI*/
connectWiFi();

/*初始化Alinyun的配置,可自动计算用户名和密码*/
myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret);

client.setServer(myAliyun.mqtt_server,PORT);

/*设置回调函数,当收到订阅信息时会执行回调函数*/
client.setCallback(callback);

/*连接到Aliyun*/
ConnectAliyun();

    /*开机先关灯*/
closeLight();

/*上报关灯信息*/
client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+LightIdentifier+"\":0},\"method\":\"thing.event.property.post\"}").c_str());
}

uint8_t tempTime = 0;
void loop(){
if(!client.connected()){
    ConnectAliyun();
}
if(tempTime > 3){
    tempTime = 0;
    DHT.read(DHT11_PIN);
    Serial.print("DHT.temperature=");
    Serial.println(DHT.temperature);
    Serial.print("DHT.humidity");
    Serial.println(DHT.humidity);
    client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+TempIdentifier+"\":"+DHT.temperature+",\""+HumiIdentifier+"\":"+DHT.humidity+"},\"method\":\"thing.event.property.post\"}").c_str());
}else{
    //digitalWrite(LED_BUILTIN, HIGH);
    if(DHT.humidity>50 || DHT.temperature>30){
      int i=0;
      for(i=0;i<2;++i){
      openLight();   
      delay(400);   
      closeLight();
      delay(400);            
      }   
    }
   else{
   openLight();
    delay(1000);
    //digitalWrite(LED_BUILTIN, LOW);
   closeLight();
    delay(1000);
}   
tempTime++;
client.loop();
}
}
套件来源:DFRobot创客商城产品链接:https://www.dfrobot.com.cn/goods-1896.html
页: [1]
查看完整版本: IoT套件测评:【二】温湿度传感器模块测试