DHT11 module monitors the temperature and humidity in the house to get the data and the micro-channel public number

Foreword

Before you are interested in the knowledge of things, very busy recently due to the epidemic at home, but also saw a number of related videos will attempt to try

ready

hardware

ESP-01s and with their writer firmware, DHT11 temperature and humidity sensor

Development Platform

Arduino IDE, dweet, micro-channel public number Development Platform

Development language

c++ 、 php

c ++ library needed

DHT.h, dweetESP8266.h need to download and add the following in the press github
Here Insert Picture Description

Process

First, the idea is to read the temperature and humidity information DHT -> to the internet dweet -> set the timing server task, timing of the data written into the access dweet then log -> Read log data is returned when the number of public query
(more than the middle step timed task, because dweet is foreign Web sites, access speed is too slow, the public response number over time on the error ...)

Part of the relevant code

Code does not give the whole, only to, in fact, very simple part, along the lines it is easy to achieve

c++:
#include "DHT.h"
#include "dweetESP8266.h"
 
char ssid[] = "";//wifi 名
char pswd[] = "";//wifi 密码

#define DHTPIN 2      //定义DHT11模块连接管脚io2
 
#define DHTTYPE DHT11   // 使用DHT 11温度湿度模块 

#define THIG_NAME "xxx" //指定dweet的标题

dweet dweetClient; //定义dweet
DHT dht(DHTPIN, DHTTYPE);    //定义dht


void setup() {
	Serial.begin(115200);
	dht.begin();
    dweetClient.wifiConnection(ssid, pswd);
}

void loop() {
	float h = dht.readHumidity();
	float t = dht.readTemperature();
	dweetClient.wifiConnection(ssid, pswd);
	dweetClient.add("dht", String(h) + "," + String(t));
	dweetClient.sendAll(THIG_NAME);
	delay(3000);
}
Set crond cron job:
*/2 * * * * /usr/bin/curl -o /home/cron/iot/dht.txt https://dweet.io/get/latest/dweet/for/xxx

The results provided here every two minutes by the acquired write curl /home/cron/iot/dht.txtin

php:
// 当用户向公众号发送关键字 '家里温湿度' 时,读取日志并返回数据的逻辑
if ($keyword == '家里温湿度') {
   $path = '/home/cron/iot/dht.txt';
         $oldData = json_decode(file_get_contents($path), true);

         if(empty($oldData) || $oldData['this'] == "failed") {
             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, "设备可能存在异常");
             echo $resultStr;
         }

         $str = explode(",", $oldData['with'][0]['content']['dht']);
         $humidity = $str[0];
         $temperature = $str[1];
         $contenStr = "湿度:" . $humidity . "%\r\n" . "温度:" . $temperature . "℃\r\n";
         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contenStr);
         echo $resultStr;
     }

Stepped pit

1. not able to find ArduinoJson library (resolved) at compile time

Solution:
dweet this library with the ArduinoJson 5. * version of the library, so you need to download 5. * version, because the function dweet with above where the call is 5. * version. . .
download link

2. The device can not continue to send data to the dweet (Resolved)

Since the server is abroad, from time to time when post data on overtime, after Posting your variables to block the
Here Insert Picture Description
solution:
Modify the beginning downloaded dweetESP8266library file code
dweetESP8266.cpp:

165   while(!_client.available());
166   while (_client.available()){
168    	char c = _client.read();
169     Serial.write(c);
   }

Delete this section of code, and returns the result does not read on the line. Because before the card has been returned because waiting to read the results, now deleted, the information you threw and ran, so it will not clog live

Published 48 original articles · won praise 56 · views 20000 +

Guess you like

Origin blog.csdn.net/zhetmdoubeizhanyong/article/details/104305257