From 0cd8ee12193002a9ea63bfb72b987526a2719425 Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Sun, 30 Aug 2020 01:54:29 +0300 Subject: [PATCH] Version 0.9, mostly works, REST API for changing settings is clunky --- LED.py | 76 ++++++++++++++++++++++++++++++ boot.py | 18 +++---- garage.py | 128 +++++++++++++++++++++++++++++++++++++------------- main.py | 95 ++++++++++++++++++++----------------- settings.json | 18 ++----- 5 files changed, 238 insertions(+), 97 deletions(-) create mode 100644 LED.py diff --git a/LED.py b/LED.py new file mode 100644 index 0000000..788fc5f --- /dev/null +++ b/LED.py @@ -0,0 +1,76 @@ +from machine import Pin +from time import sleep + +LEDPin=Pin(2,Pin.OUT) + +def Blink(number,timeon=0.2,timeoff=0.2): + n=0 + while n

Hi George!

Push the garage door button\n') - - -# Another one, more complicated page -@app.route('/button/') -async def table(request, response,nr): - # Start HTTP response with content-type text/html - Button.off() - sleep(1) - Button.on() - await response.start_html() - await response.send('

Hi George!

Button number %s pushed. Go back. Or push it again.\n'%nr) - - -def run(): - print ("running app") - app.run(host='0.0.0.0', port=80) +import tinyweb +from machine import Pin +from machine import reset +from time import sleep +import json + +settings=json.loads(''.join(open('settings.json').readlines())) + +# Create web server application +app = tinyweb.webserver() +Button={} +for name in settings["buttons"].keys(): + Button[name]=Pin(settings["buttons"][name]["pin"],Pin.OUT) + Button[name].off() + +# Index page +@app.route('/') +async def index(request, response): + # Start HTTP response with content-type text/html + await response.start_html() + # Send actual HTML page + await response.send(''' + +

George\'s Garage

+ + + + \n''') + +@app.route('/reset') +async def index(request, response): + # Start HTTP response with content-type text/html + await response.start_html() + # Send actual HTML page + await response.send(''' + +

|RESETTING...

+ \n''') + reset() + +@app.route('/button/') +async def button(request, response,nr): + # Start HTTP response with content-type text/html + Button[nr].on() + sleep(settings['buttons'][nr]['delay']) + Button[str(nr)].off() + print ('button %s (Pin %s) pressed for %s seconds'%(nr,Button[nr],settings['buttons'][nr]['delay'])) + await response.start_html() + await response.send('

Hi George!

Button number %s pushed for %s seconds. Go back.\n'%(nr,settings['buttons'][str(nr)]['delay'])) + +def Write_Settings(): + File=open('settings.json','w') + File.write(json.dumps(settings)) + File.close() + +#@app.route('/config',save_headers=[]) +class config(): + def get(self,data): + SanitizedSettings=settings.copy() + if 'password' in SanitizedSettings: + SanitizedSettings['password']='*not displayed*' + if 'AP-password' in SanitizedSettings: + SanitizedSettings['AP-password']='*not displayed*' + return SanitizedSettings + def post(self, data): + print (data) + for Setting in data.keys(): + settings[Setting]=data[Setting] + print("Setting %s to %s"%(Setting,data[Setting])) + Write_Settings() + return data + + def delete(self,data): + for Setting in data.keys(): + settings.pop(Setting) + Write_Settings() + return data + +#await response.start_html() +# print (request.query_string) +# a=tinyweb.server.parse_query_string(request.query_string) +# print (a) +# configs=str(a) +# await response.send(configs) + + +def run(): + print ("running app") + app.add_resource(config,"/config") + app.run(host='0.0.0.0', port=80) + +# +# form='''

Settings

