We all forget things, but somethings are important to remember. Often it is important for medicine to be taken before or after a meal, or at a certain time of day. The effectiveness of the medicine sometimes depends on the medicine being taken on the correct schedule.
Sometimes an interruption to our schedule or a distraction makes it much more likely to miss taking a medication. Sometimes our symptoms make it difficult to remember or keep a schedule.
Project GoalsMake a helpful reminder system with a simple Alexa Voice Skill and the Raspberry Pi computer, with a Hexiwear wearable haptic wrist reminder.
Design ConsiderationsHexiwear Reminder DeviceA Hexiwear wearable device is used as wrist worn reminder to take medicine. Hexiwear has a haptic motor and an OLED display to help with the reminder. The goal is to use the Alert/Command service to cause an event that can be detected by the K64 microcontroller, this will cause the haptic motor to vibrate serving the reminder to take medicine. Raspberry Pi will cause the Alert In function to occur by communicating a BLE Write with response to Hexiwear.
I am looking at the WolkSense Android App to learn how to establish a password key needed for encrypted BLE transactions with Raspberry Pi.
I have placed a request on the Hexiwear Forum for information on how to do this. I have also spent some time on web searches regarding using Raspberry Pi with encrypted BLE transactions.
When I figure out what needs to be done, I am hoping to write a python program or a bash shell script that establishes the encrypted BLE communications between Raspberry Pi and Hexiwear.
The possible programs on Raspberry Pi are bluetoothctl and gatttool.
There is a python library written as a wrapper around the gatttool program, so a nice implementation can be done in python. I have also learned how to execute bash shell scripts in python. If you know how to do this please leave some comments!!!
Hexiwear Raspberry Pi BLE, status 9/24/2016I have made sure that the bluez BLE stack is up to date on Raspberry PI by typing in the following bash shell command:
sudo apt-get install bluez bluez-firmware
I have also made sure that the service for pairing is up to date:
sudo apt-get install blueman
As a result of the update process new blueman code was installed, and this looks closer to being able to install the 6 digit random pin, but I am still not able to get Raspberry Pi to issue a secure pairing request to Hexiwear that causes the 6 digit random pin to be generated by the Hexiwear device.
I have messed with using the gatttool program to set sec-level to high and set sec-level to medium, but still no luck.
Workaround being consideredI really did not want to mess with the firmware for the KW40 BLE processor on hexiwear, but I am having fun reading non-encrypted data like the battery level from Hexiwear. I am considering removing the encryption from the data item so that a non-secure BLE write will cause the haptic vibrate function.
I am not happy with this as a solution, because it seems like a step backwards. Real products these days have encryption.
Hexiwear Raspberry Pi BLE, status 10/02/2016An email I have received regarding the Raspberry Pi - BLE secure pairing problem suggests that the bluez BLE software stack must be improved so that a secure pairing request can be issued to Hexiwear. When I have more definitive information I will post it to the Hexiwear forum, in the Firmware and libraries section, under the topic:
A BLE solution that will work until the bluez BLE software stack is improved, status 10/7/2016
I have received , in an email from MicroElektonika, the requirements for initiating the secure pairing:
As for the bluetooth, we used security mode 1 with level 3 (Authenticated pairing with encryption).
IO Capabilities of device is Display Only. So your device must support KeyboardOnly capabilities for authenticated connection.
I have information from Cypress Semiconductor , that it is possible to make a BLE PSoC firmware project that will do the secure pairing with Hexiwear. Since we can make the Cypress BLE module perform in the GAP Central role and implement Bluetooth 4.2 security roles. It should work as promised by Cypress. The idea is to use the BLE PSoC as a serial peripheral (SCI or SPI based ) for Raspberry Pi 3. I know this seems like much work to go through, but the good news is that we will no longer be dependent on the development state of open source BLE software stacks, or be tied to any specific hardware platform!
I am afraid I have run out of time to complete the BLE link to Raspberry Pi ! So look for this to be brought to completion and demonstrated in future Hackster.io projects. I have the Cypress BLE evaluation kit and using the BLE PSoC from Cypress will allow me to use Hexiwear for a bunch of cool projects, involving other hardware platforms.
Autodesk Fusion 360 designBecause of some of the challenges with getting the BLE to Raspberry Pi connection working, I decided to take a break and design a storage container for Hexiwear Click Modules , and also to hold the Hexiwear Docking Station and keep it from sliding around on my desk. The design is in two pieces that snap together to form a hinge. The lower base piece has 12 compartments for Click modules. The storage container is sized to fit the cardboard box that the Docking Station ships in from MicroElectronika.
The link to the storage box, in the Autodesk Fusion 360 gallery, lets you view the storage box design from various angles and even zoom in close to look at the compartments inside the box.
https://gallery.autodesk.com/fusion360/projects/click-storage-box?popupSNS=true
I spent some time exploring existing Alexa Voice Skills for inspiration and possible re-use in this project. The Google calendar Alexa voice skill does much of what I would like to see in a commercial product version of the medicine reminder. But an API for connecting this to the Internet of Things is not available just yet.
I also liked the voice user interface used with the timers which are available with Alexa, but I don't think there are enough timers available for a week long medicine schedule.
Observations About Voice SkillsAlexa Voice Skills seem best suited to a short use time, like that of the trivia game example. So to keep things simple I kept my project aligned to the short use time , and did not try to create an Alexa voice skill that always seems to be on.
Data PersistenceI also considered whether the time schedule and timing process should be kept in the cloud. In a commercialized version, user account information would provide a means to store a schedule in the cloud. But keeping things simple for now, Alexa is used merely to collect the times and relay them to the Raspberry Pi IoT device.
How it WorksA List of Times to take Medicine
Raspberry Pi runs a Python application called medminder.py , that keeps a list of times to take medicine. The application is also listening for updates from the My Medicine Reminder Alexa Voice skill to Set a Reminder time or Clear a Reminder time.
Set a Reminder Time
Amazon’s built-in type AMAZON.DATE does most of the hard work in creating the voice user interface. It will handle voice input like "nine in the morning" and turn it into a string for the medminder.py application "09:00". Likewise it will take voice input like "nine in the evening" and turn it into a string "21:00". If there are improvements to this built in type by the programmers at Amazon, my voice skill will pick up those improvements immediately without any effort on my part. This is a good reason to use the built in types when possible.
The Python program medminder.py stores the time embedded in the SetReminder IoT message into the schedule list. A check is made to avoid placing duplicate times in the list. Because the schedule is a python list it can grow and shrink as needed while medminder.py operates.
An assumption is made that the medication schedule is the same every day of the week. I thought this was reasonable, a number of changes would need to be made to take this demonstration code and turn it into a consumer product.
Clear a Reminder Time
The program medminder.py takes the time embedded in the ClearReminder Iot message and removes it from the schedule list.
Timers and LEDs
medminder.py also keeps track of time and turns on LEDs according to the schedule and the day of the week. A LED indicating the day of week will stay lit for 5 minutes after the scheduled time. This time window can be increased if desired, I have it set to 5 minutes to simplify testing the software.
My Medicine Reminder Voice User InterfaceInvocation
- "Alexa ask My Medicine Reminder"
Set Reminder Time example phrases
- "Set reminder for nine in the morning"
- "Set reminder at eight am"
- "Set the reminder for ten thirty pm"
Clear Reminder Time example phrases
- "Clear reminder for nine in the morning"
- "Clear reminder at eight am"
- "Clear the reminder for ten thirty pm"
VUI Diagram
We will start in the middle working on creating an IoT thing , certificates, and a policy for accessing our newly created IoT thing. This really is the first step since the Raspberry Pi program medminder.py and our Lambda function named tomRaspPiGpioTest ( a javascript program that lives in the cloud ) need the certificates, so here we begin.
1) Create a root certificate.
Use the following web link and copy the text displayed into a file and name it rootCA.key
2) Create IoT thing
You will need an Amazon Web Services Account, there is a one year free trial, so get your account to continue on with the step by step procedure here. Here is a link to start the process if you need to do this.
https://aws.amazon.com/start-now/?sc_channel=BA&sc_campaign=elevator&sc_publisher=captivate
Select AWS IoT services from the console.
Select create a resource:
AWS IoT things
AWS Iot things are used by the AWS Lambda function named tomRaspPiGpioTest to send messages to medminder.py . Remember that medminder.py will switch the Raspberry Pi GPIOs that light up the LEDs. The AWS Lambda function lives in the cloud and wakes up when your Alexa Voice Skill needs to figure out what to do with your voice commands. Since it only wakes up when your Alexa Voice Skill is doing things, the Lambda function does not have to be awake all the times and that is why Lambda functions are so cool. Otherwise we would need a server that was awake all the time, chewing up resources and costing somebody money.
Select create a thing
Fill out the name of the IoT thing being created and click the create button. The name of the IoT thing in my example code is tom_thing, if you customize the name there will be a few edits to replace the occurrences of tom_thing with another name if you choose to use another name for the IoT thing.
Double click on the IoT thing just created to learn useful stuff that you will need in following steps.
I typically cut and paste the highlighted information pictured below and put it in a text file for use later, because the information you see on your screen will of course be different than what is in the the code you download for this project. Putting it in a text file will be useful when it is time to make the needed edits to insert your specific IoT thing's information.
3) Create Certificates
In this step two certificates are going to be created. One certificate will be used by the Lambda function tomRaspPiGpioTest and the other will be used by medminder.py.
Use the 1 - Click create and select the activate check box.
Download the certificate files
Do the steps over again to create a second certificate with 1 - click create with the activate checkbox selected, and download the three files associated with the second certificate.
4) Attach both of the certificates to the IoT thing
Fill out the dialog box with the thing name:
5) Create a policy
Select create a resource, then select create a policy.
Fill out the name of the policy, select iot.* as the policy and check the allow box, then click the add statement button. Fill in a * for the resource that you are enabling with this policy.
Click the create button.
6) Attach the policy
Attach the policy to the certificates you created, remember to do this for both of the certificates you created.
7) Create the Alexa Voice Skill.
You will need an Amazon Developer account if you do not already have one. Here is a link to get started with getting an account https://developer.amazon.com/
Select add a new voice skill
Select a Custom Interaction Model, fill in the name for the Alex Voice Skill and the invocation name. No is selected for audio player. Please note the Application ID as it will be put into a configuration file soon!
Click Save and Next to continue.
Copy and past the intent schema file and the sample utterances file into the interaction model. The files are located in the code files toward the end of this page.
Click Save and Next to continue.
On the configuration page, select no for user account information. Note there is an arrow for the Lambda ARN ( Amazon Resource Name ) . We will need to leave this window open on the web browser , as we will do the next steps that create the Lambda function. Or you can save and come back to the configuration page again later to fill in the Lambda ARN.
8) Download the zip file from the MyMedicine Reminder GitHub Repository .
I use the program 7-Zip to unzip the zip file because it handles long directory names that the standard Windows 7 unzip fails.
Modify index.js to reference your certificates, if your region is different you will want to change that too.
9) Modify config.js
Modify the config.js to reference the information from the IoT thing and the process ID from the Alexa Voice skill. This is the stuff the Lambda function tomRaspPiGpioTest has to get correct if it is to do its job.
From the AWS console select Lamda Services
10) replace the certificates
Replace the certificates in the certs directory with the certificates you created
11) Create a Lambda function
Create the Lambda function by zipping up the following directories and files and uploading. Note the ARN information for the Lambda function you just created, go back to the last part of
and fill in the ARN for the Lamda function, since the Alexa Voice skill needs the information to send messages to our newly created Lambda function. Zip up the directories and files I have shown highlighted below - producing the zip file.
Upload the zip file. Save the Lambda function. Now it exists in the cloud!
12) Modify medminder.py
Note the directory where medminder.py expects the certificates. Copy the certificates to that directory on Raspberry Pi. Modify medminder.py to reflect the proper information about the certificate file names, and IoT thing ARN and the IoT thing topic.
Wow! That was quite a bit to go though, I have tried to make it as easy as it can possibly be. If there is anything I can make more clear, please let me know in the feedback section. I will update the procedure so it is as much fun as possible.
Running the programThe program adds set times to the schedule list with a Set Reminder Alexa Voice skill intent. When the time clock of Raspberry Pi is within a 5 minute window after the set time the LED corresponding to the day lights up. The 5 minute window is easily changed in medminder.py if desired. I started with a low number to make testing easy.
The Clear Reminder Voice skill intent removes the specified time from the schedule list.
Invocation
- "Alexa ask My Medicine Reminder"
Set Reminder Time example phrases
- "Set reminder for nine in the morning"
- "Set reminder at eight am"
- "Set the reminder for ten thirty pm"
Clear Reminder Time example phrases
- "Clear reminder for nine in the morning"
- "Clear reminder at eight am"
- "Clear the reminder for ten thirty pm"
Alexa Cards
Alexa Cards will be used to show a smartphone user of the Alexa app, pictures of what the correct medicine pill looks like.
Comments