From 2a0ba5c7eaebca926fc8c2ae23a369dcc9fadbd0 Mon Sep 17 00:00:00 2001 From: Colleen Kaku Date: Wed, 24 Mar 2021 16:45:12 -0700 Subject: [PATCH] Completed Lesson 10 activities. --- boot.py | 18 ++++++++++++++++- web_server/main.py | 48 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/boot.py b/boot.py index 91135a5..6cbf163 100644 --- a/boot.py +++ b/boot.py @@ -7,6 +7,9 @@ import time import machine +led_power = machine.Pin(2, machine.Pin.OUT) +led_power.value(0) + sta_if = network.WLAN(network.STA_IF); sta_if.active(True) try: @@ -24,7 +27,7 @@ sta_if.connect(station, password) - for i in range(15): + for i in range(30): print(".") if sta_if.isconnected(): @@ -36,3 +39,16 @@ break else: print("Connection could not be made.\n") + +if sta_if.isconnected(): + print("Connected as: {}".format(sta_if.ifconfig()[0])) + + led_wifi = machine.Pin(16, machine.Pin.OUT) + + for i in range(3): + time.sleep(.5) + led_wifi.value(1) + time.sleep(.5) + led_wifi.value(0) + + diff --git a/web_server/main.py b/web_server/main.py index dd66055..15ab4dc 100644 --- a/web_server/main.py +++ b/web_server/main.py @@ -47,11 +47,57 @@ def dummy(): return response_template % body -pin = machine.Pin(10, machine.Pin.IN) +led = machine.Pin(9, machine.Pin.OUT) + +def light_on(): + led.value(1) + body = "You turned the light on!" + return response_template % body + +def light_off(): + led.value(0) + body = "You turned the light off!" + return response_template % body + +def switch(): + body = "{{state:{}}}".format(machine.Pin(10, machine.Pin.IN).value()) + return response_template % body + +adc = machine.ADC(0) + +def light(): + body = "{{value:{}}}".format(adc.read()) + return response_template % body + +pwm = machine.Pin(13) +pwm = machine.PWM(pwm) +pwm.duty(1024) + +def bright(): + pwm.duty(0) + body = "The LED is bright." + return response_template % body + +def medium_bright(): + pwm.duty(500) + body = "The LED is medium brightness." + return response_template % body + +def dim(): + pwm.duty(1000) + body = "The LED is dim." + return response_template % body handlers = { 'time': time, 'dummy': dummy, + 'light_on': light_on, + 'light_off': light_off, + 'switch': switch, + 'light': light, + 'bright': bright, + 'medium_bright': medium_bright, + 'dim': dim, } def main():