2021年5月14日 星期五

NodeMCU ESP32S 燒錄 MicroPython v1.15

昨天順利將 MicroPython v1.15 燒錄到 D1 mini 開發板後, 找出我僅有的兩顆 NodeMCU ESP32S 開發板, 今天也將 v1.15 韌體成功燒錄進去了, 參考 : 


韌體燒錄與測試過程如下 :


1. 抹除 ESP32 Flash 記憶體 :     

D:\ESP32>esptool.py --chip esp32 --port COM9 erase_flash      
esptool.py v2.6
Serial port COM9
Connecting....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 80:7d:3a:b7:a7:5c
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 4.7s
Hard resetting via RTS pin...


2. 檢查 ESP32 Flash 記憶體 :  

D:\ESP32>esptool.py --port COM9 flash_id     
esptool.py v2.6
Serial port COM9
Connecting........__
Detecting chip type... ESP32
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 80:7d:3a:b7:a7:5c
Uploading stub...
Running stub...
Stub running...
Manufacturer: 68
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin...


3. 燒錄 v1.15 韌體 :  

D:\ESP32>esptool.py --chip esp32 --port COM9 write_flash -z 0x1000 esp32-20210418-v1.15.bin    
esptool.py v2.6
Serial port COM9
Connecting.....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 80:7d:3a:b7:a7:5c
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 1469216 bytes to 953244...
Wrote 1469216 bytes (953244 compressed) at 0x00001000 in 85.2 seconds (effective 137.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

D:\ESP32>


4. 用 Putty 連線 COM 埠 :  

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_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:5636
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
MicroPython v1.15 on 2021-04-18; ESP32 module with ESP32
Type "help()" for more information.

檢查 Flash 記憶體大小 :

>>> dir()     
['uos', '__name__', 'sys', 'gc', 'bdev', 'esp']
>>> import esp     
>>> esp.flash_size()    
4194304    (4MB 快閃記憶體)

檢查 DRAM 使用情形 : 

>>> import micropython   
>>> micropython.mem_info()      
stack: 704 out of 15360     
GC: total: 111168, used: 16128, free: 95040     (112 KB SRAM)
 No. of 1-blocks: 108, 2-blocks: 50, max blk sz: 32, max free sz: 5928


5. 開啟 WebREPL 功能 :  

>>> import webrepl_setup       
WebREPL daemon auto-start status: disabled

Would you like to (E)nable or (D)isable it running on boot?
(Empty line to quit)
> E                                                           #輸入 E 開啟 WebREPL 功能
To enable WebREPL, you must set password for it
New password (4-9 chars): 123456        #自訂 WebREPL 連線密碼
Confirm password: 123456 
Changes will be activated after reboot
Would you like to reboot now? (y/n)y     #要用小寫 y (按 enter 會熱開機)
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_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:5636
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
Started webrepl in normal mode
MicroPython v1.15 on 2021-04-18; ESP32 module with ESP32
Type "help()" for more information.
>>>


6. 更新 boot.py :  

將下列程式碼存成 boot.py : 

#boot.py
import esp
esp.osdebug(None)
import webrepl
webrepl.start()

import network
import ubinascii
import time
import gc

def connect(ssid, pwd):
    sta.connect(ssid, pwd)
    print('Connecting to WiFi AP=', ssid, ' ...')
    time.sleep(8)
    if sta.isconnected():
        print('Connected: ', sta.ifconfig()[0])
    else:
        print('Can not connect to AP=' + ssid)

def disconnect():
    sta.disconnect()
    return True

def scan():
    aps=sta.scan()
    for ap in aps:
        ssid=ap[0].decode()
        mac=ubinascii.hexlify(ap[1], ':').decode()
        rssi=str(ap[3]) + 'dBm'
        print('{:>20} {:>20} {:>10}'.format(ssid, mac, rssi))

def ip():
    return sta.ifconfig()[0]

def pre0(n):
    if n<10:
        return '0' + str(n)
    else:
        return str(n)

def now():
    from ntptime import settime
    try:
        settime()
    except: 
        pass
    utc_epoch=time.mktime(time.localtime())
    Y,M,D,H,m,S,ms,W=time.localtime(utc_epoch + 28800)
    t='%s-%s-%s %s:%s:%s' % (str(Y),pre0(M),pre0(D),pre0(H),pre0(m),pre0(S))    
    return t

def setAP():
    html="""
    <!DOCTYPE html>
    <html>
      <head><title>AP Setup</title></head>
      <body>
        %s
      </body>
    </html>
    """
    form="""
        <form method=get action='/update_ap'>
          <table border="0">
            <tr>
              <td>SSID</td>
              <td><input name=ssid type=text></td>
            </tr>
            <tr>
              <td>PWD </td>
              <td><input name=pwd type=text></td>
            </tr>
            <tr>
              <td></td>
              <td align=right><input type=submit value=Connect></td>
            </tr>
          </table>
        </form>
    """
    import socket
    ss=socket.socket()
    ss.bind(('192.168.4.1', 80))
    ss.setsockopt(4095, 4, 1)
    ss.listen(5)
    print('Web server listening on 192.168.4.1:80')
    while True:
        cs, addr=ss.accept()
        print('Client connected from', addr)
        data=cs.recv(1024)            
        request=str(data,'utf8')
        print(request, end='\n')
        if request.find('update_ap?') == 5:
            para=request[request.find('ssid='):request.find(' HTTP/')]
            ssid=para.split('&')[0].split('=')[1]
            pwd=para.split('&')[1].split('=')[1]
            sta.connect(ssid, pwd)
            print('Connecting to AP=', ssid, ' ...')
            time.sleep(8)
            if sta.isconnected():
                print('Connected:IP=', sta.ifconfig()[0])
                cs.send(html % 'Connected:IP=' + sta.ifconfig()[0])
                cs.close()
                ss.close()
                break
            else:
                print('Can not connect to AP=' + ssid) 
                cs.send(html % 'Failed.<a href=history.back()>Back</a>')
        else:
            cs.send(html % form)
        cs.close()

ap=network.WLAN(network.AP_IF)
ap.active(True)
ap.config(authmode=4, password='micropythoN')
sta=network.WLAN(network.STA_IF)
sta.active(True)

然後利用 ampy 上傳到 ESP32 根目錄下 (注意, 必須先關掉 Putty) : 

D:\ESP32>ampy --port COM9 get boot.py       # 這是預設的內容
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import webrepl
webrepl.start()

D:\ESP32>ampy --port COM9 put boot.py       # 上傳 boot.py

沒有留言 :