-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
244 lines (176 loc) · 5.81 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import rp2
import network
import ubinascii
import machine
import urequests as requests
import time
import socket
import ure
from machine import Timer
import math
tim = None
mot0dir0 = machine.PWM(machine.Pin(16, machine.Pin.OUT))
mot0dir1 = machine.PWM(machine.Pin(17, machine.Pin.OUT))
mot1dir0 = machine.PWM(machine.Pin(18, machine.Pin.OUT))
mot1dir1 = machine.PWM(machine.Pin(20, machine.Pin.OUT))
mot0dir0.freq(1000)
mot0dir1.freq(1000)
mot1dir0.freq(1000)
mot1dir1.freq(1000)
#k=1
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# If you need to disable powersaving mode
# wlan.config(pm = 0xa11140)
# See the MAC address in the wireless chip OTP
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print('mac = ' + mac)
# Other things to query
# print(wlan.config('channel'))
# print(wlan.config('essid'))
# print(wlan.config('txpower'))
# Load login data from different file for safety reasons
ssid = "Ejemplo"
pw = "12345678"
wlan.connect(ssid, pw)
# Wait for connection with 10 second timeout
timeout = 10
while timeout > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
timeout -= 1
print('Waiting for connection...')
time.sleep(1)
# Define blinking function for onboard LED to indicate error codes
def blink_onboard_led(num_blinks):
led = machine.Pin('LED', machine.Pin.OUT)
for i in range(num_blinks):
led.on()
time.sleep(.2)
led.off()
time.sleep(.2)
# Handle connection error
# Error meanings
# 0 Link Down
# 1 Link Join
# 2 Link NoIp
# 3 Link Up
# -1 Link Fail
# -2 Link NoNet
# -3 Link BadAuth
wlan_status = wlan.status()
blink_onboard_led(wlan_status)
if wlan_status != 3:
raise RuntimeError('Wi-Fi connection failed ',wlan_status)
else:
print('Connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
# Function to load in html page
def get_html(html_name):
with open(html_name, 'r') as file:
html = file.read()
return html
def findall_1(pattern, text):
matches = []
start = 0
while True:
match = re.search(pattern, text[start:])
if match is None:
break
matches.append(match.group(0))
start += match.end()
return matches
# HTTP server with socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
print('Listening on', addr)
led = machine.Pin('LED', machine.Pin.OUT)
def next_command(timer):
global angle_LOGO_deg, P0, P1, t, comandos
#commands=comandos
print('comandos',comandos)
if comandos != [] :
tiem,m1,m2=comandos.pop(0)
set_PWMs(mot0dir1,mot0dir0,m1)
set_PWMs(mot1dir0,mot1dir1,m2)
tim = Timer(period=abs(tiem), mode=Timer.ONE_SHOT, callback=next_command)
else:
set_PWMs(mot0dir1,mot0dir0,0)
set_PWMs(mot1dir0,mot1dir1,0)
tim.deinit()
tim=None
def set_PWMs(mot_dir0,mot_dir1,x_value):
print(mot_dir0,mot_dir1,x_value)
if x_value<0:
mot_dir0.duty_u16(-x_value)
mot_dir1.duty_u16(0)
elif x_value>0:
mot_dir0.duty_u16(0)
mot_dir1.duty_u16(x_value)
else:
mot_dir0.duty_u16(0)
mot_dir1.duty_u16(0)
comandos=[]
# Listen for connections
while True:
try:
cl, addr = s.accept()
led.on()
print('Client connected from', addr)
r = cl.recv(1024)
r = str(r)
print(r[:30])
# Utilizar expresiones regulares para encontrar los valores de x e y
match = ure.search(r'\?x=(-?\d+)&y=(-?\d+)', r)
if match:
x_value = int(match.group(1))
y_value = int(match.group(2))
print(f'Valor de x: {x_value}')
print(f'Valor de y: {y_value}')
set_PWMs(mot0dir1,mot0dir0,x_value)
set_PWMs(mot1dir0,mot1dir1,y_value)
if tim:
tim.deinit()
tim=None
else:
print('No se encontraron valores de x e y en la cadena de la solicitud HTTP.')
match = ure.search(r'command=([^\s]+)', r)
if match:
input_string= match.group(1)
matches = []
start = 0
while True:
match = ure.search(r'([+-]?\d+)_?', input_string[start:])
if match is None:
break
matches.append(int(match.group(1)))
start += match.end()
# Build the list of lists
result = [matches[i:i+3] for i in range(0, len(matches), 3)]
comandos += result#ure.finditer(r'([+-]?\d+)([+-]?\d+)([+-]?\d+)_?', match.group(1))
#match.group(1).split('_')
# triplas = re.findall(r'([+-]?\d+)([+-]?\d+)([+-]?\d+)_?', cadena)
if not tim:
next_command(None)
#tim=Timer(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
else:
print('No se encontraron comandos en la cadena de la solicitud HTTP.')
response = get_html('flecha.html')
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
#print(r[:20])
#time.sleep(.2)
led.off()
#time.sleep(.2)
except OSError as e:
cl.close()
print('Connection closed')
# Make GET request
#request = requests.get('http://www.google.com')
#print(request.content)
#request.close()