diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..40c1db9 Binary files /dev/null and b/.DS_Store differ diff --git a/boot.py b/boot.py index 91135a5..fc0311e 100644 --- a/boot.py +++ b/boot.py @@ -5,7 +5,6 @@ import network import time -import machine sta_if = network.WLAN(network.STA_IF); sta_if.active(True) @@ -36,3 +35,7 @@ break else: print("Connection could not be made.\n") + + +if sta_if.isconnected(): + print("Connected as: {}".format(sta_if.ifconfig()[0])) \ No newline at end of file diff --git a/demo_server/main.py b/demo_server/main.py new file mode 100644 index 0000000..6fdedef --- /dev/null +++ b/demo_server/main.py @@ -0,0 +1,125 @@ +import machine +import ntptime +import utime +from machine import RTC, Pin, ADC + +try: + import usocket as socket +except: + import socket + +response_404 = """HTTP/1.0 404 NOT FOUND + +

404 Not Found

+""" + +response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR + +

500 Internal Server Error

+""" + +response_template = """HTTP/1.0 200 OK + +%s +""" + +seconds = ntptime.time() +rtc = RTC() +rtc.datetime(utime.localtime(seconds)) + +adc = ADC(0) + +pin = Pin(0, Pin.OUT) # had problems with GPIO9, so I used GPIO0 for this part. + +switch_pin = Pin(10, Pin.IN) + + +def time(): + body = """ + +

Time

+

%s

+ + + """ % str(rtc.datetime()) + + return response_template % body + + +def dummy(): + body = "This is a dummy endpoint" + + return response_template % body + + +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 + + +def switch(): + body = "{state: " + str(switch_pin.value()) + "}" + return response_template % body + + +def light(): + body = "{value: " + str(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) + addr = ai[0][-1] + + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + s.bind(addr) + s.listen(5) + print("Listening, connect your browser to http://:8080/") + + while True: + res = s.accept() + client_s = res[0] + client_addr = res[1] + req = client_s.recv(4096) + print("Request:") + print(req) + + try: + path = req.decode().split("\r\n")[0].split(" ")[1] + handler = handlers[path.strip('/').split('/')[0]] + response = handler() + except KeyError: + response = response_404 + except Exception as e: + response = response_500 + print(str(e)) + + # 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() diff --git a/esp8266-20200421-v1.12-388-g388d419ba.bin b/esp8266-20200421-v1.12-388-g388d419ba.bin new file mode 100644 index 0000000..84e0c8c Binary files /dev/null and b/esp8266-20200421-v1.12-388-g388d419ba.bin differ diff --git a/passwords.txt b/passwords.txt index 9f2747f..a1d28db 100644 --- a/passwords.txt +++ b/passwords.txt @@ -1,2 +1 @@ -EarlGreyTea HotHotHot -MyHomeAccessPoint aueiUeh73NB +deleted_my_network deleted_my_password \ No newline at end of file