-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakeUofTConnectionsFinal.ino
404 lines (351 loc) · 8.87 KB
/
MakeUofTConnectionsFinal.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
const int buzzerPin = 13;
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
Servo servo;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET LED_BUILTIN // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
uint32_t chipId = ESP.getChipId();
boolean serialPortDebug = true;
typedef struct {
char* ssid;
char* password;
} wifiCreds_t;
const char* ssid = "OnePlus 6T";
const char* password = "HELLO1234";
const char* mqttServer = "mr2j0vvhki1l0v.messaging.solace.cloud";
const int mqttPort = 20358;
const char* mqttUser = "solace-cloud-client";
const char* mqttPassword = "h9opao4l88b69t3nvqo2svv0b0";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMqttReconnectAttempt = 0;
unsigned long loopCounter = 0;
void setup() {
randomSeed(analogRead(0));
Serial.begin(115200);
Serial.println("Booting");
servo.attach(0);
servo.write(0);
pinMode(2, OUTPUT); //red
pinMode(14,OUTPUT); //blue
pinMode(12,OUTPUT);// green
pinMode(15,OUTPUT); // motor
pinMode(buzzerPin, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
///
///Setup Pins
///
Serial.println("Connecting to wifi");
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect (true);
WiFi.setAutoReconnect (true);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Connected to Wifi");
Serial.println(mqttServer);
Serial.println(mqttPort);
Serial.println(mqttUser);
Serial.println(mqttPassword);
client.setServer(mqttServer, mqttPort);
}
void testWrite(String toSend, int delaY) {
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println((toSend));
display.display();
delay(delaY);
}
void drive(int delaY){
digitalWrite(15,HIGH);
delay(delaY);
digitalWrite(15,LOW);
}
void turnOnFan(int delaY){
testWrite("Fan Turned On",1000);
drive(delaY);
delay(50);
}
void openDoor(){
testWrite("Open Door",1000);
servo.write(90);
delay(50);
}
void closeDoor(){
testWrite("Closing Door",1000);
servo.write(0);
delay(50);
}
void lightDisplay(){
testWrite("Light Show", 1000);
for(int i = 0; i < 9; i++){
multiLED(i);
delay(1000);
}
}
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(buzzerPin, note);
delay(duration);
noTone(buzzerPin);
delay(50);
}
void firstSection()
{
testWrite("Playing Starwars", 1000);
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
}
void multiLED(int number){
if(number < 1){
analogWrite(2,255);
analogWrite(14,0);
analogWrite(12,0);
}
else if(number == 2){
analogWrite(2,255);
analogWrite(14,208);
analogWrite(12,96);
}
else if(number == 3){
analogWrite(2,160);
analogWrite(14,255);
analogWrite(12,32);
}
else if(number == 4){
analogWrite(2,80);
analogWrite(14,255);
analogWrite(12,208);
}
else if(number == 5){
analogWrite(2,0);
analogWrite(14,255);
analogWrite(12,32);
}
else if(number == 6){
analogWrite(2,96);
analogWrite(14,128);
analogWrite(12,255);
}
else if (number == 7)
{
analogWrite(2,0);
analogWrite(14,0);
analogWrite(12,192);
}
else if (number == 8)
{
analogWrite(2,255);
analogWrite(14,32);
analogWrite(12,224);
}
else
{
analogWrite(2,255);
analogWrite(14,16);
analogWrite(12,160);
}
}
void alarmMode(){
testWrite("ALARM ACTIVATED", 1000);
for(int i = 0; i < 15; i++)
{
if(i % 2 == 0){
multiLED(1);
beep(125,200);
}
else{
multiLED(5);
beep(250,200);
}
delay(200);
}
}
void partyMode(){
testWrite("PARTY MODE",1000);
for(int j = 0; j < 5; j++){
testWrite("MODE PARTY",100);
servo.write(90);
delay(50);
drive(350);
delay(50);
servo.write(90);
testWrite("PARTY MODE",100);
for(int i = 0; i < 9; i++){
multiLED(i);
delay(100);
}
}
}
void testTransistion(void) {
display.clearDisplay();
for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) {
display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE);
display.drawCircle(display.width()/2, display.height()/2, i+2, SSD1306_WHITE);
display.drawCircle(display.width()/2, display.height()/2, i+4, SSD1306_WHITE);
display.display();
delay(1);
display.clearDisplay();
}
}
boolean mqttReconnect() {
char mqttClientId[40];
sprintf(mqttClientId, "ESP8266Client%lu", chipId);
Serial.println(mqttClientId);
Serial.println("Connecting to MQTT...");
if (client.connect(mqttClientId, mqttUser, mqttPassword, NULL, NULL, NULL, NULL, false )) {
Serial.println("connected");
client.setCallback(callback);
client.subscribe("light");
client.subscribe("fan");
client.subscribe("music");
client.subscribe("openDoor");
client.subscribe("closeDoor");
client.subscribe("alarm");
client.subscribe("party");
} else {
Serial.println(" not connected");
}
return client.connected();
}
//MQTT Callback
void callback(char* topic, byte* payload, unsigned int length) {
// Make a copy of the payload
byte message[length + 10];
memcpy(message, payload, length);
message[length] = '\0';
// Make a copy of the topic
char t[sizeof(topic) * 4];
strncpy(t, topic, sizeof(topic) * 4);
String topicString(t);
if (topicString.equals("light")) {
lightDisplay();
}
else if (topicString.equals("fan")) {
turnOnFan(1000);
}
else if (topicString.equals("music")) {
firstSection();
}
else if (topicString.equals("openDoor")) {
openDoor();
}
else if (topicString.equals("closeDoor")) {
closeDoor();
}
else if (topicString.equals("alarm")) {
alarmMode();
}
else if (topicString.equals("party")) {
partyMode();
}
else {
}
}
boolean mqtConnected = false;
// the loop function runs over and over again forever
void loop() {
if (!mqtConnected) {
mqttReconnect();
mqtConnected = true;
}
client.loop();
if ((loopCounter % 100000 == 0) && !client.connected()) {
long now = millis();
if (now - lastMqttReconnectAttempt > 5000) {
lastMqttReconnectAttempt = now;
// Attempt to reconnect
if (mqttReconnect()) {
lastMqttReconnectAttempt = 0;
}
}
}
if (loopCounter >= 1000) {
loopCounter = 0;
}
}