Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:iot:examples:homesecurity [2018/05/11 10:31]
rim.puks
en:iot:examples:homesecurity [2020/06/11 01:09] (current)
heikopikner
Line 4: Line 4:
  
  
 +{{:​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@1.0.5,​ MFRC522, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED</​code>​
 <code c> <code c>
-/* 
-* 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>​
Line 26: Line 23:
 #include <​Adafruit_SSD1306.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 #define OLED_RESET 0  // GPIO0
  
-Ticker timeTicker;+// Create an object for OLED screen
 Adafruit_SSD1306 display(OLED_RESET);​ Adafruit_SSD1306 display(OLED_RESET);​
  
Line 34: Line 35:
 #define BUZZER_PIN D8 #define BUZZER_PIN D8
  
-// ITT splashs screen bitmap. Generator: 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.
-+
-  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) 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.clearDisplay();​
   display.setTextSize(1);​   display.setTextSize(1);​
Line 84: Line 45:
   display.println(msg);​   display.println(msg);​
   display.display();​   display.display();​
-  if(msg=="​Motion ​\ndetected!") //sound the buzzer+  if(msg=="​Motion ​detected!") //sound the buzzer
   {   {
     analogWrite(BUZZER_PIN,​ 512);     analogWrite(BUZZER_PIN,​ 512);
     delay(1000);​     delay(1000);​
 +    analogWrite(BUZZER_PIN,​ 0);
     digitalWrite(BUZZER_PIN,​ LOW);     digitalWrite(BUZZER_PIN,​ LOW);
   }   }
Line 95: Line 57:
 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(MODULE_TOPIC"/security"​);​
 } }
  
 void setup() void setup()
 { {
 +  // Initialize serial port and send message
   Serial.begin(115200);​   Serial.begin(115200);​
   Serial.println("​Booting"​);​   Serial.println("​Booting"​);​
Line 109: Line 74:
   display.begin(SSD1306_SWITCHCAPVCC,​ 0x3C);   display.begin(SSD1306_SWITCHCAPVCC,​ 0x3C);
  
-  // Since the buffer is intialized with an Adafruit splashscreen +  // Display "​booting..."​ message
-  // 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 
-  delay(2000);​ 
  
   // print IoT json config to serial   // print IoT json config to serial
Line 141: Line 102:
  
 The following is the program code for the controller with PIR module. The following is the program code for the controller with PIR module.
 +<​code>​lib_deps = ITTIoT@1.0.5</​code>​
 <code c> <code c>
-/* 
-* 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 red IoT module
 +#define MODULE_TOPIC "​ESP53"​
  
 //Pin definition for the PIR (GPIO14) //Pin definition for the PIR (GPIO14)
Line 165: Line 118:
 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 on.
-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.
Line 176: Line 129:
   if(msg == "​1"​)   if(msg == "​1"​)
   {   {
- onState = true;+    onState = true
 +     ​String msg = String("​PIR online"​);​ 
 +     ​iot.publishMsgTo("​ESP01/​security",​ msg.c_str(),​true);
   }   }
  
   if(msg == "​0"​)   if(msg == "​0"​)
   {   {
- onState = false;+    onState = false
 +     ​String msg = String("​PIR offline"​);​ 
 +     ​iot.publishMsgTo("​ESP01/​security",​ msg.c_str(),​true);
   }   }
 } }
Line 190: Line 147:
   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 196: Line 153:
 void setup() void setup()
 { {
 +  // Initialize serial port and send message
   Serial.begin(115200);​   Serial.begin(115200);​
   Serial.println("​Booting"​);​   Serial.println("​Booting"​);​
Line 212: Line 170:
   // IoT behind the plan work, it should be periodically called   // IoT behind the plan work, it should be periodically called
   iot.handle();​   iot.handle();​
-  delay(200);+  delay(100);
  
-  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 data to MQTT server 
 +      ​if(onState ​== true)
       {       {
-        ​digitalWrite(PIR_LED_PIN,​ HIGH); +        String msg = String("​Motion ​detected!"); 
-        ​String msg = String("​Motion ​\ndetected!"); +        iot.publishMsgTo("ESP01/security",​ msg.c_str(),true);
-        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 data to MQTT server 
 +      ​if(onState ​== true)
       {       {
-        String msg = String("​Nothing ​\ndetected."); +        String msg = String("​Nothing ​detected!"); 
-        iot.publishMsg("​security",​ msg.c_str()); +        iot.publishMsgTo("ESP01/security",​ msg.c_str(),​true);
-        digitalWrite(PIR_LED_PINLOW)+
-        pirState = false;+
       }       }
 +      pirState = false;
     }     }
-  } 
-  else //Reports that the PIR module is turned off. 
-  { 
-    String msg = String("​PIR \noffline"​);​ 
-    iot.publishMsg("​security",​ msg.c_str());​ 
-    delay(2000);​ 
   }   }
 } }
 </​code>​ </​code>​
  
en/iot/examples/homesecurity.1526023870.txt.gz · Last modified: 2018/05/11 10:31 by rim.puks
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