In this tutorial, we’ll be building a smart ‘Internet of Things’ (IoT) connected alarm clock with a Raspberry Pi, that continuously monitors bus arrival times, and delays your alarm accordingly to give you the maximum amount of sleep! Read on below to find out how to put together this fantastic project!
Most people don’t live close by to work, and if you live somewhere with good public transportation, you more than likely rely on buses or trains to get to work. But the worse thing with relying on public transportation is when the bus is late, and you just end up waiting there doing nothing productive. Wouldn’t it be great if you could just get those extra 15 minutes of sleep? Or even worse, if your bus comes a bit too early, and you just wish you would have gotten there a few minutes earlier! I feel your pain…
Well if you live in London, you’re in luck! Today we’ll be using a Raspberry Pi to build ourselves an IoT connected alarm clock, which checks your local bus stop for arrival times, and delays (or even brings closer) your alarm time! No more missed buses, your new smart alarm is constantly monitoring bus arrivals, ensuring you maximise every last minute of your day!
How does it even work?
Before I show you how to put together this project, I thought it might be useful to show you how it even works in the first place. In case you’re a bit suspicious!
If you’ve been reading this blog regularly, you would have noticed our last post which showed you how to build your own London bus arrivals ticker using a cheap LCD screen. That project relied on a nifty Python wrapper for London’s bus arrivals API (Transport for London) I wrote. Using that same API, we can link it to the very first tutorial I wrote on this blog, an alarm clock.
Now when you launch the alarm clock script, you’ll be asked which bus number and stop you need to catch a bus at, as well as how many minutes you need from waking up to reaching your bus stop (head start time). Your smart IoT connected alarm clock will then wait till about 30 minutes before your alarm time, before it starts querying Transport for London’s bus arrivals API to fetch the bus arrival times. It will filter out the list to find which is your bus, and compare the arrival times to your alarm time (factoring into account the head start time). It then finds the bus arrival time which most closely matches your alarm time, and adjusts your alarm time accordingly, such that your alarm is as many minutes before your bus, as it takes you to reach your bus stop. Convinced? To make it even more exciting, we’re using the Adafruit.IO feeds and IFTTT to connect the alarm to any other IoT connected device in your home! Excited? Read on below to put this project together and you can try it out for yourself!
What do you need?
- Raspberry Pi (any model will do)
- Internet connectivity for your Pi (WiFi, Ethernet, however you wish)
- A buzzer and switch (alternatively, Simon Monk’s Starter Kit has all these components)
Yup, that’s really all you need! You could enhance it with an LCD screen, or ditch the buzzer and connect an audio line to speakers to play out a custom MP3, but that’s all extra. If you’d really like to know how to do that, drop me a comment below and I’ll show you how!
Putting it all together
Below is a circuit diagram showing you how to connect it all together. The GPIO pin numbers are all arbitrary, and you could change them (making sure you change it in the code as well).
Getting the code and set your alarm!
Before we get the code, we need to get set up a few things. Firstly, make sure your Raspberry Pi’s time is synchronised with the Internet. This may not happen if you connect to the Internet after you start up, in which case you can run
sudo ntpd to force an update. Type
date at the terminal to just double check. If that’s OK, navigate to an empty folder on your RPi.
Next, you’ll need to have an Adafruit IO account. Currently, it’s in Beta, and invitation only, so you may not have access to it already. In that case, you’ll need to disable all the Adafruit IO features in the script. Hopefully you don’t! In that case, log in to Adafruit IO, and create a new feed with title ‘iot-alarm’. Then get your AIO key from the top, and fill it into line 32 of the
iot-alarm-london-bus.py script, under the variable
Now log into IFTTT.com, and create a new recipe. Set a trigger using the Adafruit IO channel, to be ‘Any new data on feed’ with the feed being ‘iot-alarm’. Now for the exciting part, selecting an Action! IMO, the coolest IoT action you can have is with a Philips Hue bulb. Why not have it flash your house lights blue? Other ideas:
- Send a notification to your phone
- Send an email to your phone
- Use a WeMo switch to turn on an appliance
Now you can go ahead and grab my code from GitHub, and run it by typing the following:
$ git clone https://github.com/adafruit/io-client-python.git
$ cd io-client-python
$ sudo python setup.py install
$ cd ..
$ git clone https://github.com/ismailuddin/iot-alarm-london-bus.git
$ sudo python iot-alarm-london-bus.py
Setting the alarm
You'll be asked a couple of questions:
- What is your bus line? Simply enter in the name in capitals, where there is a letter involved.
- What is your bus stop code number? This is the number written at your bus stop, which you should text to a number to get live bus arrival times.
- What time do you want to wake up at? Enter as a four digit number in HHMM format.
- How long do you need to catch your bus? Enter as a two digit number in minutes / MM format.
The script will then begin, keeping you up to date on what it's up to. It will tell you:
- What it thinks is the best time for a bus to arrive for you (ideal bus arrival time)
30 minutes or closer to your alarm time, you will then be informed of:
- The live bus arrival times, and how many minutes each of these differ from your ideal bus arrival time
- Which bus arrival time it thinks is best for your purpose
- Your new altered alarm time, so you can catch the best bus arrival time for yourself
When the time comes, the buzzer will sound producing the characteristic alarm sound! And if you've got Adafruit IO and Philips Hue bulbs, your house might even flash blue! You can also hold down the contact button for a few seconds to snooze your alarm for 8 minutes, though we really advise against that, as the alarm time is literally the most optimum time to wake up at!
Have fun and enjoy!
It is worth pointing out, that unfortunately, the nature of TfL's live bus arrivals API means that you cannot get bus arrival predictions more than 30 minutes away. So if your bus is more infrequent than that, or the time you need to catch your bus is longer than that, this script will probably be of not much use.
But the great thing is you could even use this project as a day time alarm, and not necessarily for morning wake alarms! Maybe you need to leave house exactly 25 minutes before your bus arrives, but you've got this important project to finish off? I hope you find this project not only fun, but useful too! Drop a comment below with any thoughts, suggestions or problems you may hopefully