-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (109 loc) · 3.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import _thread as th
from network import *
from socket import *
from machine import Pin, PWM
from dcmotor import DCMotor
from time import sleep
from hcsr04 import HCSR04
import esp
#from servo import Servo
esp.osdebug(None)
import gc
gc.collect()
frontSensor = HCSR04(trigger_pin=25, echo_pin=26)
rightSensor = HCSR04(trigger_pin=12, echo_pin=14)
leftSensor = HCSR04(trigger_pin=33, echo_pin=32)
cleanerMotor = Pin(2, Pin.OUT)
pompeMotor = Pin(4, Pin.OUT)
cleanerMotor.value(1)
pompeMotor.value(1)
ssid = 'Guest'
password = 'FTD-2021'
frequency = 15000
station = WLAN(STA_IF)
station.active(True)
station.ifconfig(('10.2.40.41', '255.255.255.0', '10.2.40.254', '8.8.8.8'))
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('connexion établie')
print(station.ifconfig())
auto=False
enable = PWM(Pin(13), frequency)
pin3 = Pin(19, Pin.OUT)
pin4 = Pin(18, Pin.OUT)
dc_motor01 = DCMotor(pin3, pin4, enable)
pin5 = Pin(23, Pin.OUT)
pin6 = Pin(22, Pin.OUT)
dc_motor02 = DCMotor(pin5, pin6, enable)
s = socket(AF_INET,SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
def eviteur():
while True :
if auto:
distance1 = frontSensor.distance_cm()
distance2 = rightSensor.distance_cm()
distance3 = leftSensor.distance_cm()
if (0<distance1<25) or (0<distance2<10) or (0<distance3<10) :
dc_motor01.stop()
dc_motor02.stop()
dc_motor01.backwards(2)
dc_motor02.backwards(2)
sleep(1)
distance2 = rightSensor.distance_cm()
distance3 = leftSensor.distance_cm()
if distance2>20:
dc_motor01.backwards(2)
dc_motor02.forward(2)
sleep(1.5)
elif distance3>20:
dc_motor02.backwards(2)
dc_motor01.forward(2)
sleep(1.5)
else:
dc_motor01.forward(2.5)
dc_motor02.forward(2.5)
sleep(0.07)
def web():
global auto
while True:
conn,addr =s.accept()
request =conn.recv(1024)
request =str(request)
print(request[:20])
if request.find('/?mode=auto')==6:
auto=True
elif request.find('/?mode=manuel')==6:
auto=False
dc_motor01.stop()
dc_motor02.stop()
elif request.find('/?pompe=on')==6:
pompeMotor.value(0)
elif request.find('/?pompe=off')==6:
pompeMotor.value(1)
elif request.find('/?dir=back')==6:
dc_motor01.backwards(2)
dc_motor02.backwards(2)
elif request.find('/?dir=forward')==6:
dc_motor01.forward(2.5)
dc_motor02.forward(2.5)
elif request.find('/?dir=right')==6:
dc_motor01.forward(2)
dc_motor02.backwards(2)
elif request.find('/?dir=left')==6:
dc_motor02.forward(2)
dc_motor01.backwards(2)
elif request.find('/?dir=stop')==6:
dc_motor01.stop()
dc_motor02.stop()
elif request.find('/?servo=on') == 6:
cleanerMotor.value(0)
elif request.find('/?servo=off') == 6:
cleanerMotor.value(1)
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.close()
th.start_new_thread(web,())
th.start_new_thread(eviteur,())