Skip to content

Instantly share code, notes, and snippets.

@elizabethn119
Last active August 20, 2022 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elizabethn119/25be959d124f4b4c86f7160cf916f4d4 to your computer and use it in GitHub Desktop.
Save elizabethn119/25be959d124f4b4c86f7160cf916f4d4 to your computer and use it in GitHub Desktop.
import adafruit_dht
from ISStreamer.Streamer import Streamer
import time
import board
# --------- User Settings ---------
SENSOR_LOCATION_NAME = "Office"
BUCKET_NAME = ":partly_sunny: Room Temperatures"
BUCKET_KEY = "dht22sensor"
ACCESS_KEY = "ENTER ACCESS KEY HERE"
MINUTES_BETWEEN_READS = 10
METRIC_UNITS = False
# ---------------------------------
dhtSensor = adafruit_dht.DHT22(board.D4)
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
while True:
try:
humidity = dhtSensor.humidity
temp_c = dhtSensor.temperature
except RuntimeError:
print("RuntimeError, trying again...")
continue
if METRIC_UNITS:
streamer.log(SENSOR_LOCATION_NAME + " Temperature(C)", temp_c)
else:
temp_f = format(temp_c * 9.0 / 5.0 + 32.0, ".2f")
streamer.log(SENSOR_LOCATION_NAME + " Temperature(F)", temp_f)
humidity = format(humidity,".2f")
streamer.log(SENSOR_LOCATION_NAME + " Humidity(%)", humidity)
streamer.flush()
time.sleep(60*MINUTES_BETWEEN_READS)
@ryangoe12
Copy link

Hi, when running this python script on two Raspberry Pi Zero W's I keep running into this error. Do you have any thoughts on how to resolve it?

RuntimeError, trying again...
Traceback (most recent call last):
File "tempsensor.py", line 29, in
temp_f = format(temp_c * 9.0 / 5.0 + 32.0,".2f")
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Thanks!

@elizabethn119
Copy link
Author

elizabethn119 commented Oct 7, 2020 via email

@136F
Copy link

136F commented Nov 15, 2020

Hi Elizabeth,
I'm getting the same error as Ryan above despite removing the format function. I am using a DHT11 sensor. Any suggestions for me?

Thanks,
Mike
Extra300LT@gmail.com

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 16, 2020 via email

@136F
Copy link

136F commented Nov 18, 2020

Thanks Elizabeth,
I'm running a Raspberry Pi 3 B+ connected to a breadboard containing the DHT11 and resistor. I have verified the sensor is working with a simple C script but was hoping to use IS to do some experimenting over time. When running the tempsensor.py (In my case renamed DHT11Script) I get the following error...

pi@raspberrypi:~ $ python3 DHT11Script.py
RuntimeError, trying again...
Traceback (most recent call last):
File "DHT11Script.py", line 29, in
temp_f = temp_c * 9.0 / 5.0 + 32.0
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 18, 2020 via email

@inbinder
Copy link

inbinder commented Nov 22, 2020

@elizabethn119
change the lines when using the DHT11 instead of the DHT22

dhtSensor = adafruit_dht.DHT22(board.D4) 

to
dhtSensor = adafruit_dht.DHT11(board.D4)

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 23, 2020 via email

@136F
Copy link

136F commented Nov 23, 2020

Thank you @inbinder and @elizabethn119. I got some help from my genius cousin who recognized I stupidly had the wrong input pin as my board design is a little different. Thank you so much for you help, I'm up and running!

@erasmus95
Copy link

Hi @elizabethn119,

Thanks for great project instructions, I really like InitialState and already got an outdoor weather bucket working using ClimaCell. However, when trying to use the code above, I have been hitting the following error:

Unable to set line 4 to input

Some internet digging suggests I may be getting a None value (unsure). I have double-checked my wiring, and they are on the correct pins. I am using a DHT22 sensor, python3, on a rpi-zero W. Any suggestions on where to start looking for fixes?

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 29, 2020 via email

@erasmus95
Copy link

erasmus95 commented Nov 29, 2020

Yup, I'm using a DHT22 sensor (Link to the one I bought off Amazon: https://smile.amazon.com/gp/product/B073F472JL/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1)

Edit: I bought a pack of two and tried swapping them out. But got the same results.

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 29, 2020 via email

@erasmus95
Copy link

Thanks! From your link and other links and searches what ended up working for me was to follow the instructions here: https://www.instructables.com/Raspberry-Pi-Tutorial-How-to-Use-the-DHT-22/ combined with your instructions on medium https://medium.com/initial-state/build-an-inexpensive-network-of-web-connected-temperature-sensors-using-pi-zeros-730a40f1fb60

