-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-air-monitor-transfer.ino
100 lines (81 loc) · 1.89 KB
/
arduino-air-monitor-transfer.ino
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// OLED
#include <Wire.h>
#include <SSD1306Wire.h>
#include <ESP8266HTTPClient.h>
#include "wifi.h"
#include "rx.h"
// OLED
SSD1306Wire display(0x3c, D5, D6);
void setup() {
Serial.begin(115200);
Serial.println();
// 初始化OLED
display.init();
display.flipScreenVertically();
display.setTextAlignment(TEXT_ALIGN_LEFT);
// 初始化WIFI
MyWIFI::setup(display);
// 初始化接收器
Transfer::RX.init();
}
// Last process time
long lastProcessSecond = 0;
void process() {
if (WiFi.status() == WL_CONNECTED) {
send();
} else {
MyWIFI::printFail(display);
delay(1000);
// 网络断开,直接重启
ESP.restart();
}
}
void loop() {
// process each second
if (millis() / 1000 - lastProcessSecond >= 1) {
process();
lastProcessSecond = millis() / 1000;
}
}
void send() {
// 客户端
WiFiClient client;
HTTPClient http;
// 接口信息
String host = "192.168.1.43";
// String host = "192.168.1.12";
// String host = "192.168.2.1";
// String host = "192.168.217.55";
int port = 8081;
// 接收内容
String json = Transfer::RX.read();
Serial.println();
Serial.print("JSON:");
Serial.println(json);
// 判断 display
if (json[11] == '1') {
display.displayOn();
} else {
display.displayOff();
}
// 显示 wifi 信息
MyWIFI::printWifi(display, false);
if (json.length() > 0) {
// 有数据
// 请求
String url = "/batch";
http.begin(client, "http://" + host + ":" + (String)port + url);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json);
http.end();
// 请求结果
if (httpCode == 200) {
// 打印 json 信息,自动换行
display.drawStringMaxWidth(0, 13, 128, json);
} else {
Serial.println(httpCode);
display.drawString(0, 13, "Request failed: " + (String)httpCode);
}
display.display();
}
}