出品: 1ZLAB 1zlab.com
EMP
EMP (Easy MicroPython) is a upy module to make things Easy on MicroPython.
當(dāng)前版本為0.1.11, 包含以下幾個模塊:
- emp_boot.py 一句代碼用于設(shè)置MicroPython的boot啟動模式
- emp_wifi.py 一句代碼搞定WIFI連接
- emp_utils.py 工具模塊,包含了emp_boot.py 和emp_wifi.py模塊的一些依賴
- emp_dev.py 原microide.py,封裝了一些文件操作的接口,供microide所調(diào)用的,現(xiàn)在已放置當(dāng)前項目維護
當(dāng)前版本主要包含的功能是為用戶提供便捷的WIFI連接,通過emp_wifi.py
通過EMP來設(shè)置MicroPython開機WIFI連接和WebREPL
第一步,讓你的MicroPython設(shè)備連接到網(wǎng)絡(luò)
(如果已經(jīng)連接請忽略,直接執(zhí)行第二步)
連接wifi的代碼如下:
>>> import network
>>> wifi = network.WLAN(network.STA_IF)
>>> wifi.active(True)
>>> wifi.connect('<essid>', '<password>') #essid為WIFI名
>>> I (62825) wifi: n:1 1, o:1 0, ap:255 255, sta:1 1, prof:1
I (63395) wifi: state: init -> auth (b0)
I (63405) wifi: state: auth -> assoc (0)
I (63465) wifi: state: assoc -> run (10)
I (63515) wifi: connected with 你的WIFI名字, channel 1
I (63515) wifi: pm start, type: 1
I (63515) network: CONNECTED
I (64385) event: sta ip: 192.168.0.115, mask: 255.255.255.0, gw: 192.168.0.1
I (64385) network: GOT_IP
# 至此,可能你需要回車一下,才能出現(xiàn)下一 提示符 >>>
>>>
第二步,安裝emp-1zlab
庫
>>> import upip
I (240305) modsocket: Initializing
>>> upip.install('emp-1zlab')
Installing to: /lib/
Warning: pypi.org SSL certificate is not validated
Installing emp-1zlab 0.1.11 from https://files.pythonhosted.org/packages/59/95/b9e425d00c195c9beb2e77c0b02020676115c42762a32d61047650e3884b/emp-1zlab-0.1.11.tar.gz
>>>
第三步,設(shè)置啟動模式
>>> from emp_boot import set_boot_mode
>>> set_boot_mode()
[0] Boot with nothing
attention: this option will clear up boot.py, careful!
[1] Boot with wifi startup
this mode will auto start wifi connect program.
[2] Easy to develop
this mode is for developers.In this mode you can develop much easier via EMP-IDE(emp.1zlab.com)
Please input your choice [0-2]:
簡單說明以下,第0項為啟動腳本為空,這個會清除boot.py中的所有內(nèi)容
第1項為開機時啟動WIFI連接
第2項為開機啟動WIFI連接的同時啟動WebRepl,配合我們的IDE一起使用
第四步,按照提示進行開機引導(dǎo)
在此我們以第二項 Easy to develop為例:當(dāng)你按下2回車后,你的設(shè)備將會重啟:
# 以上省略若干行日志,以下會羅列出 你當(dāng)前的可用WIFI
created config/wifi_cfg.json # 初次運行會創(chuàng)建配置文件
I (3675) network: event 1
[0] How_Router_Home -79 dBm
[1] CMCC-iEPD -83 dBm
No record available # 如果你有已經(jīng)登陸過的wifi,那么將會嘗試自動連接
scaning networks...
I (6205) network: event 1
[0] How_Router_Home -80 dBm
[1] CMCC-iEPD -84 dBm
Which one do you want to access? [0-1]
在此選擇你想要連接的WIFI,我的WIFI是第0個:
scaning networks...
I (6205) network: event 1
[0] How_Router_Home -80 dBm
[1] CMCC-iEPD -84 dBm
Which one do you want to access? [0-1]0
Password for How_Router_Home: # 輸入你的WIFI密碼,回車
初次運行設(shè)置WebRepl
# 省略若干行日志
I (369455) network: CONNECTED
I (370225) event: sta ip: 192.168.0.115, mask: 255.255.255.0, gw: 192.168.0.1
I (370225) network: GOT_IP
You are connected to How_Router_Home
IP: 192.168.0.115 # 你的內(nèi)網(wǎng) IP
Netmask: 255.255.255.0 # 掩碼
Gateway: 192.168.0.1 # 網(wǎng)關(guān)
Added record: How_Router_Home
WebREPL daemon auto-start status: enabled
Would you like to (E)nable or (D)isable it running on boot?
(Empty line to quit)
> E # E 為開啟
To enable WebREPL, you must set password for it
New password (4-9 chars): 1zlab # 輸入你的密碼
Confirm password: 1zlab # 再次輸入
No further action required
至此已成功的設(shè)置完成 你的MicroPython啟動模式至 Easy develop模式,請訪問我們的 網(wǎng)站emp.1zlab.com進行webrepl的高效開發(fā)吧!
可能你有如下的疑問:
-
自動連接wifi的話,我如何控制在多個網(wǎng)絡(luò)下的連接?比如我有家兩個路由器,分別為A,B,我都進行過連接,我該如何保證我想要連接到A而不是B呢?
>>> Wifi.set_default('<essid>')
-
如何刪除一條記錄呢? 忘記WIFI?
>>> Wifi.del_record('<essid>')
-
查看ip的接口?
>>> Wifi.ifconfig() You are connected to How_Router_Home IP: 192.168.0.115 Netmask: 255.255.255.0 Gateway: 192.168.0.1
-
斷開當(dāng)前的WIFI
>>> Wifi.disconnect() I (601244) wifi: state: run -> init (0) I (601244) wifi: pm stop, total sleep time: 565609183 us / 597113853 us I (601244) wifi: n:1 0, o:1 1, ap:255 255, sta:1 1, prof:1 I (601264) wifi: flush txq I (601264) wifi: stop sw txq I (601264) wifi: lmac stop hw txq I (601264) wifi: STA_DISCONNECTED, reason:8 E (601264) wifi: esp_wifi_connect 935 wifi not start I (601264) wifi: error attempting to reconnect: 0x3002 I (601274) network: event 3 WIFI connection has been disconnected
-
重新連接到其他WIFI?
# TODO em,有點小bug,待修復(fù) ...