Then I modified the code you provided as follows (I don't know the proper way to show this, but I'd be happy to learn):
Changed
import adafruit_dht
to
import Adafruit_DHT as dht

removed dhtSensor = adafruit_dht.DHT22(board.D4)

added DHT = 4 #change depending on which pin you are using I put it before the user settings section but anywhere before the while works

Changed
humidity = dhtSensor.humidity
temp_c = dhtSensor.temperature
to
humidity,temp_c = dht.read_retry(dht.DHT22, DHT)

Making these changes also seemed to avoid the format issue that others had. Might just be depending on the sensor output.

Thanks for the support and again for the project!

@elizabethn119
Copy link
Author

elizabethn119 commented Nov 30, 2020 via email

@1muuo
Copy link

1muuo commented Dec 10, 2020

Thanks! From your link and other links and searches what ended up working for me was to follow the instructions here: https://www.instructables.com/Raspberry-Pi-Tutorial-How-to-Use-the-DHT-22/ combined with your instructions on medium https://medium.com/initial-state/build-an-inexpensive-network-of-web-connected-temperature-sensors-using-pi-zeros-730a40f1fb60

Then I modified the code you provided as follows (I don't know the proper way to show this, but I'd be happy to learn):
Changed
import adafruit_dht
to
import Adafruit_DHT as dht

removed dhtSensor = adafruit_dht.DHT22(board.D4)

added DHT = 4 #change depending on which pin you are using I put it before the user settings section but anywhere before the while works

Changed
humidity = dhtSensor.humidity
temp_c = dhtSensor.temperature
to
humidity,temp_c = dht.read_retry(dht.DHT22, DHT)

Making these changes also seemed to avoid the format issue that others had. Might just be depending on the sensor output.

Thanks for the support and again for the project!

This worked for me. Thanks for the solution

@christh0mas
Copy link

Hello! I have two Pis with DHT22 sensors on them that I set up using your post here. Both are working great....sometimes. While I have them set to run the script at startup in cron, I have found that it doesn't always start and when I go to run it manually, I get this message that others have reported above:

RuntimeError, trying again...
Traceback (most recent call last):
File "tempsensor.py", line 28, in
temp_f = format(temp_c * 9.0 / 5.0 + 32.0, ".2f")
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

I have nearly zero experience with Python, or any coding really for that matter, so I'm not 100% sure how to troubleshoot this but I have tried some of the adjustments mentioned above without luck.

The odd thing that I really don't understand is that sometimes it'll run and sometimes it won't. Normally I wait a few minutes to try again and then it magically works. This does not compute.

I really love this project by the way. It's exactly what I was looking for.
Thanks!

@elizabethn119
Copy link
Author

elizabethn119 commented Dec 28, 2020 via email

@christh0mas
Copy link

Still getting pretty much the same thing for some reason:

pi@enviro1:/etc $ python3 tempsensor.py
RuntimeError, trying again...
Traceback (most recent call last):
File "tempsensor.py", line 28, in
temp_f = temp_c * 9.0 / 5.0 + 32
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Again, if I try to run it a couple more times, it does eventually work. Just not sure why it runs successfully sometimes and not others.

Thanks.

@elizabethn119
Copy link
Author

elizabethn119 commented Jan 4, 2021 via email

@hawtcher
Copy link

Hi Elizabeth,

Not sure if this has been resolved, but I have been having the same issue. I replaced 29 with temp_f = "{:.2f}".format(temp_c * 9.0 / 5.0 +32.0). I noticed that when I ran the python code, after I made the changes to line 29, it would come back initially with the error that others pointed out. I ran it a few more times and then refreshed my dashboard on initial state and then ran the code again and it started to work. I would have to repeat this every time I stopped it and wanted to start it back up again. Not sure why it is doing it but as long as I can SSH into the pi and willing to run the code a few times it seems to work fine now.

@MrACP1911
Copy link

Hi,

Great project but I am slightly stuck. My first issue is pip3 install pureio, for some reason I am getting a 404 error, so I am not sure if this is causing my issue, I don't think that it is but I could be wrong. My second issue is this I am using BME280 but not adafruit one, I have followed both options but still cannot get it to work. I am sure that this is caused by my sensor not being adafruit but this one or the crawlspace one still give me an error.

Screen Shot 2021-09-20 at 11 46 21 PM
Screen Shot 2021-09-20 at 11 16 47 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment