Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:pir [2018/09/05 13:55] – Added needed libraries tomyKalmen: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 <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
  
-//Pin definition for the pir (GPIO14)+#define MODULE_TOPIC "ESP30/pir" 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "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(" payload: ");   Serial.print(" payload: ");
   Serial.println(msg);   Serial.println(msg);
-  if(msg == "1") 
-  { 
- onState = true; 
-  } 
  
-  if(msg == "0")+  if(topic == MODULE_TOPIC)
   {   {
- onState = false;+    // Switching the PIR shield on or off, depending what message is received 
 +    if(msg == "1"
 +    { 
 +     onState = true; 
 +    } 
 +    if(msg == "0"
 +    { 
 +     onState = false; 
 +    }
   }   }
 } }
Line 54: Line 63:
   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);
   iot.log("IoT PIR example!");   iot.log("IoT PIR example!");
 } }
Line 60: Line 69:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // Print json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
-  // Initialize IoT library +  iot.printConfig(); // Print json config to serial 
-  iot.setup(); +  iot.setup(); // Initialize IoT library 
-  // Initialize relay pin+ 
 +  // Initialize PIR pin
   pinMode(PIR_PIN, INPUT);   pinMode(PIR_PIN, INPUT);
   pinMode(PIR_LED_PIN, OUTPUT);   pinMode(PIR_LED_PIN, OUTPUT);
Line 74: Line 84:
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  iot.handle(); +  delay(200); // Wait 0.2 seconds
-  delay(200);+
  
-if(onState == true){ +  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, HIGH); +      if(pirState == false) 
-      String msg = String("Motion detected!"); +      { 
-      iot.publishMsg("pir", msg.c_str()); +        // When PIR has detected motion, then the LED is switched on and text “Motion detected!” is published to the MQTT broker 
-      //Serial.println(msg); +        digitalWrite(PIR_LED_PIN, HIGH); 
-      pirState = true;+        String msg = String("Motion detected!"); 
 +        iot.publishMsg("pir", msg.c_str()); 
 +        //Serial.println(msg); 
 +        pirState = true; 
 +      }
     }     }
-  } +    else
-  else +
-  { +
-    if(pirState == true)+
     {     {
-      digitalWrite(PIR_LED_PIN, LOW); +      if(pirState == true) 
-      pirState = false;+      { 
 +        // PIR shields LED is switched off, when it is not detecting any motion 
 +        digitalWrite(PIR_LED_PIN, LOW); 
 +        pirState = false; 
 +      }
     }     }
   }   }
- } +  else{ 
-else{ +    // When the PIR shield has been switched off, then its offline state is sent to the MQTT broker 
-   iot.log("PIR offline"); +    iot.log("PIR offline"); 
-  delay(2000);+    delay(2000); // Waiting 2 secondes 
 +  }
 } }
-} 
- 
  
 </code> </code>
en/iot/examples/pir.1536155725.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0