This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot:examples:homesecurity [2018/05/09 12:08] – created rim.puks | en: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. | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | The program on the PIR controller will publish " | ||
| + | |||
| + | The OLED controller will then display those messages. It will print out anything it receives on " | ||
| + | If the message it receives is " | ||
| + | |||
| + | The following is the code for the controller with OLED and buzzer. | ||
| + | Needed libraries: | ||
| + | < | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the OLED display and buzzer uses | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Change it according to the real name of the PIR node | ||
| + | #define PIR_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_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 " | ||
| + | // If the message it receives is " | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | display.clearDisplay(); | ||
| + | display.setTextSize(1); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setCursor(0, | ||
| + | display.println(msg); | ||
| + | display.display(); | ||
| + | |||
| + | if(msg==" | ||
| + | { | ||
| + | analogWrite(BUZZER_PIN, | ||
| + | delay(1000); | ||
| + | analogWrite(BUZZER_PIN, | ||
| + | digitalWrite(BUZZER_PIN, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // 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(" | ||
| + | // Send message to MQTT server to show that connection is established | ||
| + | iot.log(" | ||
| + | // Subscribe to get security messages | ||
| + | iot.subscribe(PIR_TOPIC"/ | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Initialize serial port and send message | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // initialize with the I2C addr 0x3C (for the 64x48) | ||
| + | display.begin(SSD1306_SWITCHCAPVCC, | ||
| + | |||
| + | // Display " | ||
| + | display.clearDisplay(); | ||
| + | display.setTextSize(1); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.display(); | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup();// | ||
| + | |||
| + | // Initialize buzzer pin | ||
| + | pinMode(BUZZER_PIN, | ||
| + | digitalWrite(BUZZER_PIN, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | delay(200); // Wait for 0.2 second | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The following is the program code for the controller with PIR module. | ||
| + | < | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the PIR uses | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Change it according to the real name of the PIR IoT module | ||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_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(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | |||
| + | if(topic == MODULE_TOPIC"/ | ||
| + | { | ||
| + | if(msg == " | ||
| + | { | ||
| + | // PIR is switched ON and message “PIR online” is send to OLED and buzzer node | ||
| + | onState = true; // PIR is activated | ||
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | |||
| + | if(msg == " | ||
| + | { | ||
| + | // PIR is switched OFF and message “PIR offline” is send to OLED and buzzer node | ||
| + | onState = false; // PIR is deactivated | ||
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Function started once the connection to the server is established. | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | // Subscribe to the topic " | ||
| + | iot.subscribe(MODULE_TOPIC"/ | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Initialize serial port and send message | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig();// | ||
| + | iot.setup();// | ||
| + | |||
| + | // Initialize PIR pin | ||
| + | pinMode(PIR_PIN, | ||
| + | pinMode(PIR_LED_PIN, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | 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, | ||
| + | // If sensor is armed, then send a message to MQTT server | ||
| + | if(onState == true) | ||
| + | { | ||
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | pirState = true; | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | if(pirState == true) | ||
| + | { | ||
| + | // Turn off PIR red LED | ||
| + | digitalWrite(PIR_LED_PIN, | ||
| + | // If sensor is armed, then send a message to MQTT server | ||
| + | if(onState == true) | ||
| + | { | ||
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | pirState = false; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||