物联网技术
直播中

卢松涛

12年用户 419经验值
擅长:嵌入式技术
私信 关注
[资料]

【DFRobot TinkerNode NB-IoT 物联网开发板免费试用】+第2个项目

接上一个项目
【DFRobot tinkerNode NB-IoT 物联网开发板免费试用】+第一个项目
在阿里云平台上IoT初学者开发套件中
案例4 温湿度监测
在Arduino IDE中
点击文件->示例->DFRobot_IoTAliyun->Aliyun->SmartDHT11,如下图所示。


增加
#define BEDROOD_LIGHT  D4
上次项目的灯控制。
与上次一样在阿里云平台上创建设备,根据下载Excel文件更改相应配置

选择正确板和com端口,验证发现无"DFRobot_DHT11.h"文件,

在https://wiki.dfrobot.com.cn/_SKU_DFR0067_DHT11%E6%95%B0%E5%AD%97%E6%B8%A9%E6%B9%BF%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8V2网上可下载201709141149593byvtx.zip文件中包含DHT11文件包
把解包文件拷贝到ArduinolibrariesDHT11文件夹中
变更后的配置:
#include
#include
#include
#include "DFRobot_Iot.h"
#include "dht11.h"

#define DHT11_PIN  D2
#define BEDROOD_LIGHT  D4

/*Set WIFI name and password*/
const char * WIFI_SSID     = "911";//"WIFI_SSID";
const char * WIFI_PASSWORD = "31698639";//"WIFI_PASSWORD";

/*Configure device certificate information*/
String ProductKey = "a1DP0w8Z8i";//you_Product_Key";
String ClientId = "12345";
String DeviceName = "rxgcbeEhora43tMXaFMR";//you_Device_Name";
String DeviceSecret = "e7c95a299b051eb6675337e61d416e86";//you_Device_Secret";

/*Configure the domain name and port number*/
String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
uint16_t PORT = 1883;

/*Product identifier that needs to be operated*/
/*需要操作的产品标识符(温度和湿度两个标识符)*/
String TempIdentifier = "Temperature";
String HumiIdentifier = "Humidity";
String ledIdentifier = "LED_S";

/*TOPIC that need to be published and subscribed*/
const char * subTopic = "/sys/a1DP0w8Z8i/rxgcbeEhora43tMXaFMR/thing/service/property/set";//you_sub_Topic";//****set
const char * pubTopic = "/sys/a1DP0w8Z8i/rxgcbeEhora43tMXaFMR/thing/event/property/post";//******post
callback增加原来灯控制部分
//增加原来的灯控制set部分

StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject((const char *)payload);
  if(!root.success()){
    Serial.println("parseObject() failed");
    return;
  }
  uint8_t SocketStatus1,ledStatus ;//定义局部变量
  const char* ledtval =  root["params"][ledIdentifier];//判断Identifier是否存在,不存在返回NULL
if( ledtval != NULL )
{
       // Serial.println("------------");
       ledStatus = root["params"][ledIdentifier];
      Serial.print("ledStatus=");
      Serial.print(ledStatus);
      if(ledStatus == 1){
        /*打开灯*/
        openLight();
      }else{
        /*关掉灯*/
        closeLight();
      }

  
}}
String tempMsegled = "{"id":"+ClientId+","params":{""+ledIdentifier+"":"+(String)ledStatus+"},"method":"thing.event.property.post"}";

  char ledsendMseg[tempMsegled.length()];

  strcpy(ledsendMseg,tempMsegled.c_str());
  
  client.publish(pubTopic,ledsendMseg);
setup中增加
    pinMode(BEDROOD_LIGHT,OUTPUT);
     pinMode(DHT11_PIN,INPUT);
loop()中增加
    if(DHT.temperature > 31){
    openLight();
   
  }else
      closeLight();
作为超过31度指示并执行的程序。
将原来程序半分钟一次变为1分钟(由60变为120)
程序如下:
DFRobot_Iot myIot;
WiFiClient espClient;
PubSubClient client(espClient);
dht11 DHT;//DFRobot_DHT11 DHT;

static void openLight(){//led控制
  digitalWrite(BEDROOD_LIGHT, HIGH);
}

static void closeLight(){//led控制
  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();


//增加原来的灯控制set部分

StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject((const char *)payload);
  if(!root.success()){
    Serial.println("parseObject() failed");
    return;
  }
  uint8_t SocketStatus1,ledStatus ;//定义局部变量
  const char* ledtval =  root["params"][ledIdentifier];//判断Identifier是否存在,不存在返回NULL
if( ledtval != NULL )
{
       // Serial.println("------------");
       ledStatus = root["params"][ledIdentifier];
      Serial.print("ledStatus=");
      Serial.print(ledStatus);
      if(ledStatus == 1){
        /*打开灯*/
        openLight();
      }else{
        /*关掉灯*/
        closeLight();
      }

  
}
String tempMsegled = "{"id":"+ClientId+","params":{""+ledIdentifier+"":"+(String)ledStatus+"},"method":"thing.event.property.post"}";

  char ledsendMseg[tempMsegled.length()];

  strcpy(ledsendMseg,tempMsegled.c_str());
  
  client.publish(pubTopic,ledsendMseg);
}
   

void ConnectCloud(){
  while(!client.connected()){
    Serial.print("Attempting MQTT connection...");
    /*A device connected to the cloud platform based on an automatically calculated username and password*/
    if(client.connect(myIot._clientId,myIot._username,myIot._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);
    pinMode(BEDROOD_LIGHT,OUTPUT);
     pinMode(DHT11_PIN,INPUT);
       closeLight();
  /*Connect to WIFI*/
  connectWiFi();
  
  /*Initialize the configuration of Aliyun*/
  /*初始化Alinyun的配置,可自动计算用户名和密码*/
  myIot.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret);
  
  client.setServer(myIot._mqttServer,PORT);
  
  /*Set the callback function to execute the callback function when receiving the subscription information*/
    /*设置回调函数,当收到订阅信息时会执行回调函数*/
  client.setCallback(callback);
  
  /*Connect to the cloud platform*/
    /*连接到Aliyun*/
  ConnectCloud();
}

uint8_t tempTime = 0;
void loop(){
  if(!client.connected()){
    ConnectCloud();
  }
  /*One minute publish  temperature and humidity information*/
    /*一分钟上报次温湿度信息*/
  if(tempTime > 120){
    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{
    tempTime++;
    delay(500);
  }
    if(DHT.temperature > 31){
    openLight();
   
  }else
      closeLight();

  client.loop();
}


选择正确板和com端口,验证后上传
运行下,在MQTT连接完成后,在https://studio.iot.aliyun.com/可以在设备管理界面看到设备显示在线,


查看
后进入点
在线调试
灯没问题后
阿里云平台物联网平台web编辑,编辑案例4 温湿度监测
设置好后温度显示正常,但湿度无变化为0,设备正常上传数据
阿里云平台服务不太好
但是5天后在预览时就都正常了,不知为什么。



附件: 您需要登录才可以下载或查看附件。没有帐号?注册

更多回帖

×
20
完善资料,
赚取积分