Basic usage and configuration tutorial of ESP8266 serial WiFi module

Foreword:
ESP8266 is an ultra-low power UART-WiFi transparent transmission module, with the industry's most competitive package size and ultra-low energy technology, designed for mobile devices and IoT applications, which can connect the user's physical devices On the Wi-Fi wireless network, carry out Internet or local area network communication, and realize the networking function.
Since I have been engaged in the research and development of medical information processing and medical Internet of Things equipment, hardware networking has always been a troublesome thing. By using the ESP8266 WiFi module, I only need to use AT command control through the serial port to meet most network requirements. Functional Requirements. In this article, through a brief introduction and application examples, readers who are new to the WiFi module can understand the general usage of the module.
Introduction to ESP8266

ESP8266 is a low-power WiFi chip designed by Shanghai Espressif Information Technology, which integrates a complete TCP/IP protocol stack and MCU. The ESP8266 module is a serial WiFi module developed by Shenzhen Anxinke Company based on the ESP8266 chip (adding necessary peripheral circuits, serial port flash, on-board antenna, etc.), low cost, easy to use, and powerful.

The bridge between hardware and network

Like the serial port Bluetooth module, the serial port WiFi module is also another artifact to expand the functions of the microcontroller. Before I got in touch with 8266, I was very curious about how the single-chip microcomputer is connected to the Internet. I have seen in the book that it is implemented with a cumbersome and cumbersome Ethernet module, but it is not handsome at all. And the compact ESP8266 WiFi module communicates with the microcontroller through the serial port AT command to realize the serial port transparent transmission, which is very easy to use.

Transparent transmission, also known as transparent transmission, specifically means "input is output (such as characters input from the serial port of the WiFi module will be transparently transmitted to the server)", the data does not change, and the conversion between different protocols (such as serial port to WiFi, Bluetooth, etc.) are done by the module. The user does not need to care about the internal implementation, so the module is "transparent" to the user and does not seem to exist (because the intermediate implementation principle can be ignored). A highly encapsulated module should hide internal implementation details and only provide external use interfaces.

After the hardware is connected to the Internet, it is no longer "playing a single machine". With the Socket network programming on the server side, you can play many things. So I think the WiFi module is a bridge that connects software (network programming) and hardware (MCU), connecting the MCU and Web knowledge I have learned.

