Article in iOS App Development category.

How to Wake Up Without an Alarm

Create an iPhone-controlled sunrise simulation that plays out on a multicolored LED strip every morning, and wake up without an alarm. Let's get coding...

My bedroom gets very little sunlight, which makes it hard to wake up. I'm an iOS mobile app developer at Fueled, so I naturally looked for a solution I could program myself. The result was an iPhone-controlled sunrise simulation that plays out on a multicolored LED strip every morning. It lets me wake up without an alarm.

Whether you live in a garden-level apartment, want to learn how Bluetooth Low Energy (BLE) works, or are manning a mission to Mars, this is a neat project.

I’ll show you how to control a multicolor LED strip with your iPhone through Bluetooth Low Energy (also known as BLE), while learning more about how BLE works and toying about with your Raspberry Pi.


raspberry pi controlling LED light strip to replace alarm clock
The magic starts with an LED strip

What You’ll Need to Create an Artificial Sunrise

You will use the provided iOS app to set a time for the lights to gradually increase brightness, imitating a sunrise. The lights will mimic the color of the sky at the specific time. Essentially, we are designing an app to control a multicolor LED strip.

We’ll go through the following steps (jump ahead if you need):

Materials

  • iPhone or any other iOS device
  • Mac with Xcode installed
  • Raspberry Pi 3 Model B (older Raspberry Pis also work, but you will need to add and configure a Bluetooth dongle)
  • WS2812B RGB LEDs and the parts needed to make them work with a Raspberry Pi, listed here

I was using the following versions at the time of writing this post: iOS 10.3.1, Xcode 8.3.1, Swift 3.1, macOS Sierra version 10.12.4


wake up without an alarm app
The iPhone app allows you to control the colors, timing, and brightness.

The Features You Will Use in the iOS App

The app I've created has three features:

  1. Direct control — a slider for the brightness of each color (red, green, blue), and a switch to easily turn on/off the lights.
  2. Alarm — to set an alarm time for the lights to wake you up. The brightness gradually increases from 0% to 100%, reaching 50% brightness at your alarm time. It stays at 100% brightness for a fixed amount of time, then turns off automatically. The duration of each of these periods can be changed in the code (explained in the Github repository). You can also “snooze” the alarm, which moves the alarm time forward by the snooze duration (the default is 10 minutes, but can be changed in the app).
  3. Motion control— since these lights are controlled by your phone, why limit yourself to on-screen inputs? This feature uses the whole phone as the input! The phone’s compass is used to control brightness. The colors can be changed by rotating your phone about its axis.

a BLE sunrise in a below-ground apartment that lets you wake up without an alarm
Not a bad way to wake up

The Process for Building and Programming Your Artificial Sunrise

Step 1 :  Set Up the Raspberry Pi to Control the LED Strip

Adafruit has a great walkthrough on how to accomplish this, so I will leave you in their capable hands.

Note: If your LED’s are randomly flickering and not functioning properly (a common problem on the Raspberry Pi 3), disable the sound card.

Step 2 :  Set Up BLE on the Raspberry Pi

These steps are taken from this very helpful post by Tobais.

  1. As Tobais recommends, if you are unfamiliar with how Bluetooth Low Energy works, you should read Adafruit’s introduction on BLE.
  2. Follow this step-by-step tutorial on Adafruit to install BlueZ on your Raspberry Pi.
  3. Quoting Tobais directly: “The python scripts example-advertisement and example-gatt-server show how these APIs can be used to create and advertise a fake BLE heartrate monitor.” Run the example-advertisement script on your Raspberry Pi, and make sure it is working properly by using the iOS app LightBlue to discover your Raspberry Pi.
  4. Run the example-gatt-server script on your Raspberry Pi, and use LightBlue to make sure it works by inspect the services.

Step 3 :  Use an iPhone App to Control Your Lights

  1. Add these files to your Raspberry Pi: https://github.com/andynovak12/BLE-Lights/tree/master/RaspPiCode
  2. Make your Raspberry Pi run the scripts on startup: enter the command “sudo crontab -e” in your Pi’s terminal. Then add these two lines to the end of the file
* * * * * python /home/pi/repeated_script.py > /home/pi/myTestLog.txt @reboot python /home/pi/lights_main.py > /home/pi/mainLogs.txt
  1. Get the Xcode project by cloning https://github.com/andynovak12/BLE-Lights and running the .xcworkspace file.
  2. Checkout the README on the repository for an explanation of these files.
  3. Get the app on your iPhone by Build-and-Running the Xcode project with your phone plugged in.
  4. Restart your Raspberry Pi and control those lights with the app!

Components in the Rig

These are the parts I used to build the rig, available on Amazon:

the LED sunrise at its beginning in a dark bedroom
Wakey wakey

And Finally, the Hardware for Your Alarm-Free Awakening

Real hardware, I mean. To attach the LED strip to the bed, I bought screw-in hooks from my local hardware store, and screwed them into the back of the headboard and the bottom of the bed frame. I rested the LED strip inside the hooks, which allows me to remove the strip if I ever need.

Let there be light! Now I can wake up on time and be a contributing member of society.

More Articles By anovak

Recent Articles

Previous post Retro Tech: Apps That Do Less and Look More Like Yesterday August 24, 2017
Next post How to Create Your Own Mobile App August 28, 2017