Home » Uncategorized » ESP8266 快速开发指南

ESP8266 快速开发指南

这是针对 ESP8266 版的快速指南,请大家注意与 pyboard 版的区别。简单介绍一组有关 ESP8266 开发板下使用 MicroPython 开发的范例程序。

machine 和频率控制

1
2
3
4
importmachine
machine.freq() # get the current frequency of the CPU
machine.freq(160000000) # set the CPU frequency to 160 MHz

ESP模块

1
2
3
4
importesp
esp.osdebug(None) # turn off vendor O/S debugging messages
esp.osdebug(0) # redirect vendor O/S debugging messages to UART(0)

网络

1
2
3
4
5
6
7
8
9
10
11
12
13
importnetwork
wlan =network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'password') # connect to an AP
wlan.config('mac') # get the interface's MAC adddress
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap =network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point

一个用于连接到你的本地 WiFi 的函数:

1
2
3
4
5
6
7
8
9
10
11
defdo_connect():
importnetwork
wlan =network.WLAN(network.STA_IF)
wlan.active(True)
ifnotwlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
whilenotwlan.isconnected():
pass
print('network config:', wlan.ifconfig())
网络连接后,就可以创建和使用 TCP/UDP 了。

GPIO

1
2
3
4
5
6
7
8
9
10
11
12
frommachine importPin
p0 =Pin(0, Pin.OUT) # create output pin on GPIO0
p0.high() # set pin to high
p0.low() # set pin to low
p0.value(1) # set pin to high
p2 =Pin(2, Pin.IN) # create input pin on GPIO2
print(p2.value()) # get value, 0 or 1
p4 =Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
p5 =Pin(5, Pin.OUT, value=1) # set pin high on creation

定时器

1
2
3
4
5
frommachine importTimer
tim =Timer(-1)
tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambdat:print(1))
tim.init(period=2000, mode=Timer.PERIODIC, callback=lambdat:print(2))

延时

1
2
3
4
5
6
7
importtime
time.sleep(1) # sleep for 1 second
time.sleep_ms(500) # sleep for 500 milliseconds
time.sleep_us(10) # sleep for 10 microseconds
start =time.ticks_ms() # get millisecond counter
delta =time.ticks_diff(start, time.ticks_ms()) # compute time difference

PWM

1
2
3
4
5
6
7
8
9
10
frommachine importPin, PWM
pwm0 =PWM(Pin(0)) # create PWM object from a pin
pwm0.freq() # get current frequency
pwm0.freq(1000) # set frequency
pwm0.duty() # get current duty cycle
pwm0.duty(200) # set duty cycle
pwm0.deinit() # turn off PWM on the pin
pwm2 =PWM(Pin(2), freq=500, duty=512) # create and configure in one go

ADC

1
2
3
4
frommachine importADC
adc =ADC(0) # create ADC object on ADC pin
adc.read() # read value, 0-1024

软件 SPI

SPI 有两种驱动。一种是软件方式 (bit-banging),可以使用任何 pins:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
frommachine importPin, SPI
# construct an SPI bus on the given pins
# polarity is the idle state of SCK
# phase=0 means sample on the first edge of SCK, phase=1 means the second
spi =SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
spi.init(baudrate=200000) # set the baudrate
spi.read(10) # read 10 bytes on MISO
spi.read(10, 0xff) # read 10 bytes while outputing 0xff on MOSI
buf =bytearray(50) # create a buffer
spi.readinto(buf) # read into the given buffer (reads 50 bytes in this case)
spi.readinto(buf, 0xff) # read into the given buffer and output 0xff on MOSI
spi.write(b'12345') # write 5 bytes on MOSI
buf =bytearray(4) # create a buffer
spi.write_readinto(b'1234', buf) # write to MOSI and read from MISO into the buffer
spi.write_readinto(buf, buf) # write buf to MOSI and read MISO back into buf

硬件 SPI

硬件 SPI 速度更快 (最高 80Mhz),但是只能使用下面的 pins: MISO – GPIO12, MOSI – GPIO13, 以及 SCK – GPIO14。它的用法和上面的软件SPI相同,除了pin参数:

1
2
3
4
frommachine importPin, SPI
hspi =SPI(1, baudrate=80000000, polarity=0, phase=0)
(SPI(0) 仅用于内部的 FlashROM。)

I2C

1
2
3
4
5
6
7
8
9
10
frommachine importPin, I2C
# construct an I2C bus
i2c =I2C(scl=Pin(5), sda=Pin(4), freq=100000)
i2c.readfrom(0x3a, 4) # read 4 bytes from slave device with address 0x3a
i2c.writeto(0x3a, '12') # write '12' to slave device with address 0x3a
buf =bytearray(10) # create a buffer with 10 bytes
i2c.writeto(0x3a, buf) # write the given buffer to the slave

休眠

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
importmachine
# configure RTC.ALARM0 to be able to wake the device
rtc =machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# check if the device woke from a deep sleep
ifmachine.reset_cause() ==machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, 10000)
# put the device to sleep
machine.deepsleep()

onewire 总线

1
2
3
4
5
6
7
8
9
10
11
frommachine importPin
importonewire
ow =onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
ow.scan() # return a list of devices on the bus
ow.reset() # reset the bus
ow.readbyte() # read a byte
ow.read(5) # read 5 bytes
ow.writebyte(0x12) # write a byte on the bus
ow.write('123') # write bytes on the bus
ow.select_rom(b'12345678') # select a specific device by its ROM code

驱动 DS18B20

1
2
3
4
5
6
7
importtime
ds =onewire.DS18B20(ow)
roms =ds.scan()
ds.convert_temp()
time.sleep_ms(750)
forrom inroms:
print(ds.read_temp(rom))

