-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofficeServer.ino
More file actions
325 lines (274 loc) · 9.65 KB
/
officeServer.ino
File metadata and controls
325 lines (274 loc) · 9.65 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
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
/*
* This project is develped by the Student Branch of IEEE at University of Ioannina
* and demonestrates a simple solution for a smart office system using ESP8266 board
* with embedded WiFi chip. The ESP8266 is communicating via Slack API with a specific
* channel.
*
* For any queries regarding the project or bugs send an email at ieeesbuoi@gmail.com.
*/
// Include any library here.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <DHT.h>
#include <DHT_U.h>
/*****************************************************************************
* Pinout definition
*****************************************************************************/
#define DHT11_PIN 5 // Connected to GPIO pin 1 of ESP8266 (temp and hum)
#define trigPin 14 // -
#define echoPin 15 // -
#define motionPin 4 // Connected to GPIO pin 2 of ESP8266
#define magnetPin 2 // Connected to GPIO pin 4 of ESP8266
#define DHTTYPE DHT11 // Set the DHT type to DHT11
/*****************************************************************************
* RULES and THRESHOLDS
*****************************************************************************/
#define TEMP_THRESHOLD 23
#define HUM_THRESHOLD 50
/*****************************************************************************
* Credentials for the SLACK API. DO NOT CHANGE. KEEP THEM SECRET!
*****************************************************************************/
// Domainname of the slack API request
const char* host = "****";
// Second part of the slack url request
//String url = "/services/T2NUR9UG4/B81FT2HPG/4dDvXyjNA10F0PfaJcTrU4HF";
String url = "****";
// HTTPS port number for the slack API request.
const int httpsPort = "****";
// SHA1 fingerprint of the SLACK certificate
const char* fingerprint = "****";
// Credentials for the WiFi network or hotspot.
const char* ssid = "****";
const char* password = "****";
/*****************************************************************************
* GLOBAL VARIABLES and CONSTANTS
*****************************************************************************/
DHT dht(DHT11_PIN, DHTTYPE);
int maximumRange = 13; // Maximum range needed
int minimumRange = 0; // Minimum range needed
float temperature = 0.0;
float humidity = 0.0;
int MOTION_STATUS = 0;
// PIR sensor
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int prevStatus = 0; // closed
int curStatus = 0;
int DOOR = 0;
/*****************************************************************************
* Place your functions below. Before each function use comments to describe
* what it does.
*****************************************************************************/
/*
* This function updates the distance variable with the current dirstance
* that the door sensor has.
*/
void checkDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm) based on the speed of sound.
long distance = duration/58.2;
// If the distance is between the min and max range as defined
// above then update the status of the door.
if( (distance >= minimumRange) && (distance < maximumRange) ) {
// close
curStatus=0;
}else {
// open
curStatus=1;
}
}
void checkMotion() {
val = digitalRead(motionPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
MOTION_STATUS = 1;
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
MOTION_STATUS = 0;
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
/*
* It uses DHT11 sensor to gather all the information about the temperature
* and humidity sensor. Using the dht object it returns the temp and humidty
* in two different variables.
*/
void checkDHTInfo() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
}
/*
* Read a REED sensor and return the value. Then update a global variable about the
* status of the DOOR.
*/
void checkMagnet() {
int magnetValue = digitalRead(magnetPin);
if(magnetValue == 1) {
DOOR = 1;
}else if(magnetValue ==0) {
DOOR = 0;
}else {
DOOR = -1;
}
}
/*
* Create a request using the slack API to a specific channel. The function input
* is a simple string (the message to be send)
*/
void requestFunc(String myStr) {
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("Connecting to ");
Serial.println(host);
// Verify that the connection was established successfully.
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return;
}
// Verify the ESP8266 using the pre-defined fingerprint.
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
// Prepare the message as a json file.
String msgtoSend = "{\"text\": \""+myStr+"\"}";
// Prepare the reqeust headers.
String sendReq = String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + msgtoSend.length() + "\r\n"
"\r\n" + msgtoSend +
"\r\n";
// Send the request to the slack server.
client.print(sendReq);
// Read the header response from the slack server (for debuggin purposes)
while (client.connected()) {
String line = client.readStringUntil('\n');
//Serial.println(line);
if (line == "\r") {
//Serial.println("headers received");
break;
}
}
// Read the data response from the slack server (for debuggin purposes)
String line = client.readStringUntil('\n');
//Serial.println(line);
if (line.startsWith("{\"state\":\"success\"")) {
//Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
}
/*
* This function was through all of the defined rules and decides if the
* ESP8266 will send any messages to slack.
*/
void checkRules() {
// Update Values
checkDHTInfo();
checkMotion();
checkMagnet();
// Temperature is over the threshold
if(temperature > TEMP_THRESHOLD) {
String tempAlertMsg = "Temperature is over the threshold!\nTemperature: ";
requestFunc(tempAlertMsg+temperature);
}
// Humidity is over the threshold
if(humidity > HUM_THRESHOLD) {
String humAlertMsg = "Humidity is over the threshold!\nHumidity: ";
requestFunc(humAlertMsg+humidity);
}
// Humidity is over the threshold
// (ADD code that the door was left open with millis() counter )
if(DOOR == 1) {
String doorAlertMsg = "Room door was opened!";
requestFunc(doorAlertMsg);
}
}
void printStatus() {
// Get values from all sensors connected.
checkDHTInfo();
checkMotion();
checkMagnet();
// Prepare all the strings and variables (initial slack report stage)
String msg1 = "Smart office system is connected to the network...\nPreparing sensor analysis...\n";
String temp_msg = "Temperature(celcius): ";
String hum_msg = "\nHumidity(%): ";
String mot_msg = "\nMotion(0 for NO): ";
String door_msg = "\nDoor status(0 for CLOSE): ";
String end_msg = "\nUp and ready...";
String totalMsg = msg1+temp_msg+(int)temperature+hum_msg+(int)humidity+mot_msg+MOTION_STATUS+door_msg+DOOR+end_msg;
requestFunc(totalMsg);
delay(2000);
}
/*
* Setup function will be executed everytime the ESP8266 board connects
* to a power or reboots it self.
*/
void setup() {
Serial.begin(115200);
Serial.println();
dht.begin();
// Specify INPUT/OUTPUT mode for each pin you use.
pinMode(motionPin, INPUT);
pinMode(magnetPin, INPUT);
// Connect to the WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connection was successfull!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Wait 5 sec for any hardware changes that you need to make (usually connect jumper wires).
// All the TX/RX pins in the ESP8266 must NOT be connected before this step.
Serial.println("I am gonna sleep now for 5 sec...");
delay(5000);
// Get initial values from all sensors connected.
checkDHTInfo();
checkMotion();
checkMagnet();
// Prepare all the strings and variables (initial slack report stage)
String msg1 = "Smart office system is connected to the network...\nPreparing sensor analysis...\n";
String temp_msg = "Temperature(celcius): ";
String hum_msg = "\nHumidity(%): ";
String mot_msg = "\nMotion(0 for NO): ";
String door_msg = "\nDoor status(0 for CLOSE): ";
String end_msg = "\nUp and ready...";
String totalMsg = msg1+temp_msg+(int)temperature+hum_msg+(int)humidity+mot_msg+MOTION_STATUS+door_msg+DOOR+end_msg;
requestFunc(totalMsg);
delay(2000);
}
/*
* Loop function is like a while loop that is always true.
*/
void loop() {
// This function was through all of the defined rules and decides if the
// ESP8266 will send any messages to slack.
checkRules();
// Debuggin Purposes
//printStatus();
// Delay 1/2 sec before the next sensors read
delay(500);
}