Page 14 of 16
Page 276
import network
import socket
from machine import Pin, Timer
from time import sleep_ms
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")
url = "http://192.168.253.72:8080"
ai = socket.getaddrinfo("www.example.com", 80,socket.AF_INET)
addr = ai[0][-1]
s = socket.socket(socket.AF_INET)
s.connect(addr)
request = b"GET /index.html HTTP/1.1\r\nHost:example.org\r\n\r\n"
s.send(request)
print(s.recv(512))
Page 277
import network
import socket
import ssl
from machine import Pin, Timer
from time import sleep_ms
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")
ai = socket.getaddrinfo("example.com", 443,socket.AF_INET)
addr = ai[0][-1]
s = socket.socket(socket.AF_INET)
s.connect(addr)
sslSock=ssl.wrap_socket(s)
request = b"GET / HTTP/1.1\r\nHost:example.com\r\n\r\n"
sslSock.write(request)
print(sslSock.read(1024))
Page 281
import network
import socket
from time import sleep_ms
from machine import Pin, Timer
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")
ow = onewire.OneWire(Pin(22))
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>
"""
addr = socket.getaddrinfo('192.168.253.24', 8080)[0][-1]
print(addr)
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(0)
while True:
cl, addr = s.accept()
print('client connected from', addr)
print(cl.recv(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")
cl.send(buf)
cl.close()
s.close()
Page 283
import binascii
with open("iopress.key", 'rb') as f:
lines = f.readlines()
lines = b"".join(lines[1:-1])
key = binascii.a2b_base64(lines)
print("key=", key)
with open("iopress.crt", 'rb') as f:
lines = f.readlines()
lines = b"".join(lines[1:-1])
cert = binascii.a2b_base64(lines)
print()
print("cert=", cert)
Page 284
import network
import socket
from time import sleep_ms
from machine import Pin, Timer
import onewire
import ds18x20
import ssl
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())
key=b'0\x82\x04\xbf\ . . .xabp\xd1'
cert=b'0\x82\x03k0\ . . .\x98\x8a'
ow = onewire.OneWire(Pin(22))
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>
"""
addr = socket.getaddrinfo('0.0.0.0', 443)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
while True:
cl, addr = s.accept()
print('client connected from', addr)
client_s =None
try:
client_s = ssl.wrap_socket(cl, server_side=True,
key=key, cert=cert)
while True:
h = client_s.readline()
if h == b"" or h == b"\r\n":
break
print(h.decode(), end="")
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")
client_s.write(buf)
client_s.close()
except Exception as e:
print("exception ",e)
s.close()