Menu

AlarmPi

The AlarmPi is a Raspberry Pi powered smart alarm clock, which I designed and created after getting thoroughly fed up of how inflexible and un-user-friendly most alarm clocks on the market are (you can read a bit more about the AlarmPi’s inception in this blog post).

The heart of the device is a Raspberry Pi with the Raspbian OS, running a Python script which controls the hardware, and takes various inputs to control when it should play an alarm (an internet radio feed). I’ve put together this short video explaining some of the key features

Hardware

As mentioned, the AlarmPi runs off a Raspberry Pi, which is connected up to the following:

The wiring is currently all done on a temporary prototyping breadboard, when I’ve finished playing with the wiring I may get round to soldering it all properly!

The case is made from 5mm thick clear perspex, which I ordered cut to measure from PlasticSheets.com. I then drilled/cut holes in the side and top panels for the buttons/rotary encoder and speakers, and mounted the LCD and 7-segment displays using A2/M2 nuts and bolts. It’s held off the ground by 4 rubber feet, one in each corner, to ensure that screws don’t scratch whatever surface it is on. The case itself doesn’t give a great look, as I made it without access to the proper tools, and it was only supposed to be a temporary measure!

Features

Google Calendar integrationAlarms are set based on integration with my work Google Calendar, which contains my entire work schedule. Since I work shifts, it knows to wake me up with enough time to get ready and travel in the morning (see travel time below), and to let me lie in when I’m working in the afternoon/evening, all without me having to input the information manually. Any updates to my calendar are automatically taken into consideration up until the point of the alarm being set, at which point it is fixed and can only be manually edited.

Travel time calculationUsing the Google Maps Distance Matrix API, travel time to my work locations are calculated and taken into consideration when planning alarms. When an alarm is automatically set based on an event on my Google Calendar, the location of that event is read, and a base travel time calculated from my home location. One hour before the alarm is due to go off, this is then re-calculated and adjusted accordingly – so in the event of traffic being better or worse than normal, you get the optimum time in bed!

WeatherUsing the OpenWeatherMap API, the AlarmPi fetches the latest weather information and displays this on the front LCD panel. This is also read out when the alarm is cancelled.

Text-to-speechI’ve integrated the Google TTS engine mentioned in one of my blog posts so that the AlarmPi will read information as necessary (such as the weather, as above), rather than struggling to focus on the display when you’ve first woken up!

Internet radioThe FM signal in my bedroom is terrible, so the AlarmPi plays internet radio stations instead. A set of stations can be programmed in, and the alarm can set off any one in particular. In the event of the station being unplayable (such as the internet connection being down), the alarm will revert to a sound file based on the Pi, so the wakeup call isn’t missed!

Automatic backlightA lot of alarm clocks have a configurable backlight, which can be set for different brightness values. However, what may work at night is next to invisible in the light of day! The AlarmPi has a luminosity sensor on the top panel, which automatically reconfigures the backlight of the LCD and 7-segment display depending on the ambient lighting, so it’s nice and bright during the day, but dimmed at night.

Web interfaceIn the past, I’ve gone away for a week and forgotten to disable my alarm clock, which means my neighbours get the joy of an alarm going off for a couple of hours every morning before timing out. The AlarmPi has a web interface that allows the changing of the most common settings (including “holiday mode” which disables all alarms) remotely.

Home lighting integrationI use OpenHAB to control some MiLight bulbs around my house. While not strictly part of the AlarmPi, I’ve attached a NFC tag to the top of the box which tells my phone to send a signal to OpenHAB to switch off all lights when I go to bed. When the alarm goes off, another signal is sent from the AlarmPi to OpenHAB to say that I’m awake, and the lights will be switched back on if it’s still dark (very much needed at 4am, rather than groping in the dark for a light switch!).

Source Code

The AlarmPi is obviously a bespoke piece of hardware, and a lot of the code I’ve written is irrelevant unless you’re copying me piece-for-piece – but I’ve released the code here: http://projects.mattdyson.org/projects/alarmpi/ so you can have a look and a good laugh at my Python programming skills! By popular request, there’s also a GitHub project at https://github.com/mattdy/alarmpi which will hopefully be updated with contributions from fellow AlarmPi creators!

There is a slightly outdated script called installer.sh included in the project, which will give you a clue to the majority of dependencies, however there may be some I’ve missed out!

The entire program is designed to run as a Supervisor job to ensure it is always running, even after a crash or power outage.

Wiring

I’ve included a diagram of how all these components are wired together (roughly), so you can get an idea of how it works! Please forgive the lack of proper components in some places, I’ve tried to label everything as clearly as possible, but there are quite a lot of elements to fit into a small space! Click on the picture to view full-size

Future ideas

While AlarmPi does everything I need right now, there are a few things I’d like to add in future.

