John Masson
Created February 4, 2016

Remote Access Lock Box

Keep your valuables safe, while allowing access to others when you're not around.

Showcase (no instructions)359
Remote Access Lock Box

Things used in this project

Story

Read more

Code

FR6989 LockBox demo

C/C++
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Temboo.h>
#include "TembooAccount.h" // Contains Temboo account information
#include <Servo.h> 


//---------------------SERVO VARIABLES------------------------
Servo myservo;  // create servo object to control a servo  
int pos = 0;    // variable to store the servo position 
int printer = 1;      //counter for print statements
int toggle = 1; //counter to know if servo is OPEN or CLOSED


//---------------------WIFI VARIABLES--------------------------
WiFiClient client;
int maxCalls = 10;
int calls = 0;
int outputPin = P1_0;
String phone = "3053898049";

//-------------------------BUTTON------------------------------
const int buttonPin2 = P1_1;
const int buttonPin = P1_2;     // the number of the pushbutton pin
const int ledPin =  GREEN_LED;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
//------------------------BUUTON BOARD--------------------------
const int left = 28;     // the number of the pushbutton pin
const int leftMid = 27;
const int rightMid = 26;
const int right = 25;
int leftState = 0;
int leftMidState = 0;
int rightMidState = 0;
int rightState = 0;// variable for reading the pushbutton status

int corCode[] = {1,2,3,4};
int userCode[4];


//======================================================================SETUP=================================================================================

void setup() {
  //-----------------------------SERVO SETUPT------------------------------------------------
  myservo.attach(29);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
  myservo.write(0);
  //-----------------------------BUTTON SETUPT-----------------------------------------------  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  delay(500);
  digitalWrite(ledPin, LOW);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(buttonPin2, INPUT_PULLUP);
  //-----------------------------BUTTON BOARD SETUP----------------------------------------- 

  pinMode(left, INPUT_PULLUP);
  pinMode(leftMid, INPUT_PULLUP); 
  pinMode(rightMid, INPUT_PULLUP);
  pinMode(right, INPUT_PULLUP);

  //------------------------------WIFI SETUP-------------------------------------------------
  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("OK");

  // Try to connect to the local WiFi network
  while(wifiStatus != WL_CONNECTED) {
    Serial.print("WiFi:");
    wifiStatus = WiFi.begin(WIFI_SSID, WPA_PASSWORD);

    if (wifiStatus == WL_CONNECTED) {
      Serial.println("OK");
    } 
    else {
      Serial.println("FAIL");
    }
    delay(5000);
  }

  // Initialize pins
  pinMode(outputPin, OUTPUT);
  delay(500);

  //Setup Confirmation
  digitalWrite(ledPin, HIGH);
  digitalWrite(outputPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  digitalWrite(outputPin, LOW);


  Serial.println("Setup complete.\n");
  // lcd.println("Setup complete.\n");
}

//======================================================================LOOP=================================================================================
void loop() {
  //-------reset toggle variable----------------------
  if (toggle > 2)
  {
    toggle = 1;
  }
  //--------LED---------
  digitalWrite(ledPin, HIGH); //turn on GREEN LED
  delay(700);
  digitalWrite(ledPin, LOW); //turn off GREEN LED
  //---------Button Board Read---------------
  leftState = digitalRead(left);
  leftMidState = digitalRead(leftMid);
  rightMidState = digitalRead(rightMid);
  rightState = digitalRead(right);

  //-------Button 1,2 state---------
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  digitalWrite(outputPin, LOW);
  if (printer == 1) 
  {
    Serial.println("Waiting for button press...");
   
  }
  //-----If Button 2 Pressed, go to Twilio call-----------
  if (buttonState == LOW) {     

    digitalWrite(ledPin, HIGH); //turn on GREEN LED
    if (calls < maxCalls) {

      Serial.println("    Calling Twilio...");
      
      //---------------CALL TWILIO--------------------------
      int twil = runGetLastMessageFromNumber();
      if(twil == 1)
      {
        Serial.println("    Correct passcode");
      }
      else if (twil == 0)
      {
        Serial.println("    Incorrect passcode");
        for(int f = 0;f < 5; f++)
        {
          digitalWrite(outputPin, HIGH);
          delay(150);
          digitalWrite(outputPin, LOW);
          delay(150);
        }
      }
      else if (twil == 223)
      {
        Serial.println("    Failure to connect to Twilio");
        for(int f = 0;f < 5; f++)
        {
          digitalWrite(ledPin, HIGH);
          delay(150);
          digitalWrite(ledPin, LOW);
          delay(150);
        }
      }
       
      calls++;

      //---------------CALL SERVO---------------------------
      servoSweep(twil, toggle);
      //If twilio call successful, LED 1 was swithed high. Now, Set it back to LOW
      if (toggle == 1) 
      {
        Serial.println("    OPEN");
      }
      else if (toggle == 2)
      {
        Serial.println("    CLOSE");
      }
      printer = 0;
      if(twil==1)
      {
        toggle++;
      }
    } 
    else {
      Serial.println("Skipping to save Temboo calls. Adjust maxCalls as required.");
      
    }
    delay(3000);
  }
  //--------If button 1 pressed, go to Button Pad-------------------------------
  else if (buttonState2 == LOW)
  {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    int buttonCode = entryCode(); 
    if (buttonCode ==1)
    {
      servoSweep(buttonCode, toggle);
      toggle ++;
    }

  }
  else
  {
    digitalWrite(ledPin, LOW);  // turn LED off
     
  }
  printer++;
  sleep(800);
}



//======================================================================OTHER FUNCTIONS=========================================================================

//------------------------------------------TWILIO-----------------------------------------------------
int runGetLastMessageFromNumber() {
  TembooChoreo GetLastMessageFromNumberChoreo(client);
  //int open = 0;
  // Set Temboo account credentials
  GetLastMessageFromNumberChoreo.setAccountName(TEMBOO_ACCOUNT);
  GetLastMessageFromNumberChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  GetLastMessageFromNumberChoreo.setAppKey(TEMBOO_APP_KEY);

  // Set profile to use for execution
  GetLastMessageFromNumberChoreo.setProfile("TwilioAccount");
  // Set Choreo inputs
  String FromValue = phone;
  GetLastMessageFromNumberChoreo.addInput("From", FromValue);
  // Identify the Choreo to run
  GetLastMessageFromNumberChoreo.setChoreo("/Library/Twilio/SMSMessages/GetLastMessageFromNumber");

  // Run the Choreo
  unsigned int returnCode = GetLastMessageFromNumberChoreo.run();

  // A return code of zero means everything worked
  if (returnCode == 0) {
    while (GetLastMessageFromNumberChoreo.available()) {
      String name = GetLastMessageFromNumberChoreo.readStringUntil('\x1F');
      name.trim();

      if (name == "Response") {
        if (GetLastMessageFromNumberChoreo.findUntil("open", "\x1E")) {
          digitalWrite(outputPin, HIGH);
          returnCode++;
          GetLastMessageFromNumberChoreo.find("\x1E");
        }
      }
      else {
        GetLastMessageFromNumberChoreo.find("\x1E");
      }
    }
  }

  GetLastMessageFromNumberChoreo.close();
  return returnCode;
}

//---------------------------------------------SERVO-----------------------------------------------------
void servoSweep(int j, int t) 

{
  //If correct code was received, move servo 
  if(j == 1)
  {
    //OPEN
    if (t == 1)
    {
      for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
      {                                  // in steps of 1 degree 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);                       // waits 15ms for the servo to reach the position 
      } 
    }
    //CLOSE       
    if (t == 2) 
    {
      for(pos = 90; pos > 0; pos -= 1)
      {
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);
      }
    }
  }
}

