This is an old revision of the document!


PIR example

The code above will show if PIR detector detect any movement.

/*
 * IoT PIR example
 *
 * This example subscribe to the "relay" topic. When a message received, then it
 * will switch relay
 *
 * Created 21 Juni 2017 by Heiko Pikner
 */
 
#include <Arduino.h>
#include <ittiot.h>
 
//Pin definition for the relay (GPIO5)
#define PIR_PIN D5
#define PIR_LED_PIN D4
 
bool pirState;
 
// If message received switch relay. For example:
// mosquitto_pub -u test -P test -t "ITT/IOT/3/relay" -m "1"
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
  if(msg == "1")
  {
 
  }
 
  if(msg == "0")
  {
 
  }
}
 
// Function started after the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  // Subscribe to the topic "relay"
  iot.subscribe("pir");
  iot.log("IoT PIR example!");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  // Print json config to serial
  iot.printConfig();
  // Initialize IoT library
  iot.setup();
  // Initialize relay pin
  pinMode(PIR_PIN, INPUT);
  pinMode(PIR_LED_PIN, OUTPUT);
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
  delay(200);
 
  if(digitalRead(PIR_PIN))
  {
    if(pirState == false)
    {
      digitalWrite(PIR_LED_PIN, HIGH);
      String msg = String(1);
      iot.publishMsg("pir", msg.c_str());
      //Serial.println(msg);
      pirState = true;
    }
  }
  else
  {
    if(pirState == true)
    {
      digitalWrite(PIR_LED_PIN, LOW);
      pirState = false;
    }
  }
}
en/iot/examples/pir.1519742912.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