ESP8266-Arduino网络编程实例-HTTP提交数据(基于Node-Red)

HTTP提交数据

本文将介绍ESP8266的HTTP POST请求:URL编码、JSON对象或纯文本。本实例的HTTP服务器仍基于Node-Red,在发起HTTP请求之前,在Node-Red中创建一个HTTP请求节点。如下所示:

在这里插入图片描述

在函数模块中,添加如下代码:

1
2
3
msg.payload = msg.payload.temperature;
return msg;

整个节点JSON代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[
{
"id": "7f5cf345.63f56c",
"type": "http response",
"z": "65febd16d6c14bb0",
"name": "",
"statusCode": "200",
"headers": {},
"x": 640,
"y": 380,
"wires": []
},
{
"id": "e71c7a7d.e7c598",
"type": "debug",
"z": "65febd16d6c14bb0",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 650,
"y": 460,
"wires": []
},
{
"id": "c7807102.3f433",
"type": "http in",
"z": "65febd16d6c14bb0",
"name": "",
"url": "/update-sensor",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 290,
"y": 420,
"wires": [
[
"60410cde.562a34"
]
]
},
{
"id": "60410cde.562a34",
"type": "function",
"z": "65febd16d6c14bb0",
"name": "",
"func": "msg.payload = msg.payload.temperature;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 490,
"y": 420,
"wires": [
[
"e71c7a7d.e7c598",
"7f5cf345.63f56c"
]
]
}
]

最后,部署流程就可以发起HTTP请求了。接着,完成ESP8266的HTTP POST请求代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char\* ssid = "\*\*\*\*"; // WIFI名称
const char\* password = "\*\*\*\*"; // WIFI密码

// HTTP请求URL
const char\* serverName = "你的Node-Red服务器地址/update-sensor";

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

void setup() {
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());

Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
// 检查WiFi状态
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;

// 启动HTTP请求
http.begin(client, serverName);

// 设置HTTP请求头
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 所要提交数据
String httpRequestData = "api\_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";
// 发起HTTP POST请求
int httpResponseCode = http.POST(httpRequestData);

// JSON数据提交
//http.addHeader("Content-Type", "application/json");
//int httpResponseCode = http.POST("{\"api\_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");

// 纯文本数据提交
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");

Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);

// 释放HTTP
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}

在示例代码中,首先导入依赖头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char\* ssid = "\*\*\*\*"; // WIFI名称
const char\* password = "\*\*\*\*"; // WIFI密码

// HTTP请求URL
const char\* serverName = "你的Node-Red服务器地址/update-sensor";

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

接着,在setup函数中初始化WiFi:

1
2
3
4
5
6
7
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

最后,在loop函数,发起HTTP的POST请求:

1
2
3
4
5
6
7
8
9
10
11
12
HTTPClient http;

// 启动HTTP请求
http.begin(client, serverName);

// 设置HTTP请求头
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 所要提交数据
String httpRequestData = "api\_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";
// 发起HTTP POST请求
int httpResponseCode = http.POST(httpRequestData);

文章来源: https://iotsmart.blog.csdn.net/article/details/127169488