Almost everyone knows elderly people, which need to take a daily medication. But unfortunatly, it is getting harder for them to remind themselve to take there medication the older they get. For some of them it is already not possible without some external reminder, like an alarm clock. That's why the PillPal was created. This device is connected to your Matter home network and sends a reminder to your mobile phone, when the medication was not taken. Luckly, several companys, like Apple, Google and Samsung, are already providing a stable solution to integrate a matter device into your lokal network.
How it works:
The nRF7002DK is connected to your Apple HomePod (Mini) over Matter protocol. At the same time it is also connected to the Nordic Thingy:52, which will detect, if the user still needs to take the pills. This is done by the color sensor of the Thingy:52 Prototyping Platform. Nordic Semiconductor already provides a precompiled firmware for the Thingy:52, which allowes direct access to the color sensor via BLE. Cool!
The color sensor can check periodically the color of the pills. The pills can be very easily be detected in a black box since normal pills are usually white or any other bright color. So, only a box is needed to place the pills to the location of the color sensor. Additionally, the Thingy:52 supports playback of sound samples, which can be used as a reminder before actually sending a message to the Matter Bridge.
With the help of Apple HomeKit we can create automations to get a notification when pills are detected at a certain time. Easy :)
The next chapters will show you how to recreate your own PillPal.
Chapter 1: Thingy:52 PreparationAt first, you need to upload the latest firmware to your Thingy:52. You can find the precompiled hex-file in the download section of the Thingy Product page.
https://www.nordicsemi.com/Products/Development-hardware/Nordic-Thingy-52/Download
- Turn Thingy:52 on
- connect Thingy:52 to your nRF7002DK board (or any other Nordic Semiconductor Dev board. Here nRF5340-DK
- Connect USB cable to development board (nRF7002-DK or nRF5340-DK)
- Drag and Drop the Hex file to J-Link device on your PC to upload the firmware to the Thingy:52
- Disconnect all cables. Done!
First you need to create a new Project in VSCode. Copy the matter/template project from sample projects. This sample also provides a zap-file to start with (src/template.zap in your project folder).
Now we can open the zap-file with ZCL Advanced Platform Tool. Navigate to your nRF Toolchain folder.
cd /c/ncs/v2.5.0/modules/lib/matter
Open ZCL Tool
./scripts/tools/zap/run_zaptool.sh ~/Documents/Nordic/template/src/template.zap
Add an additional endpoint to the configuration. Select Matter Generic Switch (0x000F) as device.
Navigate to the Configuration of the Entpoint
Go to configuration of CHIP/Switch and change Feature Map to value 14. This enables Short Press and Long Presses Events. Do not forget this step because otherwise you will not be able to receive the Events with your HomeKit App.
Now you have to save the zap-file and press Generate button. Select project folder/src/zap-generated (~/Documents/Nordic/template/src/zap-generated) as output.
Chapter 3: Create firmware for nRF7002You can find the complete project in attachment below
To understand how to create a Matter project you can check a Nordic Semiconductor Webinar (33:45).
This video gives more information on how to create a simple button project.
Below you can see one of essential modifications in the code.
Create a Matter"Single Press" Actionon control switch event:
void AppTask::ControlSwitchHandler(const AppEvent &event)
{
if(event.ButtonEvent.Action != static_cast<uint8_t>(AppEventType::ButtonPushed)){
return;
}
uint8_t newPosition{1};
DeviceLayer::SystemLayer().ScheduleLambda([newPosition]{
Clusters::Switch::Attributes::CurrentPosition::Set(kGenericSwitchEndpointId, 1);
Clusters::SwitchServer::Instance().OnInitialPress(kGenericSwitchEndpointId, 1);
Clusters::Switch::Attributes::CurrentPosition::Set(kGenericSwitchEndpointId, 0);
Clusters::SwitchServer::Instance().OnShortRelease(kGenericSwitchEndpointId, 0);
});
}
This function creates several events on the Matter network and creates a "Single Press" action at your Matter Controller (Home Pod mini). So calling this function gives us the possibilty to interact with the HomeKit App.
Now it is neccesary to establih a connection to the Thingy:52 via Bluetooth to read back the color value of the sensor and give acoustic feedback, when the user has forgotten to take the pills.
Create a Bluetooth connection and interact with Thingy:52
First of all, you should read the Nordic documentation for the Thingy:52 firmware and it's Bluetooth services:
Thingy:52 firmware and BLE services
If you can not find your Thingy:52 during BLE scan, then it has maybe turned off it's advertising messages. Turn it back on again by pressing the button and disable the timeout by setting it to 0 in the Thingy52 App.
In order to connect to a Thingy:52 device we need to scan for nerby BLE devices and filter for the Thingy Configuration Service UUID.
EF68
0100
-9B35-4933-9B10-52FFA9740042
When we have found a device with such a Service UUID we can connect to it and start the discovery of the Enviroment Service, where we can find the Color Characteristic and the service for sound interaction.
Environment service:
EF68
0200
-9B35-4933-9B10-52FFA9740042
and the Sound service:
EF68
0500
-9B35-4933-9B10-52FFA9740042
We received the handler IDs of all characteristics after discovery has been finished. So, enabling the notification for the color characteristic is straight forward, by just writing 0x0001 to the Client Characteristic Configuration Descriptor (CCCD) of the characteristic. Now, we should receive every second a array of data which represends the color data.
As described in the documentation the Color Characteristic data contains the following values.
- uint16_t - Red
- uint16_t - Green
- uint16_t - Blue
- uint16_t - Clear
I decided, that the red value is good enough for detecting the presens of a pill on top of the sensor, after doing some tests.
As one of the last parts of the firmware, we can use the speaker in the Thingy to give the person, who is using this device, an acoustic feedback, when the pill has not been taken in time. The approach is very simialar to the one above when reading the color value.
It is also nessecary to change a configuration in the Sound Service to playback audio samples directly on the Thingy:52.
So, we have to change the speaker mode in sound config
EF68
0501
-9B35-4933-9B10-52FFA9740042
to "sample mode" by sending 0x03 0x01 to the configuration characteristic.
After that we have access to the sound samples (0-8) at the Speaker Data Characteristic
EF68
0502
-9B35-4933-9B10-52FFA9740042
Sending any value from 0 to 8 to that characteristic is all we need to do.
That's already all we need to know about the interaction with the Thingy:52 via BLE.
Chapter 4: Print 3D part and build a boxDownload the stl-file and print it. When printing it with a bright filament color you might need to paint it in black afterwards, otherwise detection of the pills might not be as stable as expected.
Test Thingy:52 color sensor:
You can check if the Color sensor can detect the pill with the Thingy:52 App (available for Android and iOS). Just open the App, connect to your Thingy and readback the color value. You can see a white blink below the pill every time the color value gets updated.
Connect Thingy:52 to nRF7002-DK via Bluetooth
The nRF7002-DK will always scan for a single Thingy device in range and connect to it.
Connect nRF7002-DK to Matter
Open HomeKit App and scan QR code to establish a connection the your Matter network.
Configure "Single Press" Action for your Matter Accessory device to execute a action on your phone. Playing some music on your HomePod is an very easy way to test if it works. A more advanced application is to open an URL which sends a notification back to your mobile phone.
Short clip of the PillPal in action ;)
Chapter 6: What's next?- Set up a webserver on a Raspberry Pi and connect HomeKit App to it. Use Node-Red to catch URL requests from HomeKit to send notifications to to other webservices.
- use pairing between Thingy:52 and nRF7002-DK to increase level of security
- improve the box and 3D print to avoid ambient light to interact with the color sensor, when the box is open, since this could lead to false detection of pills.
Comments