diff --git a/install/main.py b/install/main.py index f3ce548..0ea8058 100644 --- a/install/main.py +++ b/install/main.py @@ -3,6 +3,7 @@ import machine import json import sys +import utils from utime import sleep from micropyserver import MicroPyServer import time @@ -10,6 +11,19 @@ import ustruct as struct NTP_HOST = 'pool.ntp.org' +userconfig = {} +wifi = {} + +outlets = [ + { 'pin': machine.Pin(18, mode=machine.Pin.OUT), 'on': False }, + { 'pin': machine.Pin(19, mode=machine.Pin.OUT), 'on': False }, + { 'pin': machine.Pin(20, mode=machine.Pin.OUT), 'on': False }, + { 'pin': machine.Pin(21, mode=machine.Pin.OUT), 'on': False }, + { 'pin': machine.Pin(22, mode=machine.Pin.OUT), 'on': False } +] + +def asbool(s): + return s.lower() in {'true', 'yes', '1', 'on'} def blink(timer): led.toggle() @@ -42,13 +56,17 @@ def setup_ap(config): print(wap.ifconfig()) def run(config): - network.country = config.user["country"] + userconfig = config.user + network.country(config.user["country"]) wifi = network.WLAN(network.STA_IF) wifi.config(pm = 0xa11140) wifi.active(True) wifi.connect(config.user["ssid"], config.user["pwd"], hostname=config.user["device_name"]) - while not wifi.isconnected() and wifi.status() >= 0: - time.sleep(1) + for wait_counter in range(10): + if not wifi.isconnected() and wifi.status() >= 0: + time.sleep(1) + else: + break setTimeRTC() return @@ -59,7 +77,11 @@ def getmachineid(): return mid def setup(request): - server.send("
") + file = open('setup.html.txt', 'r') + html = file.read() + file.close() + server.send(html) + return def save_config(request): if utils.get_request_method(request) != 'POST': @@ -96,13 +118,59 @@ def status(request): server.send("") def control(request): + qparams = utils.get_request_query_params(request) + pparams = utils.get_request_post_params(request) + key = userconfig.user['id_key'] + if key != '' and not ('id_key' in pparams or pparams['id_key'] != key): + server.send("invalid key") + return + if \ + 'plug' in qparams \ + and qparams['plug'].isnumeric() \ + and (i := int(params['plug'])) >= 0 \ + and i <= 5: + if 'on' in pparams: + control_plug(i, asbool(pparams['on'])) + server.send(json.dumps(get_states())) + return + else: + server.send("Invalid state") + return + else: + server.send("invalid plug id") + return + + server.send("") +def get_states(): + states = { + 'plugs': { + '1': { 'on': outlets[0]['on'] }, + '2': { 'on': outlets[1]['on'] }, + '3': { 'on': outlets[2]['on'] }, + '4': { 'on': outlets[3]['on'] }, + '5': { 'on': outlets[4]['on'] } + }, + 'ip': wifi.ifconfig()[0], + 'device_name': userconfig['device_name'] + } + return states + def control_plug(plug_index, val): - + i = plug_index - 1 + if plug_index == -1: + for j in range(5): + control_plug(j, val) + return + else: + if val: + outlets[i]['pin'].on() + else: + outlets[i]['pin'].off() + outlets[i]['on'] = val led = machine.Pin("LED", machine.Pin.OUT) -plugs = [False, False, False, False, False] server = MicroPyServer() try: @@ -115,7 +183,7 @@ def control_plug(plug_index, val): server.add_route("/control", control) timer.deinit() -except: +except ImportError: timer = machine.Timer() timer.init(freq=.5, mode=machine.Timer.PERIODIC, callback=blink) import initial_config @@ -123,5 +191,8 @@ def control_plug(plug_index, val): server.add_route("/", setup) server.add_route("/save_config", save_config) +except Exception as err: + print(f"Unexpected {err=}, {type(err)=}") + server.start() diff --git a/install/setup.html.txt b/install/setup.html.txt new file mode 100644 index 0000000..053d319 --- /dev/null +++ b/install/setup.html.txt @@ -0,0 +1,47 @@ + + + + + + + + \ No newline at end of file