Click here to read Part 1 of this article >
In DIY Smart Home Security System Part 1 we put together various components, such as PIR sensor, temperature sensor, and pressure sensor to build a home security system/detector. In Part 2, we’ll test an offline program for the system we built earlier and also connect to the Internet to allow wireless control using Cayenne API. With myDevices Cayenne you’ll be able to wirelessly control the security system wherever and whenever you have access to WiFi through the website and/or on your smartphone via Cayenne App.
From Part 1:
With the hardware installation done, we can now run the offline program test. This is “offline” because the system is not connected to the Internet yet. We’ll do that in just a moment. The offline program will perform the following functions listed:
Notes:
To turn off the alarm, the user can simply press again the channel D on the Remote transmitter. Green LED will be turned off and buzzer will beep twice.
To see the value(s) of temperature and barometric pressure, connect USB cable from Arduino to PC. Then, go to Arduino IDE and click Tools → Serial monitor.
Now, let’s upload the program. Before compiling it, please make sure that all the required libraries are already installed.
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 |
//**************** Home security Program - offline ****************************** #define PIR_MOTION_SENSOR 2 // Use pin 2 to receive the signal from the module #define LED1 4 // Blue Led for motion detected #define LED2 6 // Green Led for triggered alarm #define LED3 8 // Red Led if motion and trigger switch are on #define buzzer 5 // Buzzer #define remote 41 // Remote Control #include <Wire.h> #include <BM1383GLV.h> #include <BD1020.h> int alarm = 0; int trigger = 0; int remote_sw = 0; int online_sw = 0; int previousState = -1; int currentState = -1; int prev_remote_stat = 0; int curr_remote_stat = 0; int currentValue = 0; int prev_online_stat = 0; int curr_online_stat = 0; int tempout_pin = A2; BM1383GLV bm1383; BD1020 bd1020; unsigned long previousMillis = 0; void setup() { Serial.begin(9600); while (!Serial); bd1020.init(tempout_pin); byte rc; while (!Serial); Wire.begin(); rc = bm1383.init(); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT); pinMode(LED3,OUTPUT); pinMode(buzzer, OUTPUT); pinMode(remote, INPUT); pinMode(PIR_MOTION_SENSOR, INPUT); } // ********************* Start Loop ***************************************** void loop() { checkSensor(); remote_sw = digitalRead(remote); Serial.print("Remote Status : "); Serial.println(remote_sw); Serial.println(); //*********************** read barometric pressure ************************ byte rc; float press; rc = bm1383.get_val(&press); if (rc == 0) { Serial.write("BM1383GLV (PRESS) = "); Serial.print(press); Serial.println(" [hPa]"); Serial.println(); } //********************** read Temperature ******************************** float temp; bd1020.get_val(&temp); temp = temp - 5; // Temperature adjustment due to heat from circuit board Serial.print("BD1020HFV Temp="); Serial.print(temp); Serial.print(" [degrees Celsius], ADC="); Serial.println(bd1020.temp_adc); Serial.println(); // ********** Check if Remote switch or online/App switch is on ********** if(remote_sw == 1 | currentValue == 1) { digitalWrite(LED2,HIGH); alarm = 1; curr_remote_stat = 1; if(curr_remote_stat != prev_remote_stat) { triggerBuzzer(2,70,30); prev_remote_stat = curr_remote_stat; } } else { curr_remote_stat = 0; if(curr_remote_stat != prev_remote_stat) { triggerBuzzer(3,70,30); prev_remote_stat = curr_remote_stat; } digitalWrite(LED2,LOW); alarm = 0; } // ********************** If motion detected *********************** if(isPeopleDetected()) //if it detects the moving people? { digitalWrite(LED1, HIGH); // Turn on Blue Led trigger = 1; delay(10); } else { digitalWrite(LED1, LOW); trigger = 0; delay(2000); } // ***************If Alarm is triggerred ************************** if (alarm == 1 && trigger == 1 ) { digitalWrite(LED3,HIGH); delay(500); triggerBuzzer(6,100,100); Serial.println("Alarm triggered"); } else { alarm = 0; trigger = 0; digitalWrite(LED3,LOW); } // *********************** If temperature is triggerred ********** if (temp > 45.00) { digitalWrite(LED3,HIGH); delay(500); triggerBuzzer(10,100,10); Serial.println("Alarm triggered"); } delay(10); } // ************************** End Loop ***************************** // Function: Write to PIR sensor indicator in Web/App *************** void checkSensor() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= 250) { currentState = digitalRead(PIR_MOTION_SENSOR); if (currentState != previousState) { previousState = currentState; } previousMillis = currentMillis; } } //*************************************************************** // Function: Detect whether anyone moves in it's detecting range boolean isPeopleDetected() { int sensorValue = digitalRead(PIR_MOTION_SENSOR); if(sensorValue == HIGH) //if the sensor value is HIGH? { Serial.println("PIR detect motion"); return true; //yes,return true } else { Serial.println("no motion"); return false; //no,return false } } //*************************************************************** // Function : activate buzzer based on parameters received void triggerBuzzer(int iteration,int delay1,int delay2) { for (int i = 1; i < iteration; i++) { digitalWrite(buzzer, HIGH); delay(delay1); digitalWrite(buzzer, LOW); delay(delay2); } } //***************************************************************** |
Let’s create a myDevices account! You can open a new account by signing up via the following link: https://mydevices.com/cayenne/signup/
If you already have an account, please feel free to use your own.
Figure 1. Sign up page – https://mydevices.com/cayenne/signup/
Next, choose Arduino as the device for the project.
Figure 2. Choosing Arduino device for the project
Read the documentation and install Cayenne library from the following website:
https://mydevices.com/cayenne/docs/#using-cayenne-library
Then continue to next step to connect to Arduino in a new web page. Choose Arduino Mega and WiFi Shield. When you select the two, you’ll receive “Authentication Token” for your device. Make sure you copy this token and save it somewhere. It will be used in our next program.
char token[] = “zzzzzzz”; // Cayenne authentication token
Figure 3. Choosing Arduino Mega & WiFi Shield
Figure 4. Creating Widget for Sensors
We can create widgets for PIR sensor, Trigger status and Alarm on/off status.
To do this, we have to click on “Add new…” then “Device/Widget”. We’ll then go to “Sensors” category and click “Generic”. Under Generic, there are 2 input options: Analog Input and Digital Input. We’ll choose “Digital Input”. Once you’re on Digital Input Setting page, set Connectivity to “Virtual” and Pin “V1” (you’ll have to scroll down a little). Don’t forget to click Step 1 and Step 2.
We’ll follow the same steps for V2 and V4 widgets. Please use the following table as a reference:
Virtual Channel No | Widget Name | Choose Widget | Choose Icon/Display | Connect to |
V1 | PIR Sensor | 0/1 2 State | Display Value | PIR Sensor – Blue Led |
V2 | Trigger Status | 0/1 2 State | Display Value | Trigger Switch – Red Led |
V4 | Alarm On/Off Status | 0/1 2 State | Icon Light | Remote Switch |
The virtual pin assignments will be used in the codes as follows:
#define VIRTUAL_PIN1 V1 // Virtual Pin for PIR sensor – Blue Led
#define VIRTUAL_PIN2 V2 // Virtual Pin for Trigger Switch – Red Led
#define VIRTUAL_PIN4 V4 // Status of Remote Switch
Next is to create a widget for Online/Application switch.
Go to “Add New…” then “Device/Widgets”. This time choose “Actuators” then “Generic”. Select “Digital Output,” and under Connectivity choose “Virtual”. Under Pin, choose “V3”.
Figure 5. Creating Widget for Online/Application Switch
Use this table as a reference:
Virtual Channel No | Widget Name | Choose Widget | Choose Icon/Display | Connect to |
V3 | Online/App Switch | Button | Icon Lock | Online/Apps Switch- Green Led |
V3 will turn on (change color to purple) when you click the widget (on the website/app). When V3 is on, green LED will also turn on. So this virtual switch does the same as the remote control transmitter. However, there is a time delay when activated through this virtual switch. Pin V3 will be referred by the following code in the next program.
#define VIRTUAL_PIN3 V3 // Online Switch – Green led
Next, we need to create widget for the temperature and pressure sensors. As before, click on “Add new…” and “Device/Widget”. Then choose “Sensors” and “Generic” followed by “Analog Input”. Under Connectivity, select “Virtual” and “V5” for Pin selection.
Figure 6. Creating Widget for Temperature and Pressure Sensors
Do the same for V6 and use the following table as reference:
Virtual Channel No | Widget Name | Choose Widget | Choose Units | Connect to |
V5 | Temperature(Celcius) | Gauge | Float- 2 Decimal | Temperature Sensor |
V6 | Barometric Pressure(hPa) | Gauge | Float -2 Decimal | Barometric Pressure Sensor |
V5 and V6 will be referred by the following code used in the next program.
#define VIRTUAL_PIN5 V5 // Temperature sensor
#define VIRTUAL_PIN6 V6 // Barometric Pressure Sensor
If all the above steps are done correctly, you will see a page as shown in Figure 7.
Figure 7. Display of the Widgets
The final step is to enable email notification (when Alarm is triggered). To do this, click on the setting symbol on Trigger status widget and select “Trigger”.
Figure 8. Creating Email trigger
You will be then directed to the following page (Figure 9). Type in your email address in “Add custom recipient” and click ”Send Email”.
Figure 9. Setting up email notification
This concludes the myDevices setup!
We’ve finished setting online connectivity with our Home Security System. Now we can test online program before running the final program.
Upload the following program. Make sure that Cayenne library for Arduino is already installed. This program will perform a simple function (i.e. displaying temperature and barometric pressure values). When run correctly, the Temperature and Barometric widgets will change color (green and orange) and values will appear at the bottom of the widgets.
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 |
//**************** Home security Program - online test*********************** * #define CAYENNE_DEBUG // Uncomment to show debug messages #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space #define VIRTUAL_PIN5 V5 // Temperature sensor #define VIRTUAL_PIN6 V6 // Barometric Pressure Sensor #include <CayenneWiFi.h> // Cayenne wifi library #include <Wire.h> #include <BM1383GLV.h> #include <BD1020.h> char token[] = "zzzzzzz"; // Cayenne authentication token. char ssid[] = "xxxxxxxxx"; // Your Wifi network name char password[] = "yyyy"; // Your Wifi password int tempout_pin = A2; BM1383GLV bm1383; BD1020 bd1020; unsigned long previousMillis = 0; void setup() { Serial.begin(9600); while (!Serial); bd1020.init(tempout_pin); byte rc; while (!Serial); Wire.begin(); rc = bm1383.init(); Cayenne.begin(token, ssid, password); } // ********************* Start Loop ***************************************** void loop() { Cayenne.run(); //*********************** read barometric pressure ************************ byte rc; float press; rc = bm1383.get_val(&press); if (rc == 0) { Cayenne.virtualWrite(VIRTUAL_PIN6, press); // Write Barometric Pressure to Cayenne Serial.write("BM1383GLV (PRESS) = "); Serial.print(press); Serial.println(" [hPa]"); Serial.println(); } //********************** read Temperature ******************************** float temp; bd1020.get_val(&temp); temp = temp - 5; // Temperature adjustment due to heat from circuit board Cayenne.virtualWrite(VIRTUAL_PIN5, temp); // Write Temperature to Cayenne Serial.print("BD1020HFV Temp="); Serial.print(temp); Serial.print(" [degrees Celsius], ADC="); Serial.println(bd1020.temp_adc); Serial.println(); // ********** Check if Remote switch or online/App switch is on ********** delay(5000); } // ************************** End Loop ***************************** |
We are almost done!
If all the above programs run perfectly, now we can upload this final program that combines the offline program plus the connectivity with Cayenne API.
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
//**************** Home security Program - Final ****************************** #define CAYENNE_DEBUG // Uncomment to show debug messages #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space #define VIRTUAL_PIN1 V1 // Virtual Pin for PIR sensor - Blue Led #define VIRTUAL_PIN2 V2 // Virtual Pin for Trigger Switch - Red Led #define VIRTUAL_PIN3 V3 // Online Switch - Green led #define VIRTUAL_PIN4 V4 // Status of Remote Switch #define VIRTUAL_PIN5 V5 // Temperature sensor #define VIRTUAL_PIN6 V6 // Barometric Pressure Sensor #define PIR_MOTION_SENSOR 2 // Use pin 2 to receive the signal from the module #define LED1 4 // Blue Led for motion detected #define LED2 6 // Green Led for triggered alarm #define LED3 8 // Red Led if motion and triger switch are on #define buzzer 5 // Buzzer #define remote 41 // Remote Control #include <CayenneWiFi.h> // Cayenne wifi library #include <Wire.h> #include <BM1383GLV.h> #include <BD1020.h> char token[] = "zzzzzzz"; // Cayenne authentication token. char ssid[] = "xxxxxxxxx"; // Your Wifi network name char password[] = "yyyy"; // Your Wifi password int alarm = 0; int trigger = 0; int remote_sw = 0; int online_sw = 0; int previousState = -1; int currentState = -1; int prev_remote_stat = 0; int curr_remote_stat = 0; int currentValue = 0; int prev_online_stat = 0; int curr_online_stat = 0; int tempout_pin = A2; BM1383GLV bm1383; BD1020 bd1020; unsigned long previousMillis = 0; void setup() { Serial.begin(9600); while (!Serial); bd1020.init(tempout_pin); byte rc; while (!Serial); Wire.begin(); rc = bm1383.init(); Cayenne.begin(token, ssid, password); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT); pinMode(LED3,OUTPUT); pinMode(buzzer, OUTPUT); pinMode(remote, INPUT); pinMode(PIR_MOTION_SENSOR, INPUT); } // ************ Check if Online/App Switch is on ***************************** CAYENNE_IN(VIRTUAL_PIN3) {currentValue = getValue.asInt();} // ********************* Start Loop ***************************************** void loop() { Cayenne.run(); Serial.print("currentValue: "); Serial.println(currentValue); checkSensor(); remote_sw = digitalRead(remote); Serial.print("Remote Status : "); Serial.println(remote_sw); Serial.println(); //*********************** read barometric pressure ************************ byte rc; float press; rc = bm1383.get_val(&press); if (rc == 0) { Cayenne.virtualWrite(VIRTUAL_PIN6, press); Serial.write("BM1383GLV (PRESS) = "); Serial.print(press); Serial.println(" [hPa]"); Serial.println(); } //********************** read Temperature ******************************** float temp; bd1020.get_val(&temp); temp = temp - 5; // Temperature adjustment due to heat from circuit board Cayenne.virtualWrite(VIRTUAL_PIN5, temp); Serial.print("BD1020HFV Temp="); Serial.print(temp); Serial.print(" [degrees Celsius], ADC="); Serial.println(bd1020.temp_adc); Serial.println(); // ********** Check if Remote switch or online/App switch is on ********** if(remote_sw == 1 | currentValue == 1) { digitalWrite(LED2,HIGH); Cayenne.virtualWrite(VIRTUAL_PIN4, HIGH); alarm = 1; curr_remote_stat = 1; if(curr_remote_stat != prev_remote_stat) { triggerBuzzer(2,70,30); prev_remote_stat = curr_remote_stat; } } else { curr_remote_stat = 0; if(curr_remote_stat != prev_remote_stat) { triggerBuzzer(3,70,30); prev_remote_stat = curr_remote_stat; } digitalWrite(LED2,LOW); Cayenne.virtualWrite(VIRTUAL_PIN4, LOW); alarm = 0; } // ********************** If motion detected *********************** if(isPeopleDetected()) //if it detects the moving people? { digitalWrite(LED1, HIGH); // Turn on Blue Led trigger = 1; delay(10); } else { digitalWrite(LED1, LOW); trigger = 0; delay(2000); } // ***************If Alarm is triggerred ************************** if (alarm == 1 && trigger == 1 ) { digitalWrite(LED3,HIGH); Cayenne.virtualWrite(VIRTUAL_PIN2, HIGH); delay(500); triggerBuzzer(6,100,100); Serial.println("Alarm triggered"); } else { alarm = 0; trigger = 0; digitalWrite(LED3,LOW); Cayenne.virtualWrite(VIRTUAL_PIN2, LOW); } // *********************** If temperature is triggerred ********** if (temp > 45.00) { digitalWrite(LED3,HIGH); Cayenne.virtualWrite(VIRTUAL_PIN2, HIGH); delay(500); triggerBuzzer(10,100,10); Serial.println("Alarm triggered"); } delay(10); } // ************************** End Loop ***************************** // Function: Write to PIR sensor indicator in Web/App *************** void checkSensor() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= 250) { currentState = digitalRead(PIR_MOTION_SENSOR); if (currentState != previousState) { Cayenne.virtualWrite(VIRTUAL_PIN1, currentState); previousState = currentState; } previousMillis = currentMillis; } } //*************************************************************** // Function: Detect whether anyone moves in it's detecting range boolean isPeopleDetected() { int sensorValue = digitalRead(PIR_MOTION_SENSOR); if(sensorValue == HIGH) //if the sensor value is HIGH? { Serial.println("PIR detect motion"); return true; //yes,return true } else { Serial.println("no motion"); return false; //no,return false } } //*************************************************************** // Function : activate buzzer based on parameters received void triggerBuzzer(int iteration,int delay1,int delay2) { for (int i = 1; i < iteration; i++) { digitalWrite(buzzer, HIGH); delay(delay1); digitalWrite(buzzer, LOW); delay(delay2); } } // ***************************************************************** |
There are several ways to test if the program is running correctly.
Check if email is sent when Alarm is triggered. You should receive the similar email as shown in Figure 10.
Figure 10. Email notification received when alarm is triggered.
Check if you can see graphs of temperature and barometric pressure. To display the graphs, click on the graphic symbol at the top right corner of the widgets.
Figure 11. Showing graphical picture of sensors
Then the graph will be shown:
Figure 12. Example of temperature graph
As mentioned previously, you can control and monitor the security system 24/7 wherever and whenever you have access to WiFi. This can be done either on the website or via Cayenne mobile app. You can download Cayenne app on your smartphone. Go to App store (for iPhone) or Google Play store (for Android) and search for Cayenne then download it.
Figure 13. Cayenne App
App icon will appear on your home screen after successfully installing the app.
Click on the app icon and log into your myDevices account (email id and password should be the same as when you set up the account previously). When logged in, you will see the widgets you created. Now you can turn on/off the Alarm by clicking the Online/App switch widget; the Alarm on/off status will change color to green as shown in the following picture:
Figure 14. Cayenne App Widgets
Congratulations! Finally, the complete system has been set up! Whenever Alarm is triggered you will receive email notifications.
It was an interesting project incorporating multiple modules. In the future, we can add more sensors for additional functionalities and multiple buzzers to make the beeping louder. We can also consider sizing down the system by replacing some components. Until then, enjoy!