⬆️ ⬇️

Working with ESP8266: Initial Setup, Firmware Update, Wi-Fi Communication, Send-Receive Data to PC

On Habré, there have already been a couple of articles on the ESP8266 chip of the Chinese company Espressif. Article number 1 and Article number 2 . Not long ago, I received an ESP-01 fee for testing. Who cares, I ask under the cat.





I ordered the simplest board with ESP8266 - ESP-01, it looks like this:





')

In the old revision of the board, only VCC, GND, URXD and UTXD were derived on the connector.

In the last revision added RST, GPIO0, GPIO2 and CH_PD.



There are a total of 11 board modifications, differing in the number of pins and design options:

ESP-01: PCB antenna after 400 meters, easy to use.

ESP-02: IPD header casing.

ESP-03: SMD package, IO leads.

ESP-04: SMD package, customers can customize their IO types.

ESP-05: SMD package, small external antenna.

ESP-06: can be had FCC CEcertification, recommended.

ESP-07: Semi-hole chip technology, can be had FCC, CE certifiedIPX external antenna, can be also built-in ceramic antenna.

ESP-08: with the ESP-07, except you can define your own.

ESP-09: Ultra-small size package, only 10 * 10 mm, four-layer board technology 1M bytes! ..

ESP-10: SMD interface, narrow-body design, 10 mm wide, suitable for light with controller.

ESP-11: SMD interface, ceramic antenna, small volume.



Pinout connector ESP-01:







The pin assignment of the ESP-01 board is:

VCC, GND - board power supply (+ 3.3V);

URXD, UTXD - RS232 pins tolerant to 3.3V

RST - Hardware reset

GPIO0, GPIO2 - GPIO Pins

CH_PD - Chip enable, for work must be connected to + 3.3V.



To switch to the firmware update mode, you need to submit a low level to GPIO0 and a high level to CH_PD.



To connect the ESP-01 board to a PC, I used a USB-to-RS232 converter on the FT232R with TTL 3.3V outputs, for example, you can use it .

ESP-01 power supply is strictly 3.3V, so I had to use a DC-DC converter, you can use one .



With the basic firmware, the ESP-01 board is controlled by AT commands, so we need a terminal program, I used CoolTerm .



There are 2 options for using the module:

1. Using the ESP-01 board in conjunction with the submicrocontroller that will control the module via UART.

2. Writing your own firmware for the ESP8266 chip and using it as a self-contained device.



Naturally, the 2nd option is more profitable, the more the potential of the ESP8266 chip is large enough.



To begin with, we will try option # 1, that is, control the ESP-01 board via RS232.



Connection diagram is very simple:

VCC pin - board power supply (+ 3.3V);

GND output is common;

Conclusions URXD, UTXD - we connect to the converter USB-to-RS232 (in 3.3V mode)

Output CH_PD - connect to board power supply (+ 3.3V);



In the terminal (CoolTerm) we set the speed of the COM port 57600. You need to install this speed, since if the ESP8266 chip has an old firmware (and most likely it is), then it will only work at that port speed.



Click Connect, enter the AT command, the answer should come OK. If everything is so, then the board is working, you can move on.







Firmware update procedure



Enter the AT + GMR command — check the AT version and the SDK, in response, issue 0016000902, where 0016 is the SDK version, 0901 is the AT version



Currently (11/06/2014), firmware 0018000902 is already available (SDK version - 0018, in the AT version - 0902)



Now we can and need to update the firmware:

1. Download the XTCOM utility from here .

2. Downloading ESP_8266_v0.9.2.2 AT Firmware.bin firmware from here

3. Turning off power to the board, connecting GPIO0's output to the common wire, turning on the power.

4. Run XTCOM_UTIL.exe, go to Tools -> Config Device, select the COM port to which the board is connected, set the port speed to 57600, click Open, then Connect, the program should say “Connect with target OK!”, Close the settings window. Go to the API TEST menu, select (4) Flash Image Download, specify the path to the file “ESP_8266_v0.9.2.2 AT Firmware.bin”, leave the address 0x00000, click DownLoad. The firmware download should begin, a message will be displayed when finished.

