Voice– and SMS–Enabled Light Sensor Using Raspberry Pi and Twilio


The Hacker School space for our batch had two restrooms: one attached to the main work area, and one downstairs. We thought it would nice to know if the bathroom is occupied before taking the time to walk down.

Our project makes it possible to check the bathroom status by phone or text message.

Voice– and SMS–Enabled Light Sensor Using Raspberry Pi and TwilioBathroom occupancy status is determined using a light sensor attached to a Raspberry Pi. If the lights are on in the bathroom, we assume that the bathroom is occupied.

We created a Heroku–hosted web application that accepts periodic bathroom state updates from the Raspberry Pi and handles incoming requests from Twilio. When a user calls or texts the Twilio phone number, Twilio sends a request to the web app, which responds with an appropriate message to be spoken or texted to the user.

In addition to the voice/SMS interface, @gelstudios created a nice web interface for the project.



Twilio is a web–based service for sending and receiving phone calls and SMS text messages. It provides an easy–to–use API accessible via HTTP and a convenient Python package. A free trial of the service is available (which inserts small nag notices into outgoing messages). We used the Twilio Python Quickstart Tutorials as our introduction.


Heroku is a service that provides a complete, integrated stack for hosting web applications with a range of choices in language, framework, web server and data store. We created the server application for the project in Python using the Flask microframework. The Heroku Dev Center article Getting Started with Python on Heroku is a good walkthrough for setting up Flask on Heroku.

Voice– and SMS–Enabled Light Sensor Using Raspberry Pi and Twilio

Server Code

Full source for the web application can be found at github.com/qqrs/twilio-light-sensor-server/blob/master/run.py.

The /twilio/voice and /twilio/text routes handle requests from Twilio. When a user calls or sends an SMS message to the phone number assigned to our account, Twilio is configured so that it will make an HTTP POST request to these routes. When the server receives the request from Twilio, it generates an appropriate message indicating the status of the bathroom. The message is returned to Twilio in the HTTP response and is sent to the user as either audio (by text–to–speech) or as an SMS message.

The /update route accepts sensor state updates from the remote sensor via HTTP POST. Each request includes sensor_id and sensor_val parameters to identify the sensor and report the current value.


For more detail: Voice– and SMS–Enabled Light Sensor Using Raspberry Pi and Twilio

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top