TaCode Tuesday: How to Create a Smart Doorbell with IoT and Zang

I’ve been working on a project recently that entails using internet of things (IoT) devices interacting with communications development platform Zang Cloud. Recently I purchased a home and realized I wanted to automate different functions using IoT devices. I have a development background in web development (mostly PHP, Python and SQL) so I decided to put those skills to the test.

My first IoT project (or first thing I decided to automate in my home) was a smart doorbell. We’re all too familiar with devices like Ring, SkyBell, and August, to name a few. Although these are great devices, they lack fundamental features I’m looking for, like the ability to save to on-premise network storage.

Most of these devices are subscription based and require you to pay monthly for a cloud service which gives you access to a custom app and storage of all of your recordings from the doorbell. These doorbells not only lack features but the upfront and monthly subscription cost is expensive.

That’s when I decided to make my own smart doorbell at half of the price — and the final result looks pretty good overall.

On the market today there is a micro computer called a Raspberry Pi Zero W which is half of the size of a credit card (not kidding, pictures below) and has full wireless capability. I decided to use this device to interact with my pushbutton, as its small enough that it can fit into my housing.

My goal is to use Zang Cloud SMS API’s to send an SMS notification every time someone pushes the doorbell. The reason I’m using SMS for this is because its native on all phones and does not require the end user to download and install a third party app. The smart doorbell program is coded using Python.

I’ve added some additional features that will play a doorbell sound over Sonos speakers throughout the house, however please change the sonos.play_uri to another web server URL, I’m using my own web server to host the doorbell MP3 file. Also remove the Account SID and Auth Token.

In this article I will only be talking about the software side of things. Even though my code shows how to interact with the GPIO pins that will be connected to the button, it does not explain how to connect the jumper cables to the button.

Please visit Aidafruit to purchase the button I used and instructions on connecting it via GPIO to the Pi. The raspberry Pin out datasheet can be found at raspberry-projects.com.

Now, let’s get started!

First we need to import all the modules we’ll be using. The ones to note are SoCo which is a modules that allows Python to interact with Sonos, Zang connector modules that will allow us to send the SMS using the python helper (you will need to follow these instructions in order to import the Python Zang helpers: https://github.com/zang-cloud/zang-python) and picamera to interact with a camera attached to the Pi (we will be talking about that another time).

import os

import httplib, urllib
import RPi.GPIO as GPIO
import time, sys
import picamera
import socoimport cv2
from datetime import date
from soco import SoCo
from zang.exceptions.zang_exception import ZangException
from zang.configuration.configuration import Configuration

from zang.connectors.connector_factory import ConnectorFactory

Once you have the modules installed into Python using PIP and imported using the code above we can start working on mapping the GPIO pins. In the case of my pushbutton I got a special button that allows the colors in the middle LED ring to change as you add power to any combination of the RGB pins found on the back of the button.

The following code adds the correct amount of voltage to each pin to make it light up. I chose two colors; white when the button is on and idle and light blue when the button is pushed down. Essentially you can make any color by turning on RGB pins or any combination of the three. More on this can be found here.

Ready to move forward? The following is the code for the GPIO pinout and to allow the button to be detected when pushed:

#switch pins set using built in pull up resistor plus a hardware 10K resistor to ground
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#color pins set by GPIO.BCM
redPin = 17
greenPin = 22
bluePin = 27
 
#setting code for colors, can customize for your own colours
#output uses GPIO.LOW because my LED is an anode
def turnOn(pin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
 
def turnOff(pin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
 
def cyanOn():
turnOn(greenPin)
turnOn(bluePin)
turnOff(redPin)
 
def whiteOn():
turnOn(redPin)
turnOn(greenPin)
turnOn(bluePin)

Please note: for the purpose of this tutorial the most important part is:

 GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

This line of code is use to detect if the button has been pushed down or not. The color LED is optional but this line of code will allow our doorbell to function.

The next function we will be focusing on is Zang SMS function, as you remember we imported all the modules so now all we need to do is use the send SMS example Zang provided us with to send an SMS. This function does not send the SMS quite just yet. This is just for the credentials that can be obtained when you sign up for a Zang account.

#code for activating sms using zang

sid = ‘Account SID HERE’
authToken = ‘Auth Token HERE’
url = ‘https://api.zang.io/v2’

configuration = Configuration(sid, authToken, url=url)
smsMessagesConnector = ConnectorFactory(configuration).smsMessagesConnector 

The next module is the optional Sonos function. This function allows all the Sonos speakers connected to your home network to play a doorbell sound:

#code for activating sonos speakers for doorbell

if __name__ == ‘__main__’:
sonos = soco.discover() # Pass in the IP of your Sonos speaker
# You could use the discover function instead, if you don’t know the IP

Finally we will show you all the logic. Firstly I activate the white LED on the button, then I detect if the button is pushed if it is I change the color to light blue, play the MP3 sound and then send an SMS using Zang. Finally after the SMS is sent I change the color back to white to indicate that the button has been pushed. Please change the TO and FROM phone numbers. The FROM phone number should be a phone number you purchased on the Zang portal. The message body can be anything. 

whiteOn()
while True: input_state = GPIO.input(23)
if input_state == False: print(‘Someone is at the door!r’)
cyanOn()
sonos.play_uri(‘https://example.ca/doorbell.mp3’)
sonos.volume += 20
track = sonos.get_current_track_info()
print track[‘title’]
sonos.pause()
#Play a stopped or paused track
sonos.play()
try: smsMessage = smsMessagesConnector.sendSmsMessage(
to=’+14168888888′,
body=’Someone is at the door!’,
from_=’+16478888888′) print(smsMessage)
except ZangException as ze: print(ze)
whiteOn()

FINALLY DONE! So to wrap up below is the final product and the entire code. Use responsibly and email me if you have any questions: phillipk@zang.io 

smart-doorbell-with-iot-built-on-zang

 

Here is the entire program doorbell-zang.py

The first image is the Raspberry Pi Zero W that I will be transferring the project to once I complete all the components. The second image is my development beta test using a larger both of these devices have similar processing power except one is significantly smaller. Also the Raspberry Pi Zero does not have GPIO headers out of the box (because it is so small). You will need to solder them on separately.

smart-doorbell-raspberry-pi smart-doorbell-in-production

Learn more about the potential of IoT and CPaaS or click below to begin building on Zang today.

Views All Time
Views All Time
593
Views Today
Views Today
6