This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:iot:examples:homesecurity [2018/12/27 12:53] 127.0.0.1 external edit |
en:iot:examples:homesecurity [2020/11/18 21:40] (current) heikopikner |
||
---|---|---|---|
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 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 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@1.0.5, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO</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> | ||
#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 | ||
+ | #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 40: | Line 34: | ||
#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 102: | Line 56: | ||
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 116: | Line 73: | ||
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 148: | Line 101: | ||
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</code> | + | <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 173: | Line 117: | ||
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 184: | Line 128: | ||
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 198: | Line 146: | ||
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 152: | ||
void setup() | void setup() | ||
{ | { | ||
+ | // Initialize serial port and send message | ||
Serial.begin(115200); | Serial.begin(115200); | ||
Serial.println("Booting"); | Serial.println("Booting"); | ||
Line 220: | Line 169: | ||
// 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 detected!"); | ||
- | iot.publishMsg("security", msg.c_str()); | + | iot.publishMsgTo("ESP01/security", msg.c_str(),true); |
- | 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 detected!."); | + | String msg = String("Nothing detected!"); |
- | iot.publishMsgTo("Itt/buzzersec/security", msg.c_str(),true); | + | iot.publishMsgTo("ESP01/security", msg.c_str(),true); |
- | 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> | ||