ESP8266 uses AT commands to send GET HTTP requests

Write in front

I started debugging the ESP8266 module today. I want to use it to send GET requests in the form of serial AT commands to report information to the server. In the process of searching for information and experimenting, I found that the online explanation was not detailed. I tried to put together a few articles before walking down. Here I write an article that is as clear as possible. Welcome everyone to communicate.

good suggestion

1. It is recommended that you write AT commands in the notepad every time, and then copy them to the serial port assistant, like this:

  1. No misoperation: Some serial port assistants will use carriage return as a sending command, which is easy to misoperation.
  2. Records can be kept: Friends who have adjusted AT commands know that we may do multiple sequential AT commands in order to achieve an operation. At this time, it is very important to keep the correct operation instructions and sequence.

2. It is recommended that you keep the habit of typing \r\n by hand, and turn off the "Add carriage return and line feed" option of the
serial port assistant : this option will be carried by almost all serial port assistants. I suggest that you do not check it, but type \r\ by hand Instead of n, what is sent is what is entered, which is very helpful to transfer the correct AT command to the microcontroller. When the author once adjusted the GSM module, many pits were planted here.

Officially begin

The following operations all close the "Add carriage return and line feed" option
0) Try to connect with the module: (optional)
  AT\r\n
  Response: OK\r\n
can be used to check whether the module is working normally and whether the baud rate is correct
1) Setting WiFi mode:
  AT+CWMODE=1\r\n
  Response: OK\r\ nSet
to STA mode, the corresponding table is as follows:

digital WiFi application mode
1 STA (Station) mode
2 AP mode
3 AP+Station mode

2) Restart takes effect:
  AT+RST\r\n
  Response: OK\r\n After
a restart, there is no need to restart (some modules don’t seem to be restarted, so let’s take a look at this)

3) Connection route:
  AT+CWJAP="ssid","password"\r\n
  Response: OK\r\n (some modules return CONNECT OK\r\n)
Wait 1-2 seconds, it will Go back to a GOT IP, and when you see this, you are officially connected to the router.

4) Query device IP (optional):
  AT+CIFSR\r\n
  Response: 192.168.3.106\r\n

5) Connect to the server:
  AT+CIPSTART="TCP","192.168.2.178",8888\r\n
  Response: OK\r\ nIncoming
protocol, server IP, port number, HTTP protocol is the encapsulation of TCP protocol, So just write TCP here.

6) Turn on the transparent transmission mode:
  AT+CIPMODE=1\r\n
  Response: OK\r\n
7) Notify the module to start data transmission:
  AT+CIPSEND\r\n
  Response: After
this step, the module will reply with " >", it means that all the serial numbers that follow are data and will not be parsed into commands.

8) Send data:
  GET /Api/sensor_info_update?id=1&bty=30&temp=0698\r\n\r\nResponse
  : the data returned by the server. A
few notes:
1. This is a test with the server I set up, the http above The header was written less and no errors were reported. It seems that it has no effect. Examples of the full version written by other bloggers are:

GET /api/dgnjd8954 HTTP/1.1\r\n
Content-Type: application/json;charset=utf-8\r\n
Host: www.liefyuan.top\r\n
Connection: Keep Alive\r\n\r\n

2. Pay special attention to the space after GET, the space is meaningful
3. Note that the end is two \r\n!!!
4. Here you can interact with the server multiple times, because it is now in transparent transmission mode.
5. What's interesting is that this TCP behaves like HTTP. The server is turned off and then turned on again. The 8266 does not need to reconnect, just send data directly on the transparent transmission interface.

9) Exit transparent transmission:
  +++
  Response: None
Note: There is no \r\n here, just send +++.
10) What if you want to start transparent transmission again:
  execute from step 7.

Complete process and demonstration effect

Notepad:
Insert picture description here
Serial port assistant:
Insert picture description here
Server background:
Insert picture description here
Web front end:
Insert picture description here

Guess you like

Origin blog.csdn.net/whstudio123/article/details/106391099