shramik salgaonkar
Published © GPL3+

Smart Lock and Doorbell Using Cayenne

A smart lock and doorbell for your smart home using the Cayenne IoT platform.

IntermediateWork in progress3,241
Smart Lock and Doorbell Using Cayenne

Things used in this project

Story

Read more

Code

Arduino Mega

Arduino
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>
#include <LiquidCrystal.h>
#include <Servo.h>

Servo myservo;

const int rs = 30, en = 32, d4 = 22, d5 = 24, d6 = 26, d7 = 28;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

int a;
int time_close;
int door_open;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);

#include <Keypad.h>
#define Password_Lenght 5
char Data[Password_Lenght];
char Master_In[Password_Lenght] = "*123";
char Master_Out[Password_Lenght] = "#123";
char Master1_In[Password_Lenght] = "*346";
char Master1_Out[Password_Lenght] = "#346";
char Master2_In[Password_Lenght] = "*165";
char Master2_Out[Password_Lenght] = "#165";
char Master3_In[Password_Lenght] = "*132";
char Master3_Out[Password_Lenght] = "#132";
char door[] = "A";
char key[1];
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
int xx;
int b;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 9, 10};
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
  Serial.begin(9600);// initialize the Serial
  EspSerial.begin(115200);
  pinMode(11, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  delay(10);
  lcd.begin(16, 2);
  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
  //myservo.attach(12);
  Cayenne.virtualWrite(99, 1, "digital_sensor", "d");
}
void loop()
{
  Cayenne.loop(10);
  lcd.setCursor(0, 0);
  lcd.print(" SMART DOORBELL!");
  if (b == 0)
  {
    lcd.setCursor(0, 1);
    lcd.print(" USING CAYENNE");
  }
  if (!digitalRead(11) && (door_open == 0))
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println("Door Open");
    myservo.write(90);
    digitalWrite(13, HIGH);
    door_open = 1;
    b = 0;
  }
  //lcd.setCursor(0, 1);
  //lcd.print(" WELCOME HOME!");
  //Serial.print("Enter Password");
  customKey = customKeypad.getKey();
  key[0] = customKey;
  if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  {
    Data[data_count] = customKey; // store char into data array

    data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
    a++;
    if (a == 1)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("*");
      b = 1;

    }
    if (a == 2)
    {
      lcd.setCursor(0, 1);
      lcd.print("**");
    }
    if (a == 3)
    {
      lcd.setCursor(0, 1);
      lcd.print("***");
    }
    if (a == 4)
    {
      lcd.setCursor(0, 1);
      lcd.print("****");
      b = 0;
    }
    if (!strcmp(Data, "A"))
    {
      lcd.clear();
      Cayenne.virtualWrite(5, 1, "digital_sensor", "d");
      scrollInFromRight(0, "Ringing");
      delay(1000);
      Cayenne.virtualWrite(5, 0, "digital_sensor", "d");
      clearData();
      b = 0;
    }
  }

  if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  {
    lcd.clear();
    Serial.print("Password is ");


    if (!strcmp(Data, Master_In)) {
      Serial.print("In 1");
      time_close = 0;
      //myservo.write(90);
      digitalWrite(13, HIGH);
      lcd.print( "Welcome ROY");
      lcd.setCursor(0, 1);
      lcd.print("Door Open");
      while ((time_close < 1000) && (digitalRead(11)))
      {
        time_close ++;
        Serial.println(time_close);

      }
      lcd.clear();
      digitalWrite(13, LOW);
      myservo.write(0);
      Cayenne.virtualWrite(1, 1, "digital_sensor", "d");

    }
    else if (!strcmp(Data, Master_Out)) {
      lcd.print( "Goodbye ROY");
      Cayenne.virtualWrite(1, 0, "digital_sensor", "d");
      door_open = 0;
      //myservo.write(90);
      digitalWrite(13, LOW);
    }

    //User 1
    else if (!strcmp(Data, Master1_In)) {
      Serial.print("In 2");
      time_close = 0;
      //myservo.write(90);
      digitalWrite(13, HIGH);
      lcd.print( "Welcome TOM");
      lcd.setCursor(0, 1);
      lcd.print("Door Open");
      while ((time_close < 500) && (digitalRead(11)))
      {
        time_close ++;
        Serial.println(time_close);
      }
      // myservo.write(0);
      digitalWrite(13, LOW);
      Cayenne.virtualWrite(2, 1, "digital_sensor", "d");
    }
    else if (!strcmp(Data, Master1_Out)) {
      lcd.print("Goodbye TOM");
      Cayenne.virtualWrite(2, 0, "digital_sensor", "d");
      door_open = 0;
      //myservo.write(0);
      digitalWrite(13, LOW);
    }

    //User 2
    else if (!strcmp(Data, Master2_In)) {
      time_close = 0;
      //myservo.write(90);
      digitalWrite(13, HIGH);
      lcd.print( "Welcome Jerry");
      lcd.setCursor(0, 1);
      lcd.print("Door Open");
      while ((time_close < 500) && (digitalRead(11)))
      {
        time_close ++;
        Serial.println(time_close);
      }
      //myservo.write(0);
      digitalWrite(13, LOW);
      Cayenne.virtualWrite(3, 1, "digital_sensor", "d");

    }
    else if (!strcmp(Data, Master2_Out)) {
      lcd.print("Goodbye Jerry");
      Cayenne.virtualWrite(3, 0, "digital_sensor", "d");
      door_open = 0;
      //myservo.write(0);
      digitalWrite(13, LOW);

    }

    //User 3
    else if (!strcmp(Data, Master3_In)) {
      time_close = 0;
      //myservo.write(90);
      digitalWrite(13, HIGH);
      lcd.print( "Welcome MIKE");
      lcd.setCursor(0, 1);
      lcd.print("Door Open");
      while ((time_close < 500) && (digitalRead(11)))
      {
        time_close ++;
        Serial.println(time_close);

      }
      //myservo.write(0);
      digitalWrite(13, LOW);
      Cayenne.virtualWrite(4, 1, "digital_sensor", "d");

    }
    else if (!strcmp(Data, Master3_Out)) {
      lcd.print("Goodbye MIKE");
      Cayenne.virtualWrite(4, 0, "digital_sensor", "d");
      door_open = 0;
      //myservo.write(0);
      digitalWrite(13, LOW);
    }

    else {
      delay(1000);
      lcd.print("Access Denied");
    }
    delay(1000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.
    clearData();
    delay(1000);
    lcd.clear();

  }

}
void clearData()
{
  a = 0;
  while (data_count != 0)
  { // This can be used for any array size,
    Data[data_count--] = 0; //clear array for new data
  }
  return;
}

