Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot:examples:homesecurity [2018/05/09 12:08] – created rim.puksen:iot:examples:homesecurity [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 ====== Home security example ====== ====== 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.
 +
 +
 +{{:en:iot:examples:homesecurity.jpg?300|}}
 +
 +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:
 +<code>lib_deps = ITTIoT, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO</code>
 +<code c>
 +// Includes global variables and librarys that the OLED display and buzzer uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Ticker.h>
 +#include <Adafruit_I2CDevice.h>
 +#include <Adafruit_GFX.h>
 +#include <Adafruit_SSD1306.h>
 +
 +// Change it according to the real name of the PIR node
 +#define PIR_TOPIC "ESP30"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +// 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(); // clears the srceen
 +  display.setTextSize(1); // sets the text size for the screen
 +  display.setTextColor(WHITE); // text color is set to white
 +  display.setCursor(0,0); // position from where the text writing is starting
 +  display.println(msg); // received message is send to the screen
 +  display.display(); // shows the new screen output
 +
 +  if(msg=="Motion detected!") //sound the buzzer, if motion has been detected
 +  {
 +    analogWrite(BUZZER_PIN, 512); // setting buzzer output to some value
 +    delay(1000); // waiting for 1 second
 +    analogWrite(BUZZER_PIN, 0); // switching the busser off
 +    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(PIR_TOPIC"/security");
 +}
 +
 +void setup()
 +{
 +  // Initialize serial port and send message
 +  Serial.begin(115200); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  // initialize with the I2C addr 0x3C (for the 64x48)
 +  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 +
 +  // Display "booting..." message on OLED screen
 +  display.clearDisplay();
 +  display.setTextSize(1);
 +  display.setTextColor(WHITE);
 +  display.setCursor(0,0);
 +  display.println("Booting...");
 +  display.display();
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig();  // print IoT json config to serial
 +  iot.setup();// Initialize IoT library
 +
 +  // Initialize buzzer pin
 +  pinMode(BUZZER_PIN, OUTPUT);
 +  digitalWrite(BUZZER_PIN, LOW);
 +}
 +
 +void loop()
 +{
 +  iot.handle(); // IoT behind the plan work, it should be periodically called
 +  delay(200); // Wait for 0.2 second
 +}
 +</code>
 +
 +The following is the program code for the controller with PIR module.
 +<code>lib_deps = ITTIoT</code>
 +<code c>
 +// Includes global variables and librarys that the PIR uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +
 +// Change it according to the real name of the PIR IoT module
 +#define MODULE_TOPIC "ESP30"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +//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 off
 +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(topic == MODULE_TOPIC"/pir")
 +  {
 +    if(msg == "1")
 +    {
 +      // PIR is switched ON and message “PIR online” is send to OLED and buzzer node
 +      onState = true; // PIR is activated
 +      String msg = String("PIR online");
 +      iot.publishMsg("security", msg.c_str());
 +    }
 +
 +    if(msg == "0")
 +    {
 +      // PIR is switched OFF and message “PIR offline” is send to OLED and buzzer node
 +      onState = false; // PIR is deactivated
 +      String msg = String("PIR offline");
 +      iot.publishMsg("security", msg.c_str());
 +    }
 +  }
 +}
 +
 +// 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); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig();// Print json config to serial
 +  iot.setup();// Initialize IoT library
 +
 +  // Initialize PIR pin
 +  pinMode(PIR_PIN, INPUT);
 +  pinMode(PIR_LED_PIN, OUTPUT);
 +}
 +
 +void loop()
 +{
 +  iot.handle(); // IoT behind the plan work, it should be periodically called
 +  delay(100); // Wait 0.1 second
 +
 +  // 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 a message to MQTT server
 +      if(onState == true)
 +      {
 +        String msg = String("Motion detected!");
 +        iot.publishMsg("security", msg.c_str());
 +      }
 +      pirState = true;
 +    }
 +  }
 +  else
 +  {
 +    if(pirState == true)
 +    {
 +      // Turn off PIR red LED
 +      digitalWrite(PIR_LED_PIN, LOW);
 +      // If sensor is armed, then send a message to MQTT server
 +      if(onState == true)
 +      {
 +        String msg = String("Nothing detected!");
 +        iot.publishMsg("security", msg.c_str());
 +      }
 +      pirState = false;
 +    }
 +  }
 +}
 +</code>
  
en/iot/examples/homesecurity.1525867738.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
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