5. Disconnect the board power, disconnect the GPIO0 output from the common wire, turn on the power, start the terminal (ATTENTION! Change the port speed to 9600), check the readiness of the board with the AT command and the firmware version with the AT + GMR command.



After upgrading to version 0018000902, the default COM port speed will change from 57600 to 9600, but this speed in the new firmware can now be set using the AT + CIOBAUD command. We look AT + CIOBAUD =? available speeds and set the command AT + CIOBAUD = 115200 speed 115200, in response, should give OK. We give the command to restart: AT + RST. We change the speed of the port in the terminal program to 115200.



Example:

AT OK AT+CIOBAUD=? +CIOBAUD:(9600-921600) OK AT+CIOBAUD=115200 BAUD->115200 OK 




Wi-Fi connection setup



Now we will try to connect our ESP-01 board to a Wi-Fi access point.

We execute the following commands:

1. Set the Wi-Fi operation mode with the command:
  AT + CWMODE = <mode> 
The following modes are available: 1 - STA, 2 - AP, 3 - BOTH

Example:

 AT+CWMODE=1 OK 
2. We look at the list of access points with the command: AT + CWLAP

Example

 AT+CWLAP +CWLAP:(3,"WiFi-DOM.ru-0474",-85,"c8:d3:a3:30:17:40",8) +CWLAP:(4,"Intersvyaz_516C",-89,"2c:ab:25:ff:51:6c",10) +CWLAP:(4,"pletneva",-96,"f8:1a:67:67:2b:96",11) +CWLAP:(4,"Test",-69,"64:70:02:4e:01:4e",13) OK 
The brackets indicate: SECURITY, SSID, RSSI, BSSID, CHANNEL

SECURITY can be:

0 - OPEN, 1 - WEP, 2 - WPA-PSK, 3 - WPA2-PSK, 4 - MIXED (WPA-WPA2-PSK)

3. Connect to our AP with the command:
  AT + CWJAP = "SSID", "PASSWORD" 
Example:

 AT+CWJAP="Test","habrahabr" OK 
The connection lasts 2-5 seconds, after which, if successful, OK appears.

3. Let's see what IP address our board received with the command: AT + CIFSR

 AT+CIFSR 192.168.1.104 OK 
Disconnection from the access point is done with AT + CWQAP.

Address received, you can move on.



The ESP-01 board can act as a Soft-AP, to enable this mode, execute the following commands:

1. Disconnect from the access point: AT + CWQAP.

2. Change the Wi-Fi mode with the command: AT + CWMODE = 2

3. Create your AP with the command:
  AT + CWSAP = "SSID", "PASSWORD", CHANNEL, SECURITY 
Example:

 AT+CWSAP="Test2","habrahabr",10,4 OK 
4. We try to connect to our AP from a computer. Let's see the result:



As you can see in the picture, the speed is only 54 Mbit / s and I’m also confused by the DNS server addresses, I think they are obviously Chinese, you can’t deliver your own via AT commands.

The AP address can be found with the command: AT + CIFSR

Example:

 AT+CIFSR 192.168.4.1 OK 
The list of clients of our AP can be viewed with the command: AT + CWLIF

Example:

 AT+CWLIF 192.168.4.101,f4:ec:38:8d:05:62 OK 




Setting the TCP server mode



On the ESP-01 board, you can start a TCP server for receiving and sending data, or it can act as a TCP client for receiving and sending data to the server.

To start the TCP server, execute the following commands:

1. Set the transfer mode by command
  AT + CIPMODE = <mode> 
mode = 0 - not data mode (the server can send data to the client and can receive data from the client)

mode = 1 - data mode (the server cannot send data to the client, but can receive data from the client)

Example:

 AT+CIPMODE=0 OK 
2. Set the possibility of multiple connections:
  AT + CIPMUX = <mode> 
mode 0 - single connection

mode 1 - multiple connection

You can check the connection mode using the command AT + CIPMUX?

Example:

 AT+CIPMUX=1 OK AT+CIPMUX? +CIPMUX:1 OK 
3. Start the server on port 8888:
  AT + CIPSERVER = <mode> [, <port>] 
