The Making of a Smart Doorbell

') except Exception as e: logging.

warning( 'Removed streaming client %s: %s', self.

client_address, str(e)) else: self.

send_error(404) self.

end_headers() class StreamingServer(socketserver.

ThreadingMixIn, server.

HTTPServer): allow_reuse_address = True daemon_threads = True with picamera.

PiCamera(resolution='640×480', framerate=24) as camera: output = StreamingOutput() #Uncomment the next line to change your Pi's Camera rotation (in degrees) #camera.

rotation = 90 camera.

start_recording(output, format='mjpeg') try: address = ('', 8000) server = StreamingServer(address, StreamingHandler) server.

serve_forever() finally: camera.

stop_recording()#Uncomment the next line if you want to demo the stream#startCamera()Demoing the Video StreamPhoto by James Sutton on UnsplashIt is helpful to test each segment individually before layering the final product.

This will help debug errors more effectively, by reducing the number of dependencies.

To run the video stream, un-comment the last line of the rpi_camera_live_stream.

py, this will invoke the deployment function.

To run the python script run the following command.

python3 rpi_camera_live_stream.

pyOnce the Python script has successfully run, you will have access to the web server that is hosting the camera stream.

Open any browser (Chrome and Firefox were tested successfully) and go to http://localhost:8000.

Text MePhoto by freestocks.

org on UnsplashFor this segment, we will incorporate the communication between the device and the “homeowners” cell phone.

Leveraging Twilio for their Python SMS service.

Install the package by entering the following line into the console.

pip3 install twilioOnce that is installed let us check that everything was installed correctly by checking the version.

Run the next command, and expect a positive response with a version number over 9.

0.

0 unless your installation specified otherwise.

pip3 –version# For more information on Twilio https://www.

twilio.

com/docs/python/install# Import Twiliofrom twilio.

rest import Client# Your Account Sid and Auth Token from twilio.

com/consoleaccount_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'auth_token = 'your_auth_token'client = Client(account_sid, auth_token)# This function will be invoked from our button eventdef text_user(): message = client.

messages.

create(# Message sent to user body="http://10.

245.

203.

106:8000/index.

html", from_='<Youre_Twilio_Number>', to='+<Receiving_Number>' )# If a GUID is logged then the message was sent successfully sent#Message id for debugging on the Twilio event logs print(message.

sid)#text_user()#Uncomment the line above to demoTactile Buttons Are Ringing!The finale joins all the other files into separate threads to avoid interrupts and blocks.

Python supports simultaneous run executions with their multiprocessing library.

In order to host the web server and listen to button events, this library will be crucial to support the prototype architectures integrity.

Without separating the python modules into their own threads the program will interrupt older processes at run time.

Let’s begin by configuring the circuit board to the schematic below.

On one side of the switch, we connect to GPIO pin 10.

The other side of the switch needs to connect to the 5V pin (Refer to your Raspberry Pi GPIO layout) interrupted by a 10k ohm current limiting resistor to protect the input pin.

In production pin 10 will be at 0V until the button is pressed closing the circuit sending 5V.

GPIO pin 10 is constantly listening for input voltages, once the event fires the button_callback function is invoked.

Using the send_SMS module written earlier to send a text message to the phone number specified.

# Import startCamera function from the video stream filefrom rpi_camera_demo import startCamera# Import function to send SMS from the send_SMS.

py file we created earlier from send_SMS import text_user# Import Process from the multiprocessing module in Python standard library from multiprocessing import Process# Import Raspberry Pi's GPIO libraryimport RPi.

GPIO as GPIOdef button_callback(channel): text_user() print("Button was pushed!")GPIO.

setwarnings(False) # Ignore warning for nowGPIO.

setmode(GPIO.

BOARD) # Use physical pin numberingGPIO.

setup(10, GPIO.

IN, pull_up_down=GPIO.

PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)GPIO.

add_event_detect(10,GPIO.

RISING,callback=button_callback) # Setup event on pin 10 rising edge# Define function to start the video stream# This is necessary to initialize multi processingdef Start_Stream(): print('Starting thread for video stream') startCamera()# Assures that the entry point of the program begins hereif __name__ == '__main__': Thread_Target=Process(target=Start_Stream) #Starts Process Thread_Target.

start()#Block the calling thread until the Thread_Target process terminates Thread_Target.

join()message = input("Press enter to quit.") # Run until someone presses enterGPIO.

cleanup() # Clean upTo finally demonstrate the final product make sure that all the independent modules are not invoking themselves.

Then use python3 to invoke the button module.

python3 button_event.

pyResources16.

6.

multiprocessing — Process-based “threading” interface — Python 2.

7.

15 documentationis a package that supports spawning processes using an API similar to the module.

The package offers both local and…docs.

python.

orgThe Twilio Python Helper LibraryTry the Twilio Python Helper Library, our SDK which helps you quickly build Python applications that interact with the…www.

twilio.

compicamera — Picamera 1.

13 DocumentationEdit descriptionpicamera.

readthedocs.

io.

. More details

Leave a Reply