Sigfox Smart Mouse Trap with Messaging and Counters

Using Sigfox solution from Unabiz (an Arduino shield) in a mouse trap to push SMS and email thru AWS IoT core and store count in DynamoDB.

IntermediateFull instructions provided5 hours757
Sigfox Smart Mouse Trap with Messaging and Counters

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
LED (generic)
LED (generic)
Industrial Pushbutton Switch, Push-Pull
Industrial Pushbutton Switch, Push-Pull
Tilt Switch, Encapsulated
Tilt Switch, Encapsulated
9V battery (generic)
9V battery (generic)
Sigfox Unashield
Mouse Trap

Software apps and online services

Arduino IDE
Arduino IDE
Amazon Web Services AWS IoT
AWS DynamoDB
Amazon Web Services AWS DynamoDB
AWS Lambda
Amazon Web Services AWS Lambda


Read more


Mouse trap detector schematics


Mouse Trap Code

#include "SIGFOX.h"
#include <dht.h>
#include <EEPROM.h>
dht DHT;
#define DHT11_PIN 7
//  IMPORTANT: Check these settings with UnaBiz to use the SIGFOX library correctly.
static const String device = "1c88ad";  //  Set this to your device name if you're using UnaBiz Emulator.
static const bool useEmulator = false;  //  Set to true if using UnaBiz Emulator.
static const bool echo = true;  //  Set to true if the SIGFOX library should display the executed commands.
static const Country country = COUNTRY_SG;  //  Set this to your country to configure the SIGFOX transmission frequencies.
// static UnaShieldV2S transceiver(country, useEmulator, device, echo);  //  Uncomment this for UnaBiz UnaShield V2S Dev Kit
static UnaShieldV1 transceiver(country, useEmulator, device, echo);  //  Uncomment this for UnaBiz UnaShield V1 Dev Kit
//////////////////////////////////   Sensor code //////////////////////////////////////////////////////////////////////////
/************************Hardware Related Macros************************************/
#define         MG_PIN                       (0)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (2)  
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier
/***********************Software Related Macros************************************/
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interval(in milisecond) between each samples in 
                                                     //normal operation
/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should derermine this value.
#define         ZERO_POINT_VOLTAGE           (0.324) //define the output of the sensor in volts when the concentration of cnt is 400PPM
#define         REACTION_VOLTGAE             (0.020) //define the voltage drop of the sensor when move the sensor from air into 1000ppm cnt
float           cntCurve[3]  =  {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};   
/*****************************  MGRead *********************************************
Input:   mg_pin - analog channel
Output:  output of SEN-000007
Remarks: This function reads the output of SEN-000007 (Sensor Initialize)
float MGRead(int mg_pin)
    int i;
    float v=0;
    for (i=0;i<READ_SAMPLE_TIMES;i++) {
        v += analogRead(mg_pin);
    v = (v/READ_SAMPLE_TIMES) *5/1024 ;
    return v;  
/*****************************  MQGetPercentage ***********************************
Input:   volts   - SEN-000007 output measured in volts
         pcurve  - pointer to the curve of the target gas
Output:  ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) 
         of the line could be derived if y(MG-811 output) is provided. As it is a 
         logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic 
int  MGGetPercentage(float volts, float *pcurve)
   if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) {
      return -1;
   } else { 
      return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
int ledPin = 1; // choose the pin for the LED
int mouseSensor =3; // choose the input pin (for a mouse sensor)
int switchPB =9;    // choose the input pin (for a reset counter pushbutton)
int rstcntLED =10;    // choose the output pin (for a reset rounter LED)
int state=HIGH;
int state1=HIGH;
int PBState=HIGH;
int lastState=HIGH;
int mouse_count;
int addr = 0;
int val = 0;
/*****************************End of Sensor Initialize**********************************/

/*****************************Called Only Once*****************************************/
void setup() {  
  //  Initialize console so we can see debug messages (9600 bits per second).
  // Pin initialize 
  pinMode(mouseSensor, INPUT);                        //Door sensor or switch to input
  pinMode(switchPB, INPUT);                           //Door sensor or switch to input
  pinMode(rstcntLED, OUTPUT);                         //Reset counter LED
  //  Initialize sigfox transmission.
  Serial.println(F(" setup..."));
  //  Check whether Running the SIGFOX module is functioning.
  if (!transceiver.begin()) stop(F("Unable to init SIGFOX module, may be missing"));  //  Will never return.
  //  Send a raw 12-byte message payload to SIGFOX.  In the loop() function we will use the Message class, which sends structured messages.
  //  Delay 10 seconds before sending next message.
  Serial.println("Waiting 10 seconds...");
  //  Interrupt function when mouse detected.
  attachInterrupt(digitalPinToInterrupt(mouseSensor), mouse, FALLING);
  //  Read EEPROM stored data.
  val = EEPROM.read(addr);
  mouse_count = val;

/*************************Called Every Time Mouse Sensor Triggered***********************/
 void mouse() {
  //// Running counter every time mouse detected and stored it in EEPROM.
    if (state==LOW){
    Serial.print( "Total Mouse Caught To Date:  " );
    EEPROM.write(addr, mouse_count);

/***********************Continous Routine***********************************************/
void loop() {  //  Will be called repeatedly.

  ///////Output voltage values from DHT11
    int percentage;
    float volts;        
    volts = MGRead(MG_PIN);
    Serial.print( "SEN0159: " );
    Serial.print( " V           " );  

  //  Send message counter, temperature and voltage as a SIGFOX message, up to 10 times.
    static int counter = 0, successCount = 0, failCount = 0;  //  Count messages sent and failed.
    Serial.print(F("\nRunning loop #")); Serial.println(counter);

  //  Get temperature and humidity reading from DHT11 then send packet using sigfox
    mouse_count = val;

    if (PBState==LOW){ EEPROM.write(addr, 0);
    digitalWrite(rstcntLED, HIGH);
    }else {digitalWrite(rstcntLED, LOW);}
    if (state1==LOW){    
    val = EEPROM.read(addr);
    mouse_count = val;     
    int temperature;  int humidity;
    int chk = DHT.read11(DHT11_PIN);
    Serial.print("***** Temperature = ");
    temperature = DHT.temperature;
    Serial.print("***** Humidity = ");
    humidity = DHT.humidity;

  //  Convert the numeric counter, temperature and voltage into a compact message with binary fields.
    Message msg(transceiver);  //  Will contain the structured sensor data.
    msg.addField("cnt", mouse_count);  //  4 bytes for the counter.
    msg.addField("tmp", temperature);  //  4 bytes for the temperature.
    msg.addField("hum", humidity);  //  4 bytes for the voltage.
  //  Total 12 bytes out of 12 bytes used.

  //  Count the message success and failure.

    if (msg.send()) {
    successCount++;  //  If successful, count the message sent successfully.
  } else {
    failCount++;  //  If failed, count the message that could not be sent.

  //  Send only 10 messages and stop transmitting.
    if (counter >= 2) {
    //  If more than 10 times, display the results and hang here forever.
    stop(String(F("Messages sent successfully: ")) + successCount +
                 F(", failed: ") + failCount);  //  Will never return.

    Serial.print("Total Mouse Caught To Date:  ");
  //  Delay 10 minutes before sending next message. 
    Serial.println("Waiting 10 minutes...");




4 projects • 1 follower
Just a hobby :)


Add projectSign up / Login