I had to alter the code but I was ultimately able to get everything working as I wanted (which is impressive considering I don't know how to code). But after an unrelated mistake I had to re-image the microSD and reinstall everything. After that the program stopped working. Now when I run it I get:
Code: Select all
AttributeError: module 'Adafruit_DHT' has no attribute 'DHT22'
I'll post the code below. I'm not a programmer, but it seemed to be working before I had to start over. And I'm running it on Raspberry Pi Lite OS. Any help would be greatly appreciated.
Code: Select all
import os
import time
import Adafruit_DHT
import datetime
import board
DHT_SENSOR1 = Adafruit_DHT.DHT22
DHT_PIN1 = 4
DHT_SENSOR2 = Adafruit_DHT.DHT22
DHT_PIN2 = 11
try:
f = open('/mnt/usb/temp.csv', 'a+')
if os.stat('/mnt/usb/temp.csv').st_size == 0:
f.write('Date, Time, Temp1, Humidity1, Temp2, Humidity2\r\n')
except:
pass
while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR1, DHT_PIN1)
temperature = temperature * 9/5.0 + 32
f = open('/mnt/usb/temp.csv', 'a+')
if humidity is not None and temperature is not None:
f.write('\r\n{0}, {1}, {2:0.1f}*F, {3:0.1f}%'.format(time.strftime('%y/%m/%d'), time.strftime('%H:%M:%S'), temperature, humidity))
now = datetime.datetime.now()
print (now.strftime("%y/%m/%d %H:%M:%S"))
print("Sensor1 Temp={0:0.1f}*F Humidity={1:0.1f}%".format(temperature, humidity))
else:
print("failed to retrieve data from humidity sensor")
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR2, DHT_PIN2)
temperature = temperature * 9/5.0 + 32
f = open('/mnt/usb/temp.csv', 'a+')
if humidity is not None and temperature is not None:
f.write(' ,{2:0.1f}*F, {3:0.1f}%'.format(time.strftime('%y/%m/%d'), time.strftime('%H:%M:%S'), temperature, humidity))
print("Sensor2 Temp={0:0.1f}*F Humidity={1:0.1f}%".format(temperature, humidity))
time.sleep(300)
else:
print("failed to retrieve data from humidity sensor")