Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Practice_Activity_Lesson10 _FP #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
File renamed without changes.
3 changes: 1 addition & 2 deletions passwords.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
EarlGreyTea HotHotHot
MyHomeAccessPoint aueiUeh73NB
Andromeda <here_was_my_home_router_password>
Binary file added photos/Espressif_ESP8266.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/PWM_1000.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/PWM_500.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/photoresistor.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 47 additions & 5 deletions web_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,60 @@ def time():
</body>
</html>
""" % str(rtc.datetime())

return response_template % body

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

return response_template % body
# ------------------------------------

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

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

def light_off():
on_off_pin.value(1)
body = "You turned a light off!"
return response_template % body
# ------------------------------------

switch_pin = machine.Pin(5, machine.Pin.IN)
def switch():
body = "{state: " + str(switch_pin.value()) + "}"
return response_template % body

pin = machine.Pin(10, machine.Pin.IN)
# Port 10 is always in high impedance mode on my microcontroller,
# which prevents wifi connection to router. Instead, I used port 5 (GPI05).
# ------------------------------------

adc_pin = machine.ADC(0)
def light():
body = "{value: " + str(adc_pin.read()) + "}"
return response_template % body
# ------------------------------------

pwm_pin = machine.PWM(machine.Pin(13))
dty=500

def pwm():
pwm_pin.duty(dty)
body = "{pwm duty: " + str(dty) + "}"
return response_template % body
# ------------------------------------

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

def main():
s = socket.socket()
Expand All @@ -66,6 +106,7 @@ def main():
print("Listening, connect your browser to http://<this_host>:8080")

while True:
sleep(0.5)
res = s.accept()
client_s = res[0]
client_addr = res[1]
Expand All @@ -87,5 +128,6 @@ def main():

client_s.close()
print()

# -------------------------------------
main()