diff --git a/pico-w/boot.py b/pico-w/boot.py deleted file mode 100644 index 2732d2a..0000000 --- a/pico-w/boot.py +++ /dev/null @@ -1,4 +0,0 @@ -from wireless import enableWireless - - -enableWireless() diff --git a/pico-w/config.py.example b/pico-w/config.py.example deleted file mode 100644 index 4394a39..0000000 --- a/pico-w/config.py.example +++ /dev/null @@ -1,2 +0,0 @@ -ssid = "ssid" -password = "password" diff --git a/pico-w/makefile b/pico-w/makefile new file mode 100644 index 0000000..5f1b466 --- /dev/null +++ b/pico-w/makefile @@ -0,0 +1,2 @@ +upload: + cd src && mpremote cp -r ./*.py : \ No newline at end of file diff --git a/pico-w/requirements.txt b/pico-w/requirements.txt new file mode 100644 index 0000000..9563cb4 --- /dev/null +++ b/pico-w/requirements.txt @@ -0,0 +1,5 @@ +importlib-metadata==6.6.0 +mpremote==1.20.0 +pyserial==3.5 +pyudev==0.24.1 +zipp==3.15.0 diff --git a/pico-w/src/boot.py b/pico-w/src/boot.py new file mode 100644 index 0000000..ecf5a53 --- /dev/null +++ b/pico-w/src/boot.py @@ -0,0 +1,22 @@ +import network +import time +from config import ssid, password + +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(ssid, password) + +max_wait = 10 +while max_wait > 0: + if wlan.status() < 0 or wlan.status() >= 3: + break + max_wait -= 1 + print('Attempting to connect to Wifi...') + time.sleep(1) + +if wlan.status() != 3: + raise RuntimeError('Wifi connection failed') +else: + print('Wifi connection established') + status = wlan.ifconfig() + print('ip:' + status[0]) diff --git a/pico-w/src/config.py.example b/pico-w/src/config.py.example new file mode 100644 index 0000000..46edddc --- /dev/null +++ b/pico-w/src/config.py.example @@ -0,0 +1,5 @@ +from micropython import const + + +ssid = const("ssid") +password = const("password") diff --git a/pico-w/src/main.py b/pico-w/src/main.py new file mode 100644 index 0000000..28c07d6 --- /dev/null +++ b/pico-w/src/main.py @@ -0,0 +1,52 @@ +import socket +from machine import Pin +from config import ssid, password + +led = Pin("LED", Pin.OUT) + +print("Successfully started pico...") + +addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] + +s = socket.socket() +s.bind(addr) +s.listen(1) + +print('listening on', addr) + + +def respond(cl, status, response): + cl.send(f'HTTP/1.0 {status} OK\r\nContent-type: text/html\r\n\r\n') + cl.send(response) + cl.close() + + +# Listen for connections +while True: + try: + cl, addr = s.accept() + print('client connected from', addr) + request = cl.recv(1024) + print(request) + + request = str(request) + led_on = request.find('/light/on') + led_off = request.find('/light/off') + print('led on = ' + str(led_on)) + print('led off = ' + str(led_off)) + + if led_on == 6: + print("led on") + led.value(1) + respond(cl, 200, '{"led": "on"}') + + if led_off == 6: + print("led off") + led.value(0) + respond(cl, 200, '{"led": "off"}') + + except OSError as e: + cl.close() + s.close() + + print('connection closed') diff --git a/pico-w/wireless.py b/pico-w/wireless.py deleted file mode 100644 index 65aaa47..0000000 --- a/pico-w/wireless.py +++ /dev/null @@ -1,40 +0,0 @@ -import time -import network -from machine import Pin -from config import ssid, password - - -def enableWireless(): - - led = Pin("LED", Pin.OUT) - led.value(1) # LED On - led.value(0) # LED Off - - wlan = network.WLAN(network.STA_IF) - wlan.active(True) - wlan.connect(ssid, password) - - # Wait for connect or fail - max_wait = 10 - while max_wait > 0: - if wlan.status() < 0 or wlan.status() >= 3: - break - max_wait -= 1 - print('waiting for connection...') - time.sleep(1) - - # Handle connection error - if wlan.status() != 3: - raise RuntimeError('network connection failed') - else: - s = 3 - while s > 0: - s -= 1 - led.value(1) - time.sleep(0.5) - led.value(0) - time.sleep(0.5) - - # print('connected') - status = wlan.ifconfig() - print('Connected to ' + ssid + '. ' + 'Device IP: ' + status[0])