The skill is published in alexa skills section and can be found at https://www.amazon.com/dp/B07C2HRYSX The skill is monitoring Peter's sleep, feel free to follow the guide and monitor your own sleep
You can follow link alexa with a user in your system guide to setup multiple users with your guide.
https://developer.amazon.com/docs/custom-skills/link-an-alexa-user-with-a-user-in-your-system.html
ProblemCDC has recently classified insufficient sleep as a public health problem. Sleepiness is the root cause of increasing the risk of accidents and other occupational errors. It can also cause health concerns such as diabetes, depression and high blood pressure.
According to a recent survey by the National Sleep Foundation and the Consumer Electronics Association, more than 60% of sleep tracking technology owners are more aware of their sleep patterns after sleep tracking. 51% of sleep tracking technology owners say they’re sleeping better knowing the technology is helping them and 49% say they feel healthier since they started using the technology.
Although most of the sleep tracking technologies are non-invasive devices such as watches and bands, they still require huge amounts of daily maintenance for hygiene and constant battery charging. The discipline of frequent charging and hygiene maintenance using the wearable device has turned a lot of people away from using them.
Using Walabot, we can build a non-invasive sleep tracking system that's always on. This way the User does not have to worry about charging their wearables, washing for hygiene and still be able to track a good night's sleep.
There are three parts of the solution:
- Walabot part
- Walabot server part
- Alexa part
Walabot can be used on any Linux or IoT machine; in this case, we will be using a laptop as the base. We will also be implementing AI locally, as well, to identify the patterns to the user.
Walabot server will be used to store the information; this can be used to display extra information for mobile devices, IoT display devices, as well as Alexa.
Alexa is one of the many output options that we can use; in this case, it's the easiest one to implement and get a quick glimpse about how your night sleep was.
Step 1: Setup Walabot with UbuntuWe first have to setup Walabot. In this case, we are detecting the amount of energy being used while asleep. We've based this off the breathing example, as it seemed Walabot was very effective at detecting not just movement but how much energy was being used through the movement, making it perfect to track sleep.
We can build this with any IoT device that runs Ubuntu and Python such as Raspberry Pi, but for this prototype's case, we are going to use a cheap laptop because we have a screen that can monitor what's going on.
#!/usr/bin/env python3
from __future__ import print_function # WalabotAPI works on both Python 2 an 3.
from sys import platform
from os import system
from imp import load_source
from os.path import join
import time, random
import math
from collections import deque
import urllib.request
modulePath = join('/usr', 'share', 'walabot', 'python', 'WalabotAPI.py')
wlbt = load_source('WalabotAPI', modulePath)
wlbt.Init()
start = time.time()
class RealtimePlot:
def __init__(self, axes, max_entries =100):
self.axis_x = deque(maxlen=max_entries)
self.axis_y = deque(maxlen=max_entries)
self.axes = axes
self.max_entries = max_entries
self.lineplot, = axes.plot([], [], "ro-")
self.axes.set_autoscaley_on(True)
def add(self, x, y):
self.axis_x.append(x)
self.axis_y.append(y)
self.lineplot.set_data(self.axis_x, self.axis_y)
self.axes.set_xlim(self.axis_x[0], self.axis_x[-1] + 1e-15)
self.axes.set_ylim(0, 0.2)
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
def animate(self, figure, callback, interval = 50):
import matplotlib.animation as animation
def wrapper(frame_index):
self.add(*callback(frame_index))
self.axes.relim(); self.axes.autoscale_view() # rescale the y-axis
return self.lineplot
animation.FuncAnimation(figure, wrapper, interval=interval)
def main():
from matplotlib import pyplot as plt
# Walabot_SetArenaR - input parameters
minInCm, maxInCm, resInCm = 30, 150, 1
# Walabot_SetArenaTheta - input parameters
minIndegrees, maxIndegrees, resIndegrees = -4, 4, 2
# Walabot_SetArenaPhi - input parameters
minPhiInDegrees, maxPhiInDegrees, resPhiInDegrees = -4, 4, 2
# Configure Walabot database install location (for windows)
wlbt.SetSettingsFolder()
# 1) Connect : Establish communication with walabot.
wlbt.ConnectAny()
# 2) Configure: Set scan profile and arena
# Set Profile - to Sensor-Narrow.
wlbt.SetProfile(wlbt.PROF_SENSOR_NARROW)
# Setup arena - specify it by Cartesian coordinates.
wlbt.SetArenaR(minInCm, maxInCm, resInCm)
# Sets polar range and resolution of arena (parameters in degrees).
wlbt.SetArenaTheta(minIndegrees, maxIndegrees, resIndegrees)
# Sets azimuth range and resolution of arena.(parameters in degrees).
wlbt.SetArenaPhi(minPhiInDegrees, maxPhiInDegrees, resPhiInDegrees)
# Dynamic-imaging filter for the specific frequencies typical of breathing
wlbt.SetDynamicImageFilter(wlbt.FILTER_TYPE_DERIVATIVE)
# 3) Start: Start the system in preparation for scanning.
wlbt.Start()
fig, axes = plt.subplots()
display = RealtimePlot(axes)
display.animate(fig, lambda frame_index: (time.time() - start, random.random() * 100))
#plt.show()
#fig, axes = plt.subplots()
#display = RealtimePlot(axes)
while True:
appStatus, calibrationProcess = wlbt.GetStatus()
# 5) Trigger: Scan(sense) according to profile and record signals
# to be available for processing and retrieval.
wlbt.Trigger()
# 6) Get action: retrieve the last completed triggered recording
energy = wlbt.GetImageEnergy()
display.add(time.time() - start, energy * 100)
#This is just for prototype purposes, we will gather the data in bulk and send them to the server in the future
if energy * 100 > 0.05 and energy * 100 <= 0.15:
urllib.request.urlopen("http://{your_server_link}/input?medium=1&high=0").read()
elif energy * 100 > 0.15:
urllib.request.urlopen("http://{your_server_link}/medium=0&high=1").read()
plt.pause(0.001)
if __name__ == "__main__": main()
Step 2: Walabot Server data storageIn order to track sleep over time, it's a good idea that we we store the data in the cloud. In this example, we are setting up a simple file storage in a file, but in the future we can store that into mongodb.
For this example we will be using node.js and host through heroku.
We can follow through the guide to setup your server: https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
Once server is setup, use the following code attached as your base. You can choose to host on other places such as Amazon, Azure, or IBM Bluemix; this is just a quick example to spin up the server.
var fs = require('fs');
var app = express()
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
fs.readFile('data.txt', 'utf8', function readFileCallback(err, data){
if (err){
console.log(err);
} else {
obj = JSON.parse(data); //now it an object
res.send(JSON.stringify(obj));
}});
})
app.get('/input', function (req, res)
{ var fs = require('fs');
var high = req.query.high;
var medium = req.query.medium;
fs.readFile('data.txt', 'utf8', function readFileCallback(err, data){
if (err){
console.log(err);
} else {
obj = JSON.parse(data); //now it an object
obj.high += parseInt(high);
obj.medium += parseInt(medium); //add some data
json = JSON.stringify(obj); //convert it back to json
fs.writeFile('data.txt', json, 'utf8', null); // write it back
res.send('success')
}});
})
Let Walabot update the sever once it reaches the threshold.
if energy * 100 > 0.05 and energy * 100 <= 0.15:
urllib.request.urlopen("http://{your_server_link}/input?medium=1&high=0").read()
elif energy * 100 > 0.15:
urllib.request.urlopen("http://{your_server_link}/medium=0&high=1").read()
Step 3: AlexaUsers can now use Alexa to find out how good your night of sleep was. We will use the Alexa Quick Skill kit through following this guide: https://developer.amazon.com/alexa-skills-kit/alexa-skill-quick-start-tutorial
The guide will teach you:
- Create Lambda function on AWS
- Create Alexa Skill on Alexa skill
Lambda hosts a serverless function which Alexa can interact with. Create one empty one using node.js instead of following the guide. We can copy/paste the Alexa node.js code from below.
After creating the function, we would have the ARN number. Write it down so we can use that in configuration for the Alexa Skill kit. We would also have to add the Alexa Skill kit into the sleep tracker - copy and paste the entire node.js code.
The intelligence right now is hosted in Alexa and it checks for moving around vs. moving a lot, such as getting up. This way we can take the off loads from the server.
Now we are moving to the Alexa Skill kit:
In the Interaction model, put the following Sleep Tracking Intent schema there:
Intent Schema:
{
"intents": [
{
"intent": "SleepTrackIntent"
},
{
"intent": "AMAZON.HelpIntent"
}
]
}
Sample Utterances:
SleepTrackIntent How was my sleep
SleepTrackIntent How was my sleep tracking
After that, in configuration, we can put the ARN that we used earlier:
Now you can test your Alexa skill by asking "Alexa, ask Sleep Tracker how was my sleep?"
What's next?We can do machine learning on how well the sleep is rather than writing our own algorithm. That will be done in the future.
Comments