Page 1 of 1

DHT22 script error handling

Posted: Tue Jul 09, 2019 12:56 pm
by Mettauk
I'm trying to run the following python to measure temperature and humidity as a background process but of course the DHT22 throws errors occasionally (quite frequently) which crash the script.

Can anyone help? Ideally it would output something like EE and then keep running to correct the read error.

Code: Select all

##!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
#deg_sym = '\u00b0'
#deg_sym = '\xb0'
#deg_sym= u'\N{DEGREE SIGN}'
#deg_sym='°' #html
deg_sym='*' #Sad face!

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.0f}".format(temperature, humidity)+deg_sym+"C Humidity={1:0.0f}%".$

    else:
        print("Failed to get data from DHT22")

    annotate = open("/dev/shm/mjpeg/user_annotate.txt", 'w')
    annotate.write('{0:0.0f}'.format(temperature, humidity)+deg_sym+'C {1:0.0f}% RH'.format$
    annotate.close()

    time.sleep(10)
the error that comes up is

Code: Select all

...
Temp=29*C Humidity=36%
Temp=29*C Humidity=35%
Failed to get data from DHT22
Traceback (most recent call last):
  File "anoTemp2.py", line 25, in <module>
    annotate.write('{0:0.0f}'.format(temperature, humidity)+deg_sym+'C {1:0.0f}% RH'.format(temperature, humidity))
ValueError: Unknown format code 'f' for object of type 'str'
Im also trying to output the degree symbol but that a minor issue at the moment :?

Re: DHT22 script error handling

Posted: Tue Jul 09, 2019 3:41 pm
by MrYsLab
How about adding a try/except block as shown below. For the degree symbol - that is easy:

Code: Select all

degree_sign= u'\N{DEGREE SIGN}'
temp = 34.5
print(str(temp) + degree_sign)

~                              
Try/except block added to your code:

Code: Select all

##!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
#deg_sym = '\u00b0'
#deg_sym = '\xb0'
#deg_sym= u'\N{DEGREE SIGN}'
#deg_sym='&#176;' #html
deg_sym='*' #Sad face!

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.0f}".format(temperature, humidity)+deg_sym+"C Humidity={1:0.0f}%".$

    else:
        print("Failed to get data from DHT22")

    annotate = open("/dev/shm/mjpeg/user_annotate.txt", 'w')
    try:
        annotate.write('{0:0.0f}'.format(temperature, humidity)+deg_sym+'C {1:0.0f}% RH'.format$
    except ValueError:
        print('EE')
        continue
    annotate.close()

    time.sleep(10)

Re: DHT22 script error handling

Posted: Wed Jul 10, 2019 5:18 am
by ghp
I looked into your print statement and do not understand the syntax.
print("Temp={0:0.0f}".format(temperature, humidity)+deg_sym+"C Humidity={1:0.0f}%".$
gives a syntax error in my system.

in python3, i'd propose to write

Code: Select all

# -*- coding: utf-8 -*-

temperature = 33.1415
humidity = 50.11

## print("Temp={0:0.0f}".format(temperature, humidity)+deg_sym+"C Humidity={1:0.0f}%".$
print("Temp={0:0.1f}°C Humidity={1:0.1f}%".format(temperature, humidity))
This file needs to be saved 'as utf-8'.