-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCU-Codes.cpp
More file actions
63 lines (52 loc) · 1.27 KB
/
Copy pathMCU-Codes.cpp
File metadata and controls
63 lines (52 loc) · 1.27 KB
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
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Deneyap_Servo.h>
const char* ssid = "";
const char* password = "";
const char* serverUrl = "";
Servo servo1, servo2, servo3, servo4;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
servo1.attach(D15);
servo2.attach(D14, 1);
servo3.attach(D13, 2);
servo4.attach(D12, 3);
servo1.write(85);
servo2.write(90);
servo3.write(90);
servo4.write(95);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
Serial.println(payload);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
int a1 = doc["angles"][0];
int a2 = doc["angles"][1];
int a3 = doc["angles"][2];
int a4 = doc["angles"][3];
servo1.write(a1);
servo2.write(a2);
servo3.write(a3);
servo4.write(a4);
}
} else {
Serial.println("Failed to connect to server.");
}
http.end();
}
delay(100);
}