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 [2020/06/10 22:07] heikopikneren:iot:examples:homesecurity [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 7: Line 7:
  
 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 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. + 
-If it receives the 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@1.0.5, MFRC522, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED</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
 #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 red IoT module +// Change it according to the real name of the PIR node 
-#define MODULE_TOPIC "ESP01"+#define PIR_TOPIC "ESP30" 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "password"
  
 // OLED reset pin is GPIO0 // OLED reset pin is GPIO0
Line 31: Line 34:
 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
  
 // It will print out anything it receives on "security" topic. // 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.+// If the message it receives is "Motion detected!". It will activate the buzzer for 1 second.
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
-  display.clearDisplay(); +  display.clearDisplay(); // clears the srceen 
-  display.setTextSize(1); +  display.setTextSize(1); // sets the text size for the screen 
-  display.setTextColor(WHITE); +  display.setTextColor(WHITE); // text color is set to white 
-  display.setCursor(0,0); +  display.setCursor(0,0); // position from where the text writing is starting 
-  display.println(msg); +  display.println(msg); // received message is send to the screen 
-  display.display(); +  display.display(); // shows the new screen output 
-  if(msg=="Motion detected!") //sound the buzzer+ 
 +  if(msg=="Motion detected!") //sound the buzzer, if motion has been detected
   {   {
-    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 61: Line 65:
   iot.log("IoT Home security example!");   iot.log("IoT Home security example!");
   // Subscribe to get security messages   // Subscribe to get security messages
-  iot.subscribe(MODULE_TOPIC"/security");+  iot.subscribe(PIR_TOPIC"/security");
 } }
  
Line 67: Line 71:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
Line 73: Line 77:
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
-  // Display "booting..." message+  // Display "booting..." message on OLED screen
   display.clearDisplay();   display.clearDisplay();
   display.setTextSize(1);   display.setTextSize(1);
Line 81: Line 85:
   display.display();   display.display();
  
-  // print IoT json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
- +  iot.printConfig();  // print IoT json config to serial 
-  // Initialize IoT library +  iot.setup();// Initialize IoT library
-  iot.setup();+
  
   // Initialize buzzer pin   // Initialize buzzer pin
Line 94: 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>
  
 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>lib_deps = ITTIoT</code>
 <code c> <code c>
 +// Includes global variables and librarys that the PIR uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
  
-// Change it according to the real name of the red IoT module +// Change it according to the real name of the PIR IoT module 
-#define MODULE_TOPIC "ESP53"+#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 114: 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=0; 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 126: Line 131:
   Serial.print(" payload: ");   Serial.print(" payload: ");
   Serial.println(msg);   Serial.println(msg);
-  if(msg == "1") 
-  { 
-    onState = true; 
-     String msg = String("PIR online"); 
-     iot.publishMsgTo("ESP01/security", msg.c_str(),true); 
-  } 
  
-  if(msg == "0")+  if(topic == MODULE_TOPIC"/pir")
   {   {
-    onState = false; +    if(msg == "1"
-     String msg = String("PIR offline"); +    { 
-     iot.publishMsgTo("ESP01/security", msg.c_str(),true);+      // 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 153: Line 164:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  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 167: 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(100);+
  
   // Read PIR sensor pin   // Read PIR sensor pin
Line 178: Line 189:
       // Turn on PIR red LED       // Turn on PIR red LED
       digitalWrite(PIR_LED_PIN, HIGH);       digitalWrite(PIR_LED_PIN, HIGH);
-      // If sensor is armed, then send data to MQTT server+      // If sensor is armed, then send a message to MQTT server
       if(onState == true)       if(onState == true)
       {       {
         String msg = String("Motion detected!");         String msg = String("Motion detected!");
-        iot.publishMsgTo("ESP01/security", msg.c_str(),true);+        iot.publishMsg("security", msg.c_str());
       }       }
       pirState = true;       pirState = true;
Line 193: Line 204:
       // Turn off PIR red LED       // Turn off PIR red LED
       digitalWrite(PIR_LED_PIN, LOW);       digitalWrite(PIR_LED_PIN, LOW);
-      // If sensor is armed, then send data to MQTT server+      // If sensor is armed, then send a message to MQTT server
       if(onState == true)       if(onState == true)
       {       {
         String msg = String("Nothing detected!");         String msg = String("Nothing detected!");
-        iot.publishMsgTo("ESP01/security", msg.c_str(),true);+        iot.publishMsg("security", msg.c_str());
       }       }
       pirState = false;       pirState = false;
en/iot/examples/homesecurity.1591826824.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