MicroPython on ESP8266

NodeMCU基于Lua语言, 用起来不习惯, 改成Python更好!

Flash Firmware

$ wget http://micropython.org/resources/firmware/esp8266-20160909-v1.8.4.bin

# manufacturer ef is Winbond (ex Nexcom) and device 4016 is W25Q32, which is a 32Mbit=4MByte device
$ esptool.py --port /dev/serial0 flash_id
esptool.py v1.1  
Connecting...  
Manufacturer: ef  
Device: 4016

$ esptool.py --port=/dev/serial0 read_flash 0 $((4*1024*1024)) esp8266-backup.bin

$ esptool.py --port /dev/serial0 erase_flash
esptool.py v1.1  
Connecting...  
Erasing flash (this may take a while)...

$ esptool.py --port /dev/serial0 write_flash -fm dio -fs 32m 0x0 esp8266-20160909-v1.8.4.bin
esptool.py v1.1  
Connecting...  
Running Cesanta flasher stub...  
Flash params set to 0x0240  
Writing 565248 @ 0x0... 565248 (100 %)  
Wrote 565248 bytes at 0x0 in 51.7 seconds (87.5 kbit/s)...  
Leaving...

$ minicom -b 115200 -o -D /dev/serial0
MicroPython v1.8.4-10-gbc28ac8 on 2016-09-09; ESP module with ESP8266  
Type "help()" for more information.  
>>> print('hello, world')
hello, world  

Wireless Network

>>> import network

>>> sta_if = network.WLAN(network.STA_IF)
>>> sta_if.active(True)
#8 ets_task(4020e33c, 28, 3fff8078, 10)
>>> sta_if.scan()
>>> sta_if.connect('MiWiFi', '********')
>>> sta_if.ifconfig()

>>> ap_if = network.WLAN(network.AP_IF)
>>> ap_if.active(True)
>>> ap_if.config(essid='MicroPython', authmode=network.AUTH_OPEN)

Access GPIO

>>> import machine, time

>>> pin = machine.Pin(0, machine.Pin.OUT)
>>> pin.low()
>>> pin.high()

>>> led = machine.PWM(machine.Pin(2), freq=1000)
>>> for i in range(1024):
...     led.duty(i)
...     time.sleep_ms(100)
>>> led.deinit()

DHT11 Sensor

>>> import dht, machine
>>> d = dht.DHT11(machine.Pin(14))
>>> d.measure()
>>> d.temperature()
27  
>>> d.humidity()
57  

WebREPL

>>> import webrepl
>>> webrepl.start()

http://micropython.org/webrepl/