Having eventually got envirphat database data scrolling across scrollphat on a different pi, I now need it to refresh itself every X minutes.
#!/usr/bin/env python
import signal
import time
import scrollphathd
from scrollphathd.fonts import font5x7
import mysql.connector
from mysql.connector import Error
con = mysql.connector.connect(host='192.168.################################)
str_len = 0
scroll_x = 0
### Create cursorS for each database element:
### Keep in buffer with =True
while True:
curT = con.cursor(buffered=True)
curY = con.cursor(buffered=True)
curL = con.cursor(buffered=True)
### Use each cursor to read required data from database:
curT.execute('SELECT Temp FROM readings ORDER BY Added DESC LIMIT 1')
curY.execute('SELECT Yaxis FROM readings ORDER BY Added DESC LIMIT 1')
curL.execute('SELECT Lux FROM readings ORDER BY Added DESC LIMIT 1')
### Get rid of trailing comma from each SELECT result:
resultT = [row[0] for row in curT.fetchall()]
resultY = [row[0] for row in curY.fetchall()]
resultL = [row[0] for row in curL.fetchall()]
### Not essential, but let's show the
### result of each SELECT query in the terminal:
print resultT
print resultY
print resultL
time.sleep(4)
### Set strings for display on Scroll PhatHD from SELECT results:
while True:
temperature = resultT[0]
yaxis = resultY[0]
lux = resultL[0]
### Dim down Scroll Phat HD, and clear its buffer:
scrollphathd.set_brightness(0.1)
scrollphathd.clear()
### Uncomment/comment the below line to rotate Scroll PhatHD by 180/upside down
scrollphathd.rotate(degrees=180)
### Uncomment line below to test all data on Scroll PhatHD in one go.
### str_len = scrollphathd.write_string(" :-) %.1fC Y%i L%i "%(temperature, yaxis, lux), brightness=0.5)
### Check light levels and door angle (Yaxis) and report appripriately. Always show the temperature:
if lux <= 100 and yaxis >=3500 :
str_len = scrollphathd.write_string("Garage: light off & door closed. %.1fC "%(temperature), x=0, y=0, font=font5x7)
elif lux <= 100 and yaxis <500:
str_len = scrollphathd.write_string("Garage: Light off & door open. %.1fC "%(temperature), x=0, y=0, font=font5x7)
elif lux > 100 and yaxis <500:
str_len = scrollphathd.write_string("Garage: Light on & door open. %.1fC "%(temperature), x=0, y=0, font=font5x7)
elif lux > 100 and yaxis >=3500:
str_len = scrollphathd.write_string("Garage: Light on & door closed. %.1fC "%(temperature), x=0, y=0, font=font5x7)
elif yaxis >500 and yaxis <3499:
str_len = scrollphathd.write_string("Garage door ajar %.1fC "%(temperature), x=0, y=0, font=font5x7)
scrollphathd.scroll_to(scroll_x, 0)
scrollphathd.show()
time.sleep(0.01)
scroll_x += 1
if scroll_x >= str_len:
scroll_x = 0
Where do those while loops need to go so that the scrollphat picks up fresh data every X minutes?
It’s scrolling just fine, but with stale data.
Thank you.