Module for the MagicMirror² smart mirror.
Leverages a bluetooth button, Puck.js, for interactive functionality on your MagicMirror. These are fun little contraptions. I had one kicking around and decided to integrate it into my MM installation.
I really wanted to try to set this up as a Javascript-only package, but the available JS libraries for Bluetooth are all rather terrible, and contain lots of old/outdated packages with a ton of vulnerabilities. Not a lot of people must be doing things with Bluetooth and JS, perhaps. In any case, no one wants a bunch of vulnerabilities either.
As a result, this requires Python to work, but I have tried to make it as seamless as possible once you get past the initial installation.
- Python3
- bleak (Python Bluetooth package)
To install the module, use your terminal to:
- Navigate to your MagicMirror²'s modules folder. If you are using the default installation directory, use the command:
cd ~/MagicMirror/modules - Clone the module:
git clone https://github.com/themox/MMM-Puck.git - Navigate into the module directory:
cd MMM-Puck - Install required packages:
npm install - Install the Python virtual environment and the required packages for Bluetooth. The below commands take you through installing Python3 and the virtual environment package for it, making a virtual environment, activating it, upgrading
pip, and then installing the required Bluetooth package,bleak. The environment is also deactivated afterwards as it is not necessary or desired to remain in it.node_helper.jswill take care of running the Python script in the required environment.
sudo apt install python3 python3-venv
python3 -m venv puckenv
source ./puckenv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade bleak
deactivate
- Follow the instructions and use the espruino web IDE to load the required code onto your Puck. This code is a super simple button press app. There are two functions in button.js; the first is an optional connection light indicator so you don't have to keep spamming the button press to see if it has reconnected. If you don't want that feature, simply don't copy it into your Puck. You will conserve battery without it.
From the MagicMirror/modules/MMM-Puck/ path:
- Pull down the latest files from GitHub:
git pull - Remove the old npm files:
rm -rf node_modules package-lock.json - Install new/updated packages:
npm install - Update Python Packages:
source ./puckenv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade bleak
deactivate
- Then restart your MagicMirror.
I've run into a couple cases where, when I tried to update, the environment would not work correctly for some reason (couldn't find pip, etc). If this happens, you simply need to uninstall the Python virtual environment and recreate it.
deactivate
rm -rf puckenv
python3 -m venv puckenv
source ./puckenv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade bleak
deactivate
To use this module, add the following configuration block to the modules array in the config/config.js file:
{
module: 'MMM-Puck',
config: {
// This section is entirely optional
buttonMsg: "PAGE_INCREMENT",
retryDelay: 3,
scanTimeout: 10,
pollInterval: 1,
}
},| Option | Details |
|---|---|
retryDelay |
Integer; Seconds to wait before retrying to connect to Puck Default value: 3 |
scanTimeout |
Integer; Seconds to wait before timing out scanning Default value: 10 |
pollInterval |
Integer; Seconds between checking for input while connected Default value: 1 |
buttonMsg |
String; The message you want the MagicMirror to send when the button is pressed. Default value: PAGE_INCREMENT |
These arguments are all optional; if they work for your situation, you can leave the section in the brackets in config: {} above blank.
I use the button with the mmm-pages module. In this use case, I have the button emit the PAGE_INCREMENT message which causes mmm-pages to advance the visible page. Set buttonMsg in the config above to the message type that you want to emit to cause the desired behavior in another module(s).
In normal usage, the button will disconnect after a while in order to save energy. This is expected. You might also bring your button out of range of your MagicMirror, which will cause it to disconnect as well. Or, your battery might need to be replaced. In any case, if the module loses connection with the button, it will go into a scanning mode until it finds the button again, at which point it will reconnect. You might have to press the button a couple times to get it to turn back on and reconnect.
node --run lint- Run linting checks.node --run lint:fix- Fix automatically fixable linting errors.