Somnos Sleep Tracker & Alarm

Final Project
May 5, 2015

Somnos

Latin for: Sleep

Team Members: Oscar Morrison and Daniel Basner

Supervisor: Amos Bloomberg

"Sometimes the most productive thing one can do is sleep"

1.0 Background
Sleep is the number one most important aspect of good health. There are hundreds, even thousands of studies that indicate the importance of sleep and the terrible consequences of poor sleep. Sleep debt is the effect of not getting enough sleep; a large debt causes mental, emotional and physical stress and problems.

1.1 Introduction
Somnos, coming from the latin word, meaning "to sleep" is a new age sleep tracker device combined with an automatic alarm clock. Somnos primary aim is to ensure that you continuously get the correct amount of sleep. Many apps and devices ensure that you get enough sleep, but very few discourage you from sleeping too much. Somnos is aimed at being transparent in your life, a single setup and it is complete. The device compliments your existing lifestyle, there is no need to recharge or sync, it all happens seamlessly.

1.2 Audience
[Original] Primarily, Somnos is aimed at the user with an active lifestyle who has an interest in their health and would like to improve with the benefits from a constant sleep cycle. This user will be someone who is aiming to get a constant 8hrs everynight, and have an alarm automatically set from the moment they get to sleep.

[Final Product] The final product ends up being aimed at somebody with the above qualities, who also wants control of their data. All data is owned by the user, and is created locally on the machine and also downloadable online (from spark). This encourages open hacking and modification of our product.

1.3 Market
Up until recently any diagnostic or sleep quantification equipment was extremely expensive and difficult to setup. With the new availability of devices with accellerometers and gyroscopes, the market has been flooded with options; Some key highlights:

  • Jawbone/Fitbit/Activity Trackers: These devices are readily available, reasonably priced and informative. Downsides are the need to wear during sleep, need to charge and the need to sync.

  • iPhone/Android: There are plenty of sleep cycle style apps that can give some excellent quantified data. Downsides are the proximity and possible radiation as well as the need to setup and keep charged near your head. Also keeping on the bed can be difficult.

  • Withings Aura: Definitely Somnos main competitor. A device that combine light sensor, microphone, speaker, LEDS and a sleep mat with various sensors (heat, pressure). Downsides are that it is very expensive ($300) its is platform specific (iOS only) data is inconsistent.

1.4 Interactivity

"Good technology is transparent"   

This quote sums up the key goal of Somnos, to be invisible to the user. The device is designed to be setup once and once only. The device has an initial calibration period(< 5seconds) and then it is set.

Generally when we think about technology we often expect that we need to use our hands, ears and eyes to interact with the technology. This can hinder our natural process, confidence and flow, it is when the technology is working with "us" in the background that can give great

2.0 Design
Designing the product is a crucial aspect of any project, and we ensured that it was a high priority for the project.

2.1 Idea
Our idea was focused around improving sleep in general. More broadly this consisted of the actions of creating a alarm and monitoring sleep. Personally I found that every night the last thing that I would do is set an alarm for the morning. The thought of getting into bed and the alarm setting automatically was of great appeal. The extension of this idea was looking at the Quantified Self movement, collecting and displaying personal data.

2.2 Parts

  • 2.2.1 Hardware

    • Arduino Uno (or any arduino)
    • MPU6050 Accellerometer Sensor
    • PC
    • USB Cable
    • Wires and Connectors
  • 2.2.2 Software

    • Arduino IDE
    • Processing IDE
    • Twitter Developer Account
    • Processing IDE Libraries:
      • Arduino MPU 6050 (Jeff Rowberg)
      • Serial
      • Twitter4j
      • Date
      • Minim (audio)
      • http.requests

2.3 Plan (Stages of build)
The general plan consisted of breaking up the project into smaller actionable parts and splitting up work that could be done on our own time.

1) Setup and understand MPU6050
2) Create Alarm Clock
3) Clean up data from MPU6050 and calibrate (with thresholds)
4) Graph data from MPU6050 (locally)
5) Stored data from MPU6050 (locally CSV)
6) Design and build 3D printed casing
7) Create PillowCover for the tracker
8) Post data from MPU6050 to the web (data.sparkfun.com)
9) Build site to retrieve data and graph (using google charts)
10) Integrate alarm clock and MPU6050 data (and test)
11) Add twitter integration
12) Pair twitter with IFTTT and LIFX lightbulb
13) Design Product Packaging

2.4 Process (The build)

1) Setup and understand MPU6050.
We did this together. We found a tutorial and went through the basic steps. We realized at this point that the MPU6050 was going to be more difficult to work with than anticipated.

2) Create Alarm Clock
Daniel did this part on his own using processing.

3) Clean up data from MPU6050 and calibrate (with thresholds)
I did this alone, and ended up having to use a little bit of math to clean up the data.
In the end I ignored the 3rd value (horizontal acceleration) and took a absolute value of both X and Y
totaled to become my raw value for the system. After hours and hours of testing this was a very realistic and accurate result. I was then able to create a general calibration function for the MPU6050 which was deemed after reading X reads, and giving a movement value of under 15. Also further finding the threshold for recognizing someone as in bed or as not in bed (generally under 15 suggest no reading).
The following video illustrates some early stages.

4) Graph data from MPU6050 (locally)
Next I graphed the real time data locally, this was not used in the end product as we went with a circula graph, however it is a good representation of the motion data:

