/

arduino-read-values-http

#從Arduino通過HTTP讀取數值

在本教程中,我想擴展Arduino Web Server教程以讀取由傳感器測量的數值,這樣我們只需打開瀏覽器上的一個頁面就可以看到數據。

例如,我們將使用DHT11傳感器測量溫度,並使用接近傳感器測量對象距離。

我們通過訪問/on URL點亮Arduino上的內置LED,並通過打開/off URL關閉它。其他任何操作都不起作用。

這是來自其他教程的代碼:

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
#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
char ssid[] = SECRET\_SSID;
char pass[] = SECRET\_PASS;

Serial.begin(9600);
while (!Serial);

int status = WL\_IDLE\_STATUS;
while (status != WL\_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}

Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
}

void loop() {
WiFiClient client = server.available();
if (client) {
String line = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c != '\n' && c != '\r') {
line += c;
}

if (c == '\n') {
if (line.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("test");
client.println("</html>");
break;
} else {
line = "";
}
}
}
}

client.stop();
}
}

在最後的else語句中,我們有一個完整的行,因此我們可以在清理之前檢查其內容。在這種情況下,我們可以檢查是否為GET /onGET /off

1
2
3
4
5
6
if (line.startsWith("GET /on ")){
digitalWrite(LED\_BUILTIN, HIGH);
}
if (line.startsWith("GET /off ")) {
digitalWrite(LED\_BUILTIN, LOW);
}

就這樣!現在將代碼加載到Arduino並調用/on URL或/off URL。

我在本地網絡路由器上為Arduino保留了一個靜態IP,並將其命名為arduino.local,在我的/etc/hosts文件中,所以訪問http://arduino.local/on打開LED,http://arduino.local/off關閉LED。

以下是完整的程序:

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
#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
char ssid[] = SECRET\_SSID;
char pass[] = SECRET\_PASS;

Serial.begin(9600);
while (!Serial);

int status = WL\_IDLE\_STATUS;
while (status != WL\_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}

Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
}

void loop() {
WiFiClient client = server.available();
if (client) {
String line = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c != '\n' && c != '\r') {
line += c;
}

if (c == '\n') {
if (line.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("test");
client.println("</html>");
break;
} else {
if (line.startsWith("GET /on ")){
digitalWrite(LED\_BUILTIN, HIGH);
}
if (line.startsWith("GET /off ")) {
digitalWrite(LED\_BUILTIN, LOW);
}

line = "";
}
}
}
}

client.stop();
}
}

tags: [“Arduino”, “HTTP”, “sensor”, “DHT11”, “proximity sensor”, “LED”]