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

Gpiozero enable ssh #68

Merged
merged 7 commits into from
Oct 17, 2018
Merged
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
13 changes: 1 addition & 12 deletions emoncmsupdate
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,11 @@ echo
#########################################################################################
echo "#############################################################"
echo
# Check emonSD base image is minimum required release date, else don't update
# Check emonSD version
image_version=$(ls /boot | grep emonSD)
echo "emonSD version: $image_version"
echo

if [ "$image_version" == "emonSD-07Nov16" ] || [ "$image_version" == "emonSD-03May16" ] || [ $image_version == "emonSD-26Oct17" ] || [ $image_version == "emonSD-13Jun18" ]; then
echo "emonSD base image check pass...continue update"
else
echo "ERROR: emonSD base image old or undefined...update will not continue"
echo "See latest verson: https://github.com/openenergymonitor/emonpi/wiki/emonSD-pre-built-SD-card-Download-&-Change-Log"
echo "Stopping update"
exit
fi
echo
echo "#############################################################"

uid=`id -u`
echo "EUID: $uid"

Expand Down
15 changes: 14 additions & 1 deletion factoryreset
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ fi
ans=n
read -p "Shutdown when complete (y/n)? Else rebooting in 10s " -t 10 ans

VERSION=$(sed 's/\..*//' /etc/debian_version)

# if version is greater than 8 then we must be running Stretch or newer
if [ "$VERSION" -gt "8" ]; then
VERSION="stretch"
echo "Detected $VERSION or newer"
else
VERSION="jessie"
echo "$VERSION or older distro detected"
fi


echo "Ensure tracking latest Emoncms & emonpi stable branch"
Expand Down Expand Up @@ -173,7 +182,11 @@ sudo dpkg-reconfigure openssh-server
echo "Clean up packages"
sudo apt-get clean


if [ "$VERSION" = "stretch" ]; then
echo "Disabling SSH"
sudo update-rc.d ssh disable
sudo invoke-rc.d ssh stop
fi

echo
if [[ "$ans" == "" ]] ; then ans=n;fi
Expand Down
2 changes: 1 addition & 1 deletion lcd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Use port 0 for very olde 256Mb RAM pi (rev1) - not recomended to use this pi ves

```
sudo apt-get update
sudo apt-get install python-smbus i2c-tools python-rpi.gpio python-pip redis-server -y
sudo apt-get install python-smbus i2c-tools python-rpi.gpio python-pip redis-server python-gpiozero -y
sudo pip install redis paho-mqtt xmltodict requests
```

Expand Down
7 changes: 7 additions & 0 deletions lcd/disablessh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Disable SSH
sudo update-rc.d ssh disable
sudo invoke-rc.d ssh stop

exit
97 changes: 54 additions & 43 deletions lcd/emonPiLCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import atexit
import os
from select import select
from gpiozero import Button

# Local files
import lcddriver
import gsmhuaweistatus

# ------------------------------------------------------------------------------------
# Script version
version = '2.1.1'
version = '2.2.0'
# ------------------------------------------------------------------------------------

# ------------------------------------------------------------------------------------
Expand Down Expand Up @@ -71,6 +72,19 @@
# ------------------------------------------------------------------------------------
uselogfile = True

# ------------------------------------------------------------------------------------
# Create interrupt call for emonPi LCD button
# ------------------------------------------------------------------------------------
shortPress = False
longPress = False

def buttonPressLong():
global longPress
longPress = True

def buttonPress():
global shortPress
shortPress = True

class IPAddress(object):
def __init__(self):
Expand All @@ -94,7 +108,7 @@ def shutdown(lcd):
lcd[1] = ''.join(str(y) + '..' for y in range(5, 5-x, -1))
time.sleep(1)

if GPIO.input(11) == 0:
if GPIO.input(17) == 0:
return
lcd[1] = "SHUTDOWN NOW!"
time.sleep(2)
Expand Down Expand Up @@ -153,22 +167,9 @@ def backlight(self, state):
def lcd_clear(self):
self.lcd.lcd_clear()


class ButtonInput(object):
def __init__(self, logger, fd):
GPIO.add_event_detect(16, GPIO.RISING, callback=self.buttonPress, bouncetime=1000)
self.logger = logger
self.press_num = 0
self.pressed = False
self.fd = fd

def buttonPress(self, channel):
self.pressed = True
self.logger.info("lcd button press " + str(self.press_num))
self.fd.write('1')


def main():
global longPress
global shortPress
# First set up logging
atexit.register(logging.shutdown)
if not uselogfile:
Expand Down Expand Up @@ -214,20 +215,17 @@ def main():
logger.info("SD card image build version: " + sd_image_version)

# Set up the buttons and install handlers
atexit.register(GPIO.cleanup)
# Use Pi board pin numbers as these as always consistent between revisions
GPIO.setmode(GPIO.BOARD)

# emonPi LCD push button Pin 16 GPIO 23
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# emonPi Shutdown button, Pin 11 GPIO 17
GPIO.setup(11, GPIO.IN)
# Uses gpiozero library to handle short and long press https://gpiozero.readthedocs.io/en/stable/api_input.html?highlight=button
# push_btn = Button(23, pull_up=False, hold_time=5, bounce_time=0.1)
# No bounce time increases responce time but may result in switch bouncing...
push_btn = Button(23, pull_up=False, hold_time=5)
push_btn.when_pressed = buttonPress
push_btn.when_held = buttonPressLong

# Create a pipe with no buffering, make the read end non-blocking and pass
# the other to the ButtonInput class
pipe = os.pipe()
pipe = (os.fdopen(pipe[0], 'r', 0), os.fdopen(pipe[1], 'w', 0))
fcntl.fcntl(pipe[0], fcntl.F_SETFL, os.O_NONBLOCK)
buttoninput = ButtonInput(logger, pipe[1])
# emonPi Shutdown button, Pin 11 GPIO 17
GPIO.setup(17, GPIO.IN)

logger.info("Connecting to redis server...")

Expand All @@ -250,15 +248,17 @@ def on_message(client, userdata, msg):
r.set("basedata", msg.payload)

def on_connect(client, userdata, flags, rc):
mqttc.subscribe(mqtt_topic)
if (rc==0):
mqttc.subscribe(mqtt_topic)

mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect

try:
mqttc.username_pw_set(mqtt_user, mqtt_passwd)
mqttc.connect(mqtt_host, mqtt_port, 60)
mqttc.reconnect_delay_set(min_delay=1, max_delay=120)
mqttc.connect_async(mqtt_host, mqtt_port, 60)
# Run MQTT background thread which handles reconnects
mqttc.loop_start()
except Exception:
Expand All @@ -283,18 +283,36 @@ def on_connect(client, userdata, flags, rc):
if now - buttonPress_time > backlight_timeout and lcd.backlight:
lcd.backlight = 0

if buttoninput.pressed:
buttoninput.pressed = False
if shortPress:
shortPress = False
if lcd.backlight:
page += 1
if page > max_number_pages:
page = 0
buttonPress_time = now
if not lcd.backlight:
lcd.backlight = 1
logger.info("Mode button pressed")
logger.info("Mode button SHORT press")
logger.info("Page: " + str(page))


if longPress:
longPress = False
logger.info("Mode button LONG press")
subprocess.call("/home/pi/emonpi/lcd/enablessh.sh")
logger.info("SSH Enabled")
lcd[0] = 'SSH Access '
lcd[1] = 'Enabled '
time.sleep(2)
lcd[0] = 'Change password '
lcd[1] = 'on first login >'
time.sleep(2)
if eval(r.get("eth:active")):
lcd[0] = 'ss pi@' + r.get("eth:ip")
if eval(r.get("wlan:active")):
lcd[0] = 'pi@' + r.get("wlan:ip")
lcd[1] = 'pass: emonpi2018'
push_btn.wait_for_press()

# Get system parameters and store in redis
# Get uptime
with open('/proc/uptime', 'r') as f:
Expand Down Expand Up @@ -403,17 +421,10 @@ def on_connect(client, userdata, flags, rc):
lcd[1] = sd_image_version

# If Shutdown button is pressed initiate shutdown sequence
if GPIO.input(11) == 1:
if GPIO.input(17) == 1:
logger.info("shutdown button pressed")
shutdown(lcd)

# Wait up to one second or until the button is pressed.
read, _, _ = select([pipe[0]], [], [], 1)
if read: # pipe is readable, consume the byte.
try:
read[0].read(1)
except IOError:
pass

if __name__ == '__main__':
main()
7 changes: 7 additions & 0 deletions lcd/enablessh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Enable SSH
sudo update-rc.d ssh enable
sudo invoke-rc.d ssh start

exit
26 changes: 26 additions & 0 deletions lcd/testing/push_and_hold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Test gpiozero library for push and hold functionality
# https://gpiozero.readthedocs.io/en/stable/api_input.html?highlight=button
from gpiozero import Button
from signal import pause

print ("Push button test")

def buttonPressLong():
print ("long press")

def buttonPress():
print "short press"

push_btn = Button(23, pull_up=False, hold_time=5, bounce_time=0.1)

push_btn.when_pressed = buttonPress
push_btn.when_held = buttonPressLong

i=0

while 1:
i = i +1



#pause()
2 changes: 1 addition & 1 deletion update
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ image_version=$(ls /boot | grep emonSD)
echo "emonSD version: $image_version"
echo

if [ "$image_version" == "emonSD-07Nov16" ] || [ $image_version == "emonSD-03May16" ] || [ $image_version == "emonSD-13Jun18" ]; then
if [ "$image_version" == "emonSD-07Nov16" ] || [ $image_version == "emonSD-03May16" ] || [ $image_version == "emonSD-13Jun18" ] || [ $image_version == "emonSD-18Oct18" ]; then
echo "emonSD base image check pass...continue update"
else
echo "ERROR: emonSD base image old or undefined...update will not continue"
Expand Down