From 5e293bc634e341ea661e9662fb0c40d43f5cbb9f Mon Sep 17 00:00:00 2001 From: Paul Jurek Date: Fri, 27 Dec 2019 14:58:54 -0800 Subject: [PATCH 1/5] commits after making flexible web sever update gitignore to hide passwords and added custom main to hold current main script. --- .gitignore | 1 + main.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa76c2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +passwords.txt diff --git a/main.py b/main.py new file mode 100644 index 0000000..dd66055 --- /dev/null +++ b/main.py @@ -0,0 +1,91 @@ +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 +""" + +import machine +import ntptime, utime +from machine import RTC +from time import sleep + +try: + seconds = ntptime.time() +except: + seconds = 0 + +rtc = RTC() +rtc.datetime(utime.localtime(seconds)) + +def time(): + body = """ + +

Time

+

%s

+ + +""" % str(rtc.datetime()) + + return response_template % body + +def dummy(): + body = "This is a dummy endpoint" + + return response_template % body + +pin = machine.Pin(10, machine.Pin.IN) + +handlers = { + 'time': time, + 'dummy': dummy, +} + +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)) + + client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")])) + + client_s.close() + print() + +main() From bfad6f4a3d12433833e2c7fba494cf38b95b348d Mon Sep 17 00:00:00 2001 From: Paul Jurek Date: Sat, 28 Dec 2019 10:17:37 -0800 Subject: [PATCH 2/5] added light_on and light_off added web control of light on and light off for microcontroller. Pretty cool! --- flexible_web_server/main.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/flexible_web_server/main.py b/flexible_web_server/main.py index d1855c1..d4b3678 100644 --- a/flexible_web_server/main.py +++ b/flexible_web_server/main.py @@ -20,10 +20,12 @@ import machine import ntptime, utime -from machine import RTC +from machine import RTC, Pin from time import sleep rtc = RTC() +led = Pin(9, Pin.OUT) + try: seconds = ntptime.time() except: @@ -46,9 +48,21 @@ def dummy(): return response_template % body +def light_on(): + led.value(1) + body = "You turned a light on!" + return response_template % body + +def light_off(): + led.value(0) + body = "You turned a light off!" + return response_template % body + handlers = { 'time': time, 'dummy': dummy, + 'light_on': light_on, + 'light_off': light_off, } def main(): From 139513e969660f9a1a39b43e84859a6fd8da45da Mon Sep 17 00:00:00 2001 From: Paul Jurek Date: Sat, 28 Dec 2019 10:17:58 -0800 Subject: [PATCH 3/5] made blank main.py so that i can utilized web repel --- main.py | 92 +-------------------------------------------------------- 1 file changed, 1 insertion(+), 91 deletions(-) diff --git a/main.py b/main.py index dd66055..6ecc2d8 100644 --- a/main.py +++ b/main.py @@ -1,91 +1 @@ -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 -""" - -import machine -import ntptime, utime -from machine import RTC -from time import sleep - -try: - seconds = ntptime.time() -except: - seconds = 0 - -rtc = RTC() -rtc.datetime(utime.localtime(seconds)) - -def time(): - body = """ - -

Time

-

%s

- - -""" % str(rtc.datetime()) - - return response_template % body - -def dummy(): - body = "This is a dummy endpoint" - - return response_template % body - -pin = machine.Pin(10, machine.Pin.IN) - -handlers = { - 'time': time, - 'dummy': dummy, -} - -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)) - - client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")])) - - client_s.close() - print() - -main() +import machine \ No newline at end of file From 51a80182b00199b30d383c75dc146352b10aacc6 Mon Sep 17 00:00:00 2001 From: Paul Jurek Date: Sun, 29 Dec 2019 22:38:39 -0800 Subject: [PATCH 4/5] added swtich to web controller added endpoint /switch which prints out state of swtich on controller. --- flexible_web_server/main.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/flexible_web_server/main.py b/flexible_web_server/main.py index d4b3678..66c952f 100644 --- a/flexible_web_server/main.py +++ b/flexible_web_server/main.py @@ -22,9 +22,11 @@ import ntptime, utime from machine import RTC, Pin from time import sleep +import ujson rtc = RTC() led = Pin(9, Pin.OUT) +switch_pin = Pin(10, Pin.IN) try: seconds = ntptime.time() @@ -58,11 +60,20 @@ def light_off(): body = "You turned a light off!" return response_template % body +def switch(): + """returns switch state""" + switch_state = switch_pin.value() + body = "The swtich is {}".format(switch_state) + return response_template % body + + + handlers = { 'time': time, 'dummy': dummy, 'light_on': light_on, 'light_off': light_off, + 'switch': switch, } def main(): From 938790412856808bc31a464782c2222ef408b253 Mon Sep 17 00:00:00 2001 From: Paul Jurek Date: Mon, 30 Dec 2019 21:25:25 -0800 Subject: [PATCH 5/5] updated the web server to read analog this is through the temperature endpoint. --- flexible_web_server/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/flexible_web_server/main.py b/flexible_web_server/main.py index 66c952f..9f48a26 100644 --- a/flexible_web_server/main.py +++ b/flexible_web_server/main.py @@ -22,11 +22,12 @@ import ntptime, utime from machine import RTC, Pin from time import sleep -import ujson rtc = RTC() led = Pin(9, Pin.OUT) switch_pin = Pin(10, Pin.IN) +temp_pin = machine.ADC(0) +print(temp_pin.read()) try: seconds = ntptime.time() @@ -66,6 +67,10 @@ def switch(): body = "The swtich is {}".format(switch_state) return response_template % body +def temperature(): + """measures the value of temp from our sensor""" + body = "{value: " + str(temp_pin.read()) + "}" + return response_template % body handlers = { @@ -74,6 +79,7 @@ def switch(): 'light_on': light_on, 'light_off': light_off, 'switch': switch, + 'temperature': temperature, } def main():