Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove GSM page and start on network connectivity #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 89 additions & 41 deletions emonPiLCD2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import itertools
import threading
import math
import json

import redis
import paho.mqtt.client as mqtt
Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -195,19 +209,19 @@ 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

if screensaver:
screensaver = False
else:
page += 1
if page > max_number_pages:
if page > len(pages)-1:
page = 0

now = time.time()
Expand All @@ -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()
Expand All @@ -231,21 +246,66 @@ 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])
r.set('uptime', seconds)

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)))
Expand All @@ -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')

Expand Down Expand Up @@ -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)))
Expand All @@ -309,36 +371,20 @@ 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?")
else:
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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()

Expand Down