with my prog , i have placed my web request in the loop , but I need to update my page for running my loop , what awaits my prog and does not do its job
thank you for your answer
Code: Select all
import machine
import utime
import os
from bme280 import *
g=0
rel= machine.Pin(12, machine.Pin.OUT)
led = machine.Pin(13, machine.Pin.OUT)
try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
mo=('Travail','Presence','absence','exception')
jr=('Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche')
host = "192.168.1.79" # serveur ntp interne
hosts="0.fr.pool.ntp.org" # serveur ntp internet
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
excep=''
h1='#0F3376'
ab=0
m=0
mm=0
def temp():
l=open('te.txt','r')
li=l.read()
bmp=BME280(i2c=i2c, address=BME280_I2CADDR)
ts=bmp.raw_values
temper=ts[0]
return round(temper+float(li),2)
def web_page():
tempera=str(temp())+' / '+str(tempf)
ddt=jr[u[6]]+' '+str(u[3])+' h '+str(u[4])
html = """<html><head> <title>Chambre parentale</title> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
h1{color: """+h1+"""; padding: 2vh;}p{font-size: 1.5rem;}.button1{display: inline-block; background-color: #ed7a0e; border: double;
border-radius: 4px; color: white; padding: 3px 5px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
.button2{display: inline-block; background-color: #0c2f5c; border: double; border-radius: 4px; color: white; padding: 3px 5px ;
text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}
.button3{display: inline-block; background-color: #0c5c13; border: double; border-radius: 4px; color: white; padding: 3px 5px ;
text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}</style></head><body> <h1>Chambre parentale</h1>
<p>"""+ddt+"""</p>
<p>Temperature: <strong>""" + tempera + """</strong><p>Mode: <strong>""" + mo[ab] + """</strong></p><p><a href="/?led=on"><button class="button1">Travail</button></a>
<a href="/?led=off"><button class="button2">Presence</button></a><a href="/?led2=on"><button class="button3">Absence</button></a><div>
<p <h1>____________________________</h1></p>
<p <h1><B>Demande exceptionnelle</B></h1></p>
<form action=”” method=”POST”>
<p align="center" style="margin-bottom: 0cm; line-height: 100%">
<input type="text" id="name1" name="name1" required
minlength="2" maxlength="2" size="3" value=''>
<label for="contactChoice1"> Degres_ </label>
<input type="text" id="name2" name="name2" required
minlength="1" maxlength="2" size="2" value=''>
<label for="contactChoice1">Heure(s) </label>
<p align="center" style="margin-bottom: 0cm; line-height: 100%"><button type="submit">Enregistrer</button><p/>
<p>"""+excep+"""</p>
</form>
</div></p></body></html>"""
return html
def exist():# choix du repertoire
a=0
b=0
c=0
d=0
global rep # repertoire
global ab # indice absence ou exception
global av # indice avant exception
ab=0
rep1=''
rep2=''
rep3=''
rep4=''
try:
f=open('p.txt','r')
rep1='p'
f.close
except :
pass
try:
f=open('t.txt','r')
rep2='t'
f.close
print(rep2)
except :
pass
try:
f=open('a.txt','r')
rep3='a'
f.close
except :
pass
try:
f=open('e.txt','r')
rep4='e'
f.close
except :
pass
if rep1=='p'and a==0: # fichier presence
a=1
b=0
c=0
d=0
rep='p'
ab=1
if rep2=='t':
try:
os.remove('t.txt')
except :
pass
if rep3=='a':
try:
os.remove('a.txt')
except :
pass
if rep4=='e':
try:
os.remove('e.txt')
except :
pass
if rep2=='t'and b==0: # fichier travail
a=0
b=1
c=0
d=0
rep='t'
ab=0
if rep1=='p':
try:
os.remove('p.txt')
except :
pass
if rep3=='a':
try:
os.remove('a.txt')
except :
pass
if rep4=='e':
try:
os.remove('e.txt')
except :
pass
if rep3=='a'and c==0: # fichier absence
a=0
b=0
c=1
d=0
ab=2
if rep2=='t':
try:
os.remove('t.txt')
except :
pass
if rep1=='p':
try:
os.remove('p.txt')
except :
pass
if rep4=='e':
try:
os.remove('e.txt')
except :
pass
if rep4=='e'and d==0: # fichier exception
a=0
b=0
c=0
d=1
ab=3
if rep2=='t':
av=rep2
try:
os.remove('t.txt')
except :
pass
if rep3=='a':
av=rep3
try:
os.remove('a.txt')
except :
pass
if rep1=='p':
av=rep1
try:
os.remove('p.txt')
except :
pass
def timep():
try:
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
except:
try:
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(hosts, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
except:
pass
def settimep():
t = timep()
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
while True :
dat=[]
aa=0
settimep()
u=utime.localtime()
exist()
if ab==0 or ab==1: # si presence ou travail
with open(rep+str(u[6])+'.txt') as file: # lecture fichier suivant le jour ( 0 = lundi )
for line in file.read().strip().split("\r"):
aa=aa+1
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
ti=(u[3]*60)+(u[4]) # actuel heure*60 + minutes
#tb=(u[3]*60)+(u[4]+2) # actuel heure*60 + minutes +2
for i in range(aa):
ta=(dat[i][0]*60)+dat[i][1] # heure*60 + minutes ( fichier du jour )
if ti >= ta: # si heure du actuel est superieur ou egale heure du fichier
tempf=float(dat[i][2]) # temperature demandee
if ab==2: # si absence
tempf=16
if ab==3: # si exception
if ind == 0:
with open('e.txt') as file: # lecture fichier exception
for line in file.read().strip().split("\r"):
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
ta=(dat[0][0]*60) # heure*60 ( fichier d exception )
tempf=int(dat[0][2]) # temperature demandee
ind = 1
ja= u[6] # garde jour actuel
tah=ta/1440
if tah >= 0 :
j = int(tah) # nombre de jours
jh=ta-(1440*j) # reste de temps - nombre de jours
else:
jh=ta # nombres de minutes
j=j+ja # jour actuel + nombre de jours
if j == u[6] and jh == u[3] :
ind=0
os.remove('e.txt')
o=open(av+'.txt','w')
o.write('')
o.close()
tem=float(temp())
if tem < (tempf - 0.25):
led.value(1)
rel.value(1)
g=1
print('allumer')
h1='#E12A1E'
if tem < (tempf +0.25) and g==1:
pass
else:
led.value(0)
rel.value(0)
g=0
print('eteint')
h1='#0F3376'
conn, addr = s.accept()
request = conn.recv(1024)
request = str(request)
na=request.find('?')
led_on = request.find('/?led=on')
led_off = request.find('/?led=off')
led2_on = request.find('/?led2=on')
if request[na+1:na+6]=='name1' :
deg=request[na+7:na+9]
heu=request[na+16:na+18]
excep=(deg+' degres '+heu+' heures')
ab=3
f=open('e.txt','w')
f.write(heu+','+deg)
f.close()
if led_on == 6:
ab=0
f=open('t.txt','w')
f.write(' ')
f.close()
if led_off == 6:
ab=1
f=open('p.txt','w')
f.write(' ')
f.close()
if led2_on == 6:
ab=2
f=open('a.txt','w')
f.write(' ')
f.close()
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()