Bridging Bluetooth LE to different wireless connections, such as cellular, is challenging and there are few out of the box solutions. This is an important piece of the IoT puzzle, creating stationary gateways from low power devices to long range backhauls without requiring smartphones. This tutorial will walk you through setting up such a gateway in a power and data efficient way.
The gateway will consist of a bluz Gateway Shield and a Particle Electron. The Electron is a 2G/3G cellular development board that operates on the Particle cloud. It comes with a Web IDE, REST API, integration with Webhooks and IFTTT, and is provided with a low cost data plan and SIM card. Bluz is an nrf51822 based development platform that has two parts: development kits (DK) and gateways. Each DK is also capable of operating on the Particle cloud with the same features as the Electron. The gateway is what makes this possible, bridging Bluetooth LE to WiFi or cellular, though this tutorial will only focus on the cellular portion.
The hardware for the gateway is designed to plug together simply and easily, the socket on the gateway shield is compatible with any Particle board. The gateway shield has an nrf51822 running s120 in Central mode which transfers data to/from the Electron over SPI.
While we could run this system as-is and allow each bluz board to connect to the Particle cloud, it would use up a lot of data. The Electron comes with a 1MB data plan per month for a low cost, and then there are additional charges for each 1MB after that. In the US, this is $2.99 for the first MB and $0.99/MB after that. While this is very cheap, allowing each bluz board to connect could consume a few MB per month as keep-alive packets are sent periodically. To mitigate this, the gateway will be setup such that each bluz board doesn't connect to the cloud, but can communicate to the Electron which can relay information.
To do this, follow the instructions on the bluz documentation website to setup the gateway shield. You can then login to the bluz console to verify the gateway shield has the latest firmware release (though you should update over WiFi using a Photon or Core if you want to save data on your Electron plan as a firmware update can be over 100KB).
You will need to change the base example for the Electron with one additional line of code:
gateway.set_ble_local(true);
This line will tell the nrf51822 Central to not connect to the cloud, normally this would happen so the device can be updated itself. Turning this off allows the system to save data.
The entire code for the gateway can be seen here:
You must also turn off the cloud connection in each bluz board. This can be seen in the following sketch:
Each bluz DK will then only connect to the gateway and pass data to the Electron, meaning no data will be sent to the cloud. This means OTA updates will be disabled, but there is a safeguard in the code where the user can pull the pin D6 HIGH at startup time to change the behavior to connect to the cloud, allowing updates easily again.
Flash this code to your bluz DK (again, updating using the bluz smartphone apps or WiFi to save data) and then let it connect to the gateway shield with the Electron.
Any data sent from the BLE.sendData() function of any bluz board would be sent to the Electron. The Electron can then re-send that data to any cloud service, aggregate or analyze it, or perform any other task with it. Data usage will be limited to only what the Electron sends, allowing for the lowest cost data plan as possible. And, the bluz DK boards can act as low cost, low powered input/output devices to the Electron, allowing battery powered BLE devices to send data to the cloud over cellular.
The Electron can poll which devices are connected to the nrf51 central as well, meaning the Electron can ask each DK to send data only when it is ready. This would allow cases where the Electron could be entirely powered off for some period of time and only wake up periodically to poll each DK and transmit the data. This can be beneficial if the cellular gateway needs to run from battery or solar power.
The two code examples for this tutorial can be found here:
- Electron: https://gist.github.com/eely22/1a6b6c295345a0bb36d006a998a65d57
- Bluz DK: https://gist.github.com/eely22/ada2225b402ffa221123ad69f0027309
All items for this tutorial, including the Electron with SIM card, can be purchased from the bluz store. All documentation for the Electron and bluz can be found here:
- Electron: https://docs.particle.io/reference/firmware/electron/
- Bluz: http://docs.bluz.io/