//----------------------------------------------BUTTON BOARD CODE------------------------------------------

int entryCode() {
  
  int correct = 0;
  int e = 0;
  getState();
  
  //--------------------------------STATE 1---------------------------
  while(leftState != LOW && leftMidState != LOW && rightMidState != LOW && rightState != HIGH && e < 10000000) 
    {
      getState();
      //e++;
    }
  //FLASH GREEN
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  
  if (leftState == LOW) {
  userCode[0] = 1;
  }
  else
  userCode[0] = 0;
  getState();

  //---------------------------------STATE 2----------------------------
  while(leftState != LOW && leftMidState != LOW && rightMidState != LOW && rightState != HIGH && e < 10000000)
      {
         getState();
         //e++;
      }
      
  //FLASH GREEN
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  
  if (leftMidState == LOW) {
    userCode[1] = 2;
   }
  else {
    userCode[1] = 0;
  }
    getState();
  //------------------------------------STATE 3-----------------------------
  while(leftState != LOW && leftMidState != LOW && rightMidState != LOW && rightState != HIGH && e < 10000000)
    {
      getState();
      //e++;
    }
    

 //FLASH GREEN  
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  
   if (rightMidState == LOW) {
    userCode[2] = 3;
   }
  else {
    userCode[2] = 0;
  }
    getState();
  //-----------------------------STATE 4----------------------------------
  while(leftState != LOW && leftMidState != LOW && rightMidState != LOW && rightState != HIGH && e < 10000000)
      {
        getState();
       // e++;    
      }

 //FLASH GREEN
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
   
   if (rightState == HIGH) {
    userCode[3] = 4;
   }
  else {
    userCode[1] = 0;
  }
    getState();
//--------------------------------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(outputPin, HIGH);
            delay(1000);
            
        }
        else {
          //FLASH GREEN 5 times
          for(int f = 0;f < 5; f++)
              {
                digitalWrite(outputPin, HIGH);
                delay(150);
                digitalWrite(outputPin, LOW);
                delay(150);
              } 
        }
        
      }
  else 
    {
      //FLASH GREEN 5 times
      for(int f = 0;f < 5; f++)
          {
            digitalWrite(outputPin, HIGH);
            delay(150);
            digitalWrite(outputPin, LOW);
            delay(150);
          }
    }
  return correct;
}  

