I am the proud owner of the lovely 16x2 Lcd+keypad Kit for Raspberry Pi.
I assembled it and I find it a beauty.
I am ready to program an interface to control house heating and light but I wanted to ask suggestions about how to program an user interface on this kind of hardware.
General ideas, suggestions, code snippets...
I am currently jotting some code to play with it:
Code: Select all
# -*- coding: utf-8 -*-
#!/usr/bin/python
import Adafruit_CharLCD as LCD
import time
import datetime
import subprocess
import urllib2
import json
import services
# temperature_1 = yellow
temperature_1_url = 'https://api.particle.io/v1/devices/2a0023001647343337363432/temperature/?access_token=62195fad61b603b03376cb4f29fa0345b4d12584'
humidity_1_url = 'https://api.particle.io/v1/devices/2a0023001647343337363432/humidity/?access_token=62195fad61b603b03376cb4f29fa0345b4d12584'
# temperature_2 = blue
temperature_2_url = 'https://api.particle.io/v1/devices/340033000147343337373738/temperature/?access_token=62195fad61b603b03376cb4f29fa0345b4d12584'
humidity_2_url = 'https://api.particle.io/v1/devices/340033000147343337373738/humidity/?access_token=62195fad61b603b03376cb4f29fa0345b4d12584'
telldus = '/usr/bin/python /home/osmc/Scripts/telldus_controller.py'
on = ' --on '
off = ' --off '
# Initialize the LCD using the pins
lcd = LCD.Adafruit_CharLCDPlate()
backlight_timeout = 30
heater_check_timeout = 900
heater_check_timeout = 10
min_temperature = 20.5
max_temperature = 22
def turn_backlight_off():
lcd.set_backlight(0.0)
def turn_backlight_on():
lcd.set_backlight(0.1)
# Splash Screen.
def splash_screen():
screen = 'splash'
lcd.clear()
lcd.message('Home Environment')
lcd.set_cursor(0,1)
lcd.message('Sensors Control')
time.sleep(1)
def get_sensors():
response = urllib2.urlopen(temperature_1_url)
data = json.load(response)
temperature_1 = data['result']
response = urllib2.urlopen(humidity_1_url)
data = json.load(response)
humidity_1 = data['result']
response = urllib2.urlopen(temperature_2_url)
data = json.load(response)
temperature_2 = data['result']
response = urllib2.urlopen(humidity_2_url)
data = json.load(response)
humidity_2 = data['result']
return [temperature_1, humidity_1, temperature_2, humidity_2]
def check_heater():
sensors = get_sensors()
temperature = sensors[0]
humidity = sensors[1]
temperature2 = sensors[2]
print "Temperatura cameretta: " + str(temperature)
print "Temperatura soggiorno: " + str(temperature2)
if temperature <= min_temperature:
print "Turning on heater"
services.change_power_status(on, '4')
if temperature >= max_temperature:
print "Turnin off heater"
services.change_power_status(off, '4')
# Main Screen
def main_screen(room, heater_status):
sensors = get_sensors()
temperature = sensors[0]
humidity = sensors[1]
if heater_status == 'on':
heater_icon = '*'
else:
heater_icon = ''
lcd.clear()
lcd.message(room)
lcd.set_cursor(16,0)
lcd.message(heater_icon)
lcd.set_cursor(0,1)
lcd.message(str(temperature) + ' C ' + str(humidity) + ' %')
lcd.set_cursor(0,2)
lcd.message('Bedroom ' + heater_icon)
lcd.set_cursor(0,3)
lcd.message(str(temperature) + ' C ' + str(humidity) + ' %')
# Heating Status Screen.
def heating_screen(status):
lcd.clear()
lcd.message('Home Environment')
lcd.set_cursor(0,1)
lcd.message('Heater now ' + status)
splash_screen()
main_screen('Living Room','off')
print get_sensors()
#data_screen("21.5", "40.4")
# Make list of button value, text, and backlight color.
buttons = ( (LCD.SELECT, 'Select', (1,1,1)),
(LCD.LEFT, 'Left' , (1,0,0)),
(LCD.UP, 'Up' , (0,0,1)),
(LCD.DOWN, 'Down' , (0,1,0)),
(LCD.RIGHT, 'Right' , (1,0,1)) )
print 'Thermostate is running...'
print 'Press Ctrl-C to quit.'
period = datetime.timedelta(seconds=1)
next_time = datetime.datetime.now() + period
seconds_backlight = 0
seconds_heater_check = 0
while True:
if next_time <= datetime.datetime.now():
# print "Seconds Backlight: " + str(seconds_backlight)
print "Seconds Heater Check: " + str(seconds_heater_check)
seconds_backlight += 1
seconds_heater_check += 1
next_time += period
if seconds_backlight == backlight_timeout:
turn_backlight_off()
if seconds_heater_check == heater_check_timeout:
check_heater()
seconds_heater_check = 0
# Loop through each button and check if it is pressed.
for button in buttons:
if lcd.is_pressed(button[0]):
if button[1] == "Select":
heating_screen("ON")
change_power_status(on, 'light_pc')
elif button[1] == "Up":
main_screen('Bedroom','21.5','40.4','off')
elif button[1] == "Down":
main_screen('Computer Room','21.5','40.4','off')
elif button[1] == "Right":
main_screen('Living Room',temperature_1,humidity_1,'on')
elif button[1] == "Left":
heating_screen("OFF")
change_power_status(off, 'light_pc')
seconds_backlight = 0
turn_backlight_on()
Thanks for your time and suggestions :)