🐛 Fixes not connecting to wifi
This commit is contained in:
parent
2f4fe0ea1e
commit
6e39a82bbb
@ -1,7 +1,8 @@
|
|||||||
import utime
|
import utime
|
||||||
import ujson
|
import ujson
|
||||||
import network
|
import network
|
||||||
import requests
|
import urequests
|
||||||
|
import machine
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
|
|
||||||
control_button = Pin(16, Pin.IN)
|
control_button = Pin(16, Pin.IN)
|
||||||
@ -56,17 +57,29 @@ previous_opened = False
|
|||||||
def connect():
|
def connect():
|
||||||
print("Connecting...")
|
print("Connecting...")
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(False)
|
||||||
|
utime.sleep_ms(250)
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
|
utime.sleep_ms(250)
|
||||||
|
|
||||||
wlan.connect(ssid, password)
|
wlan.connect(ssid, password)
|
||||||
|
utime.sleep_ms(100)
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while not wlan.isconnected():
|
while not wlan.isconnected():
|
||||||
print(f"Waiting for connection{(i % 3 + 1) * "."}", end="\r")
|
print(f"Waiting for connection{(i % 3 + 1) * "."}{3 * " "}", end="\r")
|
||||||
i += 1
|
i += 1
|
||||||
for _ in range(4):
|
for _ in range(4):
|
||||||
led.toggle()
|
led.toggle()
|
||||||
utime.sleep_ms(250)
|
utime.sleep_ms(250)
|
||||||
|
|
||||||
|
if i % 10 == 0:
|
||||||
|
print("Attempting to restart connection...")
|
||||||
|
wlan.connect(ssid, password)
|
||||||
|
for i in range(10):
|
||||||
|
led.toggle()
|
||||||
|
utime.sleep_ms(50)
|
||||||
|
|
||||||
print(f"Connected with IP {wlan.ifconfig()[0]}")
|
print(f"Connected with IP {wlan.ifconfig()[0]}")
|
||||||
show_ok()
|
show_ok()
|
||||||
|
|
||||||
@ -79,7 +92,7 @@ def send_req(opened: bool):
|
|||||||
|
|
||||||
data = {"opened": opened}
|
data = {"opened": opened}
|
||||||
raw = ujson.dumps(data)
|
raw = ujson.dumps(data)
|
||||||
r = requests.post(f"{server}/write",
|
r = urequests.post(f"{server}/write",
|
||||||
headers={"Authorization": token, "Content-Type": "application/json"},
|
headers={"Authorization": token, "Content-Type": "application/json"},
|
||||||
data=raw)
|
data=raw)
|
||||||
print(f"State updated [{r.status_code}]")
|
print(f"State updated [{r.status_code}]")
|
||||||
@ -136,9 +149,9 @@ while True:
|
|||||||
|
|
||||||
if i == 6:
|
if i == 6:
|
||||||
led.toggle()
|
led.toggle()
|
||||||
utime.sleep_ms(200)
|
utime.sleep_ms(100)
|
||||||
led.toggle()
|
led.toggle()
|
||||||
utime.sleep_ms(300)
|
utime.sleep_ms(400)
|
||||||
i = 0
|
i = 0
|
||||||
else:
|
else:
|
||||||
utime.sleep_ms(500)
|
utime.sleep_ms(500)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user