Wake-up LightingA lot of alarms now come with ‘wake-up lighting’, which simulates a sunrise over a period before the alarm goes off, supposedly helping the body wake up naturally, rather than suddenly with the alarm.

APIWhile the web interface does an adequate job of changing settings and alarms, I intend to make an API at some point to allow other systems to integrate with AlarmPi

Please get in touch if you have any comments or questions about the build!

Thanks a lot!
I have two additional questions:
Have you thought about Real Time Clock in this Project (in case of no internet access after raspberry reboot)?
What happens when an alarm occurs when there is access to the Internet?

I decided not to bother with an RTC as I was running out of GPIO pins on the Pi. This could be fixed by using a B+, but for the moment I’m happy that the time will automatically update from NTP once the internet comes back up after reboot. If an alarm triggers and the internet radio source fails to play for whatever reason, then a file stored on the SD card is played and looped instead.

I got an error while running the alarmpi.py. Where can I find or install Player.py?
Traceback (most recent call last):
File “alarmpi.py”, line 21, in
import AlarmThread
File “/home/pi/Documents/alarmpi/AlarmThread.py”, line 10, in
import MediaPlayer
File “/home/pi/Documents/alarmpi/MediaPlayer.py”, line 2, in
from mplayer import Player
ImportError: cannot import name Player

Make sure you’ve run the python setup.py install command in the installation directory, and that you’re using the full from mplayer import Player with the mplayer installation directory linked in your AlarmPi checkout.

I have copied LCDControl.py into the same directory as alarmpi.py, but still experienced the following error:
Traceback (most recent call last):
File “alarmpi.py”, line 22, in
import LcdThread
File “/home/pi/Documents/alarmpi/LcdThread.py”, line 1, in
from LCDControl.LCDControl import LCDControl
ImportError: No module named LCDControl

A few things!
– vol script not installed – see the installer script
– googletts script not installed – see my blog post on the subject
– You need to change a line in RpiLcdBackpack.py to reflect the new i2c address on the Pi – see http://mattdyson.org/blog/2013/05/using-20×4-rgb-lcd-over-i2c-with-a-raspberry-pi/ for more details
– You haven’t set up your Google Calendar credentials properly, the log output has details on what to do to fix that

Probably worth re-iterating at this point that my code isn’t there to be used directly – I’ve made it available for reference and inspiration

I had some problems with static as well – solved by using a USB soundcard rather than the Pi’s onboard audio.

You should be able to dim the backlight by finding the relevant pins and connecting them directly rather than using the I2C (or other) backpack. You may even be able to connect the lux sensor directly, but I chose to do the dimming through software as my hardware knowledge isn’t that great!

I had thought of adding a sunrise simulation to mine, but that’s a future expansion I think! Please share details of your alarm when it’s finished!

Hi Matt,
Thanks so much for the code! my alarm clock is really coming along now.

I’ve been banging my head against a wall with an issue with AlarmGatherer.py though, I wondered if you’d experienced the same problem. It appears that when the google api is requesting the calendar entries, something is prepending a slash before events? to the query so the url is malformed. I’m not good enough with code to work through how the URL is generated or what is causing the issue. Any input very gratefully received.

Glad you’re able to take inspiration from this and create your own alarm clock – would love to see the finished product!

Sadly I’ve got no idea what’s going on with that error – the actual formatting of the URL should be handled by the apiclient.discovery package, and it does seem that somewhere an extra forward-slash is being added into the URL. Perhaps you’d be better off asking in one of the Google Calendar forums, hopefully someone there may be able to help! Good luck!

I found it much nicer when converting larger numbers in the speech function in Weather.py. it also does the nice thing of saying “point” for decimals. Therefore I’m not limiting my numbers to integers and using a round to two decimals. I have now got my wind speed setup like this “2.24mph@NNE” spoken like “the wind is blowing two point two four miles per hour from the north-northeast”

The compass directions was done by converting the 360 degree information into compass coordinates. Your code is great i just can picture the same units of measurement in my head, so I’m converting measurements to units I’m familiar with.

Can you integrate a DS1307 Real Time Clock breakout board kit into the clock. Just in case the electric goes out. I am going to build this as my very first electronics project. But instead of plastic for the case, i want to enclose it in a nice redwood or mahogany wood enclosure sorta like the old fashion radios of the 30’s.

Hi Sean,
It should be possible to integrate a RTC into the clock, I chose not to because I was running out of GPIO pins, but with the newer Raspberry Pi’s you shouldn’t have this problem. I did originally intend to make a wooden case, but don’t currently have access to a workshop in order to make one, so I had to rely on what I could buy straight off the internet!

I’d love to see details of your project when it’s finished – please do share!

