WIFICFG
For ESP8266/ESP32
Content
0. Introduction
WIFICFG is a MicroPython module for ESP8266 or ESP32 device. WiFi could be set without writing the SSID and Password to the code.
Multiple WiFi configuration could be saved to Json file. So when the network environment changes, it will work fine as long as this WiFi configuration in file.
But how to configure the Json file? Do I need to edit it manually? Don't worry about it!
You only need to connect to the hotspot named "ESP-WIFICFG" (password:3.1415926), access to 192.168.4.1 in the browser, and then configure it. WiFi that has been successfully connected will save to Json file automatically.
1. Start
Quick Start
Download WIFICFG.py to your environment.
from WIFICFG import WiFi
WiFi.LOG = False
wifi = WiFi()
wifi.auto_run() #or wifi.auto_run("your_ssid", "your_password")
Connect once
from WIFICFG import WiFi
wifi = WiFi()
wifi.connect("your_ssid", "your_password")
Do something before using web
from WIFICFG import WiFi
def error():
print("Failed after trying all possible connection!")
print("Please connect to hotspot:ESP-WIFICFG(password:3.1415926).")
print("Then access to address 192.168.4.1 to upload WiFi configuration.")
WiFi.LOG = False
wifi = WiFi()
wifi.on_connect_error = error
wifi.auto_run()
2. Details
class WiFi
----main attribute:
----WiFi.LOG: Print status if WiFi.LOG == True
----main method:
----__init__(self, file="WIFICFG.json"):
Parameter (file) is the path where the Json file saved.
----auto_run(self, ssid=None, passwd=None, retry=0):
(retry) is the retry times when connection failed.
method auto_run(ssid, password) will try connecting to ssid that gived in function firstly.
If connection failed, try connecting to WiFi in Json file.
If connection failed again, try configuring WiFi with web.
----connect(self, ssid, passwd):
Connect to ssid once.
return True if successful else False.
Update the Json file every time the connection is successful.
----load_cfg(self):
Load Json file.
return dict(ssid1=passwd1, ssid2=passwd2, ...).
----update_cfg(self, ssid, passwd):
Update Json file.
----scan(self):
Scan WiFi and sort by rssi.
return list(ssid1, ssid2, ...)
----log(self, *args, **kws):
self.log(info) = print(info) if WiFi.LOG == True else print(end="")
----get_cfg_from_web(self):
while True:
get ssid and password from web
if self.connect(ssid, password):
break
----on_connect_error(self):
Do something before self.get_cfg_from_web().
Default: do noting(pass).
Could be customized: wifi_obj.on_connect_error = custom_error_function.