Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:homesecurity [2018/05/16 09:18] Somepuben:iot:examples:homesecurity [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 6: Line 6:
 {{:en:iot:examples:homesecurity.jpg?300|}} {{:en:iot:examples:homesecurity.jpg?300|}}
  
-The program on the PIR controller will publish "Motion detected" and "Nothing detected" messages to topic "security"+The program on the PIR controller will publish "Motion detected" and "Nothing detected" messages to the topic "security" when armedTo 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. + 
-If it receives message "Motion detected"buzzer shall be activated.+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. The following is the code for the controller with OLED and buzzer.
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT, MFRC522, Adafruit_GFX, Adafruit SSD1306</code>+<code>lib_deps = ITTIoT, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO</code>
 <code c> <code c>
-/+// Includes global variables and librarys that the OLED display and buzzer uses
-* 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 <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <Ticker.h> #include <Ticker.h>
-#include <SPI.h> +#include <Adafruit_I2CDevice.h>
-#include <Wire.h>+
 #include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
 #include <Adafruit_SSD1306.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 #define OLED_RESET 0  // GPIO0
  
-Ticker timeTicker;+// Create an object for OLED screen
 Adafruit_SSD1306 display(OLED_RESET); Adafruit_SSD1306 display(OLED_RESET);
  
-//Pin definition for buzzer.+//Pin definition for buzzer
 #define BUZZER_PIN D8 #define BUZZER_PIN D8
  
-// ITT splashs screen bitmapGenerator: http://javl.github.io/image2cpp/ +// It will print out anything it receives on "security" topic. 
-static const unsigned char PROGMEM logo16_glcd_bmp[] =+// If the message it receives is "Motion detected!"It will activate the buzzer for 1 second
 +void iot_received(String topic, String msg)
 { {
-  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, +  display.clearDisplay(); // clears the srceen 
- 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, +  display.setTextSize(1); // sets the text size for the screen 
- 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, +  display.setTextColor(WHITE); // text color is set to white 
- 0x00, 0x07, 0x1f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00+  display.setCursor(0,0); // position from where the text writing is starting 
- 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, +  display.println(msg); // received message is send to the screen 
- 0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00, +  display.display()// shows the new screen output
- 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 +  if(msg=="Motion detected!") //sound the buzzer, if motion has been detected
-//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); +    analogWrite(BUZZER_PIN, 512); // setting buzzer output to some value 
-    delay(1000); +    delay(1000); // waiting for 1 second 
-    analogWrite(BUZZER_PIN, 0);+    analogWrite(BUZZER_PIN, 0); // switching the busser off
     digitalWrite(BUZZER_PIN, LOW);     digitalWrite(BUZZER_PIN, LOW);
   }   }
Line 102: Line 60:
 void iot_connected() void iot_connected()
 { {
 +  // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
 +  // Send message to MQTT server to show that connection is established
   iot.log("IoT Home security example!");   iot.log("IoT Home security example!");
-  isBootModeNormal = true; +  // Subscribe to get security messages 
-  iot.subscribe("security");+  iot.subscribe(PIR_TOPIC"/security");
 } }
  
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
Line 116: Line 77:
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
-  // Since the buffer is intialized with an Adafruit splashscreen +  // Display "booting..." message on OLED screen
-  // internally, we should clear it+
   display.clearDisplay();   display.clearDisplay();
- +  display.setTextSize(1); 
-  // Load ITT splash screen into buffer +  display.setTextColor(WHITE); 
-  display.drawBitmap(0, 0,  logo16_glcd_bmp, 64, 48, 1); +  display.setCursor(0,0); 
-  // Show image buffer on the display+  display.println("Booting...");
   display.display();   display.display();
  
-  // Display ITT splashscreen for two seconds +  //iot.setConfig("wname", WIFI_NAME); 
-  delay(2000); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
- +  iot.printConfig();  // print IoT json config to serial 
-  // print IoT json config to serial +  iot.setup();// Initialize IoT library
-  iot.printConfig(); +
- +
-  // Initialize IoT library +
-  iot.setup();+
  
   // Initialize buzzer pin   // Initialize buzzer pin
Line 141: Line 97:
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  iot.handle(); +  delay(200); // Wait for 0.2 second
-  delay(200);+
 } }
 </code> </code>
Line 150: Line 105:
 <code>lib_deps = ITTIoT</code> <code>lib_deps = ITTIoT</code>
 <code c> <code c>
-/+// Includes global variables and librarys that the PIR uses
-* 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 <Arduino.h>
 #include <ittiot.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) //Pin definition for the PIR (GPIO14)
Line 170: Line 119:
 #define PIR_LED_PIN D4 #define PIR_LED_PIN D4
  
-// PIR state for detection. Used as a switch.+// PIR state for detection. Used as a switch
 bool pirState; bool pirState;
-// State that switches PIR on and off. By default it will be on. +// State that switches PIR on and off. By default it will be off 
-bool onState=1;+bool onState=0;
  
-// If message is received, turn the PIR module OFF or On.+// If message is received, turn the PIR module OFF or ON
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
Line 182: Line 131:
   Serial.print(" payload: ");   Serial.print(" payload: ");
   Serial.println(msg);   Serial.println(msg);
-  if(msg == "1") 
-  { 
- onState = true; 
-  } 
  
-  if(msg == "0")+  if(topic == MODULE_TOPIC"/pir")
   {   {
- onState = false;+    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()); 
 +    }
   }   }
 } }
Line 198: Line 157:
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
   // Subscribe to the topic "pir"   // Subscribe to the topic "pir"
-  iot.subscribe("pir");+  iot.subscribe(MODULE_TOPIC"/pir");
   iot.log("IoT PIR example!");   iot.log("IoT PIR example!");
 } }
Line 204: Line 163:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // Print json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
-  // Initialize IoT library +  iot.printConfig();// Print json config to serial 
-  iot.setup();+  iot.setup();// Initialize IoT library 
   // Initialize PIR pin   // Initialize PIR pin
   pinMode(PIR_PIN, INPUT);   pinMode(PIR_PIN, INPUT);
Line 218: Line 179:
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  iot.handle(); +  delay(100); // Wait 0.1 second
-  delay(200);+
  
-  if(onState == true)+  // Read PIR sensor pin 
 +  if(digitalRead(PIR_PIN))
   {   {
-    if(digitalRead(PIR_PIN))+    if(pirState == false)
     {     {
-      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)
       {       {
-        digitalWrite(PIR_LED_PIN, HIGH); 
         String msg = String("Motion detected!");         String msg = String("Motion detected!");
         iot.publishMsg("security", msg.c_str());         iot.publishMsg("security", msg.c_str());
-        pirState = true; 
       }       }
 +      pirState = true;
     }     }
-    else+  } 
 +  else 
 +  { 
 +    if(pirState == true)
     {     {
-      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!."); +        String msg = String("Nothing detected!"); 
-        iot.publishMsgTo("Itt/buzzersec/security", msg.c_str(),true)+        iot.publishMsg("security", msg.c_str());
-        digitalWrite(PIR_LED_PIN, LOW); +
-        pirState = false;+
       }       }
 +      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); 
   }   }
 } }
 </code> </code>
  
en/iot/examples/homesecurity.1526462315.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