之前介绍过 NodeMCU 如何刷 Micropython 固件,刷入固件之后就可以使用 Mu 编辑器编写代码了,物联网最核心最基本的应该就是网络操作了,下面就来介绍 Micropython 连接 WIFI 上网。
所需材料:
一块 Esp8266/Esp32 开发板,这里使用 NodeMCU Esp32
Microusb 数据线
将 NodeMCU 连接到电脑上, 打开 Mu 编辑器,在系统启动的时候会自动执行两个文件的脚本,一个是 boot.py, 一个是 main.py 。我们在 boot.py 文件中进行联网操作,使用 Mu 打开 boot.py,输入:
import gcimport networkgc.collect()wlan = network.WLAN(network.STA_IF)wlan.active(True)if not wlan.isconnected():wifiName = "CMCC-PebQ"wifiPassword = "your wifi password"print('connecting to wifi network...')wlan.connect(wifiName, wifiPassword)print('wifi connected .')
编辑完之后将其拖到 NodeMCU 设备中,打开 Mu 中的 REPL,设备会自动执行 boot.py 中的程序开始连接网络:
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00mode:DIO, clock div:2load:0x3fff0018,len:4load:0x3fff001c,len:5008ho 0 tail 12 room 4load:0x40078000,len:10600ho 0 tail 12 room 4load:0x40080400,len:5684entry 0x400806bc;32mI (539) cpu_start: Pro cpu up.I (539) cpu_start: Application information:mI (539) cpu_start: Compile time: Sep 2 2020 03:00:08I (543) cpu_start: ELF file SHA256: 0000000000000000...0mI (548) cpu_start: ESP-IDF: v3.3.2mI (553) cpu_start: Starting app cpu, entry point is 0x40082f30I (0) cpu_start: App cpu up.mI (564) heap_init: Initializing. RAM available for dynamic allocation:;32mI (571) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM;32mI (577) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAMI (583) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM0;32mI (589) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAMI (595) heap_init: At 3FFCA9E8 len 00015618 (85 KiB): DRAMI (601) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAMI (608) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAMI (614) heap_init: At 4009DE28 len 000021D8 (8 KiB): IRAMI (620) cpu_start: Pro cpu start user codeI (303) cpu_start: Starting scheduler on PRO CPU.I (0) cpu_start: Starting scheduler on APP CPU.I (90) wifi:wifi driver task: 3ffd1148, prio:23, stack:3584, core=0;32mI (486) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSEI (486) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSEI (526) wifi:wifi firmware version: 44aa95cI (526) wifi:config NVS flash: enabledI (526) wifi:config nano formating: disabledI (526) wifi:Init dynamic tx buffer num: 32I (526) wifi:Init data frame dynamic rx buffer num: 32I (536) wifi:Init management frame dynamic rx buffer num: 32I (536) wifi:Init management short buffer num: 32I (546) wifi:Init static rx buffer size: 1600I (546) wifi:Init static rx buffer num: 10I (546) wifi:Init dynamic rx buffer num: 32[0;32mI (646) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0I (646) wifi:mode : sta (98:f4:ab:00:a8:0c)I (646) wifi: STA_STARTconnecting to wifi network...wifi connected .MicroPython v1.13 on 2020-09-02; ESP32 module with ESP32Type "help()" for more information.>>> I (1016) wifi:new:<3,0>, old:<1,0>, ap:<255,255>, sta:<3,0>, prof:1I (1866) wifi:state: init -> auth (b0)I (1866) wifi:state: auth -> assoc (0)I (1876) wifi:state: assoc -> run (10)I (1976) wifi:connected with CMCC-PebQ, aid = 3, channel 3, BW20, bssid = 90:47:3c:39:3b:81I (1976) wifi:security type: 4, phy: bgn, rssi: -75I (1976) wifi:pm start, type: 1I (1986) network: CONNECTEDI (2006) wifi:AP's beacon interval = 102400 us, DTIM period = 1[0;32mI (5976) event: sta ip: 192.168.1.12, mask: 255.255.255.0, gw: 192.168.1.1I (5976) network: GOT_IP
如果有上面类似的输出就代表 WIFI 连接成功了,联网成功之后就可以使用 upip 来安装扩展,在 REPL 中输入:
>>> import upip[0;32mI (17586) modsocket: Initializing>>> upip.install('micropython-urequests')Installing to: lib/Warning: micropython.org SSL certificate is not validatedInstalling micropython-urequests 0.6 from https://micropython.org/pi/urequests/urequests-0.6.tar.gz
通过 urequest 请求 api :
>>> import urequests as uq>>> res = uq.get("http://api.example.com")>>> res.text'{"code":0,"data":{"title":"poetry api server","version":"0.1.0","stage":"prod"},"msg":"ok"}'>>> res.json(){'code': 0, 'data': {'title': 'poetry api server', 'stage': 'prod', 'version': '0.1.0'}, 'msg': 'ok'}
文章转载自程序猿研究所,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




