#4b_send_temperature.py
# get temperature from DHT22 and send to an Adafruit IO feed
# Import standard python modules
import time
#import board
import Adafruit_DHT
# import Adafruit IO REST client
from Adafruit_IO import Client
# Initialize the DHT device, with data pin connected to:
sensor = Adafruit_DHT.DHT22
pin = 18
# Delay between sensor reads, in seconds
DELAY_SECONDS = 300
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY'
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME'
# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Set up `temperature and humidity` feed
pi_temperature = aio.feeds('temperature')
pi_humidity = aio.feeds('humidity')
while True:
try:
# Print the values to the serial port
#temperature_c = dhtDevice.temperature
humidity, temperature_c = Adafruit_DHT.read_retry(sensor, pin)
temperature_f = temperature_c * (9 / 5) + 32
#humidity = dhtDevice.humidity
print(
"Temp: {:.1f} F / {:.1f} C Humidity: {:.1f}% ".format(
temperature_f, temperature_c, humidity
)
)
aio.send(pi_temperature.key, temperature_f)
aio.send(pi_humidity.key, humidity)
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
# Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io
time.sleep(DELAY_SECONDS)
# get temperature from DHT22 and send to an Adafruit IO feed
# Import standard python modules
import time
#import board
import Adafruit_DHT
# import Adafruit IO REST client
from Adafruit_IO import Client
# Initialize the DHT device, with data pin connected to:
sensor = Adafruit_DHT.DHT22
pin = 18
# Delay between sensor reads, in seconds
DELAY_SECONDS = 300
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY'
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME'
# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Set up `temperature and humidity` feed
pi_temperature = aio.feeds('temperature')
pi_humidity = aio.feeds('humidity')
while True:
try:
# Print the values to the serial port
#temperature_c = dhtDevice.temperature
humidity, temperature_c = Adafruit_DHT.read_retry(sensor, pin)
temperature_f = temperature_c * (9 / 5) + 32
#humidity = dhtDevice.humidity
print(
"Temp: {:.1f} F / {:.1f} C Humidity: {:.1f}% ".format(
temperature_f, temperature_c, humidity
)
)
aio.send(pi_temperature.key, temperature_f)
aio.send(pi_humidity.key, humidity)
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
# Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io
time.sleep(DELAY_SECONDS)