#4d_send_temp_LED_OLED.py
# get temperature from DHT22 and send to an Adafruit IO feed
# blink LED when data sent to Adafruit
# display temperature on OLED
# Import standard python modules
import os, sys, time
from datetime import datetime, date
import Adafruit_DHT
from gpiozero import LED
# 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
green=LED(23)
# set up display
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import sh1106
from PIL import ImageFont
# GND Pin 6
# VCC Pin 1
# SCL Pin 5
# SDA Pin 3
try :
serial = i2c(port=1, address=0x3C)
# substitute ssd1331(...) or sh1106(...) below if using that device
device = sh1106(serial)
except:
print ('no i2c device')
pass
# 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')
print ("Ignoring first 2 sensor values to improve quality...")
for x in range(2):
Adafruit_DHT.read_retry(sensor, pin)
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)
for x in range(1,3):
green.on()
time.sleep(1)
green.off()
time.sleep(1)
timenow = time.strftime("%a, %b %d, %Y %I:%M:%S %p") # for print when value changes
now = datetime.now() # for clock display
today_date = now.strftime("%d %b %y")
today_time = now.strftime("%H:%M:%S")
try :
# Display temp on OLED
# use custom font
#font = ImageFont.truetype('C&C Red Alert [INET].ttf', 40)
font = ImageFont.truetype('code2000.ttf', 40)
with canvas(device) as draw:
margin = 4
cx = 30
cy = min(device.height, 64) / 2
draw.text(((cx), 0), str(round(temperature_f))+'\u00b0F', fill="yellow", font=font)
draw.text(((cx + margin), cy + 8), today_date, fill="yellow")
draw.text(((cx + margin), cy + 16), today_time, fill="yellow")
except :
print ('image pass')
pass
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
# blink LED when data sent to Adafruit
# display temperature on OLED
# Import standard python modules
import os, sys, time
from datetime import datetime, date
import Adafruit_DHT
from gpiozero import LED
# 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
green=LED(23)
# set up display
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import sh1106
from PIL import ImageFont
# GND Pin 6
# VCC Pin 1
# SCL Pin 5
# SDA Pin 3
try :
serial = i2c(port=1, address=0x3C)
# substitute ssd1331(...) or sh1106(...) below if using that device
device = sh1106(serial)
except:
print ('no i2c device')
pass
# 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')
print ("Ignoring first 2 sensor values to improve quality...")
for x in range(2):
Adafruit_DHT.read_retry(sensor, pin)
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)
for x in range(1,3):
green.on()
time.sleep(1)
green.off()
time.sleep(1)
timenow = time.strftime("%a, %b %d, %Y %I:%M:%S %p") # for print when value changes
now = datetime.now() # for clock display
today_date = now.strftime("%d %b %y")
today_time = now.strftime("%H:%M:%S")
try :
# Display temp on OLED
# use custom font
#font = ImageFont.truetype('C&C Red Alert [INET].ttf', 40)
font = ImageFont.truetype('code2000.ttf', 40)
with canvas(device) as draw:
margin = 4
cx = 30
cy = min(device.height, 64) / 2
draw.text(((cx), 0), str(round(temperature_f))+'\u00b0F', fill="yellow", font=font)
draw.text(((cx + margin), cy + 8), today_date, fill="yellow")
draw.text(((cx + margin), cy + 16), today_time, fill="yellow")
except :
print ('image pass')
pass
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)