-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFi.ino
70 lines (54 loc) · 1.34 KB
/
WiFi.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
#include <WebServer.h>
#include <WiFi.h>
#include "esp_wpa2.h"
#include <ArduinoJson.h>
#include "include/config.h"
WebServer server(80);
int a0Val = 0;
int a0Min = 5000;
int a0Max = 0;
void handleNotFound() {
server.send(404, "text/plain", "404! ADDRESS NOT FOUND");
}
void handleData() {
StaticJsonDocument<96> resJson;
JsonObject data = resJson.createNestedObject("data");
JsonObject A0 = data.createNestedObject("A0");
A0["value"] = a0Val;
A0["min"] = a0Min;
A0["max"] = a0Max;
String resTxt = "";
serializeJson(resJson, resTxt);
server.send(200, "application/json", resTxt);
}
void setup() {
Serial.begin(9600);
while (!Serial) {}
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, SSID_PASSWORD);
int tryConnectCount = 0;
while (WiFi.status() != WL_CONNECTED && tryConnectCount < 60) {
delay(500);
Serial.print(".");
tryConnectCount++;
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) {
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi is NOT connected! 💩");
}
server.enableCORS();
server.on("/data", handleData);
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
a0Val = analogRead(A0);
a0Min = min(a0Min, a0Val);
a0Max = max(a0Max, a0Val);
server.handleClient();
delay(2);
}