This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:homesecurity [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:homesecurity [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| 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 c> | <code c> | ||
| + | // Includes global variables and librarys that the OLED display and buzzer uses | ||
| #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 red IoT module | + | // Change it according to the real name of the PIR node |
| - | # | + | # |
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| // OLED reset pin is GPIO0 | // OLED reset pin is GPIO0 | ||
| Line 32: | 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 | ||
| Line 39: | Line 41: | ||
| void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
| { | { | ||
| - | display.clearDisplay(); | + | display.clearDisplay(); |
| - | display.setTextSize(1); | + | display.setTextSize(1); |
| - | display.setTextColor(WHITE); | + | display.setTextColor(WHITE); |
| - | display.setCursor(0, | + | display.setCursor(0, |
| - | display.println(msg); | + | display.println(msg); |
| - | display.display(); | + | display.display(); |
| - | if(msg==" | + | |
| + | if(msg==" | ||
| { | { | ||
| - | analogWrite(BUZZER_PIN, | + | analogWrite(BUZZER_PIN, |
| - | delay(1000); | + | delay(1000); |
| - | analogWrite(BUZZER_PIN, | + | analogWrite(BUZZER_PIN, |
| digitalWrite(BUZZER_PIN, | digitalWrite(BUZZER_PIN, | ||
| } | } | ||
| Line 62: | Line 65: | ||
| iot.log(" | iot.log(" | ||
| // Subscribe to get security messages | // Subscribe to get security messages | ||
| - | iot.subscribe(MODULE_TOPIC"/ | + | iot.subscribe(PIR_TOPIC"/ |
| } | } | ||
| Line 68: | Line 71: | ||
| { | { | ||
| // Initialize serial port and send message | // Initialize serial port and send message | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| Line 74: | Line 77: | ||
| display.begin(SSD1306_SWITCHCAPVCC, | display.begin(SSD1306_SWITCHCAPVCC, | ||
| - | // Display " | + | // Display " |
| display.clearDisplay(); | display.clearDisplay(); | ||
| display.setTextSize(1); | display.setTextSize(1); | ||
| Line 82: | Line 85: | ||
| display.display(); | display.display(); | ||
| - | // print IoT json config to serial | + | //iot.setConfig(" |
| - | iot.printConfig(); | + | |
| - | + | | |
| - | // | + | iot.setup(); |
| - | iot.setup(); | + | |
| // Initialize buzzer pin | // Initialize buzzer pin | ||
| Line 95: | 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 | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | // 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 " | ||
| + | #define WIFI_PASSWORD " | ||
| //Pin definition for the PIR (GPIO14) | //Pin definition for the PIR (GPIO14) | ||
| Line 115: | 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 127: | Line 131: | ||
| Serial.print(" | Serial.print(" | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | | + | |
| + | | ||
| { | { | ||
| - | onState = true; | + | if(msg == " |
| - | | + | { |
| - | | + | // PIR is switched ON and message “PIR online” is send to OLED and buzzer node |
| - | } | + | |
| + | String msg = String(" | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| - | | + | |
| - | { | + | { |
| - | onState = false; | + | // PIR is switched OFF and message “PIR offline” is send to OLED and buzzer node |
| - | | + | |
| - | | + | String msg = String(" |
| + | iot.publishMsg(" | ||
| + | } | ||
| } | } | ||
| } | } | ||
| Line 154: | Line 164: | ||
| { | { | ||
| // Initialize serial port and send message | // Initialize serial port and send message | ||
| - | Serial.begin(115200); | + | 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 168: | Line 179: | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // IoT behind the plan work, it should be periodically called | + | |
| - | iot.handle(); | + | delay(100); |
| - | delay(100); | + | |
| // Read PIR sensor pin | // Read PIR sensor pin | ||
| Line 179: | Line 189: | ||
| // Turn on PIR red LED | // Turn on PIR red LED | ||
| digitalWrite(PIR_LED_PIN, | digitalWrite(PIR_LED_PIN, | ||
| - | // If sensor is armed, then send data to MQTT server | + | // If sensor is armed, then send a message |
| if(onState == true) | if(onState == true) | ||
| { | { | ||
| String msg = String(" | String msg = String(" | ||
| - | iot.publishMsgTo("ESP01/security", | + | iot.publishMsg(" |
| } | } | ||
| pirState = true; | pirState = true; | ||
| Line 194: | Line 204: | ||
| // Turn off PIR red LED | // Turn off PIR red LED | ||
| digitalWrite(PIR_LED_PIN, | digitalWrite(PIR_LED_PIN, | ||
| - | // If sensor is armed, then send data to MQTT server | + | // If sensor is armed, then send a message |
| if(onState == true) | if(onState == true) | ||
| { | { | ||
| String msg = String(" | String msg = String(" | ||
| - | iot.publishMsgTo("ESP01/security", | + | iot.publishMsg(" |
| } | } | ||
| pirState = false; | pirState = false; | ||