maulepilot
Published © GPL3+

Arduino Opla environment and intrusion

Arduino Opla, PIR sensor and sound sensor with two way communication through Blynk to smart phone

IntermediateFull instructions provided363
Arduino Opla environment and intrusion

Things used in this project

Hardware components

Arduino Oplà IoT Kit
Arduino Oplà IoT Kit
×1
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
Keyes Microphone Sound Detection Sensor Module for Arduino
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Custom parts and enclosures

Schematic

Project schematic

Code

Arduino_MKR1010-Blynk-1

Arduino
This sketch collects environmental sensor data and security
intrusion detection data from an Arduino Opla MKR IoT device and
sends them to a smart phone using Blynk Cloud service. Switch
commands from the smart phone are sent to the Opla's relays and
LEDs to actuate external functions. The Opla is composed of an
Arduino MKR WiFi 1010 board and Arduino MKR IoT Carrier. A Seeed
Studios PIR sensor hardware and sound sensor are connected to the
Opla's Grove connector ports. Blynk Cloud service is used to send
environmental data to a smart phone and actuator commands from
the smart phone to the Opla. The Opla features used are:
graphics display, environmental sensors, multicolor LEDs,
relays and buzzer.
/*************************************************************
This sketch collects environmental sensor data and security
intrusion detection data from an Arduino Opla MKR IoT device and
sends them to a smart phone using Blynk Cloud service. Switch
commands from the smart phone are sent to the Opla's relays and
LEDs to actuate external functions. The Opla is composed of an
Arduino MKR WiFi 1010 board and Arduino MKR IoT Carrier. A Seeed
Studios PIR sensor hardware and sound sensor are connected to the
Opla's Grove connector ports. Blynk Cloud service is used to send
environmental data to a smart phone and actuator commands from
the smart phone to the Opla. The Opla features used are:
graphics display, environmental sensors, multicolor LEDs,
relays and buzzer.
*************************************************************/

/* Comment this out to disable prints */
#define BLYNK_PRINT Serial

/* Template ID for Blynk.Cloud service */
#define BLYNK_TEMPLATE_ID   "TMPLV8JgWeEN"

//Number of notes in the buzzer tune
#define NOTES 11

//Included libraries
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino_MKRIoTCarrier.h>
#include <BlynkSimpleWiFiNINA.h>
#include "config.h" //Login credentials and authorization token

// Get the Blynk authorization token from the config.h file
char auth[] = BLYNK_AUTH_TOKEN;

// Get the wifi SSID and password from the config.h file
char ssid[] = WIFI_SSID;
char pass[] = WIFI_PASS;

//Instantiate the MKR IoT carrier and Blynk timer
MKRIoTCarrier carrier;
BlynkTimer timer;

//Global variables for environment sensors and run time
float temperature,humidity,pressure,hours;

bool pirStatus = false; //Logic switch to prevent multiple PIR alarms
bool soundStatus = false; //Logic switch to prevent multiple sound alarms
uint32_t t0, t1; //Declare timestamp variables

//C++ structure for a note to the buzzer
struct Note {
  String name;
  float frequency;
  int duration;
};

//Buzzer tune array on startup from Close Encounters of the Third Kind
Note tune[NOTES] = {
  {"D4",293.66,500},
  {"E4",329.63,500},
  {"C4",261.63,500},
  {"C3",130.81,500},
  {"G3",196.00,1000},
  {"R1",0.00,500},
  {"D4",293.66,500},
  {"E4",329.63,500},
  {"C4",261.63,500},
  {"C3",130.81,500},
  {"G3",196.00,1000}  
};

//Blynk timer event that reads environmental and PIR intrusion data from Opla sensors
void myTimerEvent1()
{
  temperature = carrier.Env.readTemperature(FAHRENHEIT); //Fahrenheit temperature
  humidity = carrier.Env.readHumidity(); //% humidity
  pressure = carrier.Pressure.readPressure()*0.295301; //Pressure in inches of mercury
  hours = millis()/3600000.0; //Run time in decimal hours

  //Send the environmental data from Opla to the smart phone through Blynk virtual pins
  Blynk.virtualWrite(V1, temperature);
  Blynk.virtualWrite(V2, humidity);
  Blynk.virtualWrite(V4, pressure);
  Blynk.virtualWrite(V5, hours);

  //If PIR sensor on pin A5 is high and logic switch is low then event and status are sent to Blynk Cloud
  if (digitalRead(A5) && pirStatus == false) {
    Blynk.logEvent("pir","PIR sensor tripped");
    Blynk.virtualWrite(V3,HIGH);
    Serial.println("PIR tripped");
    pirStatus = true; //Turn on the PIR logic switch
    t0 = millis(); //Take a timestamp for how long the logic switch and virtual Blynk pin stay on
  }
  //Turn off the logic switch and Blynk virtual pin after 6 seconds
  if (millis() - t0 > 6000) {
    Blynk.virtualWrite(V3,LOW);
    pirStatus = false;
  }
}

