Skip to content

Latest commit

 

History

History
226 lines (152 loc) · 8.94 KB

README.md

File metadata and controls

226 lines (152 loc) · 8.94 KB

Bluetooth Low Energy remote control for Spotify Build Status

Bluetooth Low Energy remote control for Spotify on macOS with Node.js and AppleScript.

Diagram

Description

This project allows you to configure your micro:bit to work as a Bluetooth Low Energy (BLE) remote control to control an instance of Spotify running on macOS with Node.js and AppleScript. Users can use micro:bit to perform several actions like play or pause tracks, turn volume up or down and go to next or previous track. To carry out this actions, a custom event with a specific value for each action is raised over BLE when the micro:bit buttons are pressed by the user. The events raised by the micro:bit are catched by a Node.js script that is running on a computer with macOS.

The project is composed by two pieces of software:

  • the remote control, that is the software that runs on the micro:bit.
  • the event listener, that is the software that runs on your computer.

Video

YouTube Video

How it works

Diagram

  1. In this diagram the micro:bit is ready in the Play state, this is known because the micro:bit is showing the letter P on the LED screen. An user press the A button and then a custom event is raised over BLE, with an specific value to indicate that we want to play or pause a track on Spotify.

  2. The event listener is a Node.js script that is running on the computer and is waiting for the events raised over BLE by the micro:bit device.

  3. When a custom event is catched by the Node.js script it uses the API of the spotify-node-applescript package in order to control the instance of Spotify running on the computer.

Remote Control details

States

The micro:bit device can be in one of a finite number of states at any given time. The list of available states are the following:

micro:bit's LED screen State Description
Disconnected State Disconnected The micro:bit device is not connected with the computer via BLE.
Connected State Connected The micro:bit device is connected with the computer via BLE.
Play State Play The micro:bit device is ready to play or pause a track.
Volume State Volume The micro:bit device is ready to turn volume up or down.
Next State Next The micro:bit device is ready to go to next or previous track.

Inputs

The state machine can change from one state to another in response to external inputs. In our project there are two possible inputs:

Input Description
BLE When the micro:bit establishes a BLE connection with a computer.
AB When the micro:bit buttons A and B are pressed together.

Transitions

The change from one state to another is called a transition.

State machine diagram

The state diagram for the project can be modeled as follows:

State Machine

Where each state is represented by a node (circle) and the edges (arrows) show the transitions from one state to another. Each arrow is labeled with the input that triggers that transition.

Actions

The actions that users can perform to control Spotify via BLE are:

  • Play or pause tracks.
  • Turn volume up or down.
  • Go to next or previous track.

To run these actions the micro:bit device needs to be in one of these states:

  • P (Play).
  • V (Volume).
  • N (Next).

When the micro:bit is in one of these states the user can press the A or B buttons in order to raise a custom event to control Spotify. This table shows what are the different possibilities that an user can perform.

Input \ State P V N
Press A button Play or pause track Turn volume up Go previous track
Press B button Turn volume down Go next track

No pairing security

Although this script is using BLE there is no need to pair your micro:bit with your computer. In order to get this, the pxt.json file has been configured as follows. The property of the bluetooth json object called open has been configured with a value of 1 which means no security.

"yotta": {
    "config": {
        "microbit-dal": {
            "bluetooth": {
                "open": 1
            }
        }
    }
}

How to build and deploy the remote control

  1. Install node.js.

  2. Install the PXT (Microsoft Programming Experience Toolkit) command line tool with npm.

npm install -g pxt
  1. Clone this repository to your computer.
git clone https://github.com/josejuansanchez/microbit-ble-spotify.git
cd microbit-ble-spotify
cd ble-remote-control
  1. Install the micro:bit target for PXT. This target allows you to program a micro:bit using PXT.
pxt target microbit
  1. Install the required PXT packages for this project (core and bluetooth). The dependencies of the project are defined in pxt.json.
pxt install
  1. Connect the micro:bit to your computer via USB.

  2. To build and deploy the project, execute pxt with no arguments or run make. Doing this, PXT will compile and link the code into an .hex file and will deploy it into your connected micro:bit.

pxt

When you run the pxt command, you will see a similar message to this:

Using target PXT/microbit with build engine yotta
  Target dir:   /microbit-ble-rc-spotify/node_modules/pxt-microbit
  PXT Core dir: /microbit-ble-rc-spotify/node_modules/pxt-core
running 'pxt deploy' (run 'pxt help' for usage)
Package built; written to binary.hex; size: 605847
copying binary.hex to /Volumes/MICROBIT/
   wrote hex file to /Volumes/MICROBIT/

Now, your micro:bit device is ready to work as a remote control for Spotify on macOS with Node.js and AppleScript. The next step is to run the ble-event-listener script to listen to the custom events raised by the BBC micro:bit over BLE.

Compiled Firmware

The compiled firmware is available in the resources/firmware folder of the project. If your want you can avoid the build process and copy the .hex file to micro:bit disk drive.

How to run the event listener

  1. Go to ble-event-listener directory.
cd ble-event-listener
  1. Run the Node.js script:
node index.js

When you run the script, you will see a similar message to this:

Scanning for micro:bit...

Now, your event listener is ready to accept BLE connections from a micro:bit device.

Packages used in this project

References

Author

Developed by José Juan Sánchez.

License

Licensed under the GNU General Public License, Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.gnu.org/licenses/gpl-3.0.en.html

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.