This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:pir [2018/09/05 13:55] – Added needed libraries tomyKalm | en:iot:examples:pir [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 17: | Line 17: | ||
*/ | */ | ||
+ | // Includes global variables and librarys that the PIR shield uses | ||
#include < | #include < | ||
#include < | #include < | ||
- | //Pin definition for the pir (GPIO14) | + | #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) | + | //Pin definition for the PIR LED (GPIO16) |
#define PIR_LED_PIN D4 | #define PIR_LED_PIN D4 | ||
Line 38: | Line 43: | ||
Serial.print(" | Serial.print(" | ||
Serial.println(msg); | Serial.println(msg); | ||
- | if(msg == " | ||
- | { | ||
- | onState = true; | ||
- | } | ||
- | if(msg == " | + | if(topic == MODULE_TOPIC) |
{ | { | ||
- | onState = false; | + | // Switching the PIR shield on or off, depending what message is received |
+ | if(msg == " | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | if(msg == " | ||
+ | { | ||
+ | onState = false; | ||
+ | } | ||
} | } | ||
} | } | ||
Line 54: | Line 63: | ||
Serial.println(" | Serial.println(" | ||
// Subscribe to the topic " | // Subscribe to the topic " | ||
- | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); |
iot.log(" | iot.log(" | ||
} | } | ||
Line 60: | 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 74: | Line 84: | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | + | |
- | iot.handle(); | + | delay(200); |
- | delay(200); | + | |
- | if(onState == true){ | + | |
- | + | // This part of the code is executed, when PIR shield is active | |
- | if(digitalRead(PIR_PIN)) | + | if(digitalRead(PIR_PIN)) |
- | { | + | |
- | if(pirState == false) | + | |
{ | { | ||
- | digitalWrite(PIR_LED_PIN, | + | |
- | String msg = String(" | + | { |
- | 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 |
- | | + | |
- | delay(2000); | + | delay(2000); |
+ | } | ||
} | } | ||
- | } | ||
- | |||
</ | </ |