Home Alert: Arduino + Cloud Messaging On A Large Display
In the age of mobile phones, you would expect that people would be responsive to your call 24/7.
Or… not. Once my wife gets home, the phone stays buried in her hand bag, or its battery is flat. We don’t have a land line. Calling or SMSing to ask for a lift home from the train station on a rainy night or calling to ask if my keys are still on my desk is literally wishful thinking.
I have this problem often enough to warrant a solution. A bit of tinkering with an Arduino and a Freetronics Dot Matrix Display (DMD) resulted to a very annoying (for my wife) gadget, but an amazing communication device and information center for me. I love it, and it’s only version 1!
Home Alert is made of these parts:
- A Freetronics Dot Matrix Display, which is an array of 16×32 LEDs. They come in different colours, but I use red to emphasise that this gadget is for “critical” notifications.
- An Arduino Uno with an Ethernet Shield.
- A real-time clock breakout, like this or this.
- A piezo buzzer
- A DHT22 temperature and humidity sensor.
Have a look at the home page (show in the first attached image in this step), where the form awaits a new message from the user.
The first field accepts a numerical hardware code. It’s a code that allows you to target a specific Home Alert system, as each can be given a unique code. Or, you can have multiple Home Alerts sharing the same code, so that the same message is displayed to multiple locations.
The message you want to display goes to the second field. Any text you type in there will be displayed in the DMD.
If you want to make some noise, check the Yes! checkbox, and the buzzer is sure to gain the attention of anyone nearby.
In this article, I’ll show you how to build your own Home Alert system, both Arduino hardware and software, as well as the Sinatra mini web application.
Let’s get started!
Step 1: The hardware
The DMD is the focal point of the gadget. I could have gone with a small LCD screen, but the main idea for this project was to produce something that can be seen and heard from a distance. For the visual part, I needed something big and bright, and this Freetronics display is exactly what I needed. Each panel contains an array of 16×32 LEDs, and you can stick several of these together to create much larger displays. This is something I’d like to do in the near future.
The DMD comes with an easy to use Arduino library. it communicates with the Arduino via high-speed SPI. I was able to get the library from the Freetronics Github page, then fire up the demo sketch and get it working within minutes of opening the box. I was surprised to see such a bright display using only power from the Arduino. If you want to temporarily blind your viewers, you can attach a dedicated power supply to this DMD. If this doesn’t get their attention, nothing will!
Physically, this display measures 320mm (W), 160mm (H) and 14mm (D).
The back panel contains the connectors for the external power, 5V with at least 4Amps capacity, the Arduino connector marked HUB1, and the connector for daisy-chaining additional displays on the opposite side. According to the documentation, you can daisy-chain up to four DMDs.
The DMD is controlled by an Arduino Uno. Freetronics provides a very convenient “DMDCON” connector that just snaps directly onto the correct SPI and data pins.
Other than the DMD, I used an Arduino Uno, an Ethernet Shield, a real-time clock breakout, a buzzer, and a DHT22. For all of these components, I have created lectures describing their operation in my Udemy course. (Shameless self-promotion: sign up to my email list at arduinosbs.com and receive a coupon that give you discounted access to all 55 lectures).
The real-time clock, a breakout based on the DS18072 clock IC, is an I2C device so it is connected to the Uno’s analog pins 1 and 2, which implement the I2C bus.
The buzzer is connected to digital pin 3, from where I control it using the tone() function.
The DHT22 sensor is connected to digital pin 2. Be careful to connect the 10KΩ pull-up resistor between the 5V line and the data line.
Step 2: The Arduino sketch
The sketch is not large in terms of the line count, but it almost exhausts the Uno’s available flash memory thanks to all the included libraries. There is lots of room for memory optimisation, but since I am at the prototyping stage, that’s a project for another day. This code is available on Github.
Here is the sketch, with embedded comments (see PDF attachment).
The main responsibility of this sketch is to make the Arduino a consumer of a web service. The web service is a simple web site with two end-points, one for a human user to access via a web browser and submit a text string that they wish to display on the DMD, and another one where the Arduino will access in order to retrieve that text string.
Please download and read the attached PDF file, it contains embedded comments that describe its operation.
Step 3: Sinatra takes the stage!
There are many ways to create web sites and web services. From programming languages with web-supporting libraries, to full-featured frameworks, it can be confusing and hard to choose one for this job.
I have used and played with a fair number of web application technologies, and find that Sinatra is ideal for building web services and small web sites. In particular, when I build a web service to support an Arduino gadget, Sinatra is a really good choice.
What is Sinatra, and why is it such a good choice? I’m glad you asked!
Sinatra is a language for the rapid development of web applications. It is build on top of Ruby, a very popular and expressive general purpose scripting language. You may hear Sinatra being referred to as a “DSL”, a Domain Specific Language. The domain here is the Web. The keywords (words) and the syntax created for Sinatra is such that it makes it easy and quick for people to create web applications.
At a time where so-called “opinionated” frameworks for web app development like Ruby on Rails and Django are super popular, Sinatra captures the opposite end of the spectrum. While Ruby on Rails and Django require the programmer to follow a specific convention and way of doing things (which, in turn, implies a steep and long learning curve), Sinatra makes no such requirements.
Sinatra is conceptually much simpler than the Rails and Djangos of the world. You can get up and running with a web application that can interact with your Arduino within minutes.
I will demonstrate with an example. Here’s what a Sinatra minimal web app looks like (just read the following for now, don’t actually do this on your computer because you probably don’t have the prerequisites setup for this yet):
In a single file, let’s call it my_app.rb, add this text:
get ‘/’ do
On the command line, start the app like this:
Your app will start, and you will see this text in the console:
[email protected]:~/arduino/sinatra_demo$ ruby my_app.rb
Puma 2.8.1 starting…
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Puma
The app is now ready to receive a client requests. Open a browser, point it to http://localhost:4567, and this is what you will see (see attached screenshot).
That’s four simple lines of code in a single file. In contrast, Rails would have required over a hundred files, generated simply to satisfy the framework’s requirements. Don’t get me wrong, I love Rails, but really?…
So, Sinatra is simple, and quick to run. I will assume that you know nothing about Ruby, Sinatra, and application deployment to the Cloud, so in the next section I will take you step by step from zero to deployment of your Arduino web service to the Cloud.
Step 4: Setup your development machine
Sinatra is based on the Ruby programming language. So, you need to install Ruby before you install Sinatra.
You will also need to install a key-value store server called Redis. Think of Redis as a database that stores data against a key. You use the key to retrieve the data, and it is optimised for speed rather than flexibility of the data structures that a traditional relational database is designed for. Home Alert stores its messages in Redis.
For more detail: Home Alert: Arduino + Cloud Messaging On A Large Display