diff --git a/docs/auto_wifi_setup.md b/docs/auto_wifi_setup.md index 6d14db1..f096260 100644 --- a/docs/auto_wifi_setup.md +++ b/docs/auto_wifi_setup.md @@ -6,9 +6,9 @@ Can be used for setup and/or re-configuring WiFi if something changes. ``` sh -git clone https://git.kemonine.info/PiFrame/piframe.git /opt/piframe -cp /opt/piframe/wifi_setup.py /usr/local/bin -chmod a+x /usr/local/bin/wifi_setup.py +DL_URL=$(curl https://git.kemonine.info/api/v1/repos/PiFrame/piframe-go/releases | jq -r '.[0].assets[] | select(.name == "wifi") | .browser_download_url') +wget $DL_URL -O /usr/local/bin/pf-wifi +chmod a+x /usr/local/bin/pf-wifi cat > /etc/systemd/system/wifi_setup.service < /etc/systemd/system/lcd_control.service < /etc/systemd/system/inotify-pictures.service <=4: - addresses.append(addr[3].split('/')[0]) - - # Write some text - lux = 'Light: %.2f Lux' % sensor.lux - y = top - draw.text((x, y), lux, font=font_small, fill='#DCDCDC') - for addr in addresses: - y += font_small.getsize('0')[1] - draw.text((x, y), addr, font=font_small, fill='#DCDCDC') - - # Display image. - disp.image(image, rotation) - -######################################## -# Method for drawing on the screen -# Called by the button handlers since we don't need anything more than a basic while(1) main for this setup -######################################## -def refresh_screen(screen): - global draw, width, height - # Draw a black filled box to clear the image. - draw.rectangle((0, 0, width, height), outline=0, fill=0) - - if screen == 'main': - draw_main_ui() - elif screen == 'debug': - draw_debug_ui() - elif screen == 'reboot': - draw_reboot_ui() - elif screen == 'poweroff': - draw_poweroff_ui() - elif screen == 'rebooting': - draw_rebooting_ui() - elif screen == 'poweringoff': - draw_poweringoff_ui() - - -######################################## -# Write current album when restarting the slideshow -######################################## -def write_selected_album(): - with open('/etc/default/fim_album_index', 'w') as f: - f.write(str(selected_album)) - with open('/etc/default/fim_album', 'w') as f: - if not selected_album == 0: - f.write('/tank/pictures/' + albums[selected_album]) - else: - f.write('/tank/pictures') - -######################################## -# Button held tracking date/times -######################################## -up_held_rising = None -dn_held_rising = None -btn_is_prompting = False - -######################################## -# Interrupt callback for changing albums (up) -######################################## -def up_button(channel): - global selected_album, albums, backlight, up_held_rising, btn_is_prompting, current_ui - backlight.value = True - # Pull ups on the LCD flip the standard logic you're expecting - # Button Released - if not GPIO.input(channel): - up_held_rising = datetime.now() - # Button pressed - else: - if btn_is_prompting: - if current_ui == 'debug': - btn_is_prompting = False - up_held_rising = None - refresh_screen('main') - return - if current_ui == 'reboot': - refresh_screen('rebooting') - os.system('/usr/bin/systemctl reboot') - elif current_ui == 'poweroff': - refresh_screen('poweringoff') - os.system('/usr/bin/systemctl poweroff') - btn_is_prompting = False - return - if up_held_rising is not None: - now = datetime.now() - diff = (now - up_held_rising).total_seconds() - if diff >= 3: - up_held_rising = None - btn_is_prompting = True - refresh_screen('reboot') - return - selected_album += 1 - if selected_album >= len(albums): - selected_album = 0 - refresh_screen('main') - write_selected_album() - watchdog.reset() - -######################################## -# Interrupt callback for changing albums (dn) -######################################## -def dn_button(channel): - global selected_album, albums, backlight, dn_held_rising, btn_is_prompting - backlight.value = True - # Pull ups on the LCD flip the standard logic you're expecting - # Button released - if not GPIO.input(channel): - dn_held_rising = datetime.now() - # Button pressed - else: - if btn_is_prompting: - # Show debug menu if long pressed dn on reboot/shutdown screens - if dn_held_rising is not None: - now = datetime.now() - diff = (now - dn_held_rising).total_seconds() - if diff >= 3: - dn_held_rising = None - btn_is_prompting = True - refresh_screen('debug') - return - btn_is_prompting = False - refresh_screen('main') - if dn_held_rising is not None: - now = datetime.now() - diff = (now - dn_held_rising).total_seconds() - if diff >= 3: - dn_held_rising = None - btn_is_prompting = True - refresh_screen('poweroff') - return - selected_album -= 1 - if selected_album < 0: - selected_album = len(albums) - 1 - refresh_screen('main') - write_selected_album() - watchdog.reset() - -######################################## -# Setup button interrupts -######################################## -GPIO.add_event_detect(23, GPIO.BOTH, callback=up_button, bouncetime=250) -GPIO.add_event_detect(24, GPIO.BOTH, callback=dn_button, bouncetime=250) - -######################################## -# Draw initial screen state -######################################## -refresh_screen(current_ui) - -######################################## -# Kick off watchdog to start the 10s initial 'on' status -######################################## -watchdog.reset() - -######################################## -# We use watchdogs/interrupts for ALL operations, no need for anything in a main loop other than keeping the app alive -######################################## -while True: - time.sleep(300) diff --git a/wifi_setup.py b/wifi_setup.py deleted file mode 100644 index e813b3d..0000000 --- a/wifi_setup.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- - -import os -import subprocess - -# Make sure mount point exists -if not os.path.isdir('/var/wifi'): - os.mkdir('/var/wifi') - -# Scan unmounted disks/partitions to see if we need to reconfig wifi -blkids = subprocess.run(['/usr/sbin/blkid', '-o', 'device'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) -for blkid in blkids.stdout.split('\n'): - blkid = blkid.strip() - if not blkid: - continue - # Verify the disk/partition isn't mounted - findmnt = subprocess.run(['/usr/bin/findmnt', '-n', '-o', 'TARGET', blkid], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) - mnt = findmnt.stdout.strip() - if not mnt and not 'zram' in blkid: - print('checking ' + blkid) - try: - # Mount the disk - subprocess.run(['/usr/bin/mount', blkid, '/var/wifi/'], universal_newlines=True) - # Check if the wifi.txt confg file is present (line 1 is essid, line 2 is PSK) - if os.path.exists('/var/wifi/wifi.txt'): - # Get wifi config - essid = '' - password = '' - with open('/var/wifi/wifi.txt', 'r') as f: - essid = f.readline().strip() - password = f.readline().strip() - if essid and password: - print('Found wifi config (' + essid + ' / ' + password + ')') - # Look for existing wifi connections that should be cleaned up - nmcli = subprocess.run(['/usr/bin/nmcli', '-t', 'connection', 'show'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) - for connection in nmcli.stdout.split('\n'): - # Ignore empty lines in output - if not connection: - continue - connection_details = connection.split(':') - if connection_details[2] == '802-11-wireless': - # If we cannot clean up a wifi connection assume it's a non-issue - try: - # Cleanup existing wifi connection - print('cleaning up wifi connection ' + connection_details[0]) - subprocess.run(['/usr/bin/nmcli', 'connection', 'del', connection_details[1]], universal_newlines=True) - except: - pass - print('Connecting to ' + essid + ' with password ' + password) - subprocess.run(['/usr/bin/nmcli', 'd', 'wifi', 'connect', essid, 'password', password], universal_newlines=True) - # Unmount disk so it's safe to remove - subprocess.run(['/usr/bin/umount', '/var/wifi/'], universal_newlines=True) - except: - # Unmount disk so it's safe to remove - subprocess.run(['/usr/bin/umount', '/var/wifi/'], universal_newlines=True)