Bluetooth Low Energy Gateway for ClearBlade
This is a BLE Gateway for the ClearBlade Platform that can be executed on any Linux operating system (including Raspberry Pi) supporting bluetooth. The adapter requires the bluez library to be installed. The adapter connects to the ClearBlade platform over MQTT and sends status and error messages back to the platform for processing. In addition, the adapter will automatically scan for BLE devices and add those devices to the ClearBlade Platform.
Commands can be sent to this gateway from the platform for reading and writing data to and from connected BLE devices.
- Install the cb-cli tool from http://docs.clearblade.com/v/2/4-Developer_Reference/CLI/1_GettingStarted/
- You will need the ClearBlade Python SDK installed. Instructions can be found at https://github.com/ClearBlade/Python-API
- Eclipse Paho Python SDK is required. Installation instructions can be found at https://eclipse.org/paho/clients/python/
- Lastly, the bluepy BLE library for Python is required and can be downloaded from https://github.com/IanHarvey/bluepy
Import the BLE System
- Create a new account on the ClearBlade platform if you haven't already
- Clone this repository
- You will need to import the BLE system into your account
- After you have installed the cb-cli tool
cdinto the BLE directory
- Then run
cb-cli import -importrows -importusers
- It will ask you for the platform URL and your account username and password and then the system will be successfully imported
- This system contains all the code needed to process messages coming from the bluetooth gateway and also send messages to the gateway
- The BLEGateway collection contains the gateway status.
- The BLEDevice collection contains the low energy devices connected to the gateway.
- The BLEReadValues collection contains the data read from these low energy devices.
- The BLEErrors collection contains various error messages sent from the gateway.
Add device entry to devices table
Before we start the gateway, we need to add a device, representing the gateway, to the ClearBlade Platform.
- From the BLE system within the ClearBlade Platform Developers console, select Auth --> Devices
- Click + Device
- Enter the following information in the New Device dialog:
- Name: The name of the device
- Type: A description of the type of device being added (for example: Raspberry Pi 3 model B)
- Enabled: YES
- Allow Key Authorization: YES
- Click Create
- In the newly inserted row, double click within the active_key cell and enter an authentication key (ex. 1234567890)
Starting the Gateway
Open a terminal session on the device where the gateway will run and enter the following command:
- sudo python main.py --systemKey <SYSTEM_KEY> --systemSecret <SYSTEM_SECRET> --deviceName <DEVICE_NAME> --activeKey <ACTIVE_KEY> [--platformUrl <PLATFORM_URL>] [--scanInterval <SCAN_INTERVAL>]
Once the gateway has started, it will automatically:
- Start scanning for BLE devices
- Add any found devices to the platform
- Read any available information from the device
- Store data read from the device in the BLEReadValues collection