DHT22温湿度计——记录55秒温度

import dht
import network
import socket
import utime
from machine import Pin, Timer

led2 = Pin(2, Pin.OUT)
led2.value(1)
utime.sleep(10)
espht = dht.DHT22(Pin(4))
tm = Timer(10)
tm1 = Timer(11)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
dht22t = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
dht22h = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def ap(ESSID):
if_ap = network.WLAN(network.AP_IF)
if_ap.active(True)
if_ap.config(essid=ESSID, authmode=0)

def LED_up(t):
led2.value(0)
utime.sleep_ms(5)
led2.value(1)

def Getdht22tha():
del dht22t[0]
del dht22h[0]
espht.measure()
dht22t.append(str(espht.temperature()))
dht22h.append(str(espht.humidity()) + "%")
return dht22t, dht22h

def web_page():
blist = []
tlist = []
hlist = []
bb = ""
th22t, th22h = Getdht22tha()
for i in th22t:
tlist.append(i)
for i in th22h:
hlist.append(i)
tlist.reverse()
hlist.reverse()
for i in range(12):
c = "<p>" + str((i*5)) + "ago:T" + str(tlist[i]) + ",H" + str(hlist[i]) + "</p>"
bb += c
blist.append(bb)
html = """<!DOCTYPE HTML><html><head><title>DHT22_T&H</title>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:;base64,="><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<script> function countDown(){ var time = document.getElementById("Time"); if(time.innerHTML == 0){ window.location.href="http://192.168.4.1"; }
else{ time.innerHTML = time.innerHTML-1; } } window.setInterval("countDown()",1000); </script>
<style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }
h2 { font-size: 3.0rem; } p { font-size: 1.0rem; } .units { font-size: 1.2rem; }
.ds-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } #Time,#p{ font-size: 1.0rem; text-align: center; }
</style></head><body><h2><i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span id="temperature"> """+str(tlist[0])+""" </span><sup class="units">&deg;C</sup></h2><h2><i class="fa fa-tint" style="color:#059e8a;"></i><span id="humidity"> """+str(hlist[0])+""" </span><sup class="units"></sup></p>
"""+str(blist[11])+"""<font color="yellow"><p id="Time" >5</p></font></body></html>"""
return html


if __name__ == "__main__":
tm.init(freq=(1 / 60), mode=Timer.PERIODIC, callback=LED_up)
ap("DHT22_T&H")
while True:
try:
conn, addr = s.accept()
conn.settimeout(5.0)
request = conn.recv(1024)
conn.settimeout(None)
request = str(request)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
except OSError as e:
conn.close()