This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:pir [2018/03/20 08:57] – old revision restored (2018/02/27 16:48) Somepub | en:iot:examples:pir [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| {{: | {{: | ||
| - | The code above will show if PIR detector detect any movement. | + | Needed libraries: |
| + | < | ||
| + | |||
| + | The code below will show if PIR detector detect any movement. | ||
| <code c> | <code c> | ||
| Line 8: | Line 11: | ||
| * IoT PIR example | * IoT PIR example | ||
| * | * | ||
| - | * This example subscribe to the "relay" topic. When a message received, then it | + | * This example subscribe to the "pir" topic. When a message received, then it |
| - | * will switch | + | * will switch |
| * | * | ||
| - | * Created 21 Juni 2017 by Heiko Pikner | + | * Created 21 Febuary 2018 by Heiko Pikner |
| */ | */ | ||
| + | // Includes global variables and librarys that the PIR shield uses | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | //Pin definition for the relay (GPIO5) | + | #define MODULE_TOPIC " |
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Pin definition for the PIR (GPIO14) | ||
| #define PIR_PIN D5 | #define PIR_PIN D5 | ||
| + | //Pin definition for the PIR LED (GPIO16) | ||
| #define PIR_LED_PIN D4 | #define PIR_LED_PIN D4 | ||
| + | // PIR state for detection | ||
| bool pirState; | bool pirState; | ||
| + | // State that switches PIR on and off | ||
| + | bool onState; | ||
| - | // If message received | + | // If message received |
| - | // mosquitto_pub -u test -P test -t " | + | // mosquitto_pub -u test -P test -t " |
| void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
| { | { | ||
| Line 31: | Line 43: | ||
| Serial.print(" | Serial.print(" | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | if(msg == " | ||
| - | { | ||
| - | | + | if(topic == MODULE_TOPIC) |
| - | + | ||
| - | | + | |
| { | { | ||
| + | // Switching the PIR shield on or off, depending what message is received | ||
| + | if(msg == " | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | if(msg == " | ||
| + | { | ||
| + | | ||
| + | } | ||
| } | } | ||
| } | } | ||
| Line 46: | Line 62: | ||
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | // Subscribe to the topic "relay" | + | // Subscribe to the topic "pir" |
| - | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); |
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 53: | Line 69: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | 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 | + | |
| + | // Initialize | ||
| pinMode(PIR_PIN, | pinMode(PIR_PIN, | ||
| pinMode(PIR_LED_PIN, | pinMode(PIR_LED_PIN, | ||
| Line 67: | Line 84: | ||
| void loop() | void loop() | ||
| { | { | ||
| - | // IoT behind the plan work, it should be periodically called | + | |
| - | iot.handle(); | + | delay(200); |
| - | delay(200); | + | |
| - | if(digitalRead(PIR_PIN)) | + | if(onState == true){ |
| - | | + | // This part of the code is executed, when PIR shield is active |
| - | if(pirState == false) | + | if(digitalRead(PIR_PIN)) |
| { | { | ||
| - | digitalWrite(PIR_LED_PIN, | + | |
| - | String msg = String(1); | + | { |
| - | iot.publishMsg(" | + | // When PIR has detected motion, then the LED is switched on and text “Motion detected!” is published to the MQTT broker |
| - | // | + | |
| - | pirState = true; | + | String msg = String(" |
| + | iot.publishMsg(" | ||
| + | // | ||
| + | pirState = true; | ||
| + | } | ||
| } | } | ||
| - | } | + | |
| - | | + | |
| - | { | + | |
| - | if(pirState == true) | + | |
| { | { | ||
| - | digitalWrite(PIR_LED_PIN, | + | |
| - | pirState = false; | + | { |
| + | // PIR shields LED is switched off, when it is not detecting any motion | ||
| + | | ||
| + | pirState = false; | ||
| + | } | ||
| } | } | ||
| + | } | ||
| + | else{ | ||
| + | // When the PIR shield has been switched off, then its offline state is sent to the MQTT broker | ||
| + | iot.log(" | ||
| + | delay(2000); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||