Thanks Matt, I think ill just stay with the 4×20 lcd. I’m sorry too ask such Noob questions. This will sure be an undertaking for me, but i enjoy new challenges. I was running thru the wiring diagram. I will be using a raspberry pi b+ for my clock. Where would you recommend installing the RTC board up too the PI??

The wiring of the RTC will vastly differ depending on the component you use – most seem to require either multiple GPIO pins (the B+ will have plenty of spares), or I2C, so you can just piggy-back it on to the other I2C components.

Hi, I’d love to get you Project running and i investigate some days now. Allmost working, but still one prob.

I ran into the same problem as Neil did. It isn’t a problem of the duble // in the uri I guess. Found a page that says it’s not a typo.
I think It’s because of a faulty development key. Actually I dont get what the development key is?
I created a Client ID for native application and was provided a client id and a client secret but not a derlopers key.
Can you tell me what the development key and how to get it?
Probebly obvious. But I’m somehow blind for the obvious::)

Thanks, Matt for this awesome project. Has anyone made any updates / breakthroughs lately? By chance Matt will you be doing a video tutorial about this project? I think it would really helpful not only to me but other NOOBS.

Hi Matt,
I took a brief look in the project dir, looking for the Python logic that controls the amplifer but could not see it. Probably pilot error but i’ll ask just the same where should i look for the amplifier logic

* Red 2″ Adafruit LED backpack with digits inverted. I chose red so it’s less disturbing at night. This is mounted in a black plastic enclosure from Maplin with a red cellophane sheet to tone down to a richer red colour;

* TSL2561 luminosity sensor to dim the display at night. I was using a simple photoresistor and capacitor drain setup before, but it was slowing things down;

* 433MHz RF transmitter to control mains switches linked to lamps and a desk fan; and also to my computer monitors so I can switch my Mac from my desk to a monitor mounted on an arm above my bed so I can carry on where I left off if my back starts to hurt;

* HTU21D and BMP180 for monitoring air pressure, temperature and humidity. They give an early warning alert for possible migraines.

* IR sensor for getting control signals from cheap remote controls I’ve put in various places for convenience;

* USB battery pack, which acts as a poor man’s UPS for the Pi: it’s on constant charge, and the Pi runs off it;

* A box under my bed with an Arduino and four relays to control the recline/sit motors for my bed, and accelerometers on the bed frame to detect current status. This is done, but it’s not programmed or wired to the Pi yet.

* A servo motor connected to an Espruino with WiFi for controlling the tilt on the venetian blind in the window. This works, but I’m awaiting parts to power it properly.

* A drill motor and relay bank to hoist the blind itself. It’s a heavy wooden venetian blind, so it needs a good, well-geared motor to lift the 10kg. A power screwdriver/drill from Lidl whose battery had died serves the purpose. I’m just finding time to hook it up.

The same Pi also acts as a Wii remote proxy to send Bluetooth commands to my iPad (long story), and a printer server for a couple of label printers and other widgets.