int i = 0;

int j = 0;

int k = 0;

int delayTime2 = 150; // Delay between shifts



void scrollInFromRight (int line, char str1[]) {

  // Written by R. Jordan Kreindler June 2016

  i = strlen(str1);

  for (j = 16; j >= 0; j--) {

    lcd.setCursor(0, line);

    for (k = 0; k <= 15; k++) {

      lcd.print(" "); // Clear line

    }

    lcd.setCursor(j, line);

    lcd.print(str1);

    delay(delayTime2);

  }
}
CAYENNE_IN(6)
{
  int value = getValue.asInt();
  digitalWrite(13, value);
}

Raspberry pi

Python
import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email import encoders

from picamera import PiCamera

from time import sleep

#!/usr/bin/env python

import cayenne.client

import time

x = 0

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.**

MQTT_USERNAME = " "

MQTT_PASSWORD = " "

MQTT_CLIENT_ID = " "

**#email id credential**

email_user = ""

email_password = ""

email_send = ""

subject = 'Smart IoT DoorBell &amp; Lock'

camera = PiCamera()

# The callback for when a message is received from Cayenne.

def on_message (message):

global x

print ( "message received: " + str(message))

# If there is an error processing the message return an error string, otherwise return nothing.

if (message.channel == 4) and (message.value == "1" ):

pic_capture()

x = 1

sleep(2)

# If there is an error processing the message return an error string, other$**

if (message.channel == 5) and (message.value == "1" ):

quit()

def pic_capture ():

sleep(1)

camera.capture( '/home/pi/Cayenne-MQTT-Python/examples/image.jpg')

sleep(1)

msg = MIMEMultipart()

msg[ 'From' ] =email_user

msg[ 'To' ] = email_send

msg[ 'Subject' ] = subject

body = 'You have a new visitor'

msg.attach(MIMEText(body, 'plain' ))

filename = 'image.jpg'

attachment = open(filename, 'rb' )

        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition',"attachment; filename= "+filename)

        msg.attach(part)
        text = msg.as_string()
        server = smtplib.SMTP('smtp.gmail.com',587)
        server.starttls()
        server.login(email_user,email_password)

        server.sendmail(email_user, email_send, text)
        server.quit()

    
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)

i=0
timestamp = 0

while True:
    client.loop()
    if (time.time() > timestamp + 10):
        client.virtualWrite(1,i)
        if x == 1:
                client.virtualWrite(4,0)
                x = 0
        timestamp = time.time()
        i = i+1

Credits

shramik salgaonkar

shramik salgaonkar

1 project • 0 followers

Comments