//---------------------------------------GET THE STATE OF THE BUTTON PAD-----------------------------------
void getState()
{
  leftState = digitalRead(left);
  leftMidState = digitalRead(leftMid);
  rightMidState = digitalRead(rightMid);
  rightState = digitalRead(right);

  return;  
}

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.
*/

ReadMe

C/C++
/*

  FR6989 Launchpad Lockbox Demo
  By John Masson 
  08/03/15
  
  This demo consists of a FR6989 launchpad, Fuel Tank BoosterPack, CC3100 Wifi BoosterPack, a homemade button pad, a mini servo motor, and a lock box. 
  
  The purpose of this demo is to use the FR6989 launhcpad to control a servo motor, which locks and unlocks a lockbox. The user can toggle the lock by entering a code locally on the button pad or 
  texting a code to the Twilio account of the user.
  
  Correct Button Pad Code: LEFT, LEFT MIDDLE, RIGHT MIDDLE, RIGHT
  Correct Code to text to Twilio number (case sensitive): open
  
  To configure this demo for any user, they will need their own Temboo account and twillio account. All changes to the code that are required are:
       1)replacing TembooAccount.h file with one that contains the desired account info and wifi login credentials, (*see how below)
       2)switching the phone number in the phone variable (top of script under WIFI VARIABLES) of DemoWithTwilioAndButtons_v3.ino to the new user's phone number
       
   *How to replace TembooAccount.h  
       1) Create an account with Temboo.com 
       2) Create an account with twilio.com (the phone number they give you will be the one you text in the demo)
       3) go to https://temboo.com/library/Library/Twilio/SMSMessages/GetLastMessageFromNumber/ and switch on IoT Mode in the top right corner
          -  Be sure to select TI Launchpad, and input the correct wifi credentials for the CC3100 boosterpack  
       4) Fill out "input" section 
         - only fill out AccountSID and AuthToke, dont worry about the "From" field in the input section (you should've alread taken care of that - see above) 
       5) Scroll down and copy the code from the Header File that Temboo generates for you and paste it over the TembooAccount.h file in this project
 
       
  Set up:
  1) Attach wifi BoosterPack to the top of the laucnhpad
  2) Attach Fuel Tank booster pack to the bottom of the launchpad
  3) Attach servo VCC and ground wires to button pad's power node (1 of 2 left-side pin headers) and ground node (1 of 2 right-side pin headers).
  4) Attach servo's PWM wire (yellow wire) to pin 29 on wifi boosterpack (P9.5 on launhcpad)
  5) Attach button pad's Vcc (1 of 2 left-side pin headers) and ground (1 of 2 right-side pin headers) to the 3V3 vcc and ground on launchpad via female jumper wires
  6) Attach button pad's yellow wire bus to pins (28-25) on wifi boosterpack
    - orient the yellow wire bus so that the wire with the electrical tape insulation (not heat shrink) is attached to pin 28. In other words, the left most button should attach to pin 28, the left middle button 
      should attach to pin 27 etc.
  
  
  Demo Walkthourgh and LED indicator guide:
  ** when it come time showcase this demo, there will be various LED indicators to let the user know the demo's status  
  
  1) When you power up the demo, there will be 1 GREEN LED blink and the servo will set it's position to completely horizontal 
  2) Once it is connected to the Wifi, BOTH the GREEN and RED LEDs with turn on , then off  simeoutlaneously. 
  3) Once set up is complete, and the launchpad is waiting for user input, the GREEN LED will periodically flash until Button 1 or Button 2 ON THE LAUCHPAD (not seperate button pad) are pressed
  4) If the user wishes to enter a code on the Button Pad, press and hold Button 1 on the launchpad until GREEN LED is constant, not flahsing:
    - OnceEnter 4 digit code on button pad. Every button press will be followed by a GREEN LED blink
    b) If the correct code is entered, there will be a apporx. 2 second long RED LED flash, and the servo will rotate
    c) If the incorrect code is entered, there will be 5 short RED LED flashes, 
    d) After any 4 digit code is entered, the system will return to Step 3 of walkthough
  5) If the user wishes to check if Twilio has recieved the correct code (must be sent beforehand), press and hold Button 2 on the launchpad until the GREEN LED is constant, not flashing. 
    - The GREEN LED will remian on until 1 of 3 things happens
    a) If the right code was sent to Twilio, there will be a approx. 2 second long RED LED flash and the servo will rotate
    b) If the wrong code was sent to Twilio, there will be 5 short RED LED flashes
    c) If there was an issue connecting to Twilio (usually poor wifi Connection), there will be 5 short GREEN LED flahes
    d) After any of these occurences, the system will return to Step 3 of walkthough 
   
*/

Credits

John Masson
4 projects • 8 followers

Comments