diff --git a/emonPiLCD2.py b/emonPiLCD2.py index 253e12d..36ad0ae 100755 --- a/emonPiLCD2.py +++ b/emonPiLCD2.py @@ -16,6 +16,7 @@ import itertools import threading import math +import json import redis import paho.mqtt.client as mqtt @@ -92,8 +93,21 @@ # ------------------------------------------------------------------------------------ # Default Startup Page -max_number_pages = 8 page = default_page + +# Define pages in array +pages = [ + "emonHP Data", + "Ethernet", + "WiFi", + "WiFi AP", + "WiFi AP Toggle", + "SSH", + "Shutdown", + "System Info", + "Uptime" +] + screensaver = False sd_image_version = '' @@ -150,11 +164,11 @@ def drawText(x,y,msg,update=False): def buttonPressLong(): - global page + global page, pages logger.info("Mode button LONG press") - if page == 7: + if page == pages.index("SSH"): ret = subprocess.call(ssh_status, shell=True) if ret > 0: drawText(0,0,'Enabling SSH',True) @@ -173,11 +187,11 @@ def buttonPressLong(): drawText(0,0,'SSH Disabled') drawText(0,14,'',True) - elif page == 8: + elif page == pages.index("Shutdown"): logger.info("Shutting down") shutdown() - elif page == 5: + elif page == pages.index("WiFi AP Toggle"): ret = subprocess.call(wifiAP_status, shell=True) if ret == 0: logger.info("Stopping WiFi AP") @@ -186,7 +200,7 @@ def buttonPressLong(): time.sleep(1) drawText(0,0,'WiFi AP Stopped',True) time.sleep(1) - page = 4 + page = pages.index("WiFi AP") updateLCD() else: logger.info("Starting WiFi AP") @@ -195,11 +209,11 @@ def buttonPressLong(): time.sleep(3) drawText(0,0,'WiFi AP Started',True) time.sleep(1) - page = 4 + page = pages.index("WiFi AP") updateLCD() def buttonPress(): - global page + global page, pages global screensaver global buttonPress_time @@ -207,7 +221,7 @@ def buttonPress(): screensaver = False else: page += 1 - if page > max_number_pages: + if page > len(pages)-1: page = 0 now = time.time() @@ -221,8 +235,9 @@ def buttonPress(): def updateLCD(): # Lock thread to avoid LCD being corrupted if a button press interrupt interrupts the LCD update with lock: - global page + global page, pages global screensaver + global r # Create object for getting IP addresses of interfaces ipaddress = IPAddress() @@ -231,11 +246,24 @@ def updateLCD(): drawClear(); return - if page == 0: + # Load emoncms inputs from redis + inputs = {} + inputids = r.smembers('user:inputs:1') + for input in inputids: + input_obj = r.hgetall('input:' + input) + input_last_timevalue = r.hgetall('input:lastvalue:' + input) + # by nodeid and name + if input_obj['nodeid'] not in inputs: + inputs[input_obj['nodeid']] = {} + inputs[input_obj['nodeid']][input_obj['name']] = input_last_timevalue + + + if page == pages.index("System Info"): drawText(0,0,sd_image_version) drawText(0,14,"Serial: " + serial_num,True) + return - if page == 1: + if page == pages.index("Uptime"): # Get uptime with open('/proc/uptime', 'r') as f: seconds = float(f.readline().split()[0]) @@ -243,9 +271,41 @@ def updateLCD(): drawText(0,0,datetime.now().strftime('%b %d %H:%M')) drawText(0,14,'Uptime %.2f days' % (seconds / 86400),True) + return + + # Display emonTx4 data + if page == pages.index("emonHP Data"): + nodeid = 'heatpump' + name = 'electric_Power' + if nodeid in inputs: + if name in inputs[nodeid] and 'value' in inputs[nodeid][name]: + updated_ago = time.time() - float(inputs[nodeid][name]['time']) + value = float(inputs[nodeid][name]['value']) + if updated_ago < 30: + # ELEC 0W (10s ago) + drawText(0,0,'ELEC: %.0fW (%ds ago)' % (value, updated_ago)) + else: + drawText(0,0,'ELEC: ERROR') + else: + drawText(0,0,'ELEC: ERROR') + + nodeid = 'heatpump' + name = 'heatmeter_Power' + if nodeid in inputs: + if name in inputs[nodeid] and 'value' in inputs[nodeid][name]: + updated_ago = time.time() - float(inputs[nodeid][name]['time']) + value = float(inputs[nodeid][name]['value']) + if updated_ago < 30: + # Heat 0W (10s ago) + drawText(0,14,'HEAT: %.0fW (%ds ago)' % (value, updated_ago),True) + else: + drawText(0,14,'HEAT: ERROR',True) + else: + drawText(0,14,'HEAT: ERROR',True) + return # Now display the appropriate LCD page - if page == 2: + if page == pages.index("Ethernet"): # Update ethernet eth0ip = ipaddress.get_ip_address('eth0') r.set("eth:active", int(bool(eth0ip))) @@ -262,8 +322,9 @@ def updateLCD(): else: drawText(0,0,"Ethernet:") drawText(0,14,"NOT CONNECTED",True) + return - if page == 3: + if page == pages.index("WiFi"): # Update wifi wlan0ip = ipaddress.get_ip_address('wlan0') @@ -294,8 +355,9 @@ def updateLCD(): drawText(0,14,"NOT CONNECTED",True) oled.image(image) oled.show() + return - if page == 4: + if page == pages.index("WiFi AP"): ap0ip = ipaddress.get_ip_address('ap0') r.set("ap0:active", int(bool(ap0ip))) @@ -309,8 +371,10 @@ def updateLCD(): drawText(0,14,"DISABLED",True) oled.image(image) oled.show() + return + - if page == 5: + if page == pages.index("WiFi AP Toggle"): ret = subprocess.call(wifiAP_status, shell=True) if ret == 0: drawText(0,0,"Disable WiFi AP?") @@ -318,27 +382,9 @@ def updateLCD(): drawText(0,0,"Enable WiFi AP?") drawText(0,14,"Y press & hold",True) + return - if page == 6: - # Update Hi-Link 3G Dongle - connects on eth1 - if ipaddress.get_ip_address("eth1") and gsmhuaweistatus.is_hilink(hilink_device_ip): - gsm_connection_status = gsmhuaweistatus.return_gsm_connection_status(hilink_device_ip) - r.set("gsm:connection", gsm_connection_status[0]) - r.set("gsm:signal", gsm_connection_status[1]) - r.set("gsm:active", 1) - else: - r.set("gsm:active", 0) - - if eval(r.get("gsm:active")): - drawText(0,0,r.get("gsm:connection")) - drawText(0,14,r.get("gsm:signal"),True) - elif eval(r.get("eth:active")) or eval(r.get("wlan:active")): - page += 1 - else: - drawText(0,0,"GSM:") - drawText(0,14,"NO DEVICE",True) - - if page == 7: + if page == pages.index("SSH"): ret = subprocess.call(ssh_status, shell=True) if ret > 0: #ssh not running @@ -348,10 +394,12 @@ def updateLCD(): drawText(0,0,"SSH Disable?") drawText(0,14,"Y press & hold",True) + return - if page == 8: + if page == pages.index("Shutdown"): drawText(0,0,"Shutdown?") drawText(0,14,"Y press & hold",True) + return class IPAddress: @@ -566,7 +614,7 @@ def on_connect(client, userdata, flags, rc): last_btn_state = btn_state if push_btn is not None: btn_state = push_btn.is_pressed - + if btn_state != last_btn_state: if btn_state and not last_btn_state: @@ -585,7 +633,7 @@ def on_connect(client, userdata, flags, rc): if btn_state: press_time = math.floor(now - btn_press_timer) - if page==5 or page == 7 or page == 8: + if page == pages.index("WiFi AP Toggle") or page == pages.index("SSH") or page == pages.index("Shutdown"): if press_time>=1.0 and press_time<=5.0: draw.rectangle((108, 15, 120, 25), outline=0, fill=0) draw.text((110,14), str(press_time), font=font, fill=255) @@ -595,11 +643,11 @@ def on_connect(client, userdata, flags, rc): buttonPressLong() - if (now-buttonPress_time)>=59: + if (now-buttonPress_time)>=300: screensaver = True #Update LCD in case it is left at a screen where values can change (e.g uptime etc) - if (now-lcd_update_time)>=10.0: + if (now-lcd_update_time)>=1.0: lcd_update_time = now updateLCD()