1

Preface: I'm a noob at Pi/Python

After a lot of fighting with this DHT22 sensor, I finally got the thing to work with the Adafruit CircuitPython Library as seen here. I used the script as show on their page (and below) and got the temperature. From there I moved on to the LCD and from many tutorials settled on this guide using the "Output to an LCD" (also below) using the Python option. They both work, but now I'm struggling with figuring out how to combine the two scripts properly. This is what I've come up with based on some example shown on the CircuitBasics site and have fought through many of the syntax errors to correct them and am just not combining them properly. I'd like to get the 16x2 to display the temp in F on line 1 and the humidity on line two while doing the standard 2 second sleep and yet ignore the errors (as I get many). Here my latest attempt with the added suggestions from @Frank and some modifications:

#!/usr/bin/python
import time
import board
import adafruit_dht

from RPLCD import CharLCD
from RPi import GPIO

dhtDevice = adafruit_dht.DHT22(board.D4)
lcd - CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows =2, pin_rs=37, pin_e=35, pins_data=[33,31,29,40]

while True:
    try:
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity

        lcd.cursor_pos = (0, 0)
        lcd.write_string("Temp: {:.1f} F ".format(temperature_f, temperature_c))
        lcd.cursor_pos = (1, 0)
        lcd.write_string("Humidity: {}% ".format(humidity))
    except:
        pass

So ultimately I don't know how to join these two properly or if instead I just leave them as separate scripts and call them both with another. Like I said, I am pretty new to Python and coding in general and would appreciate some help from the community.

Thanks in advance.

EDIT: to simplify everyone's life (and what I should have done in the first place) here are the two I am trying to put together.

dht script

import time
import board
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D4)

while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print(
            "Temp: {:.1f} F / Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
            )
        )

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])

    time.sleep(2.0)

and the current lcd script

from RPLCD import CharLCD
from RPi import GPIO
lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33,31,29,40])
lcd.cursor_pos = (0, 0)
lcd.write_string(u'This is')
lcd.cursor_pos = (1, 0)
lcd.write_string(u'a test')

Zach
  • 11
  • 2

2 Answers2

0

It looks like your code indention is not correct. Remove the space in front of the while loop and do the same for the block in the while loop but keeping one indention.

In python the indention is important.

The second issue is, that the try does not have an except How to properly ignore exceptions

#!/usr/bin/python
import time
import board
import adafruit_dht

from RPLCD import CharLCD
from RPi import GPIO

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D4)
lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 40])

while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        lcd.cursor_pos = (0, 0)
        lcd.write_string(temperature_f)
        lcd.cursor_pos = (1, 0)
        lcd.write_string(humidity)

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])

    time.sleep(2.0)
Frank Mohaupt
  • 69
  • 1
  • 8
  • Thank you for the support, Frank. I've fixed the indentation and read up on exceptions a bit. That being said, I am still getting a SyntaxError: invalid syntax for line 12 which is where the while true loop starts. From what I gather it isn't the actual while True line or indentation now, but rather in the following code. I realized that there were not closing parenthesis on lines 19 and 21 as well, so I added those but it still gives me the same SyntaxError issue. – Zach Jul 25 '20 at 20:24
  • @Zach I updated the code and put both scripts together. This should work now. Regarding to the Syntax error you got, there is also a missing closing bracket at line 10. I suggest to work with an IDE that shows issues like this, if you are planning to programming more in the future. I'm using PyCharm Community and are very happy with the IDE. – Frank Mohaupt Jul 27 '20 at 17:03
  • thank you for the effort. I've discovered that the main issue with this is trying to call both "import board" which uses BCM naming and RPi.GPIO which uses the standard pin numbering on the GPIO is causing conflicts. I have to get them to the same scheme - either GPIO or BCM and given that Adafruit depracated the old method that would allow me to use GPIO, I am stuck with their newer method of "import board" using BCM. Ultimately, i'm going to have to figure out how to get the CircuitPython method of charlcd working and abandon RPLCD in order to get them both onto Adafruits BCM method. – Zach Jul 27 '20 at 20:15
0

Ultimately I have been able to accomplish this using the CircuitPython libraries for both the DHT sensor and controlling the 16x2 LCD. If anyone else needs to accomplish this feel free to use this code and just change the data pins for your lcd/dht.

import time
import board
import adafruit_dht
import adafruit_character_lcd.character_lcd as characterlcd
import digitalio

dhtDevice = adafruit_dht.DHT22(board.D4)

# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2

# Raspberry Pi Pin Config:
lcd_rs = digitalio.DigitalInOut(board.D26)
lcd_en = digitalio.DigitalInOut(board.D19)
lcd_d7 = digitalio.DigitalInOut(board.D21)
lcd_d6 = digitalio.DigitalInOut(board.D5)
lcd_d5 = digitalio.DigitalInOut(board.D6)
lcd_d4 = digitalio.DigitalInOut(board.D13)
lcd_backlight = digitalio.DigitalInOut(board.D4)

# Initialise the lcd class
lcd = characterlcd.Character_LCD_Mono(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, 
lcd_backlight
)

while True:
    try:
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        lcd.message = ("Temp: {:.1f} F \nHumidity: {}% ".format(
                           temperature_f, temperature_c, humidity
                       )
                   )

    except:
       pass

    time.sleep(2.0)
Zach
  • 11
  • 2