Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize TCP Communication based on Python and ESP8266 Module

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "based on Python combined with ESP8266 module how to achieve TCP communication", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "based on Python combined with ESP8266 module how to achieve TCP communication" bar!

Required components

-1 ESP-01 WIFI module

-1 board of TPYBoard V102

-1 USB to TTL

-1 micro USB cable

-several DuPont lines

This experiment uses TPYBoardv102 serial port AT instruction to connect the control ESP8266 module (model ESP-01).

Step 1: burn the firmware that writes the AT instruction

Download the corresponding firmware of AT instruction

Here I downloaded a version of AiCloud 2.0 AT (512+512map), version number: v0.0.0.6.

Download the burning tool ESPFlashDownloadTool

Hardware connection

Pin diagram of the ESP-01 module:

Burning firmware also needs to use USB to TTL,ESP-01 mode, preferably using other external devices for power supply. Here, I use the TPYBoardv102 development board. However, don't forget that there is a need for sharing between the development board and TTL.

Wiring diagram:

ESP-01 module TPYBoard v102 | VCC3V3GNDGNDCH_PD3V3GPIO 0 suspended GPIO 2 suspended TXDX2 (UART 4) RXDX1 (UART 4)

Start to burn and write

USB to TTL plug in the computer, device manager to check the port number.

What I download here is the synthetic firmware, so the address is directly set to 0x00000. Other settings are as follows:

When it appears waiting for power-on synchronization, power up the ESP-01 module, or reset it, and wait for the download to succeed.

Step 2: test the AT instruction

After burning the firmware, unplug the cable connected to GPIO 0, leave it suspended, and the ESP-01 module will enter normal mode.

Open the serial helper tool http://old.tpyboard.com/download/tool/198.html, and the default baud rate is 115200.

When the ESP-01 module starts, it outputs a bunch of garbled codes. (it's not garbled, it's some inside information) as follows:

The output of the internal information, we can ignore, as long as the output of ready, it means that the module is fine.

Enter AT instruction mode

AT instruction documentation, AT firmware, burning tools.

Send instruction: AT

(note: after that, you need to change the line with an enter. Select enter and enter in the tool.)

The module returns AT\\ r\\ nOK, indicating that the module has successfully entered AT instruction mode.

Step 3: local area network TCP communication test

1. First, let ESP-01 connect to our router.

ESP-01 connects to the Server as a TCP Client. Send instructions AT+CIPSTART= "TCP", "192.168.101.116", 80

The module will automatically transmit the received data through the serial port.

Above, we are combined with USB to TTL to achieve AT instruction forwarding. Next, we implement the above functions through the TPYBoardv102 development board.

Wiring diagram:

ESP-01 module TPYBoard v102VCCGNDGNDCH_PD3V3GPIO 0 | suspended GPIO 2 suspended TXDX2 (UART 4) RXDX1 (UART 4)

Program source code:

From pyb import UARTfrom pyb import LED# waits for ESP-01 module initialization Ignore the internal information at startup pyb.delay (1000) ESP_UART = UART (4Jing 115200 timeouts 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 ('\ Ren'). Replace ('\ nMed'). Replace ('' '') print (buf) if buf.find ('busyp') >-1 or buf.find (' ERROR') >-1: # AT instruction execution failed # ending program troubleshooting reason break elif buf.find ('ATOK') >-1: # indicates AT instruction Successfully executed # successfully entered AT instruction mode # set WIFI mode to sta mode sendToUart (CWMODE_CUR) elif buf.find (CWMODE_CUR) >-1: # set sta mode successfully Connect AP sendToUart (CWJAP_CUR) LED (1). On () elif buf.find ('WIFIGOTIP') >-1: # connection AP succeeded # start connecting TCP Server pyb.delay (150) sendToUart (CIPSTART) LED (2). On () elif buf.find (CIPSTART) >-1: # connected to TCP Server successfully Send data CIPSEND = CIPSEND% str (len (msg)) sendToUart (CIPSEND) LED (3). On () elif buf.find () >-1: # send data sendToUart (msg) LED (4). On ()

Note: when the ESP-01 module is powered on, the current is relatively large, which will cause the REPL of the board to be unresponsive. Close putty and other tools, press the RST of the board to reset it, and then connect it. It is best to use an external power supply to power the ESP-01 module, and pay attention to sharing the ground with the board.

Thank you for your reading, the above is the content of "how to achieve TCP communication based on Python combined with ESP8266 module". After the study of this article, I believe you have a deeper understanding of how to achieve TCP communication based on Python combined with ESP8266 module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report