5) Stored data from MPU6050 (locally CSV)
The need for persistency is key. Due to the nature of the motion tracker and the unreliability of RAM simply storing the motion points in memory is not acceptable. As such every single point (around 800 a sec) is stored in a overarching file, and then the average of 10points (to reduce data size) averaged with the max value in the range is store in another file. This is what is graphed.

6) Design and build 3D printed casing
The casing of the MPU6050 is a very important aspect of the project. Partly because it is the main part of the project that the consumer would see (and interact with), and secondly because it protect and stabilizers the MPU6050.

The casing is designed to replicate the ergonomics of a pillow, with no sharp edges, such that if the user rolls over on the case they will not be poked. Furthermore the casing allows for breathing of the MPU6050 on bottom and top. Once the case was built the MPU6050 was placed in an antistatic bag and secured inside the casing using tape and cardboard.

7) Create PillowCover for the tracker
As tracker will reside on top of a bed it was very important that it assimilated with the style and feel of bedding. As such, a soft but durable pillow case style cover was made using available materials. This allowed for the device to be easily safety-pinned to the bottom sheet, keeping in position for an extended period of time.

8) Post data from MPU6050 to the web (data.sparkfun.com)
Next I wanted the data from the Sleep Tracker to be available at any time, anywhere. I envisioned the user going to work and picking up there phone and loading an url and seeing their nights sleep data as a graph.
To do so the data needed to be pushed to the web, to a simple server. I ended up going with data.sparkfun.com which uses a phant.io IOT server. It used a basic REST Api to receive the post values (the movement). Due to the constraints of the system (no more than 500 posts per 15min, and 50mb storage) I had to change the formatting of the data to be posted and average out of 9seconds intervals, once again taking the average of the value in the range, then averaging that number with the max value in the range to better represent the peaks of the data. One of the other excellent advantages of using this is that the user has complete access to their own data and is able to download it in a variety of formats (JSON, CSV, MySQL etc):
Active URL: https://data.sparkfun.com/streams/Jx975RjRYZHpnA7qgjzz
(This stream is cleared with each new alarm)

9) Build site to restrieve data and graph (using google charts)
Using this live data as the source, I then went ahead and built a straight forward one-page graph representing the sleep movement over the period the tracker is active. I used Google charts API and JQuery to download the data and graph. This data is updated every 9seconds. I then hosted this page and created the URL: http://oscarmorrison.com/projects/sleep/

10) Integrate alarm clock and MPU6050 data (and test)
This was a very time consuming process where we needed to integrate both our sections of work (software). We ended up using the Alarm Clock as the master and slowly added aspects of my tracking code. At this point the core of the product was working, and we were able to recognize the key states of isCalibrate and inBed. (universal to both programs). https://gist.github.com/oscarmorrison/0d8ec9b28caa34d72073

11) Add twitter integration
As some finished touches I connected the Sensor Processing app with the Twitter4j library such that it could tweet once the user was in bed. This is the primary connection for IFTTT (e.g connected to the internet of things).

12) Pair twitter with IFTTT and LIFX lightbulb
Pairing with IFTTT was very simple and a matter of signing up and creating a recipe. The recipe consist of when a user with username @physicalcomp sends a tweet, to turn off the light. This essentially uses the twitter feed as an action switch. However with some simple modification it can look for a specific hashtag.
https://ifttt.com/recipes/289266-when-a-user-with-username-tweet-turn-off-lights

13) Design Product Packaging
The Product Packaging is a fun aspect of the project. As the project has some commercial viabilities (with adjustments and refinements) it is important to be able to visualize the future product. Using a clean a simple design inspired by Quirky and Zepp Sensor Packaging the SOMNOS packaging was created:

3.0 Final Product
The final product. The video below demonstrates the working product below. The SleepTracker was successful, and we are both happy with the end product.
http://oscarmorrison.com/projects/sleep/

3.1 Challenges
The project was much bigger with a much greater level of complexity than expected. Generally speaking it was very successful over short alarm periods (e.g 1-4hrs) while someone was working on the PC, however to be able to enable the device to work all night it is imperative the the network (wifi) is active and the computer did not sleep. Therefore some hacks such as using apps (NoSleep and Caffine and Insomnia) were needed to be able to record over night.

Furthermore dealing with a large set of data, as well as multiple components (software as well as hardware) made the complexity very high.

3.2 Improvements
Going forward the next step would be to make the tracker independent of a machine. Creating a basic interface (perhaps a phone app, or a dial and small lcd) to set sleep hours, would be ideal.
Then adding a smaller wifi enabled chip such as a Spark Core or a ESP8266 to send the data.
Then adding on this to create mobile application to view and track sleep over an extended period, with a server for data persistency.

4.1 Appendix
Somnos Sleep Tracker Code
https://gist.github.com/oscarmorrison/0d8ec9b28caa34d72073
Somnos Sleep Tracker (Online)
http://oscarmorrison.com/sleep/
Keynote Presentation Slides
http://cl.ly/2s0X0J2i1L1l
Sample Data (Screen Shots and CSV)
https://www.dropbox.com/sh/8khzlxb3rfrtfvw/AAAfp9OHXsxPcKaEkawou9jka?dl=0

4.2 Reference
http://en.wikipedia.org/wiki/Sleep
https://data.sparkfun.com Google Charts
http://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
MPU6050 Documentation and Information
https://www.sparkfun.com/products/11028