[Springboot][STM32]Springboot+STM32+ESP8266 Use HTTP GET and POST to send requests to upload data to the Springboot project and display

I’m coming to Java, la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la

 

       What's the matter with STM32+ESP8266 uploading data? I believe everyone is familiar with STM32+ESP8266, but how does STM32+ESP8266 upload data? Let me take you to understand it.
  STM32+ESP8266 uploads data. In fact, it uses HTTP protocol to send GET or POST requests to access the server. You may be surprised how STM32+ESP8266 uploads data? But this is the fact, the editor is also very surprised.
  This is about STM32+ESP8266 uploading data. What are your thoughts? Please tell me in the comment section to discuss it together!

✿✿ヽ(°▽°)ノ✿ is over!

 

Oh, you're still there, it's over, there's nothing left behind

Really! Nodding your head Really!! Definitely

 


When we rushed to the editor's house, only this article was left

 

 

Preface

I am a JavaWeb programmer I am a JavaWeb programmer I am a JavaWeb programmer 

Writing STM32 is just a hobby, the main business is to develop background programs


This article mainly talks about the differences and advantages and disadvantages of using GET and POST to send data, and using Springboot as the server to receive and display data

I hope to help developers and enthusiasts with similar ideas to reduce the time wasted to find information

For details on how to write STM32 to control the ESP8266 driver and sending program, please refer to the article I wrote before.

[STM32] Stm32f103c8t6+ESP8266-01s+DHT11 realizes uploading temperature and humidity data to the server

 

table of Contents

Achievement display

Temperature data collected at 5 minute intervals

Temperature data collected at one-minute intervals

Humidity data collected at 5-minute intervals

Humidity data collected at 1 minute intervals

About Springboot

Data sending method

         GET access method

GET is implemented in STM32

           POST+JSON access method

POST+JSON is implemented in STM32


 

Achievement display

Temperature data collected at 5 minute intervals

Temperature data collected at one-minute intervals

Humidity data collected at 5-minute intervals

Humidity data collected at 1 minute intervals

The above pages are all displayed on the front end using Echarts

 

About Springboot

This article mentions Springboot with relatively little space, but I still want to say that the server is very important. The Internet of Things is to say that small devices upload and download data through the Internet. The real decision-making is the server that sends instructions.

Some of the people I have contacted send data to simulated servers such as network debugging assistants. Although they can see the received data, the data is not actually stored and cannot be used.

This is the reason why I built the client, and I am a JavaWeb developer, and I am very familiar with the backend, which happens to be a combination of software and hardware.

ESP8266 connects to the Springboot server to upload data via TCP

The server is developed using the Springboot project, the front end uses Echarts to display the data curve, and the database uses mysql

The data is uploaded every 30 seconds, and the server will save 1 minute of all day 1440 points of data, 5 minutes of 288 points of data, 15 minutes of 96 points of data, 30 minutes of 48 points of data, a total of four granularities

A lot of consideration was taken into the database design. First of all, the number of fields in the table was considered more, because more than 100 fields in a table would affect the query efficiency, and because I am a horizontal table, the smallest granularity of data storage is 1 minute. The value is 1440 values ​​throughout the day, and there are 288 values ​​in five minutes.The final conclusion is to reduce the use of domains to improve performance.If the data is too long, increase the number of data storages, which can meet both database performance and all particles. For example, 1440 pieces of data will be split into multiple pieces of data and stored in the database. Of course, the actual situation is a one-minute storage. The specific logic of how to store is a bit complicated. The logic is very strong and it is not easy to share. You can freely follow your own program design

The specific background design can monitor and alert the data according to your own needs, that is, you can play freely.

 

 

Data sending method

ESP8266 uses HTTP protocol to send requests to access the server and upload data

I won’t talk about the HTTP protocol here. I don’t know much about conceptual knowledge, and it’s easy to find such knowledge on the Internet.

Here we mainly talk about how ESP3266 sends GET and POST requests

 

GET access method

There are many ways to GET, here is the splicing of parameters in the URL, such as: IP: port/route/parameter1/parameter 2

Of course, you can also use another type such as: IP: Port/Route? Parameter name 1 = Parameter 1 & Parameter name 2 = Parameter 2

It's all possible, the server program can be accessed as long as the HTTP access method is spliced ​​according to the method used.

 Write GET access method in Springboot 's controller

It is recommended to use postman to familiarize yourself with http access

Select the GET method and separate the parameters with "/" after utl

Send the status code 200 to indicate that you can access

Status code: 200 The server received a response, 400 The parameter was incorrect, and 404 could not be accessed

