Skip to content

Instantly share code, notes, and snippets.

@elizabethn119
Last active August 20, 2022 20:32
Show Gist options
  • 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)
@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