Mittwoch, 13. Januar 2016

Home Measurement with Raspberry Pi, ESP8266 and DHT22

I have a Raspberry Pi A+ sitting in my kitchen window for over a year already, using it as webcam (first idea was some timelapse over a year with all seasons. Too much movement in the first time, so the whole-year overview takes a little longer.) Yesterday I remembered that I also have a few DHT22 temperature and humidity sensors flying around.


So I decided to hook one up to the RasPi and see what it would spit out. Just wired it up to GND, 3.3V and to GPIO17, modified a Python script which uses the AdaFruit DHT library which I found on the net, and it spits out what I need. Here is a guide from them for a starting point.

This data needs to be visualized somehow. I remembered using MRTG 15 years ago. Good news is that MRTG is still maintained and easily available. Setting it up and adding the sensor to /etc/mrtg.cfg:

### Global Config Options
WorkDir: /var/www/mrtg
Options[_]: growright, nobanner
EnableIPv6: no
WriteExpires: Yes

######################################################################
# System: CamPI DHT22
# Description: Temperature + Humidity in Kitchen Windows
######################################################################

Target[CamPi-dht]: `/usr/local/bin/dht22.py`
Title[CamPi-dht]: Temperature and Humidity in Kitchen Window
MaxBytes[CamPi-dht]: 1000
AbsMax[CamPi-dht]: 1000
WithPeak[CamPi-dht]: dwmy
Options[CamPi-dht]: gauge, growright, nopercent, pngdate
YTicsFactor[CamPi-dht]: 0.1
Factor[CamPi-dht]: 0.1
#kMG[CamPi-dht]: ,k
YLegend[CamPi-dht]: °C / %
ShortLegend[CamPi-dht]: °C/%
Legend1[CamPi-dht]: Temperature in °C
Legend2[CamPi-dht]: Relative Humidity in %
LegendI[CamPi-dht]: Temperature °C
LegendO[CamPi-dht]: Rel. Humidity %
PageTop[CamPi-dht]: <h1>Temperature and Humidity in Kitchen Window</h1>

To get the fractions of temperature and humidity, the YTicFactor and Factor options are important.

Start the index building, let mrtg run once, and you get the first empty graphs. It takes about 10 minutes so the grpahs get filled with valid data. Remember to put mrtg to the list of cronjobs so it runs every 5 minutes.

Today I found out about ESP8266 being able to directly read out a DHT22 and offer the data as web server. I needed to flash the latest NodeMCU firmware - a simple guide is available here. You should download the latest development build of NodeMCU and flash that. Just enter the path and the name of the downloaded file under "configuration" (by hitting the gear symbol you can browse the file system for that) of the esp8266-flash.exe tool, address 0x00000 is correct. Hint: Solder that CE_PD-pin on the ESP-01-boards directly to Vcc. For flashing, you need to connect GPIO0 to GND. In all cases, an eletrolytic capacitor of 1000µF between GND and Vcc helps keeping the board stable.
This firmware offers plenty of functions which you can use with LUA, a basic programming language. ESPlorer makes it extremely easy to write and test a program. My version of init.lua looks like this (adopt SSID and PW to your Wifi).

-- setup Wifi
wifi.setmode(wifi.STATION)
wifi.setphymode(wifi.PHYMODE_N)
wifi.sta.config("SSID", "PASSWORD")
wifi.sta.connect()

--start server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive", function(client,request)

-- read DHT22 on GPIO2
    pin = 4
    status,temp,humi = dht.readxx(pin)
    h=10*humi
    t=10*temp
    if( status == dht.OK ) then
      buf = t.."\n"..h.."\n"
    end
-- reading DHT done, now spit it out
    client:send(buf)
    client:close()
    buf = nil
    t = nil
    h = nil
    status = nil
    temp = nil
    humi = nil
    pin = nil
    collectgarbage()
  end)
end)

The temperature and humidity are now accessable via webbrowser. On the Pi, create a bash script, for example /usr/local/bin/nodemcu1.sh (make it executable via "sudo chmod a+x /usr/local/bin/nodemcu1.sh"):










wget -q -O nodemcu1.txt http://nodemcu1/
cat nodemcu1.txt
echo 0
echo NodeMCU1 Living Room
rm nodemcu1.txt

Add an entry to your /etc/mrtg.cfg like this (take the rest from the example above and adopt the device name in the brackets):

######################################################################
# System: NodeMCU1 DHT22
# Description: Temperature + Humidity in Living Room
######################################################################

Target[NodeMCU1-dht]: `/usr/local/bin/nodemcu1.sh`
Title[NodeMCU1-dht]: Temperature and Humidity in Living Room

Run the mrtg indexmaker and after that mrtg again.

Now you have a mobile temperature and humidity sensor which you can put anywhere in your Wifi range.
You only need a 3.3V power supply - either via USB and step-down, or battery and step-up. There are rumors that with connecting to pin 1 of the mcu you can enable a deep sleep mode, but as the ESP8266 is working as web server it should be always-on. My USB-Meter shows that the current drawn is always jumping around between 0-200mA. This is not bad for a web server!

Update 21.01.2016: Two more DHT-22 arrived yesterday, now I have four sensors in different rooms. I like the overview over the climate at home :)

Kommentare:

simon yang hat gesagt…

What Will dht22.py file is?

Koepi hat gesagt…

Can you rephrase the question? I don't understand how I could help you.

Koepi hat gesagt…

I think I finally understand your question. I just used the Adafruit library like explained here:
http://www.home-automation-community.com/temperature-and-humidity-from-am2302-dht22-sensor-displayed-as-chart/

And adopted the example script there to just spit out temperature on the first line and humidity on the second, without any additional text.