-
Notifications
You must be signed in to change notification settings - Fork 0
/
scruffy.ino
236 lines (205 loc) · 7.5 KB
/
scruffy.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
/**
* Based on Arduino Real-Time Slack Bot, Copyright (C) 2016, Uri Shaked.
* Licensed under the MIT License
*/
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WebSocketsClient.h>
#include <WiFi.h>
#include "config.h"
#define USE_SERIAL Serial
#define REED_SWITCH_PIN 27
#define REED_SWITCH_WAIT_TIME 300000
#define RELAY_PIN 12
#define RELAY_WAIT 1000
#define SLACK_API_SSL_CERT \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \
"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMT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zp30d8tbQk\n" \
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \
"-----END CERTIFICATE-----\n"
#define SLACK_API_SSL_CERT_FINGERPRINT "C1 0D 53 49 D2 3E E5 2B A2 61 D5 9E 6F 99 0D 3D FD 8B B2 B3"
#define NOPE_RESPONSES 4
WebSocketsClient webSocket;
String nope_responses[NOPE_RESPONSES] = {"https://i.imgur.com/JdiC5zK.gif", "https://i.gifer.com/7GTC.gif",
"https://i.imgur.com/Wt4gkK0.gif", "https://media.giphy.com/media/PAO4KoQ532CRi/giphy.gif"};
long nextCmdId = 1;
bool connected = false;
unsigned long lastPing = 0;
int doorState;
int previousDoorState;
unsigned long doorOpenDuration = 0;
void openDoors() {
USE_SERIAL.printf("Opening doors...");
digitalWrite(RELAY_PIN, HIGH);
delay(RELAY_WAIT);
USE_SERIAL.printf("done\n");
digitalWrite(RELAY_PIN, LOW);
}
void sendPing() {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = "ping";
root["id"] = nextCmdId++;
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void sendOpenDoorMessage() {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = "message";
root["id"] = nextCmdId++;
root["channel"] = SLACK_API_CHANNEL;
root["text"] = "<!channel> the door is open for more than 5min!";
String json;
root.printTo(json);
USE_SERIAL.printf("[WebSocker] Sending response: %s\n", json.c_str());
webSocket.sendTXT(json);
}
void processSlackMessage(char *payload) {
boolean respond = true;
String json;
DynamicJsonBuffer jsonBuffer;
DynamicJsonBuffer jsonRespBuffer;
long rand;
JsonObject &root = jsonBuffer.parse(payload);
if (!root.success()) {
USE_SERIAL.println("Parsing failed!");
return;
}
if (!root.containsKey("type")) {
return;
}
if (root["type"] != "message") {
return;
}
JsonObject &resp = jsonRespBuffer.createObject();
resp["type"] = "message";
resp["id"] = nextCmdId++;
resp["channel"] = root["channel"];
respond = true;
if (root["channel"] == SLACK_API_CHANNEL) {
String message = root["text"];
message.toLowerCase();
if (message.indexOf("open") > -1) {
openDoors();
resp["text"] = "Scruffy's work here is done.";
} else {
respond = false;
}
} else {
if (root.containsKey("subtype") && root["subtype"] == F("bot_message")) {
respond = false;
} else {
rand = random(NOPE_RESPONSES);
resp["text"] = nope_responses[rand];
}
}
if (respond) {
resp.printTo(json);
USE_SERIAL.printf("[WebSocker] Sending response: %s\n", json.c_str());
webSocket.sendTXT(json);
}
}
void webSocketEvent(WStype_t type, uint8_t *payload, size_t len) {
switch (type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WebSocket] Disconnected :-( \n");
connected = false;
break;
case WStype_CONNECTED:
USE_SERIAL.printf("[WebSocket] Connected to: %s\n", payload);
sendPing();
break;
case WStype_TEXT:
USE_SERIAL.printf("[WebSocket] Message: %s\n", payload);
processSlackMessage((char *)payload);
break;
}
}
bool connectToSlack() {
// Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.connect)
HTTPClient http;
http.begin("https://slack.com/api/rtm.connect?no_latest=1&token=" SLACK_API_TOKEN, SLACK_API_SSL_CERT);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
USE_SERIAL.printf("HTTP GET failed with code %d\n", httpCode);
return false;
}
WiFiClient *client = http.getStreamPtr();
client->find("wss:\\/\\/");
String host = client->readStringUntil('\\');
String path = client->readStringUntil('"');
path.replace("\\/", "/");
// Step 2: Open WebSocket connection and register event handler
USE_SERIAL.println("WebSocket Host=" + host + " Path=" + path);
webSocket.beginSSL(host, 443, path, SLACK_API_SSL_CERT_FINGERPRINT, "");
webSocket.onEvent(webSocketEvent);
return true;
}
void setup() {
USE_SERIAL.begin(115200);
USE_SERIAL.setDebugOutput(true);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
pinMode(REED_SWITCH_PIN, INPUT);
digitalWrite(REED_SWITCH_PIN, LOW);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
USE_SERIAL.println("Connecting to WiFi...");
}
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
}
void loop() {
webSocket.loop();
doorState = digitalRead(REED_SWITCH_PIN);
if (doorState != previousDoorState) {
if (doorState != HIGH) {
doorOpenDuration = millis();
}
delay(50);
}
previousDoorState = doorState;
if (doorState != HIGH && millis() - doorOpenDuration > REED_SWITCH_WAIT_TIME) {
sendOpenDoorMessage();
doorOpenDuration = millis();
}
if (connected) {
// Send ping every 5 seconds, to keep the connection alive
if (millis() - lastPing > 5000) {
sendPing();
lastPing = millis();
}
} else {
// Try to connect / reconnect to slack
connected = connectToSlack();
if (!connected) {
delay(500);
}
}
if (WiFi.status() != WL_CONNECTED) {
USE_SERIAL.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
}