In the previous article, I have built a simple Resens BLE gateway running on a Raspberry Pi board. The gateway is able to discover BLE devices within its range, and extract the manufacturer data. It also can connect to devices to receive data sending through notification, and save them to local .csv files.

However, it would be useful if we could also upload this data to a cloud service. After doing a research, in the end, we have decided to check out Amazon Web Services (AWS) IoT platform.

In this post, I note steps to build a Resens Edge Gateway based on AWS IoT. This device is also ready to function as an edge device.

AWS IoT

I’ve registered a root account on AWS IoT. It is good that you could get a free trial period with a new account.

There are so many features on AWS IoT, which I’m not entirely familiar with. For now, I’m interested in testing AWS IoT Greengrass, which is a software that could extend cloud capabilities to local devices.

In the root account, I created the first IAM user. I’ve used this user to test AWS IoT features. In this user, I noted down Access key and Secret key. I would need this information when setting up a core device so that it can connect to AWS IoT cloud platform.

Setup AWS IoT Greengrass Core Device on Raspberry Pi

Follow the instructions here to install a Greengrass core device.

I’ve already got a Raspberry Pi running Raspbian lying around, and have access to it using SSH. Firstly, export user’s keys into the environmental variables

$ export AWS_ACCESS_KEY_ID=<user-access-key-here>
$ export AWS_SECRET_ACCESS_KEY=<user-secret-key-here>

Then download and start the installation script

$ wget -q -O ./gg-device-setup-latest.sh https://d1onfpft10uf5o.cloudfront.net/greengrass-device-setup/downloads/gg-device-setup-latest.sh && chmod +x ./gg-device-setup-latest.sh && sudo -E ./gg-device-setup-latest.sh bootstrap-greengrass-interactive

The next steps were simple, just follow instructions on the screen, reboot the board and restart the script as following

$ sudo -E ./gg-device-setup-latest.sh bootstrap-greengrass-interactive

If you access to AWS IoT Console, select Greengrass and Groups you should see a GreengrassTestingGroup.

I let the script installed an example lambda function called Hello World, which constantly publishes data to AWS IoT cloud. Test this function by selecting Test in AWS IoT Console and subscribe to the MQTT topic hello/world to see messages sent from the core.

Build a Resens Edge Gateway "connected" to AWS IoT through the core device

A good thing with having a Greengrass core device at the edge is that I could implement a Resens BLE gateway into the same hardware and let it "connect" to the AWS IoT cloud through the core device. Although this is not an officially supported method for BLE devices to communicate with the cloud, as the BLE gateway can communicate with the core device, data from any BLE devices connected to the gateway could also have sent to the cloud. I prefer this method as I can build low-power BLE devices without worrying about them having to constantly maintain a TCP connection with the cloud.

If you want BLE devices to communicate with AWS IoT cloud, have a look here for MQTT over BLE.

Follow this instruction to add a new device to the Greengrass group.

  • In AWS IoT Greengrass, select Devices in the group and Add Device.
  • In Create a Registry entry for a device, enter device name, and Next.
  • Select Use Defaults in 1-Click.
  • In the next screen, download device’s security resources, including device certificate, public and private key. The device code will retrieve these files to verify itself with AWS IoT, therefore, save or include them in the device software/firmware. Additionally, download root CA for AWS IoT if it hasn’t been done before.
  • Click Finish to create the device in AWS IoT.

After this, a new device is created and added to the Greengrass group in AWS IoT. The next step is to determine how this device interact with other devices in the group and with the cloud, then deploy the group to the Core device.

Define interaction between the newly created device and others in the Greengrass group

Select Subscriptions in the GreengrassTestingGroup group and define how the device interact with IoT cloud, lambda functions or other devices in the group. See here for examples.

For the Resens Edge Gateway, it simply sends manufacturing data of discovered devices to an MQTT topic, for example ble/discovered_device.

In Subscriptions, select Add Subscription. Source is the newly created device, Target can be IoT Cloud, and topic is ble/discovered_device. After implementing the software for the BLE gateway device, I can test messages sent by the gateway to the cloud using Test on AWS IoT Console as earlier, subscribe to the ble/discovered_device topic.

Finally, on AWS IoT Console, in the Greengrass group, deploy this new group definition to the core device.

Implement Resens Edge Gateway software

Follow the instruction here to download the AWS IoT Device SDK for Python to develop the gateway software.

  • Put the certificates and keys downloaded earlier to the same folder with the main.py of the gateway software.
  • Re-use the code section in basicDiscovery.py, where it attempts connecting to the cloud, discovers the groups, verifies the group it belongs to using certificates and keys, and finally connects to the cloud.
  • Scan BLE devices using bluepy as presensted here.
  • Publish data of discovered devices using the SDK.

Run the software of the newly created device, i.e. the BLE gateway as follows

$ python main.py –endpoint AWS_IOT_ENDPOINT –rootCA root-ca-cert.pem –cert <device>.cert.pem –key <device>.private.key –thingName <device_name> –topic 'ble/discovered_device' –mode publish

in which <device> is the hash in the device certificates, and <device_name> is the name of the device defined in the group on AWS IoT.

messages