Page 14 of 15
Page 308
import uasyncio
from machine import Pin
async def blink(led, period_ms):
while True:
led.on()
await uasyncio.sleep_ms(5)
led.off()
await uasyncio.sleep_ms(period_ms)
async def main(led1, led2):
uasyncio.create_task(blink(led1, 700))
uasyncio.create_task(blink(led2, 400))
await uasyncio.sleep_ms(10_000)
uasyncio.run(main(Pin(2,Pin.OUT), Pin(4,Pin.OUT)))
Page 309
import uasyncio
from machine import Pin
from time import sleep_ms
async def blink(led, period_ms):
while True:
led.on()
await uasyncio.sleep_ms(5)
led.off()
await uasyncio.sleep_ms(period_ms)
async def timewaste():
while True:
sleep_ms(10)
await uasyncio.sleep_ms(0)
async def main(led1, led2):
uasyncio.create_task(blink(led1, 700))
uasyncio.create_task(blink(led2, 400))
uasyncio.create_task(timewaste())
await uasyncio.sleep_ms(10_000)
uasyncio.run(main(Pin(2,Pin.OUT), Pin(4,Pin.OUT)))
Page 311
import uasyncio
from time import sleep_ms
from machine import Pin, Timer
import network
def setup(country, ssid, key):
network.country(country)
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
LED = Pin(2, Pin.OUT)
LED.on()
timeout = 20000
wifi.connect(ssid,key)
timer = Timer(1)
timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))
s = network.STAT_IDLE
while timeout>0:
print("connecting",s)
s = wifi.status()
if s != network.STAT_CONNECTING:
break
sleep_ms(100)
timeout = timeout-100
if(s==network.STAT_GOT_IP):
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))
else:
timer.deinit()
LED.on()
return wifi
async def main():
reader,writer= await uasyncio.open_connection("www.example.com",80)
request = b"GET /index.html HTTP/1.1\r\nHost:example.org\r\n\r\n"
writer.write(request)
await writer.drain()
print(await reader.read(512))
reader.close()
wifi=setup(country, ssid, key)
print("Connected")
print(wifi.ifconfig())
uasyncio.run(main())
Page 314
Note DS1820 is connected to GPIO4
import uasyncio
import network
from machine import Pin, Timer
from time import sleep_ms
import onewire
import ds18x20
def setup(country, ssid, key):
network.country(country)
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
LED = Pin(2, Pin.OUT)
LED.on()
timeout = 20000
wifi.connect(ssid,key)
timer = Timer(1)
timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))
s = network.STAT_IDLE
while timeout>0:
print("connecting",s)
s = wifi.status()
if s != network.STAT_CONNECTING:
break
sleep_ms(100)
timeout = timeout-100
if(s==network.STAT_GOT_IP):
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))
else:
timer.deinit()
LED.on()
return wifi
wifi=setup(country, ssid, key)
print("Connected")
print(wifi.ifconfig())
ow = onewire.OneWire(Pin(4))
presence = ow.reset()
if presence:
print("Device present")
else:
print("No device")
DS = ds18x20.DS18X20(ow)
roms = DS.scan()
template = """<!DOCTYPE html>
<html>
<head> <title>Temperature</title> </head>
<body> <h1>Current Temperature</h1>
Hello ESP32 Server World <br/>
The Temperature is: <!--#temp--><br/>
</body>
</html>
"""
async def serve_client(reader,writer):
print("client")
print(await reader.read(512))
DS.convert_temp()
temp = DS.read_temp(roms[0])
html=template.replace("<!--#temp-->",str(temp))
headers = ("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Server:ESP32\r\n"
f"Content-Length:{len(html)}\r\n\r\n"
)
buf = headers.encode("utf-8")+html.encode("utf-8")
writer.write(buf)
await writer.drain()
writer.close()
await writer.wait_closed()
async def main():
await uasyncio.start_server(serve_client, '192.168.253.24', 80,backlog=5)
while True:
print("heartbeat")
await uasyncio.sleep(1)
uasyncio.run(main())