''' +# form += ""%settings['AP-ssid'] +# form +="
AP-ssid%s
" +# return form \ No newline at end of file diff --git a/main.py b/main.py index 6cea21b..6c4df38 100644 --- a/main.py +++ b/main.py @@ -1,42 +1,53 @@ -import network -import json -import time - -settings=json.loads(''.join(open('settings.json').readlines())) - -def netstop(): - n=network.WLAN(network.STA_IF) - n.active(False) - n=network.WLAN(network.AP_IF) - n.active(False) - -def STA(): - sta=network.WLAN(network.STA_IF) - if sta.isconnected(): - return True - sta.active(True) - if ('NetMask') in settings and 'DNS' in settings and 'Gateway' in settings and 'IP' in settings: - print("Static configuration active") - sta.ifconfig((settings["IP"],settings["NetMask"],settings["Gateway"],settings["DNS"])) - else: - print("DHCP active") - sta.connect(settings['ssid'],settings['password']) - timeout=0 - while timeout < 60: - if sta.isconnected(): - return True - else: - time.sleep(1) - timeout += 1 - print("Time waiting: %s sec"%timeout) - sta.active(False) - return False - -def AP(): - ap=network.WLAN(network.AP_IF) - ap.active(True) - ap.config(essid=settings['AP-ssid'],password=settings['AP-password']) - print("AP configured") - -import garage -#garage.run() +import network +import json +import time +from machine import Pin +import LED +import garage + +#import settings +settings=json.loads(''.join(open('settings.json').readlines())) + +def netstop(): + n=network.WLAN(network.STA_IF) + n.active(False) + n=network.WLAN(network.AP_IF) + n.active(False) + +def STA(): + """Connects Board as Client to WiFi-Network. LED.Blinks 5 times short if successful""" + sta=network.WLAN(network.STA_IF) + if sta.isconnected(): + LED.Blink(5) + return True + sta.active(True) + if ('NetMask') in settings and 'DNS' in settings and 'Gateway' in settings and 'IP' in settings: + print("Static configuration active") + sta.ifconfig((settings["IP"],settings["NetMask"],settings["Gateway"],settings["DNS"])) + else: + print("DHCP active") + sta.connect(settings['ssid'],settings['password']) + timeout=0 + while timeout < 30: + if sta.isconnected(): + LED.Morse('ip %s'%sta.ifconfig()[0],1) + return True + else: + LED.Blink(1,.05,.95) + timeout += 1 + print("Time waiting: %s sec"%timeout) + sta.active(False) + return False + +def AP(): + """Provides Access point with settings in settings.json. LED.Blinks 3 long, 3 short on success""" + ap=network.WLAN(network.AP_IF) + ap.active(True) + ap.config(essid=settings['AP-ssid'],password=settings['AP-password']) + print("AP configured") + LED.Morse ('AP') + +netstop() +if STA() != True: + AP() +garage.run() diff --git a/settings.json b/settings.json index d509101..017a1e7 100644 --- a/settings.json +++ b/settings.json @@ -1,14 +1,4 @@ -{"password": "blueground856", - "AP-password": "OpenSesame2020", - "DNS": "8.8.8.8", "IP": - "192.168.1.234", - "AP-ssid": "Georges Garage Door Prototype", - "NetMask": "255.255.255.0", - "buttons": { - "2": {"pin": 3, "delay": 2}, - "1": {"pin": 2, "delay": 2}, - "3": {"pin": 4, "delay": 2}, - }, - "ssid": "BLUEGROUND 856", - "Gateway": "192.168.1.1" - } \ No newline at end of file +{"password": "blueground856", + "DNS": "8.8.8.8", + "AP-password": "OpenSesame2020", + "buttons": {"1": {"pin": 5, "delay": 2}, "3": {"pin": 13, "delay": 2}, "2": {"pin": 4, "delay": 2}}, "AP-ssid": "Georges Garage Door Prototype", "NetMask": "255.255.255.0", "ssid": "BLUEGROUND 856", "Gateway": "192.168.1.1"} \ No newline at end of file