Recently, the "Internet of Things" or "smart hardware" seems to be on the rise. The emergence of many powerful chips and modules has greatly lowered the threshold for the development of Internet-connected devices. It is because of these major manufacturers that pave the way for the great cause of the Internet of Things, scumbags such as me who know nothing about the network can easily connect the hardware to the Internet and realize network communication.
Schematic and pin description
! [Insert picture description here](https://img-blog.csdnimg.cn/img_convert/3136f36fe26f087f1261acf2203d818b.pinsert image description hereinsert image description here

To make the module power on and work normally, only three connections are needed: VCC and CH_PD are connected to the positive pole of the 3.3 power supply, and GND is grounded. It just so happens that there is a 3.3V power supply on the USB-TTL download module. When using serial port debugging, cross-connect the module with the TXD and RXD of the downloader. In this way, the WiFi module can be connected to the computer and tested with the serial port assistant.
insert image description here
The following is to perform AT mode debugging on the serial port assistant after connecting the module with USB to TTL; 1. AT
test:
After installing the driver of the USB-TTL module on the computer, insert the module into the USB port, check the serial port number in the device manager, and open it. Serial debugging tool XCOM (punctual atom), the serial port number selection module corresponds to the default baud rate 115200, data-stop-check-flow control: 8-1-None-None, check "send new line" (must be certain Remember to check "Send new line", otherwise WiFi will not recognize AT commands), click the "Open Serial Port" button, and then send AT, if the serial port prints garbled characters and sees "OK", it means the module has entered the AT mode. Different from some Bluetooth modules that need to press the button to enter the AT command mode, the ESP8266 runs in the AT command mode after it is powered on.
insert image description here2. AT commands of ESP8266

The AT command was first contacted on the Bluetooth module. The so-called AT command is essentially some special strings that play a controlling role. The module can be controlled and used through AT commands and developed with source code API functions. The former is fast in development and very low in difficulty (for foolish use). The latter is more flexible and more difficult. This article only covers the former, and the latter is in the next blog;
the following only lists some of the most commonly used AT commands and their usage. For detailed parameters and usage instructions of the commands, please refer to the official document: ESP8266 AT Command Set

AT               测试模块是否启动,进入AT模式
AT+RST           重启
AT+GMR           查看版本信息
AT+CWMODE 	     设置WiFi模式(sta/AP/sta+AP)
AT+CWLAP 	     扫描附近的AP信息
AT+CWJAP 	     连接AP
AT+CWQAP 	     与AP断开连接
AT+CWSAP 	     设置ESP8266 softAP配置
AT+CWLIF 	     获取连接到 ESP8266 softAP 的 station 的信息

3. How the module connects to our mobile hotspot or our own home router:

1. Send AT+CWMODE=1 to configure the module as sta mode (parameters 1, 2, and 3 correspond to modes sta, AP and sta/AP respectively). In this mode, the esp8266 module is equivalent to a client and can connect to other routers;
insert image description here

2. Send the AT+CWLAP command to scan the current nearby WiFi, and the module will return the list of available APs.
insert image description here3. Use AT+CWJAP="WiFi name", "WiFi password" to connect to the specified router, for example, my mobile phone hotspot is "fly", the password is "hhhhjjjj", the command sent to actually connect to WiFi is AT+CWJAP="" fly", "hhhhjjjj".
insert image description here
The returned "WIFI CONNECTED" indicates that the connection is successful, and "WIFI GOT IP" means that the module has been assigned an IP.

Finally, use AT+CWQAP to disconnect the currently connected WiFi.

4. esp8266 can also be used as a wifi hotspot by itself; it is the same as opening a hotspot on a mobile phone.
Steps: 1. Use AT+CWMODE=2 command to configure the module as AP mode
insert image description here
2. Send AT+CWSAP="ESP8266", "12345678", 3,4 to set the AP's The ssid is "ESP8266", the password is 12345678, and the last two parameters 3 and 4 represent the channel and encryption method, respectively. When the mobile phone is connected to the WiFi of the module, use AT+CWLIF to view the list of clients currently connected to the AP.
insert image description hereinsert image description here5. How to connect TCP server with WiFi module

TCP/IP protocol:

Transmission Control Protocol (English: Transmission Control Protocol, abbreviated as TCP) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, defined by RFC 793 of the IETF. In the simplified OSI model of computer network, it completes the functions specified by the fourth layer transport layer, and the User Datagram Protocol (UDP) is another important transport protocol within the same layer.

In the Internet protocol suite, the TCP layer is an intermediate layer above the IP layer and below the application layer. Reliable, pipe-like connections are often required between application layers of different hosts, but the IP layer does not provide such a streaming mechanism, but provides unreliable packet exchange. --Wikipedia

We often talk about the Internet, how do the two devices connected to the Internet "communicate" with each other? TCP connection is one of the most commonly used methods. TCP is a connection-oriented transport layer protocol. Both parties must implement the TCP protocol. One party only needs the target IP address and port number to initiate a connection. Once the connection is established, it is like pulling a pipe between the two parties. Full-duplex (two-way simultaneous transmission and reception) communication is possible.

TCP is a transport layer protocol, which is encapsulated on the basis of the network layer IP protocol. And the implementation details of these encapsulations are irrelevant to us. We only need to use the relevant interfaces provided by the system, such as Socket in network programming. The ESP8266 module also implements the TCP/IP protocol stack. As a client, the module can easily use AT commands to initiate a TCP connection to the server. After connecting to the TCP server and enabling the transparent transmission mode, the data received by the serial port of the module will be transparently transmitted to the server through the TCP connection, thus completing the transmission of data from the hardware serial port to the program process through the network, realizing the combination of software and hardware.

Related AT commands:


AT+CIPSTATUS 	   查询网络连接信息
AT+CIPMUX 	       设置多连接模式
AT+CIPSTART 	   建立TCP连接UDP传输或者SSL连接
AT+CIPCLOSE 	   关闭TCP/UDP/SSL传输
AT+CIPMODE 	       设置透传模式
AT+CIPSEND 	       发送数据

Steps to connect to the server:
1. First of all, we need a TCP server. We can create a TCP server by using the debugging tool "Network Debugging Assistant", so that the server can listen to the TCP connection request on the specified port. Of course, we can also write a program by ourselves. Just use what's already available.

Open the "Network Debugging Assistant", select "TCPServer" on the left, the computer and the WiFi module are connected to the same mobile phone hotspot, after checking the mobile phone hotspot IP, here is 192.168.43.258, this step is very important, you must choose the right one, choose this IP Enter the custom port number I filled in here as 1234. It is best to change it next time. As long as it is 1024-65535, it is easy to open it. Click the "Connect" button to start monitoring the local TCP connection request. Before proceeding to the next step, please make sure that the computer and the WiFi module are connected to the same router.
insert image description here

2. Enter the command AT+CIPMUX=0 to set up a single connection
insert image description here3. Learn the IP and port of the machine from the "Network Debugging Assistant", and enter the command AT+CIPSTART="TCP", "192.168.43.140", 8080 (the command parameters are Connection type, target IP address and port number) (corresponding to step 1) initiate a TCP connection request to the server. After the handshake is successful and the connection is established, the "Network Debugging Assistant" on the server side will display the client IP and port information. Be prepared to send and receive data;
insert image description hereinsert image description here

4. Enter the command AT+CIPMODE=1 to enable the transparent transmission mode
insert image description here
5. Enter the command AT+CIPSEND to enter the transparent transmission mode, at this time the module will send all the data received by the serial port to the server from the TCP port. The received data will also be sent from the module serial port and printed to sscom. In this way, the WiFi module has truly become a bridge connecting the hardware and the network, realizing the protocol conversion from serial port to TCP.

If you want to exit the transparent transmission mode and return to the AT command mode, you need to send +++ without carriage return and line feed (uncheck the "send new line" of xcom and then send the command) I
insert image description here
have seen the power of TCP transparent transmission. Learn about socket network programming, then for the function of sending and receiving data through the serial port of the single-chip microcomputer, you can now write the TCP program on the server yourself to achieve.
Written at the end: some details and precautions for using the WiFi module

1. The module uses 3.3V power supply, be sure to pay attention to the stability of the power supply, some USB-to-serial module power supply can not meet the requirements, I use the USB-to-serial port module 5V (laugh and cry...).

2. If the module is powered off after connecting to WiFi, it will automatically reconnect after the next power-on. Similarly, after the module is powered off in the transparent transmission mode, it will enter the transparent transmission mode next time it is powered on, and it does not respond to AT commands, so it is easy to mistake the module for failure. To exit the transparent transmission mode, you need to send +++ without carriage return and line feed

3. If the complete TCP exit process is not implemented when the TCP connection is disconnected, ESP8266 will judge it as abnormally open and reconnect to the TCP server.

4. The AT command must end with a carriage return line feed character " ", or check "Send new line", you only need to choose one of the two, but to exit the transparent transmission mode, you need to send +++ without carriage return line feed;
5. If the AT mode is abnormal, don't rush to restart, confirm whether the mode is correct once AT+CWMODE Set the WiFi mode (sta/AP/sta+AP); whether to exit the transparent transmission mode;
6. There is only so much to write about the WiFi module . After trying the above AT commands and observing the effect, then the AT commands of the ESP8266 module are basically no problem, and then you can use your imagination to develop more interesting things with the module~~~
Reference from: http ://www.elecfans.com/d/1445744.html

おすすめ

転載: blog.csdn.net/jinanhezhuang/article/details/118975506