RAPIFIRE

Blog

IoT Smart Barn Stall Motion Detector

This blog describes using an Arduino Yun with PIR Motion Sensor (JST) SEN-13285 ROHS to create a smart barn motion detector app using RAPIFIRE. It can be used to notify a farmer that an animal such as a horse or cow is in their stall or not, based on detecting a motion in the stall.

For this app you will need the following items:

  • 1 Arduino Yun
  • 1 PIR Motion Sensor (SEN-13285)
  • 3 jumper cables (purple, grey, and white)
  • 1 red LED
  • 1 resistor 240 ohm
  • 1 breadboard
  • USB cable to connect Arduino to computer
  • Arduino IDE v1.6.7 installed on computer

Okay let's get started. First will assemble the IoT sensor and then will create the Things in RAPIFIRE. The owner Bob the User can then login to the RAPIFIRE dashboard will be to make sure all the horses have returned to their stall after a hard day's work.

  • Connect the purple wire to the VCC pin on the PIR motion sensor and to the +5v on the Arduino Yun.

  • Connect the grey wire to the GND pin on the PIR motion sensor and to the GND pin on the Arduion Yun beside the +5v pin.

  • Connect the white wire to the OUT pin on the PIR motion sensor and to the digital pin 2 on the Arduino Yun.

  • Connect the LED short lead to a hole in a row on the breadboard and LED long lead to the GND pin on the Arduino Yun.

  • Connect the resistor to a hole on the same row on the breadboard as the LED lead and to the pin 13 on the Arduion Yun.

  • Connect the USB cable to the Arduino Yun and to a computer with the Arduino IDE installed. If you don't have the IDE installed see here for instructions on how to do that.

  • Start the Arduino IDE and change the code to this code from Rui Santos.
/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/

int led = 13;                // the pin that the LED is attached to  
int sensor = 2;              // the pin that the sensor is attached to  
int state = LOW;             // by default, no motion detected  
int val = 0;                 // variable to store the sensor status (value)

void setup() {  
   Serial.begin(9600);  
   Serial.println("start steup"); 
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
       // initialize serial
    Serial.println("done steup"); 
}

void loop(){

  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 

    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 

      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}
  • Save the sketch file to C:\arduino on Windows. Otherwise you might get a compile error when pressing the verify button

  • Press the Verify button.

  • Press the Upload button to upload the code to Arduion Yun. Make sure that the COM message at the bottom of the IDE matches the value for the Arduion Yun in the Device manager otherwise you will get an error. In the Tools -> ports menu you can change the value.

  • Open the Serial Monitor in the IDE.

  • Wave your hand in front of the PIM motion sensor. The LED will light

The Serial monitor will show output "Motion detected!" and "Motion stopped!" as you wave your hand in front of the motion detection sensor.

  • Now let's create in RAPIFIRE the thing Barn Stall 1 for the Product Smart Barn. First login here into RAPIFIRE.

  • Click on the Products menu item and add the new Product Smart Barn with a heartbeat of 60 seconds.

  • Click on the Things menu item and add the thing Barn Stall 1 for the product Smart Barn and user Bob the User

  • Click in the Arduino IDE the Sketch -> Include Libaries -> Manage Libaries to show the Library Manager

  • Enter rapifire in the text field of the Library Manager. Click on the Rapfire result and press the Install button. Press the close dialog after download is completed.

Enter pubsub in the text field of the Library Manager. Click on the PubSubClient result and press the Install button. Press the close dialog after download is completed.

  • Click File -> Examples -> MqttRapifireClient -> rapifire-mqtt-simple-publish.

This opens up a separate template file with an example to connect and publish an event to RAPIFIRE.

  • Copy Barn Stall 1's Thing ID into the "YOUR-THING-ID" of define THINGID and the same for the "YOUR-THING-KEY" of define THINGKEY.

  • Merge the code from the example into our other motion detection code.

  • Change the rapifire.addValue() to rapfire.addStringValue(). To see what funcations are available in the rapifire client libary see the file RapifireMqttClient.h installed in libraries folder C:\Users\myuser\Documents\Arduino\libraries\RapifireMqttClient on Window.

Below is the merged code modified.

#include <YunClient.h>
#include <PubSubClient.h>
#include <RapifireMqttClient.h>

#define THING_ID "bPtcklgOLIkhfMJyM6zu0c"
#define THING_KEY "Xf6meY6cZTn3"

YunClient client;

RapifireMqttClient rapifire(THING_ID, THING_KEY, client);  
int counter = 0;

int led = 13;                // the pin that the LED is attached to  
int sensor = 2;              // the pin that the sensor is attached to  
int state = LOW;             // by default, no motion detected  
int val = 0;                 // variable to store the sensor status (value)

void setup() {  
  Serial.begin(9600);
  Serial.println("Start setup");
  Serial.println("Starting bridge...");
  Bridge.begin();
  Serial.println("Bridge ready.");  
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
       // initialize serial
  Serial.println("Done setup"); 
}

void loop(){

    if (!rapifire.connected()) {
    Serial.println(F("Connecting to RAPIFIRE.."));

    if (rapifire.connect()) {
      Serial.println(F("Connected."));
    }
  }

  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 

    if (state == LOW) {
      Serial.println("Motion detected!"); 
       rapifire.addStringValue("Motion", "detected");
       rapifire.publish();
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 

      if (state == HIGH){
        rapifire.addStringValue("Motion", "stopped");
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }


}
  • Press verify and upload to add the code to the Arduino.

  • Wave your hand in front of the motion sensor. The Serial Monitor will show the messages being sent.

  • Open the Barn Stall 1 thing in Rapifire. The Motion messages of detected or stopped will appear in the Raw Data tab

That's all. More things and motion detectors for other stalls 2, 3, 4, etc can be added for this Smart Barn product. Working with sensors and RAPIFIRE has never been easier for you. Old McDonald can now for sure say "...and on his farm he had a cow" with an IoT solution like this.

Author image
About Mark Garzone