//Blynk timer event that sends sound sensor data from Opla to the smart phone through a Blynk virtual pin 
void myTimerEvent2()
{
   //If sound sensor on pin A6 is high and logic switch is low then event and status are sent to Blynk Cloud
  if (digitalRead(A6) && soundStatus == false) {
    Blynk.logEvent("sound","Sound sensor tripped");
    Blynk.virtualWrite(V7,HIGH);
    Serial.println("Sound tripped");
    soundStatus = true; //Turn on the sound logic switch
    t1 = millis(); //Take a timestamp for how long the logic switch and virtual Blynk pin stay on
  }
  //Turn off the logic switch and Blynk virtual pin after 5 seconds
  if (millis() - t1 > 5000) {
    Blynk.virtualWrite(V7,LOW);
    soundStatus = false;
  }
}

//Blynk timer event that displays environmental data on the Opla's display
void myTimerEvent3()
{
  displayEnv();
}

//Flashes Opla LEDS blue 4 times and opens/closes Opla relay 1 on command from smart phone
BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigns incoming value from Blynk virtual pin V0 to a variable
  int i;
  if (pinValue) {
    for (i=0;i<=4;i++) {
      carrier.leds.setPixelColor(i,  0 ,  0 , 50);
    }
    carrier.leds.show();
    carrier.Relay1.open();
  } else {
    for (i=0;i<=4;i++) {
      carrier.leds.setPixelColor(i, 0, 0, 0);
    } 
    carrier.leds.show();
    carrier.Relay1.close();
  }
}

//Flashes Opla LEDS red 4 times and opens/closes Opla relay 2 on command from smart phone
BLYNK_WRITE(V6)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V6 to a variable
  int i;
  if (pinValue) {
    for (i=0;i<=4;i++) {
      carrier.leds.setPixelColor(i, 0, 50, 0);
    }
    carrier.leds.show();
    carrier.Relay2.open();
  } else {
    for (i=0;i<=4;i++) {
      carrier.leds.setPixelColor(i, 0, 0, 0);
    } 
    carrier.leds.show();
    carrier.Relay2.close();
  }
}

//Initial setup section
void setup()
{
  Serial.begin(9600); //For debugging
  pinMode(A5,INPUT); //Read PIR sensor input on pin A5
  pinMode(A6,INPUT); //Read sound sensor input on pin A6
  //Initialize the MKR IoT carrier
  if (!carrier.begin()) {
    Serial.println("Failed to initialize MKR IoT carrier!");
    while (1);
  }
  playTune(); //Play the buzzer tune on startup
  Blynk.begin(auth, ssid, pass); //Connect to Blynk Cloud
  timer.setInterval(1000L, myTimerEvent1); //Set the Blynk timer1 PIR sensor interval
  timer.setInterval(500L, myTimerEvent2); //Set the Blynk timer2 sound sensor interval
  timer.setInterval(5000L, myTimerEvent3); //Set the environmental display timer3 interval
}

// Main loop section
void loop()
{
  Blynk.run(); //Run the Blynk connection
  timer.run(); //Run the Blynk timer
}

//Displays the environmental sensor values on the Opla's graphics display
void displayEnv() {
  carrier.display.fillScreen(ST77XX_BLACK); //oled clear()
  carrier.display.setTextSize(2); //Set the display text size to medium
  carrier.display.setCursor(50, 30); //Move the cursor to the first line
  carrier.display.setTextColor(ST77XX_BLUE); //Set the display text color to blue
  carrier.display.print("Temperature"); //Write the label
  carrier.display.setCursor(50, 50); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_WHITE); //Set the text color to white
  carrier.display.print(temperature); //Display the temperature sensor value
  carrier.display.setCursor(50, 70); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_MAGENTA); //Set the text color to magenta
  carrier.display.print("Humidity"); //Write the label
  carrier.display.setCursor(50, 90); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_WHITE); //Set the text color to white
  carrier.display.print(humidity); //Display the humidity sensor value
  carrier.display.setCursor(50, 110); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_GREEN); //Set the text color to green
  carrier.display.print("Pressure"); //Write the label
  carrier.display.setCursor(50, 130); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_WHITE); //Set the text color to white
  carrier.display.print(pressure); //Display the pressure sensor value
  carrier.display.setCursor(50, 150); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_RED); //Set the text color to red
  carrier.display.print("Hours"); //Write the label
  carrier.display.setCursor(50, 170); //Move the cursor to the next line
  carrier.display.setTextColor(ST77XX_WHITE); //Set the text color to white
  carrier.display.print(hours, 3); //Display the run time hours
}

//Plays the buzzer tune
void playTune() {
  for (int x=0;x<NOTES;x++) {
    carrier.Buzzer.sound(tune[x].frequency);
    delay(tune[x].duration);
    carrier.Buzzer.noSound();
  }
}

Credits

maulepilot

maulepilot

0 projects • 1 follower

Comments