import machine
import dht
import socket
import network
import time
d = dht.DHT22(machine.Pin(5))
html = """
ESP8266
ESP8266 - température et humidité
{}
"""
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP8266',password='12345678') # set the ESSID of the access point
addr1 = socket.getaddrinfo('192.168.4.1', 80)[0][-1]
s = socket.socket()
s.bind(addr1)
s.listen(1)
print('listening on', addr1)
while True:
cl, addr2 = s.accept()
print('client connected from', addr2)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
d.measure()
time.sleep(1)
contenu = "température : {} °Chumidité : {} %
".format(d.temperature(), d.humidity())
response = html.format(contenu)
cl.send(response)
cl.close()