This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:homesecurity [2018/05/11 07:53] – rim.puks | en:iot:examples:homesecurity [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| {{: | {{: | ||
| - | The program on the PIR controller will publish " | + | The program on the PIR controller will publish " |
| - | The OLED controller will then display those messages. | + | |
| - | If it receives | + | The OLED controller will then display those messages. It will print out anything it receives on " |
| + | If the message | ||
| 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: | ||
| + | < | ||
| <code c> | <code c> | ||
| - | /* | + | // Includes global variables |
| - | * Home security example - OLED and buzzer shield program | + | |
| - | * | + | |
| - | * This program is for the controller with OLED and buzzer | + | |
| - | * It will print out anything it receives on " | + | |
| - | * If the message it receives is " | + | |
| - | * | + | |
| - | * Author: Rim Puks | + | |
| - | * May 2018 | + | |
| - | */ | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include <SPI.h> | + | #include <Adafruit_I2CDevice.h> |
| - | #include <Wire.h> | + | |
| #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 | #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 bitmap. Generator: http://javl.github.io/ | + | // It will print out anything it receives on " |
| - | static const unsigned char PROGMEM logo16_glcd_bmp[] = | + | // If the message it receives is " |
| + | void iot_received(String topic, String msg) | ||
| { | { | ||
| - | | + | |
| - | 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 " | + | |
| - | #if (SSD1306_LCDHEIGHT != 48) | + | |
| - | # | + | |
| - | #endif | + | |
| - | + | ||
| - | bool isBootModeNormal; | + | |
| - | + | ||
| - | // If message is received, print it on OLED. If it says " | + | |
| - | void iot_received(String topic, String msg) | + | |
| - | { | + | |
| - | Serial.print(" | + | |
| - | Serial.print(topic); | + | |
| - | Serial.print(" | + | |
| - | Serial.println(msg); | + | |
| - | display.clearDisplay(); | + | |
| - | display.setTextSize(1); | + | |
| - | display.setTextColor(WHITE); | + | |
| - | display.setCursor(0, | + | |
| - | display.println(msg); | + | |
| - | display.display(); | + | |
| - | | + | |
| { | { | ||
| - | analogWrite(BUZZER_PIN, | + | analogWrite(BUZZER_PIN, |
| - | delay(1000); | + | delay(1000); |
| + | analogWrite(BUZZER_PIN, | ||
| digitalWrite(BUZZER_PIN, | digitalWrite(BUZZER_PIN, | ||
| } | } | ||
| Line 99: | Line 60: | ||
| void iot_connected() | void iot_connected() | ||
| { | { | ||
| + | // Send message to serial port to show that connection is established | ||
| Serial.println(" | Serial.println(" | ||
| + | // Send message to MQTT server to show that connection is established | ||
| iot.log(" | iot.log(" | ||
| - | | + | |
| - | iot.subscribe(" | + | iot.subscribe(PIR_TOPIC"/security" |
| } | } | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | |
| + | | ||
| Serial.println(" | Serial.println(" | ||
| Line 113: | Line 77: | ||
| display.begin(SSD1306_SWITCHCAPVCC, | display.begin(SSD1306_SWITCHCAPVCC, | ||
| - | // Since the buffer is intialized with an Adafruit splashscreen | + | // Display " |
| - | // internally, we should clear it | + | |
| display.clearDisplay(); | display.clearDisplay(); | ||
| - | + | display.setTextSize(1); | |
| - | | + | |
| - | display.drawBitmap(0, 0, logo16_glcd_bmp, | + | display.setCursor(0,0); |
| - | | + | display.println(" |
| display.display(); | display.display(); | ||
| - | // Display ITT splashscreen for two seconds | + | //iot.setConfig(" |
| - | delay(2000); | + | //iot.setConfig(" |
| - | + | iot.printConfig(); | |
| - | // print IoT json config to serial | + | iot.setup(); |
| - | iot.printConfig(); | + | |
| - | + | ||
| - | // | + | |
| - | iot.setup(); | + | |
| // Initialize buzzer pin | // Initialize buzzer pin | ||
| Line 138: | Line 97: | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // IoT behind the plan work, it should be periodically called | + | |
| - | iot.handle(); | + | delay(200); |
| - | delay(200); | + | |
| } | } | ||
| </ | </ | ||
| 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 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 " | + | |
| - | * If PIR shield detects movement it will send " | + | |
| - | * To turn the PIR shield OFF and ON send " | + | |
| - | * | + | |
| - | * Author: Rim Puks | + | |
| - | * May 2018 | + | |
| - | */ | + | |
| #include < | #include < | ||
| #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) | //Pin definition for the PIR (GPIO14) | ||
| Line 166: | 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 178: | Line 131: | ||
| Serial.print(" | Serial.print(" | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | if(msg == " | ||
| - | { | ||
| - | onState = true; | ||
| - | } | ||
| - | if(msg == "0") | + | if(topic == MODULE_TOPIC"/pir") |
| { | { | ||
| - | onState = false; | + | 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 | ||
| + | | ||
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| } | } | ||
| } | } | ||
| Line 194: | Line 157: | ||
| Serial.println(" | Serial.println(" | ||
| // Subscribe to the topic " | // Subscribe to the topic " | ||
| - | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC"/pir"); |
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 200: | Line 163: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | |
| + | | ||
| Serial.println(" | Serial.println(" | ||
| - | // Print json config to serial | + | //iot.setConfig(" |
| - | iot.printConfig(); | + | |
| - | // Initialize IoT library | + | |
| - | iot.setup(); | + | iot.setup(); |
| // Initialize PIR pin | // Initialize PIR pin | ||
| pinMode(PIR_PIN, | pinMode(PIR_PIN, | ||
| Line 214: | Line 179: | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // IoT behind the plan work, it should be periodically called | + | |
| - | iot.handle(); | + | delay(100); // Wait 0.1 second |
| - | delay(200); | + | |
| - | if(onState == true) | + | |
| + | | ||
| { | { | ||
| - | if(digitalRead(PIR_PIN)) | + | if(pirState == false) |
| { | { | ||
| - | if(pirState | + | |
| + | digitalWrite(PIR_LED_PIN, | ||
| + | // If sensor is armed, then send a message to MQTT server | ||
| + | | ||
| { | { | ||
| - | | + | String msg = String(" |
| - | | + | |
| iot.publishMsg(" | iot.publishMsg(" | ||
| - | pirState = true; | ||
| } | } | ||
| + | pirState = true; | ||
| } | } | ||
| - | | + | } |
| + | | ||
| + | { | ||
| + | if(pirState == true) | ||
| { | { | ||
| - | if(pirState | + | |
| + | digitalWrite(PIR_LED_PIN, | ||
| + | // If sensor is armed, then send a message to MQTT server | ||
| + | | ||
| { | { | ||
| - | String msg = String(" | + | String msg = String(" |
| iot.publishMsg(" | iot.publishMsg(" | ||
| - | digitalWrite(PIR_LED_PIN, | ||
| - | pirState = false; | ||
| } | } | ||
| + | pirState = false; | ||
| } | } | ||
| - | } | ||
| - | else //Reports that the PIR module is turned off. | ||
| - | { | ||
| - | String msg = String(" | ||
| - | iot.publishMsg(" | ||
| - | delay(2000); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||