mode 0 - to close server

mode 1 - to open server

Example:

 AT+CIPSERVER=1,8888 OK 


Now you can connect to ESP-01 and send or receive any data. We will use the SocketTest utility to connect

Run java -jar SocketTest.jar, on the Client tab, enter the address and port of ESP-01, click Connect. If the connection is successful, then the message Link will appear in the terminal and the Message string and the Send button will become active in SocketTest.

You can view the list of active connections to ESP-01 using the command AT + CIPSTATUS

Example:

 AT+CIPSTATUS STATUS:3 +CIPSTATUS:0,"TCP","192.168.1.100",44667,1 OK 
You can close the active connection with the command
  AT + CIPCLOSE = <id> 
or all AT + CIPCLOSE connections without parameters.

Example:

 AT+CIPCLOSE=0 OK Unlink 
4. Send data from ESP-01 to PC

For Single connection mode (+ CIPMUX = 0) the sending goes like this:
  AT + CIPSEND = <length> 
For Multiple connection mode (+ CIPMUX = 1), the sending goes like this:
  AT + CIPSEND = <id>, <length> 
After completing AT + CIPSEND, you need to enter text, input is completed and sent by Enter.

Example:

 AT+CIPSEND=0,16 > Ping Habrahabr SEND OK 
5. We send a test message from the PC:



The line appears in the terminal
  + IPD, 0.16: Ping Habrahabr 
Message accepted.

The format of the received data is:

For Single Connection mode (CIPMUX = 0):
  + IPD, <len>: <data> 
For Multiple Connection Mode (CIPMUX = 1):
  + IPD, <id>, <len>: <data> 




Setting TCP client mode



Now let's change the roles, PC - server, ESP-01 - client, try:

1. Restart the AT + RST board

2. Set the transfer mode with the command
  AT + CIPMODE = <mode> 
mode = 0 - not data mode (the client can send data to the server and can receive data from the server)

mode = 1 - data mode (the client cannot send data to the server, but can receive data from the server)

Example:

 AT+CIPMODE=0 OK 
3. Connection mode set Multiple connection: AT + CIPMUX = 1

4. Start the server on port 8888 on the PC in SocketTest

5. Run the client on ESP-01

For Single connection mode (+ CIPMUX = 0), the format is
  AT + CIPSTART = <type>, <addr>, <port> 
For Multiple connection mode (+ CIPMUX = 1), the format is
  AT + CIPSTART = <id> <type>, <addr>, <port> 
Possible parameter values:

id = 0-4

type = TCP / UDP

addr = IP address

port = port

Example:

 AT+CIPMUX=1 OK AT+CIPSTART=0,"TCP","192.168.1.100",8888 OK Linked 
6. Send data from ESP-01 to PC

For Single connection mode (+ CIPMUX = 0) the sending goes like this:
  AT + CIPSEND = <length> 
For Multiple connection mode (+ CIPMUX = 1), the sending goes like this:
  AT + CIPSEND = <id>, <length> 
After completing AT + CIPSEND, you need to enter text, input is completed and sent by Enter.

Example:

 AT+CIPSEND=0,16 > Ping Habrahabr SEND OK 


Example of sending and receiving data:





Useful documentation:

Description of AT commands (in Chinese)

ESP8266 chip specification (in Chinese)

ESP8266 chip specification (in English)



Conclusion:



As we can see, the board successfully copes with its tasks, namely connecting to Wi-Fi as a client, can act as a Soft-AP, on the board you can raise a TCP server to receive and send data, or you can be a TCP client .

In this article, we looked at working with the ESP-01 board via RS232, the PC was a controlling controller, you can easily connect an Arduino board or any microcontroller with a UART and send and receive data via a Wi-Fi network between controllers or a PC.



In the next article (as Karma allows) I will try to talk about the principles of writing my own firmware for the ESP8266 chip, thus the ESP-01 board will be completely autonomous, it will not need an additional controller to control all parameters. We will try to connect various peripheral devices to the board.



I will be glad to answer questions, although I have not yet learned the ESP-01 board until the end.

Source: https://habr.com/ru/post/362623/



All Articles