-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
312 lines (241 loc) · 6.82 KB
/
main.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
#include <ESP8266WiFi.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include "WiFiManager.h"
String pub_key = "pub-c-957647d4-c417-4a35-969f-95d00a04a33f";
String sub_key = "sub-c-0bbe0cb0-e2b6-11e8-a575-5ee09a206989";
double saved_threshold = 60;
// Structure for channel metadata
struct chann
{
String name;
String timestamp;
};
//struct chann user_settings;
struct chann alarm = {"alarm", "0"};
struct chann temperature = {"temperature", "0"};
struct chann user_settings = {"user_settings", "0"};
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void setup() {
Serial.begin(115200);
//WiFiManager
WiFiManager wifiManager;
pinMode(16,OUTPUT);
//reset settings - for testing
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
ESP.reset();
delay(1000);
}
Serial.println("Connected to WiFi");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
String publish_request (String channel, String msg, String signature, String callback)
{
HTTPClient http;
//publishing events
String Link = "http://pubsub.pubnub.com/publish/"; //pub-c-957647d4-c417-4a35-969f-95d00a04a33f/sub-c-0bbe0cb0-e2b6-11e8-a575-5ee09a206989/0/test/0/%22Hello%20World%22";
Link += pub_key + "/" + sub_key; // {publish_key}/{subscribe_key}
Link += "/" + signature + "/"; // {signature}
Link += channel + "/"; // {channname}
Link += callback + "/"; // {callback_function}
Link += msg; // {message}
http.begin(Link); //Specify request destination
int httpCode = http.GET(); //Send the request
String payload = http.getString(); //Get the response payload
http.end(); //Close connection
return payload;
}
String subscribe_request (chann* channel, String callback)
{
HTTPClient http;
// Subscribing to events
String Link = "http://pubsub.pubnub.com/subscribe/"; //sub-c-0bbe0cb0-e2b6-11e8-a575-5ee09a206989/threshold/0/" + timestamp;
Link += sub_key + "/";
Link += channel->name;
Link += "/" + callback + "/";
Link += channel->timestamp;
Serial.println(channel->timestamp);
http.begin(Link);
int httpCode = http.GET();
String payload = http.getString();
Serial.println(payload);
Serial.println(httpCode);
int i;
String ret = "";
Serial.println(payload);
if (httpCode == 200)
{
ret = parse_(payload);
channel->timestamp = "";
for (i=0;payload[i]!=']';i++);
i += 3;
for (; payload[i]!='"'; i++)
{
channel->timestamp += payload[i];
}
}
http.end();
Serial.println(channel->timestamp);
return ret;
}
String parse_ (String input)
{
if (input[2] == ']')
{
return "";
}
int i;
for (i=0; input[i]!=':'; i++);
i += 2;
String ret;
for (;input[i]!='"';i++)
{
ret += input[i];
}
return ret;
}
String command (chann* channel, String callback)
{
String instruction = subscribe_request(channel, callback);
Serial.println(instruction);
if (instruction == "LED ON")
{
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Instruction: LED ON"); // turn the LED off by making the voltage LOW
delay(1000);
publish_request (channel->name, urlencode("{\"message\":\"Done\"}"), "0", "0");
}
else if (instruction == "LED OFF")
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Instruction: LED OFF"); // turn the LED off by making the voltage LOW
delay(1000);
publish_request (channel->name, urlencode("{\"message\":\"Done\"}"), "0", "0");
}
else if (instruction == "ALARM OFF")
{
digitalWrite(LED_BUILTIN, HIGH);
tone(16, 0);
Serial.println("Instruction: ALARM OFF");
publish_request (channel->name, urlencode("{\"message\":\"Done\"}"), "0", "0");
}
else
{
String num = instruction;
Serial.println("User settings timestamp: " + String(user_settings.timestamp));
Serial.print("Threshold recieved: "+num);
if (num != "")
{
double n = (double)num.toInt();
if ( n > 20 )
{
saved_threshold = n;
}
}
}
return instruction;
}
void raise_alarm ()
{
digitalWrite(LED_BUILTIN,LOW);
tone(16, 500);
delay(500);
tone(16, 1000);
digitalWrite(LED_BUILTIN,HIGH);
}
double get_temp ()
{
double analogValue = analogRead(A0);
Serial.print("Sensor Value: ");
Serial.print(analogValue);
delay(500);
double analogVolts = (analogValue * 3.3)/1024;
double temp = (analogVolts - 0.5) * 100;
Serial.print(" Voltage: ");
Serial.print(analogVolts);
Serial.print(" Temperature: ");
Serial.println(temp);
return temp;
}
double set_temp ()
{
String num = subscribe_request (&user_settings, "0");
Serial.println("User settings timestamp: " + String(user_settings.timestamp));
Serial.print("Threshold recieved: "+num);
if (num == "")
{
return 0;
}
double n = (double)num.toInt();
return n;
}
void send_temp (double temp, String channel)
{
String t = String(temp);
publish_request(channel, urlencode("{\"message\":\""+t+"\"}"), "0", "0");
}
int counter = 0;
void loop ()
{
command(&user_settings, "0");
double currentTemp = get_temp();
send_temp(currentTemp, temperature.name);
Serial.println(saved_threshold);
if ( currentTemp > saved_threshold )
{
publish_request(alarm.name, urlencode("{\"message\":\"House-is-on-fire\",\"temperature\":\"" + String(currentTemp) + "\"}"), "0", "0");
while (1)
{
raise_alarm();
String cmd = command(&user_settings, "0");
if (cmd == "ALARM OFF")
{
break;
}
}
}
delay(1000); // 1 second interval between each new round
}