#1 2016-07-31 21:44:17

contactform
Member
Registered: 2015-10-30
Posts: 45

Reading a DHT11 sensor

I am in the process of familiarizing myself with the Rex control system with the intention of using it as a basis for a product I am developing.


For a prototype I am attempting to read a DHT11 temperature / humidity sensor.  There are existing arduino libraries for reading the sensor and displaying it via serial monitor.

I am having some difficulty determining the best path for reading the DHT11 using Rex controls. The sensor has a single digital connection (http://www.candrian.gr/index.php/dht-11-one-wire-bus/).  Would you suggest creating function blocks to access the sensor directly or is it possible to use Arduino libraries to read sensor data which could then be read via RexDuino.

Any advice / suggestions would be greatly appreciated in speeding up this prototype.

Offline

#2 2016-08-02 12:04:33

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hello,

thank you for your interest in Rex control system. I presume that you are using Raspberry PI as target platform, am I right?

There are many possibilities how to read a DHT11 temperature / humidity sensor into REX. As you are starting with REX I would suggest using python script for reading the sensor values and storing them to text files on your target platform. REX will read the files using SILO blocks afterwards. The reason is that you don't need any advanced function blocks which require advanced licence.

DHT - temperature and humidity sensor reading - DHT11_sensor_reading.zip

  1. Install Adafruit Python DHT Sensor Library

  2. Connect the sensor to the Raspberry PI (e.g. according to the tutorial to the GPIO4)

  3. Try to run the DHT example to see if the sensor works well:
    sudo /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4
    (= read DHT11 sensor connected to the GPIO4)

  4. Prepare ramdisk for storing the text files in order to avoid damaging your SD card with excessive file writting:
    Edit fstab: sudo nano /etc/fstab
    Add the line: none      /rex/rexcore/ramdisk        tmpfs      size=10M          0        0
    Save and reboot.

  5. Copy python script "dht11_read.py" to the Raspberry PI and make it executable:
    sudo chmod +x dht11_read.py

  6. Run the script. (you should see 2 new text files in folder /rex/rexcore/ramdisk)

  7. Compile and download the Rex project to the target device using RexDraw.

  8. Watch the trend with humidity and temperature using RexView.

Note: the python script must be running all the time you want to read the sensor. You can for example use "crontab" with parameter "@reboot" to make the script running after reboot.

Hope that it will help you to start your prototype.

If you will need further information feel free to contact me.

Regards, Tomas

Last edited by tomáš čechura (2016-08-06 22:09:01)

Offline

#3 2016-08-03 00:11:35

chandc
Member
Registered: 2016-08-03
Posts: 14

Re: Reading a DHT11 sensor

Thank you very much for the excellent details Tomas!

I am on travel at the moment and will implement them asap this weekend.  The level of detail you provided will jump start both the prototype and my understanding of Rex.

Regards,  Chand

Offline

#4 2016-08-06 21:46:21

chandc
Member
Registered: 2016-08-03
Posts: 14

Re: Reading a DHT11 sensor

Hi Tomas,

Step 5  above references a dht11.py file.  I see a dht11_read.py file in the DHT_sensor_reading.zip.  Is that the py file you are describing or is it another?

Thank you,

Chand

Offline

#5 2016-08-06 22:08:23

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Chand,
you are completely right - the file you are looking for is "dht11_read.py". Thank you for your notice. Will update my previous post.

Good luck with your project!

Cheers, Tomas

Offline

#6 2016-08-08 01:12:54

chandc
Member
Registered: 2016-08-03
Posts: 14

Re: Reading a DHT11 sensor

Hi Tomas,


Thank you for clarifying.  I am now able to see the temp & humidity trends.


Is the ramdisk method common for Pi based Rex control systems.  A couple of follow up questions:
--> What design factor guided you to select the ramdisk method?  Would you advise using the ramdisk method for keeping a history of up to 10 measurements?
--> The ramdisk files contain only a single value.  Where is the trend data stored?  Is it possible to configure trend data to be stored on another drive?


Do you have a thermostat control type Rex sample project I might be able to take a look at?


Thanks again Tomas!

Offline

#7 2016-08-08 11:18:17

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Chand,

I'm glad that you made it work smile

chandc wrote:

--> What design factor guided you to select the ramdisk method?  Would you advise using the ramdisk method for keeping a history of up to 10 measurements?

Data exchange via files has one drawback - it writes the text files over and over again with certain period (every 5s in my example). Since SD card live is often affected by amount of write cycles we try to avoid damaging your SD card by writing the files into RAM memory. It can be used for that many measurements which can fit in your RAM memory (example uses only 10MB of RAM - it can be easily resized).

chandc wrote:

--> The ramdisk files contain only a single value.  Where is the trend data stored?  Is it possible to configure trend data to be stored on another drive?

Trend data are stored also in RAM memory in internal format. See documentation of TRND block for more details. Pay attention to "Function description" paragraph about memory requirements.

Trend data will be stored in RAM memory - it can not be changed. However it is possible to use Archives which can be configured to be stored on another drive. Check out Archiving analog signals example and Data archiving documentation for more details.

chandc wrote:

Do you have a thermostat control type Rex sample project I might be able to take a look at?

Yes, we do. See Simple Thermostat example in our GitHub repository.


If you require any further information, feel free to contact me.

Cheers, Tomas

Offline

#8 2017-02-14 07:43:47

chandc
Member
Registered: 2016-08-03
Posts: 14

Re: Reading a DHT11 sensor

Hi Tomas,


I have installed v2.50 and am revisiting this example.  I am able to see that temp and humidity values are updating in the ramdisk files; however, RexDraw & RexView continue to read a 0 (zero) value.  Could you please give me some tips on troubleshooting why RexDraw & RexView might be unable to retrieve the values in the text files?


Thanks in advance !

Offline

#9 2017-02-14 14:36:13

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Chand,

the problem you are facing is caused by security features introduced in Rex v2.50.1. Default working folder is now set to "/rex/data" and can be configured - look for "data.path" parameter in RexCore documentation - https://www.rexcontrols.com/media/2.50. … pdf#page=4

Try to enable the ramdisk inside /rex/data directory and accordingly change the SILO block fname parameters.

Hope the above is useful for you.

Regards, Tomas

Offline

#10 2017-02-14 20:03:12

chandc
Member
Registered: 2016-08-03
Posts: 14

Re: Reading a DHT11 sensor

That worked.  Good to know about the default working folder change.

Do you have any I2C temperature sensor examples?  specifically with the MCP9808 board?

Thanks again Tomas!

Offline

#11 2017-02-15 08:36:39

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Chand,

good to hear that it is working now for you.

We don't have specificaly any I2C temperature sensor examples. However, please, investigate attached commpressed folder which contains updated library of various I2C examples. At least it can serve as a good starting point for your implementation.

Rex_I2C_examples.zip

Cheers, Tomas

Offline

#12 2017-03-14 09:35:49

darren.macleod@internode.on.net
Member
Registered: 2016-04-09
Posts: 8

Re: Reading a DHT11 sensor

Hi Tomas,
I have tried to modify the script to work on DHT22 using GPIO25 as per below, and I am having issues.


#!/usr/bin/python

import Adafruit_DHT
import socket
import struct
import time


while True:
        humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 25)
        f_temp = open('/rex/rexcore/ramdisk/dht11_temp.txt', 'w')
        f_temp.write('%.4f \n' % (temperature))   #writes temperature in specified txt file

        f_humid = open('/rex/rexcore/ramdisk/dht11_humid.txt', 'w')
        f_humid.write('%.4f \n' % (humidity))   #writes humidity in specified txt file
        time.sleep(5)




