Burglary and tamper detector for door and window



  • Hello,

    I develop for some time sensors for this purpose.
    And I think now I have a variant found.
    The technology is nothing special.
    I simple use a MMA8452 connect to a Mysensors Note.
    The evaluation does the Sketch.
    Here my Sketch:

    /******************************************************************************
    MMA8452Q_Basic.ino
    SFE_MMA8452Q Library Basic Example Sketch
    Jim Lindblom @ SparkFun Electronics
    Original Creation Date: June 3, 2014
    https://github.com/sparkfun/MMA8452_Accelerometer
    
    This sketch uses the SparkFun_MMA8452Q library to initialize the
    accelerometer, and stream values from it.
    
    Development environment specifics:
    	IDE: Arduino 1.0.5
    	Hardware Platform: Arduino Uno
    	
    	**Updated for Arduino 1.6.4 5/2015**
    
    This code is beerware; if you see me (or any other SparkFun employee) at the
    local, and you've found our code helpful, please buy us a round!
    
    Distributed as-is; no warranty is given.
    ******************************************************************************/
    #include <Wire.h> // Must include Wire library for I2C
    #include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library
    #include <MySensor.h>  
    #include <Vcc.h>
    
    const float VccMin   = 0.0;           // Minimum expected Vcc level, in Volts.
    const float VccMax   = 5.5;           // Maximum expected Vcc level, in Volts.
    const float VccCorrection = 5.0/5.0;  // Measured Vcc by multimeter divided by reported Vcc
    
    unsigned int countr = 0;
    float peakMax0 = -1000.0;
    float peakMax1 = -1000.0;
    float peakMax2 = -1000.0;
    unsigned long startMillis = 0;
    Vcc vcc(VccCorrection);
    
    #define CHILD_ID_BATT 0
    #define CHILD_ID_MAXX 1
    #define CHILD_ID_MAXY 2
    #define CHILD_ID_MAXZ 3
    
    MySensor gw;
    MyMessage battmsg(CHILD_ID_BATT, V_VOLTAGE);
    MyMessage maxxmsg(CHILD_ID_MAXX, V_LEVEL);
    MyMessage maxymsg(CHILD_ID_MAXY, V_LEVEL);
    MyMessage maxzmsg(CHILD_ID_MAXZ, V_LEVEL);
    
    
    MMA8452Q accel;
    
    void setup()
    {
      Serial.begin(115200);
      accel.init(SCALE_2G, ODR_800);
      gw.begin(NULL, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("MMA8452", "1.0");
    
      // Register all sensors to gateway (they will be created as child devices)
      gw.present(CHILD_ID_BATT, S_POWER);
      gw.present(CHILD_ID_MAXX, S_VIBRATION);
      gw.present(CHILD_ID_MAXY, S_VIBRATION);
      gw.present(CHILD_ID_MAXZ, S_VIBRATION);
    }
    
    void loop()
    {     
           countr = 0;
           float peakToPeak0 = 0.0;   // peak-to-peak level
           float peakToPeak1 = 0.0;   // peak-to-peak level
           float peakToPeak2 = 0.0;   // peak-to-peak level
           
           float signalMax0 = -100.0000;
           float signalMin0 = 100.0000;
           float signalMax1 = -100.0;
           float signalMin1 = 100.0;
           float signalMax2 = -100.0;
           float signalMin2 = 100.0;
         
           
           while (countr < 20)
               {
                gw.process();
              if (accel.available())
                 {
                  accel.read();
                  delay(2);
                  double accxx = accel.cx;
                  double accyy = accel.cy;
                  double acczz = accel.cz;
                     //--------------------------------------------------
                     if (accxx > signalMax0)
                      {
                         signalMax0 = accxx;  // save just the max levels
                      }
                     if (accxx < signalMin0)
                      {
                         signalMin0 = accxx;  // save just the min levels
                      }
                     //--------------------------------------------------
                     if (accyy > signalMax1)
                      {
                         signalMax1 = accyy;  // save just the max levels
                      }
                      
                     if (accyy < signalMin1)
                      {
                         signalMin1 = accyy;  // save just the min levels
                      }
                     //-------------------------------------------------
                     if (acczz > signalMax2)
                      {
                        signalMax2 = acczz;  // save just the max levels
                      }
                      
                     if (acczz < signalMin2)
                      {
                        signalMin2 = acczz;  // save just the min levels
                      }
                      countr = countr + 1;
                 }
             }
                 //------------------------------------------------------
                 peakToPeak0 = signalMax0 - signalMin0;
                 peakToPeak1 = signalMax1 - signalMin1;
                 peakToPeak2 = signalMax2 - signalMin2;
                 //-----------------------------------------------------
                 //------------------------------------------------------
                 if (peakToPeak0 > peakMax0)
                 {
                    peakMax0 = peakToPeak0;  // save just the max levels
                 }
                 if (peakToPeak1 > peakMax1)
                 {
                    peakMax1 = peakToPeak1;  // save just the max levels
                 }
                 if (peakToPeak2 > peakMax2)
                 {
                    peakMax2 = peakToPeak2;  // save just the max levels
                 }
                 //-----------------------------------------------------
                 
     if (millis() > startMillis)
     {
     float v = vcc.Read_Volts();
      gw.send(battmsg.set(v,2));
      gw.send(maxxmsg.set(peakMax0,4));
      gw.send(maxymsg.set(peakMax1,4));
      gw.send(maxzmsg.set(peakMax2,4));
      
     peakMax0 = -1.0;
     peakMax1 = -1.0;
     peakMax2 = -1.0;
     startMillis= millis() + 15000;
     }
     
    }
    

    Improvements and suggestions are welcome.

    regards
    Thomas


Log in to reply
 

375
Online

6.9k
Users

7.8k
Topics

82.8k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.