This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot:examples:pir [2018/02/27 14:37] – created Somepub | en:iot:examples:pir [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PIR example ====== | ====== PIR example ====== | ||
+ | {{: | ||
+ | Needed libraries: | ||
+ | < | ||
+ | |||
+ | The code below will show if PIR detector detect any movement. | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * IoT PIR example | ||
+ | * | ||
+ | * This example subscribe to the " | ||
+ | * will switch the pir module " | ||
+ | * | ||
+ | * Created 21 Febuary 2018 by Heiko Pikner and Mallor Kingsepp | ||
+ | */ | ||
+ | |||
+ | // Includes global variables and librarys that the PIR shield uses | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #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 | ||
+ | bool pirState; | ||
+ | // State that switches PIR on and off | ||
+ | bool onState; | ||
+ | |||
+ | // If message received for PIR topic. For example: | ||
+ | // mosquitto_pub -u test -P test -t " | ||
+ | void iot_received(String topic, String msg) | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.print(topic); | ||
+ | Serial.print(" | ||
+ | Serial.println(msg); | ||
+ | |||
+ | if(topic == MODULE_TOPIC) | ||
+ | { | ||
+ | // Switching the PIR shield on or off, depending what message is received | ||
+ | if(msg == " | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | if(msg == " | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Function started after the connection to the server is established. | ||
+ | void iot_connected() | ||
+ | { | ||
+ | Serial.println(" | ||
+ | // Subscribe to the topic " | ||
+ | iot.subscribe(MODULE_TOPIC); | ||
+ | iot.log(" | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | Serial.println(" | ||
+ | |||
+ | // | ||
+ | // | ||
+ | iot.printConfig(); | ||
+ | iot.setup(); | ||
+ | |||
+ | // Initialize PIR pin | ||
+ | pinMode(PIR_PIN, | ||
+ | pinMode(PIR_LED_PIN, | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | iot.handle(); | ||
+ | delay(200); // Wait 0.2 seconds | ||
+ | |||
+ | if(onState == true){ | ||
+ | // This part of the code is executed, when PIR shield is active | ||
+ | if(digitalRead(PIR_PIN)) | ||
+ | { | ||
+ | if(pirState == false) | ||
+ | { | ||
+ | // When PIR has detected motion, then the LED is switched on and text “Motion detected!” is published to the MQTT broker | ||
+ | digitalWrite(PIR_LED_PIN, | ||
+ | String msg = String(" | ||
+ | iot.publishMsg(" | ||
+ | // | ||
+ | pirState = true; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | if(pirState == true) | ||
+ | { | ||
+ | // PIR shields LED is switched off, when it is not detecting any motion | ||
+ | digitalWrite(PIR_LED_PIN, | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |