-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionAPI.ino
202 lines (186 loc) · 6.99 KB
/
functionAPI.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
void handleRoot() {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"),
F("Welcome to the Parking System"));
});
server.on(F("/api/set/network/station"), HTTP_POST, setNetworkStation);
server.on(F("/api/set/distance-value"), HTTP_POST, setDistanceValue);
server.on(F("/api/get/sensor/info"), HTTP_GET, getSensorInfo);
server.on(F("/api/get/network/info"), HTTP_GET, getNetworkInfo);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
handlePost("Failed", message, 404);
}
void handlePost(const String statusMessage, const String message, const int statusCode) {
StaticJsonDocument<200> doc;
doc["status"] = statusMessage;
doc["message"] = message;
String data_message;
serializeJson(doc, data_message);
server.send(statusCode, "application/json", data_message);
}
void handlePostErorr(const String message, const String dataLabel, const String dataInput) {
StaticJsonDocument<200> doc;
doc["status"] = "Failed";
doc["message"] = message;
doc["data"][dataLabel] = dataInput;
String data_message;
serializeJson(doc, data_message);
server.send(400, "application/json", data_message);
}
bool isNumeric(const String& str) {
for (size_t i = 0; i < str.length(); i++) {
if (!isdigit(str.charAt(i))) {
return false;
}
}
return true;
}
void setNetworkStation() {
String data_string = server.arg("plain");
StaticJsonDocument<400> jDoc;
DeserializationError err = deserializeJson(jDoc, data_string);
String ssid_input = jDoc["ssid"];
String password_input = jDoc["password"];
flash.writeFile("/ssid_sta.txt", ssid_input.c_str());
flash.writeFile("/pass_sta.txt", password_input.c_str());
ssid = ssid_input;
password = password_input;
Serial.println("--- Request POST Station Network ---");
Serial.println("Input SSID : " + ssid_input);
Serial.println("Password SSID : " + password_input);
Serial.println("------------------------------------");
Serial.println("");
trigger = 1;
StaticJsonDocument<200> doc;
doc["status"] = "Success";
doc["message"] = "Setting Station Network Successfully";
doc["data"]["ssid"] = ssid;
doc["data"]["password"] = password;
String data_message;
serializeJson(doc, data_message);
server.send(200, "application/json", data_message);
}
void setDistanceValue() {
Serial.println("--- Request POST Set Pin AC ---");
String data_string = server.arg("plain");
StaticJsonDocument<400> jDoc;
DeserializationError err = deserializeJson(jDoc, data_string);
String valueDistance[2] = { jDoc["minDistance"], jDoc["maxDistance"]};
String errors[2];
Serial.println("Minimum Distance: " + valueDistance[0]);
Serial.println("Maximum Distance: " + valueDistance[1]);
Serial.println("---------------------------");
Serial.println("");
for (int i = 0; i < 2; i++) {
if (valueDistance[i] != "") {
if (!isNumeric(valueDistance[i])) {
errors[i] = "variabel must be a number";
handlePostErorr("Invalid input variabel must be a number", "Value" + String(i + 1), valueDistance[i]);
Serial.println("Invalid input variabel must be a number");
return;
}
int paramValue = valueDistance[i].toInt();
// if (paramValue <= 0) {
// errors[i] = "paramValue AC must be greater than 0";
// handlePostErorr("Invalid input paramValue AC must be greater than 0", "Value" + String(i + 1), valueDistance[i]);
// Serial.println("Invalid input paramValue AC must be greater than 0");
// return;
// }
// for (int j = 0; j < 4; j++) {
// if (i != j && paramValue == valueDistance[j].toInt()) {
// errors[i] = "Each AC paramValue must have a different number";
// handlePostErorr("Invalid input Each AC paramValue must have a different number", "ac" + String(i + 1), valueDistance[i]);
// Serial.println("Invalid input Each AC paramValue must have a different number");
// return;
// }
// }
flash.writeFile(("/value" + String(i + 1) + ".txt").c_str(), valueDistance[i].c_str());
switch (i) {
case 0: minDistance = paramValue; break;
case 1: maxDistance = paramValue; break;
}
}
}
StaticJsonDocument<200> doc;
doc["status"] = "Success";
doc["message"] = "Setting AC Pin Successfully";
doc["data"]["minimum_distance"] = minDistance;
doc["data"]["maximum_distance"] = maxDistance;
String data_message;
serializeJson(doc, data_message);
server.send(200, "application/json", data_message);
}
void getNetworkInfo() {
WiFiState();
StaticJsonDocument<400> doc;
String modewifi;
if (dual_wifi == 1) {
modewifi = "AP Mode";
} else {
modewifi = "Dual Mode";
}
doc["status"] = "Success";
doc["message"] = "Get Network Station Succesfully";
doc["data"]["mode"] = modewifi;
doc["data"]["ap"]["mac"] = mac;
doc["data"]["ap"]["ip"] = ip_ap;
doc["data"]["ap"]["ssid"] = ssid_ap;
doc["data"]["ap"]["password"] = password_ap;
doc["data"]["station"]["mac"] = mac_station;
doc["data"]["station"]["ip"] = ip;
doc["data"]["station"]["ssid"] = ssid;
doc["data"]["station"]["password"] = password;
doc["data"]["station"]["rssi"] = wifi_rssi;
doc["data"]["station"]["state"] = status_wifi;
String data_string;
serializeJson(doc, data_string);
Serial.println("--- Request GET Network ---");
Serial.println("Mode WiFi : " + modewifi);
Serial.println("MAC Address AP : " + mac);
Serial.println("MAC Address Station : " + mac_station);
Serial.println("SSID AP : " + ssid_ap);
Serial.println("IP Address AP : " + ip_ap);
Serial.println("SSID Station : " + ssid);
Serial.println("IP Address Station : " + ip);
Serial.println("RSSI Station : " + wifi_rssi);
Serial.println("---------------------------");
Serial.println("");
server.send(200, "text/json", data_string);
}
void getSensorInfo() {
readSensor();
if (jarak >= minDistance && jarak <= maxDistance) {
statusPark1 = 1;
} else {
statusPark1 = 0;
}
StaticJsonDocument<400> doc;
doc["device"] = "Parking1";
doc["status"] = "Success";
doc["message"] = "Get Distance Succesfully";
doc["data"]["distance"] = jarak;
doc["data"]["minimum_distance"] = minDistance;
doc["data"]["maximum_distance"] = maxDistance;
doc["data"]["status_parking"] = statusPark1;
String data_string;
serializeJson(doc, data_string);
Serial.println("--- Request GET Distance Information ---");
Serial.println("Device : Parking1");
Serial.println("Jarak : " + String(jarak));
Serial.println("Status Parking : " + String(statusPark1));
Serial.println("---------------------------");
Serial.println("");
server.send(200, "text/json", data_string);
}