Based MicroPython ESP8266 binding module implements TCP communication (AT command Edition)

Reproduced please specify the source of the article, more tutorials can self-refer docs.tpyboard.com, QQ technical exchange group: 157 816 561, the public number: MicroPython player sinks

 Purpose

- Learn to use ESP8266 Module (ESP-01) of.
- Further study using TPYBoardv10x in UART modules.
- ESP-01 binding module performs TCP communication experiment LAN.

Required components

- ESP-01 WIFI module 1
- TPYBoard v102 board 1
- USB transfer the TTL 1
- micro USB cable 1
- Dupont line Some

This experiment TPYBoardv102 AT commands serial port, connected to the control module ESP8266 (Model ESP-01).

The first step: WRITE AT command Firmware

AT commands corresponding firmware download

Here, I downloaded a AiCloud 2.0 AT (512 + 512map) version, the version number: v0.0.0.6.
http://wiki.ai-thinker.com/_media/aicloud/sdk/ai-thinker_esp8266_dout_aicloud_v0.0.0.6_20170517.7z

Shun can address scientific and technical information concerning ESP8266 related modules. http://wiki.ai-thinker.com/esp8266

AT commands firmware download other versions. http://wiki.ai-thinker.com/esp8266/sdk

Download burning tools ESPFlashDownloadTool

http://wiki.ai-thinker.com/_media/esp8266/flash_download_tools_v3.6.4.rar

Hardware connection

ESP-01 module pin diagram:

 

Program firmware need to use USB to TTL, ESP-01 is preferably molded using other external devices to supply power. Here, TPYBoardv102 development board I use. However, do not forget the development board between TTL and need common ground.

Wiring diagram:

ESP-01 module TPYBoard v102
|VCC 3V3
GND GND          
CH_PD        3V3          
GPIO 0 Suspended  
GPIO 2   Suspended 
TXD X2(UART 4 ) 
RXD X1(UART 4 ) 

 

 

 

 

 

 

 

 

 

 

Start programming

USB to TTL plug in the computer, the Device Manager to view the port number.

 

Open ESPFlashDownloadTool tool, select [ESP8266 DownloadTool].

I am here to download the firmware is synthetic, it is set to directly address 0x00000, other settings refer to the following:

 

Choose a good port, click [START] to start programming.

 

Wait for power synchronization occurs, then power on the ESP-01 module, or reset it, wait for the download to succeed.

Step two: Test the AT command

After programming the firmware is completed, the line connected to GPIO 0 unplug it floating, ESP-01 module into normal mode.
Open the serial port assistant tool http://old.tpyboard.com/download/tool/198.html , the default baud rate of 115200.
When the ESP-01 module starts, it will output a pile of garbage. (In fact, not garbled, some internal information) as follows:

The reason garbled, as follows:

Internal information output, we can ignore, as long as the output ready, it shows the module as usual.

Enter AT command mode

AT commands documentation, AT firmware, programming tool. http://old.tpyboard.com/download/tool/ESP-01.rar

Send command: AT

(注意:后面需跟一个回车换行,选择在工具中勾选加回车换行)
模块返回AT\\r\\nOK,说明模块已经成功进入AT指令模式。

查询固件版本

发送指令:AT+GMR

第三步:局域网TCP通信测试

1、首先,让ESP-01接入我们的路由器。

 

AT指令 功能
AT+CWMODE_CUR?

查询当前WIFI模式
1 : station 模式
2 : softAP 模式
3 : softAP+station 模式

AT+CWMODE_CUR=1  设置WIFI为station 模式
AT+CWJAP_CUR="essid","password"  连接AP 

 

.

 

 

 

 

 

 

2、接下来,我们做一个局域网内TCP通信的测试。本地用网络调试助手建立一个TCP Server如下:

ESP-01作为TCP Client连接Server。发送指令AT+CIPSTART="TCP","192.168.101.116",80

TCP链接建立,开始发送数据。单路链接时发送指令为AT+CIPSEND=x,x为发送的字节数。出现符号">"后就可以发送数据了。

模块会把收到的数据自动通过串口透传过来。

以上,我们都是结合USB转TTL实现的AT指令转发。接下来,我们通过TPYBoardv102开发板实现上面的功能。

接线图:

ESP-01模块 TPYBoard v102
VCC   
GND  GND 
CH_PD  3V3 
GPIO 0 | 悬空 
GPIO 2 悬空 
TXD  X2(UART 4 )
RXD  X1(UART 4 )

 

 

 

 

 

 

 

程序源码:

from pyb import UART
from pyb import LED

#等待ESP-01模块初始化,忽略启动时的内部信息
pyb.delay(1000)

ESP_UART = UART(4,115200,timeout=100)

def sendToUart(msg):
    ESP_UART.write(msg+'\r\n')

CWMODE_CUR = 'AT+CWMODE_CUR=1'
CWJAP_CUR = 'AT+CWJAP_CUR="TurnipSmart","turnip2016"'
CIPSTART = 'AT+CIPSTART="TCP","192.168.1.116",80'
CIPSEND = 'AT+CIPSEND=%s'
msg = 'Hello,I am TPYBoard.'

if __name__  == '__main__':
    sendToUart('AT')
    while True:
        if ESP_UART.any()  > 0:
            buf = ESP_UART.read().decode().replace('\r','').replace('\n','').replace(' ','')
            print(buf)
            if buf.find('busyp') > -1 or buf.find('ERROR') > -1:
                # AT指令执行失败
                # 结束程序排查原因
                break
            elif buf.find('ATOK') > -1:
                # 说明AT指令执行成功
                # 成功进入AT指令模式
                # 设置WIFI模式为sta模式
                sendToUart(CWMODE_CUR)
            elif buf.find(CWMODE_CUR) > -1:
                # 设置sta模式成功,连接AP
                sendToUart(CWJAP_CUR)
                LED(1).on()
            elif buf.find('WIFIGOTIP') > -1:
                # 连接AP成功
                # 开始连接TCP Server
                pyb.delay(150)
                sendToUart(CIPSTART)
                LED(2).on()
            elif buf.find(CIPSTART) > -1:
                # 连接TCP Server成功,发送数据
                CIPSEND = CIPSEND % str(len(msg))
                sendToUart(CIPSEND)
                LED(3).on()
            elif buf.find('>' )> -1 :
                 # transmit data 
                sendToUart (MSG) 
                the LED ( . 4) .on ()

 

Note: When the ESP-01 module power-up current is relatively large, will lead the board REPL no response. Close putty and other tools, according to the board RST reset it, and then connect just fine. The best way to use an external power supply module ESP-01, and the board to be noted that common ground.

In summary, TCP test experiment is completed. In this study, just do some simple examples, you can combine documents according to their own ideas to develop a more interesting works, such as intelligent control switch, WIFI smart car and so on.

Download Source: https://github.com/TPYBoard/TPYBoard-v102

 

Guess you like

Origin www.cnblogs.com/xiaowuyi/p/10969247.html