When I run this script as per line 6 of your earlier post I get the following:-

Traceback (most recent call last):
  File "dht22_read.py", line 10, in <module>
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 25)
  File "/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py", line 94, in read_r                                                                                                                               etry
    humidity, temperature = read(sensor, pin, platform)
  File "/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py", line 80, in read
    platform = get_platform()
  File "/home/pi/Adafruit_Python_DHT/Adafruit_DHT/common.py", line 48, in get_pl                                                                                                                               atform
    from . import Raspberry_Pi
  File "/home/pi/Adafruit_Python_DHT/Adafruit_DHT/Raspberry_Pi.py", line 22, in                                                                                                                                <module>
    from . import Raspberry_Pi_Driver as driver
ImportError: cannot import name Raspberry_Pi_Driver



Any suggestions?
Thanks
Darren

Offline

#13 2017-03-14 11:10:30

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Darren,

what Raspberry Pi version do you use? It seems that the Adafruit DHT library does not support Raspberry Pi 3 by default.
See the link: https://github.com/adafruit/Adafruit_Py … /issues/60

Hope it will help.

Cheers, Tomas

Offline

#14 2017-03-15 07:22:15

darren.macleod@internode.on.net
Member
Registered: 2016-04-09
Posts: 8

Re: Reading a DHT11 sensor

Currently using a RPI 1 , I think it is a B model. it has the 26 pin header.
I was looking at upgrading to RPI 3.

Would it be easier if I used a different sensor? perhaps one with I2c interface?

Offline

#15 2017-03-15 09:38:21

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 341

Re: Reading a DHT11 sensor

Hi Darren,

DHT sensors should work with Raspberry Pi. Try to contact Adafruit and explain them your issue. The problem is not on REX side since you are not able to run the external Python script.

Did you try to reinstall the library? Did you follow the installation guides here: https://github.com/adafruit/Adafruit_Python_DHT?
Did you also run the DHT library setup command?

sudo python setup.py install

Regards, Tomas

Offline

Board footer

Powered by FluxBB