Home security example

This example demonstrates how to build a home security system with the ITT IoT kit. To build this example you will need two controller modules. Attach the PIR module to one of the controllers. And OLED and buzzer modules to the other.

The program on the PIR controller will publish “Motion detected” and “Nothing detected” messages to topic “security”. The OLED controller will then display those messages. If it receives message “Motion detected”, buzzer shall be activated.

The following is the code for the controller with OLED and buzzer. Needed libraries:

lib_deps = ITTIoT, MFRC522, Adafruit_GFX, Adafruit SSD1306
/*
* Home security example - OLED and buzzer shield program
*
* This program is for the controller with OLED and buzzer shields.
* It will print out anything it receives on "security" topic.
* If the message it receives is "Motion \ndetected!". It will activate the buzzer for 1 second.
*
* Author: Rim Puks
* May 2018
*/
#include <Arduino.h>
#include <ittiot.h>
#include <Ticker.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define OLED_RESET 0  // GPIO0
 
Ticker timeTicker;
Adafruit_SSD1306 display(OLED_RESET);
 
//Pin definition for buzzer.
#define BUZZER_PIN D8
 
// ITT splashs screen bitmap. Generator: http://javl.github.io/image2cpp/
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x07, 0x1f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00,
	0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00,
	0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00,
	0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe3, 0xff, 0xff, 0xf8, 0x00,
	0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00,
	0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x10, 0xe3, 0xfc, 0xff, 0xf8, 0x00,
	0x00, 0x07, 0x10, 0xe3, 0xfc, 0xff, 0xf8, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x07, 0xf0, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
//Checks if the OLED screens header file is configured correctly. If problems appear you need to
//change "Adafruit_SSD1306.h". The correct definition is #define SSD1306_64_48
#if (SSD1306_LCDHEIGHT != 48)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
 
bool isBootModeNormal;
 
// If message is received, print it on OLED. If it says "Motion \ndetected", sound the buzzer.
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println(msg);
  display.display();
  if(msg=="Motion detected!") //sound the buzzer
  {
    analogWrite(BUZZER_PIN, 512);
    delay(1000);
    analogWrite(BUZZER_PIN, 0);
    digitalWrite(BUZZER_PIN, LOW);
  }
}
 
// Function started after the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  iot.log("IoT Home security example!");
  isBootModeNormal = true;
  iot.subscribe("security");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  // initialize with the I2C addr 0x3C (for the 64x48)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, we should clear it
  display.clearDisplay();
 
  // Load ITT splash screen into buffer
  display.drawBitmap(0, 0,  logo16_glcd_bmp, 64, 48, 1);
  // Show image buffer on the display
  display.display();
 
  // Display ITT splashscreen for two seconds
  delay(2000);
 
  // print IoT json config to serial
  iot.printConfig();
 
  // Initialize IoT library
  iot.setup();
 
  // Initialize buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
  delay(200);
}

The following is the program code for the controller with PIR module.

lib_deps = ITTIoT
/*
* Home security example - PIR shield program
*
* This program is for the controller with PIR shield.
* If nothing is moving, controller sends "Nothing detected." to topic "security".
* If PIR shield detects movement it will send "Movement detected!" to topic "security".
* To turn the PIR shield OFF and ON send "0" or "1" to topic "pir".
*
* Author: Rim Puks
* May 2018
*/
 
#include <Arduino.h>
#include <ittiot.h>
 
//Pin definition for the PIR (GPIO14)
#define PIR_PIN D5
//Pin definition for the PIR LED (GPIO16)
#define PIR_LED_PIN D4
 
// PIR state for detection. Used as a switch.
bool pirState;
// State that switches PIR on and off. By default it will be on.
bool onState=1;
 
// If message is received, turn the PIR module OFF or On.
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
  if(msg == "1")
  {
	onState = true;
  }
 
  if(msg == "0")
  {
	onState = false;
  }
}
 
// Function started once the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  // Subscribe to the topic "pir"
  iot.subscribe("pir");
  iot.log("IoT PIR example!");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  // Print json config to serial
  iot.printConfig();
  // Initialize IoT library
  iot.setup();
  // Initialize PIR pin
  pinMode(PIR_PIN, INPUT);
  pinMode(PIR_LED_PIN, OUTPUT);
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
  delay(200);
 
  if(onState == true)
  {
    if(digitalRead(PIR_PIN))
    {
      if(pirState == false)
      {
        digitalWrite(PIR_LED_PIN, HIGH);
        String msg = String("Motion detected!");
        iot.publishMsg("security", msg.c_str());
        pirState = true;
      }
    }
    else
    {
      if(pirState == true)
      {
        String msg = String("Nothing detected!.");
        iot.publishMsgTo("Itt/buzzersec/security", msg.c_str(),true);
        digitalWrite(PIR_LED_PIN, LOW);
        pirState = false;
      }
    }
  }
  else //Reports that the PIR module is turned off.
  {
    String msg = String("PIR offline");
    iot.publishMsgTo("Itt/buzzersec/security", msg.c_str(),true);
    delay(2000);
  }
}
en/iot/examples/homesecurity.txt · Last modified: 2018/05/16 12:18 by Somepub
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0