Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@
break
else:
print("Connection could not be made.\n")

if sta_if.isconnected():
print("Connected as: {}".format(sta_if.ifconfig()[0]))
74 changes: 59 additions & 15 deletions simple_web_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,61 @@
import machine
import ntptime, utime
from machine import RTC
from time import sleep

seconds = ntptime.time()
rtc = RTC()
try:
seconds = ntptime.time()
except:
seconds = 0
rtc.datetime(utime.localtime(seconds))

adc = machine.ADC(0)

def time():
body = """<html>
<body>
<h1>Time</h1>
<p>%s</p>
</body>
</html>
""" % str(rtc.datetime())
<body>
<h1>Time</h1>
<p>%s</p>
</body>
</html>
""" % str(rtc.datetime())

return response_template % body


def dummy():
body = "This is a dummy endpoint"

return response_template % body

pin = machine.Pin(16, machine.Pin.OUT)

def light_on:
pin.value(1)
body = "You turned a light on!"
return response_template % body

def light_off:
pin.value(0)
body = "You turned a light off!"
return response_template % body
switch_pin = machine.Pin(10, machine.Pin.IN)

def switch():
body = "{state: " . switch_pin.value() . "}"
return response_template % body

adc = machine.ADC(0)

def light:
body = "{value: " . adc.read() . "}"
return response_template % body

handlers = {
'time': time,
'dummy': dummy,
'light_on': light_on,
'light_off': light_off,
'switch': switch,
'light': light,
}

def main():
s = socket.socket()
ai = socket.getaddrinfo("0.0.0.0", 8080)
Expand All @@ -42,19 +77,28 @@ def main():
print("Listening, connect your browser to http://<this_host>:8080/")

while True:
sleep(.5)
res = s.accept()
client_s = res[0]
client_addr = res[1]
req = client_s.recv(4096)
print("Request:")
print(req)

response = time()
# The first line of a request looks like "GET /arbitrary/path/ HTTP/1.1".
# This grabs that first line and whittles it down to just "/arbitrary/path/"
path = req.decode().split("\r\n")[0].split(" ")[1]

# Given the path, identify the correct handler to use
handler = handlers[path.strip('/').split('/')[0]]

response = handler()

# A handler returns an entire response in the form of a multi-line string.
# This breaks up the response into single strings, byte-encodes them, and
# joins them back together with b"\r\n". Then it sends that to the client.
client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")]))

client_s.close()
print()

main()
main()