John Masson
Created February 4, 2016

Laptop Guard

Ever been worried about your laptops or personal items being stolen? Even if you step away for just a moment? Introducing the Laptop Gaurd

Laptop Guard

Story

Read more

Code

Laptop Guard

C/C++
#include <Keypad.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>

#include <Temboo.h>
#include "TembooAccount.h" // Contains Temboo account information

WiFiClient client;

int numRuns = 1;   // Execution count, so this doesn't run forever
int maxRuns = 100;   // Maximum number of times the Choreo should be executed

//Buzzer and FSR variables 
int sensePin = 28;
int buzzPin = 9;

//button and LED variables 
int buttonPin = PUSH1;     // the number of the pushbutton pin
int ledPin_G =  29;      // the number of the LED pin
int ledPin_R =  30;
int buttonState = 0; 

//Keypad variables
const byte ROWS = 1; //one row
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = { {'2','1','4','3'}};
byte rowPins[ROWS] = {27}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 25, 24, 23}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );




void setup() {
    
    
    Serial.begin(9600);
    delay(100);
    
    //SETUP LEDs
    pinMode(ledPin_G, OUTPUT); 
    pinMode(ledPin_R, OUTPUT);    
    digitalWrite(ledPin_R, LOW);
    digitalWrite(ledPin_G, LOW); 
    
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(buzzPin, OUTPUT);
    //=========================     WIFI       ========================
            int wifiStatus = WL_IDLE_STATUS;
            
               //Determine if the WiFi Shield is present
            //   Serial.print("\n\nShield:");
            //   if (WiFi.status() == WL_NO_SHIELD) {
            //     Serial.println("FAIL");
            
            //     // If there's no WiFi shield, stop here
            //     while(true){Serial.println("no sheild");};
            //   }
            
              Serial.println("OK");
              digitalWrite(RED_LED, HIGH);
              delay(500);
              digitalWrite(RED_LED, LOW);
             
            
              // Try to connect to the local WiFi network
              while(wifiStatus != WL_CONNECTED) {
                Serial.println("WiFi:");
                wifiStatus = WiFi.begin(WIFI_SSID, WPA_PASSWORD);
            //Serial.println("got status");
                if (wifiStatus == WL_CONNECTED) {
                  Serial.println("OK");
                } else {
                  Serial.println("FAIL");
                }
                delay(5000);
              }
            
              Serial.println("Setup complete.\n");
              digitalWrite(RED_LED, HIGH);
              delay(500);
              digitalWrite(RED_LED, LOW);
              delay(500);
              digitalWrite(RED_LED, HIGH);
              delay(500);
              digitalWrite(RED_LED, LOW);

}

