Phillip Smart Doorbell IoT Project – final

综合编程 2018-03-13

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.

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: https://www.adafruit.com/product/3423 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: (http://www.raspberry-projects.com/pi/wp-content/uploads/2016/05/rpi_zero_io_pinouts.jpg?_sm_au_=i5Vts1Lf1K1MZSRs)

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 entire code. Use responsibly and email me if you have any questions:phillipk@zang.io

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.

Learn more about the potential ofIoT and CPaaSor click the button to begin building on Zang today.

您可能感兴趣的

Python | 数据分析实战Ⅰ “Talk is cheap,Show me the code.”翻译为中文是“废话少说,放码过来。”我觉得可谓信达雅。 在编程之路上,实践的重要性无可比拟。这也是很多同学感觉学了很多,但还是不会写代码的原因;也是很多有意转行的人士,自学了大半年,仍不见起色的缘故。 le...
Learning ML – From theory to practice I got interested in ML some time ago and was looking for some comprehensive introduction. The Machine Learning course taught by prof. Andrew Ng seeme...
Python爬虫基本原理 爬虫基本原理 1. 什么是爬虫 请求网站并 提取 数据的 自动化 程序。 2. 爬虫基本流程 发起请求 通过HTTP库向目标站点发起请求,即发送一个Request,请求可以包含额外的headers等信息,等待服务器响应。 获取响应 如果服务器能正常响应,会得到...
实用又强大,6 款 Python 时间& 日期库推荐... 在使用 Python 的开发过程中,除了使用 datetime 标准库来处理时间和日期,还有许多第三方的开源库值得尝试。 1、Arrow Arrow 是一个专门处理时间和日期的轻量级 Python 库,它提供了一种合理、智能的方式来创建、操作、格式化、转换时间和日期,并提供了一个支持许...
Appium python自动化测试系列之Android UIAutomator终极定位(七)... android uiautomator text定位 可能有人不知道为什么说android uiautomator是终极定位,而且android uiautomator和appium有什么关系呢?如果之前写了一些脚本,发现有的时候一些元素不能够很好的定位时,那么就需要采用android uiau...