Things used in this project

Hardware components:
13711 01
SparkFun ESP8266 Thing - Dev Board
×1
IR based reflective object/proximity sensor.
×1
Servo (generic)
×1
Software apps and online services:
Cayenne logo hi
myDevices Cayenne
Ide web
Arduino IDE

Custom parts and enclosures

IoT Live Trap Cad V1.0
The cad file for the Live Trap. Designt for 4 mm material thickness
iotlivetrap1_0_otvoLzpO2i.svg

Schematics

Servo and Ir connections to the Sparkfun Esp8266 Thing
Trapschema2 ir2tl5k1a6

Code

IoTLiveTrap 0.4Arduino
ArduinoIDE with Esp8266 library and Cayenne library
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <Servo.h>
Servo myservo;

int doormove;
int trapstat; // 1 = active 0 = deactivated
int motion = 0;


// Settings
int posopen = 60; //Door open position
int posclose = 2; //Door close position

// WiFi network info.
char ssid[] = "XXXXXXXXX"; //Your SSID
char wifiPassword[] = "XXXXXXXXXX"; // Your WifiPassword

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "XXXXXXXXXXXXXXXXXXXXXXX";
char password[] = "XXXXXXXXXXXXXXXXXXXXXXX";
char clientID[] = "XXXXXXXXXXXXXXXXXXXXXXX";
// Settings End

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(4, OUTPUT);   // Servo. You can use every PWM output pin
  pinMode(5, OUTPUT);   // Led. You can use any another pin
  pinMode(12, INPUT_PULLUP);    // Proximity Sensor. You can use any another pin
  attachInterrupt(digitalPinToInterrupt(12), motiondetect, FALLING); //Ir detection- pin 12 going Low and jumps to function motiondetect. Must be the same pin as for the proximity sensor
  
}

void loop() {
Cayenne.loop();

if (motion == 1)
    {
    digitalWrite(5, HIGH); // Onboard Led on
    Cayenne.virtualWrite(11, HIGH, "digital_sensor", "d");
    Cayenne.virtualWrite(13, 1);
         
         if (trapstat == 1)
         {
         doormove = (0);     
         door(doormove);
         trapstat = 0;
         Cayenne.virtualWrite(1, LOW, "digital_sensor", "d"); // Set Cayenne Button "Open/close Door"to low
         Cayenne.virtualWrite(2, LOW, "digital_sensor", "d"); // Set Cayenne Button "Activate Trap"to low
         Cayenne.virtualWrite(12, HIGH, "digital_sensor", "d"); // Set Cayenne Trigger "You got a mouse" to high
         }
    
    motion = 0; 
    delay(500);
    digitalWrite(5, LOW); // Onboard Led off
    Cayenne.virtualWrite(11, LOW, "digital_sensor", "d");
    Cayenne.virtualWrite(13, 0);
     
    }


}

void door(int doormove) {
  int pos;
  if (doormove == 1 )
  {
      Cayenne.virtualWrite(1, HIGH, "digital_sensor", "d"); // Set Doorstatus to open
      pos=(posopen); // Door open 
  }
  else
  {
      Cayenne.virtualWrite(1, LOW, "digital_sensor", "d"); // Set Doorstatus to close
      pos=(posclose); // Door close 
  }    
      
      myservo.write(pos);
      myservo.attach(4);  // attaches the servo on GIO4 to the servo object
      delay(300);
      myservo.detach();  // detaches the servo on GIO4 to the servo object  
      Cayenne.virtualWrite(10, (doormove), "digital_sensor", "d");
                      }

void motiondetect() {
      motion = (1);
                    }

CAYENNE_IN(1) // Control Door open/close vis Cayenne channel 1
  {
    String value;
    value = (String)getValue.asString();

    if (value == "1")
    {
    doormove = (1);
    door(doormove);
    Cayenne.virtualWrite(12, LOW, "digital_sensor", "d"); // Set Cayenne Trigger "You got a mouse" to low
    }
    else
    {
    doormove = (0);
    door(doormove);   
    }
  
  }
CAYENNE_IN(2) // Trap activ/inactiv Cayenne channel 2
  {
    String stat;
    stat = (String)getValue.asString();

    if (stat == "1")
    {
    trapstat = 1;
    digitalWrite(5, HIGH); // Onboard Led on
    }
    else
    {
    trapstat = 0;
    digitalWrite(5, LOW); // Onboard Led off 
    }
  
 }
  

Credits

B4f70eb5e890f57755ae7a91b5cbbe57
Alain Mauer
7 projects • 12 followers
Love to find solutions
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog