-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_data_fetching.py
62 lines (47 loc) · 1.48 KB
/
esp32_data_fetching.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
import network
import gc
import time
import urequests as requests
import json
from machine import Pin, PWM
gc.collect()
pwm0 = PWM(Pin(0), freq=500, duty=0)
pwm1 = PWM(Pin(2), freq=500, duty=0)
pwm2 = PWM(Pin(4), freq=500, duty=0)
SSID = ''
PASSWORD = ''
URL_API = 'https://example.com/api/data'
def connectWifi(ssid, password):
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
print('Conectando...')
time.sleep(1)
print('Conexión establecida. Dirección IP:', station.ifconfig()[0])
print(f'Conexión exitosa a {ssid}')
def fetchApi(api_url, timeout=20):
try:
response = requests.get(api_url, timeout=timeout)
if response.status_code == 200:
# print('Respuesta de la API:', response.text)
return response.text
else:
print('Error en la solicitud. Código de respuesta HTTP:', response.status_code)
return None
except Exception as e:
print('Error en la solicitud:', str(e))
return None
connectWifi(SSID, PASSWORD)
def map_range(x, in_max = 255, out_max = 1023):
return x * out_max // in_max
while True:
data = fetchApi(URL_API)
if data == "[]":
break
data = json.loads(data)
pwm0.duty(map_range(data[0].get("output1")))
pwm1.duty(map_range(data[0].get("output2")))
pwm2.duty(map_range(data[0].get("output3")))
time.sleep(1)
print("Error")