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 the topic “security” when armed. To arm module, send to the topic “ESP53/pir” message “1”. For disarming, send “0”. The default value is “0”. PIR module RED LED always indicating when motion is detected.

The OLED controller will then display those messages. It will print out anything it receives on “security” topic. If the message it receives is “Motion detected!”. It will activate the buzzer for 1 second.

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

lib_deps = ITTIoT@1.0.5, MFRC522, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED
#include <Arduino.h>
#include <ittiot.h>
#include <Ticker.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
// Change it according to the real name of the red IoT module
#define MODULE_TOPIC "ESP01"
 
// OLED reset pin is GPIO0
#define OLED_RESET 0  // GPIO0
 
// Create an object for OLED screen
Adafruit_SSD1306 display(OLED_RESET);
 
//Pin definition for buzzer.
#define BUZZER_PIN D8
 
// It will print out anything it receives on "security" topic.
// If the message it receives is "Motion detected!". It will activate the buzzer for 1 second.
void iot_received(String topic, String 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()
{
  // Send message to serial port to show that connection is established
  Serial.println("MQTT connected callback");
  // Send message to MQTT server to show that connection is established
  iot.log("IoT Home security example!");
  // Subscribe to get security messages
  iot.subscribe(MODULE_TOPIC"/security");
}
 
void setup()
{
  // Initialize serial port and send message
  Serial.begin(115200);
  Serial.println("Booting");
 
  // initialize with the I2C addr 0x3C (for the 64x48)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 
  // Display "booting..." message
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Booting...");
  display.display();
 
  // 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@1.0.5
#include <Arduino.h>
#include <ittiot.h>
 
// Change it according to the real name of the red IoT module
#define MODULE_TOPIC "ESP53"
 
//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=0;
 
// 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;
     String msg = String("PIR online");
     iot.publishMsgTo("ESP01/security", msg.c_str(),true);
  }
 
  if(msg == "0")
  {
	   onState = false;
     String msg = String("PIR offline");
     iot.publishMsgTo("ESP01/security", msg.c_str(),true);
  }
}
 
// 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(MODULE_TOPIC"/pir");
  iot.log("IoT PIR example!");
}
 
void setup()
{
  // Initialize serial port and send message
  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(100);
 
  // Read PIR sensor pin
  if(digitalRead(PIR_PIN))
  {
    if(pirState == false)
    {
      // Turn on PIR red LED
      digitalWrite(PIR_LED_PIN, HIGH);
      // If sensor is armed, then send data to MQTT server
      if(onState == true)
      {
        String msg = String("Motion detected!");
        iot.publishMsgTo("ESP01/security", msg.c_str(),true);
      }
      pirState = true;
    }
  }
  else
  {
    if(pirState == true)
    {
      // Turn off PIR red LED
      digitalWrite(PIR_LED_PIN, LOW);
      // If sensor is armed, then send data to MQTT server
      if(onState == true)
      {
        String msg = String("Nothing detected!");
        iot.publishMsgTo("ESP01/security", msg.c_str(),true);
      }
      pirState = false;
    }
  }
}
en/iot/examples/homesecurity.txt · Last modified: 2020/06/11 01:09 by heikopikner
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