Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion emonPiLCD1.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# ------------------------------------------------------------------------------------
redis_host = config.get('redis', 'redis_host')
redis_port = config.get('redis', 'redis_port')
r = redis.Redis(host=redis_host, port=redis_port, db=0, charset="utf-8", decode_responses=True)
r = redis.Redis(host=redis_host, port=redis_port, db=0, decode_responses=True)

# ------------------------------------------------------------------------------------
# General Settings
Expand Down
137 changes: 95 additions & 42 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 @@ -58,7 +59,7 @@
# ------------------------------------------------------------------------------------
redis_host = config.get('redis', 'redis_host')
redis_port = config.get('redis', 'redis_port')
r = redis.Redis(host=redis_host, port=redis_port, db=0, charset="utf-8", decode_responses=True)
r = redis.Redis(host=redis_host, port=redis_port, db=0, decode_responses=True)

# ------------------------------------------------------------------------------------
# General Settings
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,70 @@ 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')
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)
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 +326,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 +359,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 +375,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 +398,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 +618,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 +637,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,15 +647,16 @@ 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()

time.sleep(0.1)

if __name__ == '__main__':
main()

40 changes: 40 additions & 0 deletions initramfs_splash/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
echo "Installing OLED Boot Script..."

# check that this is a Raspberry Pi 4
if [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "armv7l" ]; then
echo "This script is intended for Raspberry Pi 4 only."
exit 1
fi

# change to the directory of the script
cd "$(dirname "$0")"
echo "Current directory: $(pwd)"

# compile the C program
echo "Compiling oled_boot.c..."
gcc -o oled_boot oled_boot.c

# check if the compilation was successful
if [ $? -ne 0 ]; then
echo "Compilation failed. Please check the source code for errors."
exit 1
fi

# copy the compiled binary to /usr/local/bin
echo "Copying oled_boot to /usr/local/bin..."
sudo cp oled_boot /usr/local/bin/oled_boot

# copy oled to /etc/initramfs-tools/scripts/init-top/oled
echo "Copying oled script to /etc/initramfs-tools/scripts/init-top/oled..."
sudo mkdir -p /etc/initramfs-tools/scripts/init-top
sudo cp oled /etc/initramfs-tools/scripts/init-top/oled
sudo chmod +x /etc/initramfs-tools/scripts/init-top/oled

# copy oled_display to /etc/initramfs-tools/scripts/oled_display
echo "Copying oled_display script to /etc/initramfs-tools/scripts/oled_display..."
sudo cp oled_display /etc/initramfs-tools/scripts/oled_display
sudo chmod +x /etc/initramfs-tools/scripts/oled_display

# Update initramfs
echo "Updating initramfs..."
sudo update-initramfs -u -k $(uname -r)
14 changes: 14 additions & 0 deletions initramfs_splash/oled
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
PREREQ=""

prereqs() { echo "$PREREQ"; }
case "$1" in
prereqs) prereqs; exit 0 ;;
esac

modprobe i2c-bcm2835
modprobe i2c-dev

sleep 0.5
lsmod > /run/oled.lsmod.txt
/usr/local/bin/oled_boot >> /run/oled.log 2>&1
Loading