User avatar
Mettauk
Posts: 237
Joined: Mon Dec 10, 2012 12:40 pm
Location: Zarg

DHT22 script error handling

Tue Jul 09, 2019 12:56 pm

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 :?
As humans we have been the same for a very very long time, technology changes how we do... not who we are as people.

User avatar
MrYsLab
Posts: 375
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: DHT22 script error handling

Tue Jul 09, 2019 3:41 pm

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)

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: DHT22 script error handling

Wed Jul 10, 2019 5:18 am

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'.

Return to “Python”