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