Connect ESP8266 from Raspberry Pi

使用树莓派的串口于ESP8266进行通讯.

  • RPi(<3B): /dev/ttyAMA0
  • RPi(=3B): /dev/serial0

释放串口

树莓派串口, 已被系统使用, 需要释放它.

在重启之前, 需要修改/boot/config.txt

$ sudo vi /boot/config.txt
- enable_uart=0
+ enable_uart=1

$ sudo reboot

导线连接

进行通讯

$ sudo apt install minicom
$ minicom -b 115200 -o -D /dev/ttyAMA0
  • 使用Enter + Ctrl-J执行指令.
  • 输入Ctrl-A + Shift-X退出

常用指令

>>> AT
OK

>>> AT+GMR
AT version:1.1.0.0(May 11 2016 18:09:56)  
SDK version:1.5.4(baaeaebb)  
Ai-Thinker Technology Co. Ltd.  
Jun 13 2016 11:29:20  
OK

>>> AT+CWMODE=3
OK

>>> AT+CWLAP
+CWLAP:(0,"aWiFi",-75,"00:24:6c:14:35:51",1,-17,0)
+CWLAP:(4,"PHICOMM_419",-85,"00:6b:8e:d9:d3:3a",1,-9,0)
+CWLAP:(2,"ChinaNet-F2YL",-88,"68:1a:b2:46:b4:7f",1,5,0)
+CWLAP:(0,"ChinaNet",-71,"00:24:6c:14:35:50",1,32767,0)
+CWLAP:(3,"WXGoldMICwifi01",-93,"00:b0:c6:42:1a:30",1,6,0)
+CWLAP:(3,"MiWiFi",-53,"f0:b4:29:25:a5:21",4,8,0)
+CWLAP:(4,"TP-LINK_PZH",-38,"5c:63:bf:85:87:0c",6,0,0)
OK

>>> AT+CWJAP="MiWiFi","********"
WIFI CONNECTED  
WIFI GOT IP  
OK

>>> AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"5e:cf:7f:28:6a:56"
+CIFSR:STAIP,"192.168.31.172"
+CIFSR:STAMAC,"5c:cf:7f:28:6a:56"
OK

>>> AT+CIPMUX=1
OK

>>> AT+CIPSERVER=1,80
OK

0,CONNECT  
+IPD,0,7:hello

>>> AT+CIPSTATUS
STATUS:3  
+CIPSTATUS:0,"TCP","192.168.31.102",64777,80,1
OK

>>> AT+CIPSEND=0,6
OK  
>world<Ctrl-J>
Recv 6 bytes  
SEND OK

>>> AT+CIPCLOSE=0
0,CLOSED  
OK  
$ nc -v 192.168.31.172 80
Connection to 192.168.31.172 port 80 [tcp/http] succeeded!  
>>> hello<Ctrl-V><Enter><Enter>
world  

Flash MicroPython Firmware

GPIO0 => GND

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

$ wget --content-disposition http://bbs.espressif.com/download/file.php?id=157

$ unzip ESP8266_NONOS_SDK_V1.5.4.1_patch_20160704.zip esp_init_data_default.bin

$ esptool.py --port=/dev/serial0 flash_id
esptool.py v1.1  
Connecting...  
Manufacturer: e0  
Device: 4014

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

$ esptool.py --port /dev/serial0 --baud 9600 write_flash -fm qio -fs 8m 0x00000 esp8266-20160909-v1.8.4.bin 0xfc000 esp_init_data_default.bin
esptool.py v1.1  
Connecting...  
Running Cesanta flasher stub...  
Flash params set to 0x0020  
Writing 565248 @ 0x0... 565248 (100 %)  
Wrote 565248 bytes at 0x0 in 606.9 seconds (7.5 kbit/s)...  
Writing 4096 @ 0xfc000... 4096 (100 %)  
Wrote 4096 bytes at 0xfc000 in 4.6 seconds (7.1 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.  
>>> from machine import Pin, PWM
>>> led = machine.PWM(Pin(2), freq=10)
>>> led.duty(0)
>>> led.duty(512)
>>> led.high(1023)
>>> led.deinit()
>>>
>>> import network
>>> sta = network.WLAN(network.STA_IF)
>>> sta.active(True)
>>> sta.scan()
>>> sta.connect('MiWiFi', '********')
>>> sta.ifconfig()

>>> ap = network.WLAN(network.AP_IF)
>>> ap.config(essid='ESP8266', authmode=network.AUTH_OPEN)
>>> ap.ifconfig()