Bluetooth Low Energy over RFC7668

Program an nRF52-DK to Publish some MQTT Data

NOTE: You are performing the following steps on your development laptop or PC, not on your Linux VM!!!

The CS7NS2 Zephyr distribution contains a sample application that publishes the state of the four on-board pushbuttons to a ThingsBoard instance. It also implements an RPC endpoint that allows a ThingsBoard instance to set the state of the four on-board LEDs. To do this, the sample program accepts a BLE connection that is used to establish IPv6 communication using RFC7668.

You can find the sample application in zephyr/samples/net/tb_device.

NOTE: If your nRF52-DK will be communicating over Bluetooth with a Linux Kernel version 4.12 or above, you will need to change the prj.conf by deleting (or commenting out) the line CONFIG_NET_L2_BT_ZEP1656=y. (It is not sufficient to set the value to n.) COnversely, if you are using a kernel version below 4.12, you must include the line CONFIG_NET_L2_BT_ZEP1656=y You can determine your kernel version by executing uname -a on the Linux host running your VM instance (which may be your Linux VM).

We will eventually need to configure the IPv6 address of our ThingsBoard instance, to which the application will send MQTT packets. For the moment, however, simply build and flash the application to your nRF52-DK.

Enable RFC7668 communication over BLE

NOTE: You are performing the following steps on the Linux host running your ThingsBoard instance (which may be your Linux VM, if you are using one). You should open a terminal session on your ThingsBoard host (or use SSH to connect to your VM, if you prefer).

If you are using a VM, make sure you have a Bluetooth version 4-capable adapter connected to your VM instance. If your laptop has a built-in BLE-capable device, you can use this, as long as you don't need it for your mouse or keyboard. You can alternatively obtain a low-cost Bluetooth v4 "dongle". (A small number of Dongles are available from the SCSS reception desk on a first-come-first-served basis.)

You can verify that the Bluetooth module is working by executing hcitool lescan. You should see the MAC addresses of any nearby BLE devices.

All of the steps that follow will require root permission. We can avoid prefixing every command with sudo by doing the following:

    $ sudo su -

NOTE: On some linux distributions (e.g. Ubuntu) it will be necessary for you to disable secure boot to allow access to the debug filesystem.

Load the bluetooth_6lowpan kernel module

    $ modprobe bluetooth_6lowpan

You should not see any errors (or indeed any output) after executing modprobe.

Note that you can automatically load bluetooth_6lowpan at boot time by adding the module name to your /etc/modules file.

Enable 6lowpan routing over bluetooth

    $ echo 1 > /sys/kernel/debug/bluetooth/6lowpan_enable

Search for the address of your nRF52-DK device

    $ hcitool lescan
    C5:B4:C0:69:32:65 (unknown)
    C7:DD:34:8F:45:65 Zephyr <--- this is the one we want!!
    2C:5E:F3:A9:61:63 (unknown)

Establish a (Bluletooth) connection with the device

    $ echo "connect <btaddr> 2" > /sys/kernel/debug/bluetooth/6lowpan_control

for example

    $ echo "connect C7:DD:34:8F:45:65 2" > /sys/kernel/debug/bluetooth/6lowpan_control

NOTE: every time you reset your nRF52-DK, you will need to repeat the above echo connect step to re-establish a connection between the nRF52-DK and the host running your ThingsBoard instance.

The IPv6 address automatically assigned to your newly connected device will depend on the device's hardware Bluetooth address. The simplest way to determine the IPv6 address of your device is to send a multicast pink to all link-local nodes:

    $ ping6 -I bt0 ff02::1

You should see output similar to this and you are looking for an IPv6 address that has the same suffix as your devices MAC address:

    PING ff02::1(ff02::1) from fe80::21a:7dff:feda:7111%bt0 bt0: 56 data bytes
    64 bytes from fe80::21a:7dff:feda:7111%bt0: icmp_seq=1 ttl=64 time=0.129 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=1 ttl=64 time=511 ms (DUP!)
    64 bytes from fe80::21a:7dff:feda:7111%bt0: icmp_seq=2 ttl=64 time=0.057 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=2 ttl=64 time=57.9 ms (DUP!)
    64 bytes from fe80::21a:7dff:feda:7111%bt0: icmp_seq=3 ttl=64 time=0.060 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=3 ttl=64 time=55.9 ms (DUP!)

once we know the IPv6 address of our device, we can ping it individually ...

    $ ping6 -I bt0 fe80::c5dd:34ff:fe8f:4565

... resulting in the following output ...

    PING fe80::c5dd:34ff:fe8f:4565(fe80::c5dd:34ff:fe8f:4565) from fe80::21a:7dff:feda:7111%bt0 bt0: 56 data bytes
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=1 ttl=64 time=95.9 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=2 ttl=64 time=143 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=3 ttl=64 time=99.5 ms
    64 bytes from fe80::c5dd:34ff:fe8f:4565%bt0: icmp_seq=4 ttl=64 time=91.7 ms

Test Thingsboard with a Zephyr Application

We are finally ready to test communication (MQTT over IPv6 over BLE) between our sample Zephyr application and our ThingsBoard installation.

Before proceeding, we need to provision a new device in ThingsBoard. Add a new device to ThingsBoard by following the same procedure that you used when testing ThingsBoard. Set the new device's Access Token to the Bluetooth MAC address of your nRF52-DK. You can obtain this MAC address from the hcitool lescan output. (In the example above, this was C7:DD:34:8F:45:65.) Note that the BLE MAC address must be entered in lower case. Using the same example, the access token would be c7:dd:34:8f:45:65.

In the tb_device application (zephyr/samples/net/tb_device), edit the prj.conf file and set CONFIG_NET_APP_PEER_IPV6_ADDR to the IPv6 link local address of the host running your ThingsBoard instance. (In the example above, by observing the PING output, we can see the address is fe80::21a:7dff:feda:7111.

Now is also a good time to set CONFIG_BT_DEVICE_NAME to a name other than "Zephyr", as every other instance of the application within a 50-100m radius will also appear with the same name! You might consider names such as TEAM01-DEV00, TEAM01-DEV01, TEAM01-DEV02, etc.

After configuring CONFIG_NET_APP_PEER_IPV6_ADDR, rebuild and re-flash the application on your nRF52-DK. When the application is running, repeat the steps above to connect your ThingsBoard host to the Zephyr application, e.g.

    $ echo "connect C7:DD:34:8F:45:65 2" > /sys/kernel/debug/bluetooth/6lowpan_control

After a delay of approximately 30 seconds, you should be able to observe the attributes and telemetry published by your nRF52-DK / Zephyr device on ThingsBoard. To view this in ThingsBoard, select your device and navigate to either the attributes tab or the telemetry. When you press a button on the nRF52-DL, you should see the telemetry values change from false to true.