Code: Select all
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Adafruit_GFX
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
import mysql.connector as mariadb
import RPi.GPIO as GPIO
import time
# button setup -
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # uses board physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # initialises with down/off
# Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0
# Load font
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 18)
def display_on_oled():
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
conn = mariadb.connect(user='xx', password='xxxx', database='weather')
cur = conn.cursor()
cur.execute('SELECT TempSensor3, TIME_FORMAT(TS, "%H:%i") AS HourMin FROM readings WHERE TempSensor3 IS NOT NULL ORDER BY readingID DESC LIMIT 1;')
balcony = cur.fetchone()
cur.execute('SELECT HumidSensor1, TIME_FORMAT(TS, "%H:%i") AS HourMin FROM readings WHERE HumidSensor1 IS NOT NULL ORDER BY readingID DESC LIMIT 1;')
humid1 = cur.fetchone()
cur.close()
draw.text((x, top), str(balcony[0]) + " " + str(balcony[1]), font=font, fill=255)
# draw.text((x, top+8), str(balcony[1]), font=font, fill=255)
draw.text((x, top+17), str(humid1[0]) + "% " + str(humid1[1]), font=font, fill=255)
# draw.text((x, top+25), str(Disk), font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
time.sleep(10)
disp.clear()
disp.display()
return
while True:
if GPIO.input(1) == GPIO.HIGH:
display_on_oled()
time.sleep(10)