feat: added file upload

This commit is contained in:
Elias Renman
2023-05-21 21:24:02 +02:00
parent a9a1915a5e
commit 7cb963bb24
8 changed files with 86 additions and 46 deletions

View File

@@ -1,4 +0,0 @@
from wireless import enableWireless
enableWireless()

View File

@@ -1,2 +0,0 @@
ssid = "ssid"
password = "password"

2
pico-w/makefile Normal file
View File

@@ -0,0 +1,2 @@
upload:
cd src && mpremote cp -r ./*.py :

5
pico-w/requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
importlib-metadata==6.6.0
mpremote==1.20.0
pyserial==3.5
pyudev==0.24.1
zipp==3.15.0

22
pico-w/src/boot.py Normal file
View File

@@ -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])

View File

@@ -0,0 +1,5 @@
from micropython import const
ssid = const("ssid")
password = const("password")

52
pico-w/src/main.py Normal file
View File

@@ -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')

View File

@@ -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])