I’ve looked into using Jasper (http://jasperproject.github.io) for constant speech recognition to control all this, but I haven’t got a good enough recognition rate yet.

Hi Dylan
The plastic was ordered from PlasticSheets.com cut to size (the main panels being 1/2 A4 size for ease of prototyping). I drilled pilot holes for the screws with a standard hand-held drill, and the holes for buttons/speakers were done by making multiple holes with the drill and then filing the remainder away – this was quite a tedious process for the speaker holes!

I started out working on a wake-up light/alarm on the arduino with additional fm tuner, rtc and PWM-controllable power output for slowly rising first yellow light and then white light up to 100 Watt in total. (Why call it a wake up light when there is hardly any light output..)

I have now reconsidered and would like to base it all on a raspberry instead. Your project seems like a good start allthough I would want to switch to a touch lcd screen probably. They allow for very nice visual presentation/gui and make navigating menu’s way more simple for the user. 🙂 Also, touch screens are getting very cheap lately. A 5″ hdmi based screen is only 30 $ and if we want to go very cheap we can even use a 3,5″ touchscreen for just 15 $. 🙂

Great work.
My Feature Wish List:
– Smartphone Like simple to use Alarm Interface (Apple iPhone)
– Display additional International Times (Local + 2 More)
– Time Zone Support for Alarm: Wake me up when it is 3:00pm in India
– Precision: Atomic and/or other sources. The time should be sync’d with some source. Automatic DST.
– Fail Safe mechanisms (don’t want to miss the flight) like backup battery power

I really like this project, I am making my own “timer” for a school project and would like to use both a 20×4 lcd and an 4 digit 7 segment display, would you mind explaining how you wired them together, and the code you used to make them run?

Using a heat gun I bent each side, one at a time, until I had a box. For added strength I epoxied the two ends together where they met.
Total cost of materials was a little under 20 US dollars for an 18″x24″ sheet of acrylic, which was enough to build two cases.

Thank you for your blog! I’m using a 1.2″ 7 segments LED HT16K33 and a I2C 20×4 LCD both connected by I2C. I have the problem that the raspberry pi only detect one of them when they’re both connected. I’m using the command sudo i2cdetect -y 1. When I disconnect one It can detect both, the problem is when I connect both together.

The 7 segments has 3v and 5v Vcc, and both have to be connected. I’ve tryied to desconnect one of them and see what happen… And If i disconnect 5v. The raspberry pi can detect both but the 7 segments don’t work well.

I think I’m going to buy 0.56″ 7 segments, but any help will save some money!

When I disconnect one It can detect both, the problem is when I connect both together

I’m a bit confused by this statement – if one of the devices is disconnected, how can the rPi detect it?!

The only obvious thing I can think of is if they’re trying to use the same I2C address? If not, try using a separate 5v pin on the rPi for each device, perhaps combined they’re trying to pull too much power and not functioning correctly?

Sorry for the misunderstanding. The problem is when both are connected the 7 segments is not detected and it is not working. I get the “Error accessing 0x70: Check your I2C address “. The address for the LCD is 27 and 70 for the 7 segments, so it can’t be an address problem. I’ve tried using separate 5v and is still not detected.
I’ve bought the LCD from china, so maybe that’s the problem, I think I’m going to buy an adafruit LCD and try it.

Awesome project (and glad to see you’re still responding to peeps!). I had an idea to do something similar this and your project has saved me some of the legwork and inspired me to experiment some more, e.g. I’m integrating a RTC module and touch sensors instead of buttons, and no LCD display.. at least for now, I was contemplating an App/API for management. I note you used a USB soundcard and I was wondering which one you recommend? (looked like: Plugable USB Audio Adapter (C-Media HS 100B Chip)

Some other ideas:
1.) I sometimes forgot to switch off the alarm clock when I´m not at home (working in another country). So an option to discover me (e.g. my phone is in the same WLAN as the clock) might prevent it from waking up the neighbors 😀
2.) it would be great if we could have a 3rd button which can be enabled or not. The 3rd button will switch off the numbers and the display during the night. Most clocks i had (and hate) produce so a bright light from the numbers for the clock that’s hard to sleep. So if i need the time in the night, i could then press the 3rd (maybe bigger button) to get that solved. Or even cooler … it listen to my voice and if I need the clock i could as for it ;-).

You might consider this one.
Some projects of mine make use of a so called Out-0f-Office mechanism. At the moment it is implemented as a semaphore e.g. ooo.sem (filename). The ooo.sem can be set even when I am out via a vpn tunnel to one of the raspi’s. Contents ooo.sem: 2017-10-01|15:15:49 | cv_utils.py Out of Office t=NO. Scripts read the ooo.sem check date/time stamp en status – YES/NO
The system is not connected to google agenda or other outside dependencies since these services might be disrupted, not properly updated or even changed without notice.
Thus some of the outside lights stay off, hot water boiler is not heated, some parts of the security system are de/activated, and a daily report on system performance etc. is mailed; contents is depending on am I in or out.

– The circuit diagram shows a breakout from the Pi using an Adafruit “Pi Cobbler” – this is exactly the same as the pinout on the pi.
– Those were just a couple of cheap male to female extensions, I believe they were both from Adafruit
– I don’t think photos will help really – mine looks like an absolute mess! The wiring diagram is about as simple as I can make it, hopefully you can figure it out from that!

I was going through this project. While I was trying to run the alarmgatherer.py. I always get the same error : unparsedFlagAccessError: Trying to access flag auth_local_webserver before flags were parsed. Enter verification code:
I tried to fix it using different ways but was not able to. Do you want what the problem is ? with this.
Thank you

Hello Matt,
Liked your alarm clock so much I built one for myself. Some modifications, of course.
I added the Pi Touchscreen and eliminated all buttons and switches. Added support
for SDR radio in addition to streaming & MP3. Added a temperature sensor and thermometer
on the clock face. The wife vetoed a GPS, wifi lighting control and Google integration,
but it was still fun!

I was having problem with mplayer so have swapped to vlc to see if I could make it work plus I have gone with the uses of using a json file for the authentication instead of the placing the details in the file. As the google, guys have done here: https://developers.google.com/gmail/api/quickstart/python

I am new at this. I have everything wired. How do I get the .py on the raspberry pi and how do I get them all running to make the finished project? I have no coding knowledge and I cannot figure out how to get it all running. I have tried it before. Should I reinstall the RPI software and start from scratch?

This has been a fun project but i have an issue.
i have everything working except the on screen menu as long as i comment out the imports of rotaryencoder and switch gaugettes in the InputWorker.py. if i don’t comment them out i get “wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code.” any idea how to fix this error?