DIY Arduino Home Security System using ROHM Sensor Kit Part 2 – Cayenne Setup

DIY Arduino Home Security System using ROHM Sensor Kit Part 2 – Cayenne Setup

image_01

Click here to read Part 1 of this article >

arduino home security system

 

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.

Hardware

  • Laptop/PC or smartphone

From Part 1:

Software

Test the hardware with offline program

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:

  • Remote control transmitter (in the example shown, we use channel D) to turn on/off the alarm. If it is on, Green LED will turn on and buzzer will beep once.
  • PIR sensor will detect the movement. If movement is detected Blue LED will turn on.
  • When alarm is “ON”, and movement is detected, Red LED will turn on and buzzer will go off.
  • Similarly, when alarm is “ON” (i.e. Green LED is on) and heat is above the setting target, Red LED will turn on and buzzer will go off.

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.

//**************** 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);

     }

}

//*****************************************************************

Prepare online connectivity

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.

arduino home security system

Figure 1. Sign up page – https://mydevices.com/cayenne/signup/

Next, choose Arduino as the device for the project.

arduino home security system

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

arduino home security system

Figure 3. Choosing Arduino Mega & WiFi Shield

arduino home security system

Figure 4. Creating Widget for Sensors

Create widgets

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

  • V1 will turn on (change color to green) when PIR sensor detect movement; blue LED will also be turned on.
  • V2 will turn on (change color to green) when Trigger status is on; when alarm is on and PIR sensor detect movement, red LED will also be turned on.
  • V4 will turn on (change color to green) when Alarm status is on by remote control transmitter or by Online/App 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”.

arduino home security system

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.

arduino home security system

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.

arduino home security system

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”.

arduino home security system

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”.

arduino home security system

Figure 9. Setting up email notification

This concludes the myDevices setup!

Test online program

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.

 

//**************** 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!

Final program

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.

 

//**************** 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.

arduino home security system

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.

arduino home security system

Figure 11. Showing graphical picture of sensors

Then the graph will be shown:

arduino home security system

Figure 12. Example of temperature graph

Download Smartphone App

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.

 

arduino home security system

Figure 13. Cayenne App

App icon will appear on your home screen after successfully installing the app.

arduino home security system

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:

arduino home security system

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!