-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHomeIoT.ino
294 lines (257 loc) · 8.82 KB
/
MyHomeIoT.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <ESP8266WiFi.h>
#include "Credentials.h"
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <Servo.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Servo.h>
#include <WiFiClient.h>
WiFiClient client;
// Create servo objects for each servo motor
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
ESP8266WebServer server(80); // Create a web server on port 80
bool checkAuth() {
return server.authenticate(www_username, www_password);
}
void handleRoot() {
server.send(200, "text/plain", "You are logged in");
}
void sendStateToAPI(String endpoint, bool state) {
if (WiFi.status() == WL_CONNECTED) {
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
client->setInsecure(); // Ignore SSL certificate validation
HTTPClient https;
String url = "https://x8ki-letl-twmt.n7.xano.io/api:QgTMv-fR" + endpoint;
if (https.begin(*client, url)) { // HTTPS
https.addHeader("Content-Type", "application/json");
String jsonBody = "{\"State\":" + String(state ? "true" : "false") + "}";
Serial.println("url: " + url);
Serial.println("json to send: " + jsonBody);
int httpCode = https.POST(jsonBody);
if (httpCode > 0) {
String payload = https.getString();
Serial.println("http code: " + httpCode);
Serial.println("Response: " + payload);
} else {
Serial.printf("Error on HTTPS POST: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.println("HTTPS Connection Failed");
}
} else {
Serial.println("Not connected to WiFi");
}
}
void setup() {
Serial.begin(115200);
// Attach servos to their respective pins
servo0.attach(16);
servo1.attach(5);
servo2.attach(4);
servo3.attach(0);
servo4.attach(2);
servo5.attach(14);
servo6.attach(12);
servo7.attach(13);
// Connect to WiFi, print IP address, etc.
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
setupServoRoutes(); // Setup web server routes for controlling the servos
server.begin(); // Start the web server
Serial.println("HTTP server started");
server.on("/", HTTP_GET, []() {
if (!checkAuth()) return;
handleRoot();
});
}
void loop() {
server.handleClient(); // Handle incoming client requests
}
// Function to control a specific servo
// servoNumber: the number of the servo to control
// turnOn: whether to turn the servo to the 'on' position or 'off' position
void controlServo(int servoNumber, bool turnOn) {
//These values can(should) be tweaked for the servos using the other tweak.ino file in the project.
int onPosition = 20; // 'On' position for the servo
int offPosition = 160; // 'Off' position for the servo
int restPosition = 90; // Rest position for the servo
Servo* servo; // Pointer to the servo to control
switch (servoNumber) {
case 0: servo = &servo0; break;
case 1: servo = &servo1; break;
case 2: servo = &servo2; break;
case 3: servo = &servo3; break;
case 4: servo = &servo4; break;
case 5: servo = &servo5; break;
case 6: servo = &servo6; break;
case 7: servo = &servo7; break;
default: return; // Exit if an invalid servo number is given
}
servo->write(turnOn ? onPosition : offPosition); // Move servo to 'on' or 'off' position
delay(200);
servo->write(restPosition); // Return servo to rest position
}
// Function to control the lamp servos
// lampNumber: the specific lamp number
// turnOn: whether to turn the lamp on or off
void lamp(int lampNumber, bool turnOn) {
controlServo(lampNumber, turnOn);
// Sending state to API
sendStateToAPI("/lamp" + String(lampNumber), turnOn);
}
// Function to control the blinds
// blindNumber: the number of the blind (1 or 2)
// lowerBlinds: whether to lower or raise the blinds
void blinds(int blindNumber, bool lowerBlinds) {
if (blindNumber == 1) {
if (lowerBlinds) {
// Blind 1 Going Down
controlServo(5, true); // Turn off servo 5
controlServo(3, true); // Turn on servo 3
} else {
// Blind 1 Going Up
controlServo(3, false); // Turn off servo 3
controlServo(5, false); // Turn on servo 5
}
} else if (blindNumber == 2) {
if (lowerBlinds) {
// Blind 2 Going Down
controlServo(6, true); // Turn off servo 6
controlServo(4, true); // Turn on servo 4
} else {
// Blind 2 Going Up
controlServo(4, false); // Turn off servo 4
controlServo(6, false); // Turn on servo 6
}
}
// Sending state to API
String endpoint = "/blinds" + String(blindNumber);
sendStateToAPI(endpoint, lowerBlinds);
}
// Function to control the AC
void acControl() {
if (WiFi.status() == WL_CONNECTED) {
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
client->setInsecure(); // Ignore SSL certificate validation
HTTPClient https;
String urlGet = "https://x8ki-letl-twmt.n7.xano.io/api:QgTMv-fR/ac";
// GET Request
if (https.begin(*client, urlGet)) {
int httpCodeGet = https.GET();
if (httpCodeGet > 0) {
String payload = https.getString();
Serial.println("GET Response: " + payload);
// Parse JSON response and toggle state
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
bool currentState = doc["State"];
bool newState = !currentState;
controlServo(7, false); // Control AC using servo 7
https.end(); // End the GET request
// Delay before POST request
delay(500);
// POST Request
String urlPost = "https://x8ki-letl-twmt.n7.xano.io/api:QgTMv-fR/ac";
if (https.begin(*client, urlPost)) {
https.addHeader("Content-Type", "application/json");
String jsonBody = "{\"State\":" + String(newState ? "true" : "false") + "}";
int httpCodePost = https.POST(jsonBody);
if (httpCodePost > 0) {
String response = https.getString();
Serial.println("POST Response: " + response);
} else {
Serial.printf("Error on HTTPS POST: %s\n", https.errorToString(httpCodePost).c_str());
}
https.end(); // End the POST request
} else {
Serial.println("HTTPS POST Connection Failed");
}
} else {
Serial.printf("Error on HTTPS GET: %s\n", https.errorToString(httpCodeGet).c_str());
}
} else {
Serial.println("HTTPS GET Connection Failed");
}
} else {
Serial.println("Not connected to WiFi");
}
}
// Function to set up web server routes for controlling servos
void setupServoRoutes() {
// Routes for controlling all lamps
server.on("/lamps/on", []() {
if (!checkAuth()) return;
for (int i = 0; i <= 2; i++) { // For lamps 0, 1, and 2
lamp(i, true); // Turn on each lamp
}
server.send(200, "text/plain", "All lamps turned on");
});
server.on("/lamps/off", []() {
if (!checkAuth()) return;
for (int i = 0; i <= 2; i++) { // For lamps 0, 1, and 2
lamp(i, false); // Turn off each lamp
}
server.send(200, "text/plain", "All lamps turned off");
});
// Routes for controlling individual lamps
for (int i = 0; i <= 2; i++) { // Only for lamps 0, 1, and 2
server.on("/lamp" + String(i) + "/on", [i]() {
if (!checkAuth()) return;
lamp(i, true);
server.send(200, "text/plain", "Lamp " + String(i) + " turned on");
});
server.on("/lamp" + String(i) + "/off", [i]() {
if (!checkAuth()) return;
lamp(i, false);
server.send(200, "text/plain", "Lamp " + String(i) + " turned off");
});
}
// Routes for controlling blinds
server.on("/blinds1/down", []() {
if (!checkAuth()) return;
blinds(1, true);
server.send(200, "text/plain", "Blinds 1 lowered");
});
server.on("/blinds1/up", []() {
if (!checkAuth()) return;
blinds(1, false);
server.send(200, "text/plain", "Blinds 1 raised");
});
server.on("/blinds2/down", []() {
if (!checkAuth()) return;
blinds(2, true);
server.send(200, "text/plain", "Blinds 2 lowered");
});
server.on("/blinds2/up", []() {
if (!checkAuth()) return;
blinds(2, false);
server.send(200, "text/plain", "Blinds 2 raised");
});
// Route for AC control
server.on("/ac/toggle", []() {
if (!checkAuth()) return;
acControl();
server.send(200, "text/plain", "AC toggle command sent");
});
}