void loop() {
digitalWrite(ledPin_R, LOW);
digitalWrite(ledPin_G, HIGH);
delay(250);
digitalWrite(ledPin_G, LOW);
delay(250);
char key = keypad.getKey();
int armed = 0;
int disarm = 0;
    
    if (key) {
        digitalWrite(ledPin_G, LOW);
        digitalWrite(ledPin_R, LOW);
        delay(250);
       
        armed = entryCode(); 
        key = keypad.getKey();
       
        int old_val = analogRead(sensePin);
         Serial.println(old_val);
        
        
        while(armed == 1){
            digitalWrite(ledPin_R, HIGH);    
            delay(200);
            key = keypad.getKey();
          
            if(key){
                disarm = entryCode();
                if(disarm == 1){
                   armed = 0;
                }
             }
        
            int val = analogRead(sensePin);
             Serial.println(val);
         
            if (  (val < (old_val - 75)) || (val > (old_val + 75))   )
                {
                 key = keypad.getKey();
                 digitalWrite(buzzPin, HIGH);
                 digitalWrite(ledPin_R, HIGH);
                 runSendSMS();
                    while(disarm != 1){
                        key = keypad.getKey();
                        digitalWrite(buzzPin, HIGH);
                        digitalWrite(ledPin_R, HIGH);
                        delay(80);
                        key = keypad.getKey();
                        digitalWrite(buzzPin, LOW);
                        digitalWrite(ledPin_R, LOW);
                        delay(80);
                        key = keypad.getKey();
                         
                         Serial.println(val);
                        if(key){
                            digitalWrite(buzzPin, HIGH);
                            digitalWrite(ledPin_R, HIGH);
                            disarm = entryCode();
                            
                            if(disarm == 1){
                                digitalWrite(buzzPin,LOW);
                                digitalWrite(ledPin_R, LOW);
                                armed = 0;
                            }
                         }
                        key = keypad.getKey();
                    }
                   
                    
                }
        }
          key = keypad.getKey();
          
    }
        
} 
  
 //====================================================OTHER FUNCTIONS====================================================
  
  
  
  int entryCode() {
      
      int correct = 0;
      int e = 0;
     char key = keypad.getKey();
     int corCode[] = {1,2,3,4};
     int userCode[4];
      
      //--------------------------------STATE 1---------------------------
      while(!key && e < 10000000) 
        {
          key = keypad.getKey();
          //e++;
        }
      //FLASH GREEN
      digitalWrite(ledPin_G, HIGH);
      delay(500);
      digitalWrite(ledPin_G, LOW);
      
      if (key == '1') {
      userCode[0] = 1;
      }
      else
      userCode[0] = 0;
      key = keypad.getKey();
    
      //---------------------------------STATE 2----------------------------
      while(!key && e < 10000000) 
        {
          key = keypad.getKey();
          //e++;
        }
          
     //FLASH GREEN
      digitalWrite(ledPin_G, HIGH);
      delay(500);
      digitalWrite(ledPin_G, LOW);
      
      if (key == '2') {
        userCode[1] = 2;
       }
      else {
        userCode[1] = 0;
      }
       key = keypad.getKey();
      //------------------------------------STATE 3-----------------------------
      while(!key && e < 10000000) 
        {
          key = keypad.getKey();
          //e++;
        }
        
    
     //FLASH GREEN
      digitalWrite(ledPin_G, HIGH);
      delay(500);
      digitalWrite(ledPin_G, LOW);
      
       if (key == '3') {
        userCode[2] = 3;
       }
      else {
        userCode[2] = 0;
      }
        key = keypad.getKey();
      //-----------------------------STATE 4----------------------------------
      while(!key && e < 10000000) 
        {
          key = keypad.getKey();
          //e++;
        }
    
     //FLASH GREEN
      digitalWrite(ledPin_G, HIGH);
      delay(500);
      digitalWrite(ledPin_G, LOW);
      
       if (key == '4') {
        userCode[3] = 4;
       }
      else {
        userCode[1] = 0;
      }
        key = keypad.getKey();
    //--------------------------------CHECK-----------------------------------
      if (e <10000000)
          { 
            boolean isCorrect = true;
            for (int m = 0; m < 4; m++) {
                 if (corCode[m] != userCode[m]) isCorrect = false;
            }
            if(isCorrect)
            {
                correct = 1;
                digitalWrite(ledPin_G, HIGH);
                delay(1000);
                
            }
            else {
              //FLASH GREEN 5 times
              for(int f = 0;f < 5; f++)
                  {
                    digitalWrite(ledPin_R, HIGH);
                    delay(150);
                    digitalWrite(ledPin_R, LOW);
                    delay(150);
                  } 
            }
            
          }
      else 
        {
          //FLASH GREEN 5 times
          for(int f = 0;f < 5; f++)
              {
                digitalWrite(ledPin_G, HIGH);
                delay(150);
                digitalWrite(ledPin_G, LOW);
                delay(150);
              }
        }
        digitalWrite(ledPin_G, LOW);
        digitalWrite(ledPin_R, LOW);
        
      return correct;
}

void runSendSMS() {
  if (numRuns <= maxRuns) {
    Serial.println("Running SendSMS - Run #" + String(numRuns++));

    TembooChoreo SendSMSChoreo(client);

    // Invoke the Temboo client
    SendSMSChoreo.begin();

    // Set Temboo account credentials
    SendSMSChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendSMSChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set Choreo inputs
    String AuthTokenValue = "fbdb45ef610a6e907410917c30656b11";
    SendSMSChoreo.addInput("AuthToken", AuthTokenValue);
    String BodyValue = "ALERT!! ALERT!! Your laptop is being stolen!!";
    SendSMSChoreo.addInput("Body", BodyValue);
    String ToValue = "3053898049";
    SendSMSChoreo.addInput("To", ToValue);
    String AccountSIDValue = "ACb223c923dee744e363a77d1da6f9a656";
    SendSMSChoreo.addInput("AccountSID", AccountSIDValue);
    String FromValue = "2056145755";
    SendSMSChoreo.addInput("From", FromValue);

    // Identify the Choreo to run
    SendSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS");

    // Run the Choreo; when results are available, print them to serial
    SendSMSChoreo.run();
    
    while(SendSMSChoreo.available()) {
      char c = SendSMSChoreo.read();
      Serial.print(c);
    }
    SendSMSChoreo.close();
  }


}

TembooAccount.h

C/C++
/*
IMPORTANT NOTE about TembooAccount.h

TembooAccount.h contains your Temboo account information and must be included
alongside your sketch. To do so, make a new tab in Energia, call it TembooAccount.h,
and copy this content into it. 
*/

#define TEMBOO_ACCOUNT "jmasson"  // Your Temboo account name 
#define TEMBOO_APP_KEY_NAME "myFirstApp"  // Your Temboo app name
#define TEMBOO_APP_KEY "7868c05db2624a529cb13561439af5fe"  // Your Temboo app key

#define WIFI_SSID "JMasson"
#define WPA_PASSWORD "1oetkuJP"

/* 
The same TembooAccount.h file settings can be used for all Temboo sketches.

Keeping your account information in a separate file means you can share the 
main .ino file without worrying that you forgot to delete your credentials.
*/

Credits

John Masson
4 projects • 8 followers
Thanks to Steph Gedal, Chris Polster, Bernardo Herrera , and John Masson.

Comments