Please note that I am doing a ping after light sleep and if it fails, I do a wlan.connect(). No good.
Code: Select all
from machine import lightsleep
from machine import freq
from machine import Pin # Only for WiPy2
from time import sleep_ms
import network
import uping
import config
TESTHOST = 'google.com'
def test_ping():
try:
result = uping.ping(
host=TESTHOST,
count=1,
timeout=1000,
quiet=True,)
if result[1] >= 1:
print('Connected')
return True
else:
print('Not pinging')
return False
except OSError: # TODO Specifically, -202
print('Not connected')
return None
except: # noqa FIXME What other exceptions?
pass
return None
# 80MHz, slowest possible which still allows Wi-Fi, saves power
freq(80000000)
antenna = Pin(16, Pin.OUT, value=0) # Only for WiPy2
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(config.wifi.get('ssid'), config.wifi.get('password'))
while not wlan.isconnected():
sleep_ms(100)
print('Network config:', wlan.ifconfig())
sleep_ms(2000)
while True:
print('Zzz')
lightsleep(10000)
print('Wakey wakey')
try:
if test_ping():
continue
else:
print('Reconnecting...')
wlan.connect(config.wifi.get('ssid'), config.wifi.get('password'))
except OSError: # TODO And more specifically, Wifi Internal Error
pass
except: # noqa
pass
while not wlan.isconnected():
sleep_ms(100)
print('Retrying test ping...')
test_ping()
Code: Select all
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:5624
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
MicroPython v1.15 on 2021-05-09; ESP32 module with ESP32
Type "help()" for more information.
>>> import testmain
Network config: ('192.168.137.39', '255.255.255.0', '192.168.137.1', '192.168.137.1')
Zzz
Wakey wakey
Connected
Zzz
Wakey wakey
Not pinging
Reconnecting...
Retrying test ping...
Not pinging
Zzz
Wakey wakey
Not pinging
Reconnecting...
E (23976) wifi:sta is connecting, return error