Create a visual and audio notifier

In one of my previous posts, I had shown you how to hack an Amazon dash button to do whatever you want. In our case, we had it so that it alerted us via SMS and Telegram. But what if we want to be visually alerted as well as by sound? Alexa will soon open their notification API, but until then, we have to make our own. On the plus side, this will look cooler!

Hardware

This is what you will need:

  • 1 x Raspberry Pi
  • 1 x SD Card
  • 1 x Power Supply for Raspberry Pi
  • 1 x Unicorn Hat (if you want the display version)
  • 1 x Bluetooth Speakers (if you want the audio version), I used one of my Amazon Echos

Setup

Now, I must admit, this is very tricky, especially the bluetooth part, however I’ll do my best to explain.

Bluetooth

Security Policy

For this part, you will have to access bluetooth through the pi user (default raspberry pi user) instead of root.
Start by adding editing the D-bus security policy:

sudo nano /etc/dbus-1/system.d/bluetooth.conf

add this to the file:

<policy user=”pi”>
<allow send_destination=”org.bluez”/>
<allow send_interface=”org.bluez.Agent1″/>
<allow send_interface=”org.bluez.GattCharacteristic1″/>
<allow send_interface=”org.bluez.GattDescriptor1″/>
<allow send_interface=”org.freedesktop.DBus.ObjectManager”/>
<allow send_interface=”org.freedesktop.DBus.Properties”/>
</policy>

 

Pair your speaker

Start by putting your speaker in paring mode.

pi@raspinotifier:~ $ bluetoothctl
[NEW] Controller XX:XX:XX:XX:XX:XX raspinotifier [default]
[bluetooth]# scan on
Discovery started
[CHG] Controller XX:XX:XX:XX:XX:XX Discovering: yes
[NEW] Device XX:XX:XX:XX:XX:XX Echo Dot-1KU <- My Speakers
[bluetooth]# scan off
Discovery stopped
[CHG] Controller XX:XX:XX:XX:XX:XX Discovering: no
[bluetooth]# pair XX:XX:XX:XX:XX:XX
Attempting to pair with XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Connected: yes
[CHG] Device XX:XX:XX:XX:XX:XX Modalias: usb:v1949p1200d5100
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: yes
[CHG] Device XX:XX:XX:XX:XX:XX Paired: yes
Pairing successful
[CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: no
[CHG] Device XX:XX:XX:XX:XX:XX Connected: no
[bluetooth]# trust XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Trusted: yes
Changing XX:XX:XX:XX:XX:XX trust succeeded

at this point, if you try to connect to your speakers, you will get an error. This seems to be a limitation of the command line. To get around it you need to do some edits.
edit the following file:

nano ~/.asoundrc

add this to it:

defaults.bluealsa.interface “hci0”
defaults.bluealsa.device “XX:XX:XX:XX:XX:XX”
defaults.bluealsa.profile “a2dp”
defaults.bluealsa.delay 10000

Where “XX:XX:XX:XX:XX:XX” is the mac address of your speakers.
You should now be able to connect

[bluetooth]# connect XX:XX:XX:XX:XX:XX
Attempting to connect to XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Connected: yes
Connection successful
[CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: yes
[Echo Dot-1KU]#

 

Python

Required packages

These are the packages you will need to get from pip3:
  1. requests
  2. flask_restful
  3. flask_httpauth
  4. redis

This you will need to get from pimoroni:

  1. unicorn-hat (make sure to follow the instructions)

Components

I decided to segregate the components into 3 scripts:

  1. The REST server
  2. The visual notifier
  3. The audio notifier

This allows us to be a bit modular in our design.

The REST server

The purpose of this application will be to accept incoming requests for notifications. Essentially, through a REST interface we will be accepting requests for notifications and storing them into redis, an in-memory database for further processing. I won’t go into specifics aside from the comments on the code itself, but if you’d like to learn more, Miguel has an excellent tutorial.

Here is the code

The visual notifier

The purpose of this application will be to cycle through the notifications stored on redis and display the appropriate icon on the Unicorn HAT. In addition to that it will also take care of deleting expired notifications.

Here is the code

The audio notifier

The purpose of this application will be to cycle through the notifications stored on redis and sound the appropriate notification through the bluetooth speaker.

Here is the code

Testing

To test this, you can create a simple script that publishes to your rest server. Here is a sample:

import requests
from requests.auth import HTTPBasicAuth
from time import time
user = ‘pi’
password = ‘raspberry’
url = ‘http://127.0.0.1:5000/notification/api/notify’
payload = {
‘id’ : 1,
‘duration’ : 2 ,
‘expiration’ : time() + 10
}
r = requests.post(url=url, json = payload, auth=HTTPBasicAuth(user, password))
print(r.text)
print(r.json())
print(r.status_code)

 

Try it out!

Other thoughts

You might want to think about setting these up as services so they start up when you turn on the pi. This can be accomplished by running it as a service.

Conclusion

This was a very fun project for me. I learnt a lot about how bluetooth works within linux. Please let me know if you find this useful and what are you going to use it for. I personally will be paring it with my alexa dash button hack.

Links

Here are some of the links that helped me through creating this project:

 

Leave a Reply

Your email address will not be published. Required fields are marked *