from machine import Pin, PWM, ADC
from machine import Timer
import _thread
import network
import socket
import time
h=Pin(0, Pin.OUT)
a=Pin(1, Pin.OUT)
e=Pin(10, Pin.OUT)
adc0=ADC(3)
adc1=ADC(4)
pwm8=PWM(Pin(8), freq=5, duty_u16=32768)
consigne=10**6
consignei=0
acceleration=5000
TempsMax=1000
#fdec=200
#PeriodeE=1250
#full=int((1/fdec)*1000000)
#t=0
timerTempsMax=Timer(0)
def ConsigneInstant():
global consignei,consigne,acceleration
if (consignei<consigne):
consignei=consignei+acceleration
def StopTempsMax(ti):
Stop()
def Stop():
global consigne,consignei,lock
lock.acquire()
consigne=0
consignei=0
lock.release()
timerTempsMax.deinit()
e.value(0)
h.value(0)
a.value(0)
def MarcheAvant(Constmp,Ttmp): # consigne en pourcentage Ttmp en seconde
global consigne, TempsMax
Stop()
consigne = int(10**6* Constmp)
TempsMax = int(Ttmp*1000)
h.value(1)
timerTempsMax.init(period=TempsMax,mode=Timer.ONE_SHOT,callback=StopTempsMax)
#timerAsservicement.init(freq=fdec,mode=Timer.PERIODIC,callback=asservicement)
#timerAsservicement.init(period=PeriodeE,mode=Timer.PERIODIC,callback=asservicement)
def MarcheArriere(Constmp,Ttmp): # consigne en pourcentage Ttmp en seconde
global consigne, TempsMax
Stop()
consigne = int(10**6* Constmp)
TempsMax = int(Ttmp*1000)
a.value(1)
timerTempsMax.init(period=TempsMax,mode=Timer.ONE_SHOT,callback=StopTempsMax)
#timerAsservicement.init(period=PeriodeE,mode=Timer.PERIODIC,callback=asservicement)
def ChangeConsigne(Constmp,Ttmp):
global consigne,consignei, TempsMax
consigne = int(10**6* Constmp)
TempsMax = int(Ttmp*1000)
if consignei > consigne:
consignei = consigne
def asservicement():
global consignei, TempsMax,lock
while True:
lock.acquire()
ConsigneInstant()
lock.release()
if abs(adc1.read_uv()-adc0.read_uv())<consignei:
#print(" ",abs(adc1.read_uv()-adc0.read_uv()),"<", consignei,abs(adc1.read_uv()-adc0.read_uv())<consignei)
#abs(adc1.read_uv()-adc0.read_uv())
e.value(1)
time.sleep_us(1250)
e.value(0)
time.sleep_us(1250)
else:
e.value(0)
time.sleep_us(2500)
def setup():
global consigne,consignei,s,lock
network.country('FR')
wlan=network.WLAN(network.AP_IF)
wlan.config(ssid='TGVTrain', security=3, key='locowifi')
wlan.active(True)
while wlan.active==False:
pass
print('Access point actif : ', wlan.ifconfig())
addr=socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s=socket.socket()
s.bind(addr)
s.listen(1)
lock = _thread.allocate_lock()
_thread.start_new_thread(asservicement, ())
time.sleep_us(10**6*10)
MarcheAvant(70/100,20)
#MarcheArriere(70/100,30)
#try:
# print("Timer initialisé avec succès")
#except Exception as e:
# print("Erreur lors de l'initialisation du timer:", e)
html="""<!DOCTYPE html>
<html>
<head><title>Loco WiFi</title></head>
<body>
<h1>Tension ADC0 (µV) :</h1>
<h1>%s</h1>
<h1>Tension ADC1 (µV) :</h1>
<h1>%s</h1>
<hr>
<h1>Distance courte (30s) :</h1>
<input type="button" value="-->ALLER A LA GARE D'AVANT<--" onclick="document.location='/prec30'"/>
<input type="button" value="-->ALLER A LA GARE D'APRES<--" onclick="document.location='/suiv30'"/>
<hr>
<h1>Distance moyenne (60s) :</h1>
<input type="button" value="-->ALLER A LA GARE D'AVANT<--" onclick="document.location='/prec60'"/>
<input type="button" value="-->ALLER A LA GARE D'APRES<--" onclick="document.location='/suiv60'"/>
<hr>
<h1>Distance longue (90s) :</h1>
<input type="button" value="-->ALLER A LA GARE D'AVANT<--" onclick="document.location='/prec90'"/>
<input type="button" value="-->ALLER A LA GARE D'APRES<--" onclick="document.location='/suiv90'"/>
<hr>
<h1>Stop :</h1>
<input type="button" value="-->Arrête<--" onclick="document.location='/Stop'"/>
<hr>
</body>
</html>"""
def loop():
global t,consignei,s
#print('Serveur actif : ', wlan.ifconfig())
#pass
cl, addr=s.accept()
chemin=str(cl.recv(1024)).split()[1]
if chemin.find('/prec30')!=-1:
MarcheArriere(50/100,30)
elif chemin.find('/suiv30')!=-1:
MarcheAvant(50/100,30)
elif chemin.find('/prec60')!=-1:
MarcheArriere(50/100,60)
elif chemin.find('/suiv60')!=-1:
MarcheAvant(50/100,60)
elif chemin.find('/prec90')!=-1:
MarcheArriere(50/100,90)
elif chemin.find('/suiv90')!=-1:
MarcheAvant(50/100,90)
elif chemin.find('/Stop')!=-1:
Stop()
else:
pass
uadc0="{:.2f}".format(adc0.read_uv())
uadc1="{:.2f}".format(adc1.read_uv())
reponse=html%(uadc0,uadc1)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(reponse)
cl.close()
setup()
while True:loop()