网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
importnetwork
wlan =network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'password') # connect to an AP
wlan.config('mac') # get the interface's MAC adddress
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap =network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
defdo_connect():
importnetwork
wlan =network.WLAN(network.STA_IF)
wlan.active(True)
ifnotwlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
whilenotwlan.isconnected():
pass
print('network config:', wlan.ifconfig())

NeoPixel 驱动

1
2
3
4
5
6
7
8
frommachine importPin
fromneopixel importNeoPixel
pin =Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels
np =NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels
np[0] =(255, 255, 255) # set the first pixel to white
np.write() # write data to all pixels
r, g, b =np[0] # get first pixel colour

底层驱动

1
2
importesp
esp.neopixel_write(pin, grb_buf, is800khz)

APA102驱动

1
2
3
4
5
6
7
8
9
frommachine importPin
fromapa102 importAPA102
clock =Pin(14, Pin.OUT) # set GPIO14 to output to drive the clock
data =Pin(13, Pin.OUT) # set GPIO13 to output to drive the data
apa =APA102(clock, data, 8) # create APA102 driver on the clock and the data pin for 8 pixels
apa[0] =(255, 255, 255, 31) # set the first pixel to white with a maximum brightness of 31
apa.write() # write data to all pixels
r, g, b, brightness =apa[0] # get first pixel colour

底层驱动

1
2
importesp
esp.apa102_write(clock_pin, data_pin, rgbi_buf)

webrepl

1
2
3
importwebrepl
webrepl.start()
webrepl.stop()

 

ESP8266-12F wifi模块烧录AT指令固件过程总结

 

这个过程也是比较折腾的,先是去乐鑫官网或者机智云官网下载最新的烧录软件flash_download_tools_v3.6.4和AT指令固件版本esp8266_at_bin_v1.6.1。

按照这个网页http://docs.gizwits.com/zh-cn/deviceDev/ESP8266%E4%B8%B2%E5%8F%A3%E7%83%A7%E5%86%99%E8%AF%B4%E6%98%8E.html提示,先修改硬件接线

开始按如下配置下载限选,发现下载成功后,WIFI模块不可用,发送AT命令没有任何反馈。

支持云端升级 (Boot 模式)

文件名称 8Mbit 地址分配 16Mbit 地址分配 32Mbit 地址分配 备注
boot.bin 0x00000 0x00000 0x00000 由乐鑫在 SDK 中提供,建议一直使用最新版本
user1.bin 0x01000 0x01000 0x01000 主程序,由代码编译生成
user2.bin 0x81000 0x81000 0x81000 主程序,由代码编译生成
esp_init_data_default.bin 0xFC000 0x1FC000 0x3FC000 由乐鑫在 SDK 中提供
blank.bin 0xFE000 0x1FE000 0x3FE000 由乐鑫在 SDK 中提供

很无奈,只能到安信可的官网http://wiki.ai-thinker.com/esp_download去查资料,按照如下说明重新检查一遍。

重新下载了如下合成的固件(4M Bytes大小):

出厂默认 AT 固件

特性:

一条指令即可连接到安信可云服务
操作简单,任何mcu均可直接接入

下载地址: ai-thinker_esp8266_at_firmware_dout_v1.5.4.1-a_20171130.rar

更新时间:2017年11月30日

更新说明:

  v1.5.4.1 AT Firmware,内含 8Mbit 和 32Mbit 两个版本

注意:1MByte flash的模组因为内存不足,所有带AiCloud2.0的AT固件都不能使用,flash需>=2MByte。

使用CIUPDATE命令升级前,请确认模组的flash>=2MByte!

烧录完成后界面:

1. 首先确保模组可以正常运行工作,发送 AT 指令可以有回复 AT OK(即确保电源和串口连接正常);

2. 拉低 GPIO0,打开串口工具,在波特率74880下观察模组的启动或复位后的打印信息;

3. 若出现以下红色字符则认为模组已经进入了下载模式(后面的7数值不用理会),可以去正常进行下载。

ets Jan 8 2014,rst cause 1, boot mode:(1,7)

打开串口工具,在波特率74880下观察模组的启动或复位后的打印信息如下:

ets Jan  8 2013,rst cause:2, boot mode:(7,0)
waiting for host
后来跟安信可的技术工程师沟通发现,正常工作时需要将 ESP8266 的 boot 模式切换到 flash boot,即 GPIO15→0, GPIO0→1, GPIO2→1。

修改后可以正常进行AT命令通信了。

刚启动时波特率设置成74880:

ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 1856, room 16
tail 0
chksum 0x63
load 0x3ffe8000, len 776, room 8
tail 0
chksum 0x02
load 0x3ffe8310, len 552, room 8
tail 0
chksum 0x79
csum 0x79

2nd boot version : 1.5

SPI Speed      : 40MHz
SPI Mode       : DOUT
SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000
rf cal sector: 1017

rf[112] : 00
rf[113] : 00
rf[114] : 01

SDK ver: 1.5.4.1(39cb9a32) compiled @ Jul  1 2016 20:04:35
phy ver: 972, pp ver: 10.1

h恏駃p?x?卶(血8幄纉tN

正常启动后,修改波特率为115200,发送AT命令:AT+GMR

AT version:1.2.0.0(Jul  1 2016 20:04:45)
SDK version:1.5.4.1(39cb9a32)
Ai-Thinker Technology Co. Ltd.
v1.5.4.1-a Nov 30 2017 15:54:29
OK
AT+GMR

AT version:1.2.0.0(Jul  1 2016 20:04:45)
SDK version:1.5.4.1(39cb9a32)
Ai-Thinker Technology Co. Ltd.
v1.5.4.1-a Nov 30 2017 15:54:29
OK