Background printing

Data is received in the background, indicating that there is no problem with the access method

So how do you use the microcontroller to access this address?

This will use the postman tool

Click Code to see the http access code, this is all the parameters behind the browser to access an address

We only need to enter the address to visit, and the rest of the parameters are added by the browser for us, here you can see what parameters are needed behind the access address

In addition to cookies, all we want to use, plus the Host address

GET /STM32/stm32esp8266/weather/123/456 HTTP/1.1
Accept-Language: zh-CN,zh;q=0.9
Host:192.168.3.8

Converting into a template is:

GET {项目路由地址} HTTP/1.1\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n
Host:{服务器ip}\r\n
\r\n换行

It should be noted that the line break is convenient for explanation. In ESP8266, it is the transfer character "\r\n" for the line break of a whole string, and a line break must be added at the end.

 

Knock on the blackboard and draw the key points

GET is implemented in STM32

After knowing how the HTTP request is written, it is necessary to assemble the parameters in STM32 and send it out with ESP8266

The string sent by STM32 to 8266 is the following writing. 8266 uses TCP to connect to the server and send this string to achieve a GET access.

"GET /STM32/stm32esp8266/weather/123/456 HTTP/1.1\r\nAccept-Language: zh-CN,zh;q=0.9\r\nHost:192.168.3.8\r\n\r\n"

 

The GET method is over

 

 

Next is the data upload method I recommend at this stage

POST+JSON access method

Because the GET method requires unlimited splicing of parameters in the URL

As a result, after adding new sensor data, not only the microcontroller has to re-burn the new code,

Even the Springboot project that receives data on the server must add new parameters to the receiving method, which is time-consuming and labor-intensive to maintain

For example, this is the case. Every time a parameter is added, the server code must be modified. It is not easy to maintain and troubleshoot.

POST+JSON is a method in which data is assembled according to JSON key-value pairs and then sent to the server as a whole. The server traverses according to the JSON key value and executes the parameter corresponding method

In this way, even if you add sensors to increase sending parameters, you only need to add them in JSON, and you don't need to modify the server code.

 Write POST access method in Springboot 's controller

The server receives a key-value pair parameter in JSON format, and uses a for loop to traverse the parameters, and then passes them to the method for processing business logic

I know the receiving method of the server, how does the microcontroller send JSON? ( This question is obviously I am so stupid, how can I ask and answer myself )

Also simulate a POST+JSON sending on the postman tool first

Use postman to send JSON

Here are the parameters of adding and subtracting JSON string at will, no need to consider one-to-one like GET method

Because the data is written into JSON uniformly, it is not necessary to receive the parameters of the MCU and the server one-to-one like the GET method

Also use postman's Cide tool to view the HTTP access code

Make a slight change

POST /STM32/stm32esp8266/weathermirror? HTTP/1.1
Host: 192.168.8.108:8085
Content-Type: application/json;charset=utf-8
Content-Type: text/plain
Content-Length:38
cache-control: no-cache
{"temperature":"123","humidity":"456"}

POST+JSON mode template

POST {项目路由地址}? HTTP/1.1\r\n
Host: {服务器ip}:{项目部署端口}\r\n
Content-Type: application/json;charset=utf-8\r\n
Content-Type: text/plain\r\n
Content-Length:{下面JSON长度包括大括号}\r\n
cache-control: no-cache\r\n
{JSON}

 

Knock on the blackboard and draw the key points

POST+JSON is implemented in STM32

Same as the get method, it is also written as a string for 8266 to send

POST {项目路由地址}? HTTP/1.1\r\nHost: {服务器ip}:{项目部署端口}\r\nContent-Type: application/json;charset=utf-8\r\nContent-Type: text/plain\r\nContent-Length:{下面JSON长度包括大括号}\r\ncache-control: no-cache\r\n{JSON}

There are a few points to note

1. The KEY and VALUE values ​​in the JSON string must be quoted with double quotes, otherwise the sending will fail and an error will be reported in the background 

如: {\"10001\":\"%d\",\"10002\":\"%d\",\"10003\":\"%d\"}

2. The content-Length: XX parameter should be added when using JSON, which means the length of the JSON string to be sent, and this length includes the curly braces of JSON

 

So far, the method of using STM32+ESP8266 to send data using HTTP protocol has been finished.

 

Really end with flowers ✿✿ヽ(°▽°)ノ✿

 

 

Finally, share my development time

Show me the code of STM32

 

This is the first time the project successfully received a POST request sent by 8266

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41873771/article/details/114012598