In part one of our 6lowpan series using the RAK itracker nrf52 board we saw how to implement a simple 6lowpan connection between your RAK itracker and a linux machine having a BLE 4.1 module connected.
if not you can have a quick look at the tutorial here:
For this tutorial we will see how to build on top of this understanding and get mqtt client running on the nrf52. The connection will again be over BLE via the IPSP service exposed by the nrf52 IoT SDK. The hardware is the same itracker module. However on the software side of thigns, we are gonna work on a completely different RTOS. Before that lets delve a little bit into ipv6 shall we.
What is IPv6 anyways?IPv6 is the next generation Internet Protocol (IP) address standard intended to supplement and eventually replace IPv4, the protocol most Internet services use today. Every computer, mobile phone, and any other device connected to the Internet needs a numerical IP address in order to communicate with other devices. The original IP address scheme, called IPv4, is running out of numbers.
Link Local and Global Address:
A nice answer in a stack-overflow puts these two address types in perspective:
Any IPv6 adapter should always have two IP addresses if you're using it for internet traffic - your link-local address and your global address.
Your global address is world-routable, so anyone anywhere in the world can see that IP address (although of course, there should be a firewall between you and them to stop them from actually accessing you).
Your link-local address is just for your local area network. Consider it the equivalent of a 192.168.0.1 or 10.1.1.1 address. They are not routable, and can be used for internal communications, so that if your world-routable prefix changes, you don't have to update all your IP references to internal IP addresses.
The only functional difference is that your link-local address is not routable, and your global address must be routable. There are issues where non-routable global addresses have been allocated (via DHCPv6) for whatever reason, your computer THINKS it has a routable IPv6 address when it doesn't, and then all your IPv6 connectivity breaks.
Contiki is an open source operating system for the Internet of Things. Contiki connects tiny low-cost, low-power microcontrollers to the Internet. Contiki is a powerful toolbox for building complex wireless systems.
Contiki provides powerful low-power Internet communication. Contiki supports fully standard IPv6 and IPv4, along with the recent low-power wireless standards: 6lowpan, RPL, CoAP. With Contiki's ContikiMAC and sleepy routers, even wireless routers can be battery-operated.
Some of the advanced feature of Contiki can be found here:
Lets get our OS ready for compilationI will consider a Ubuntu System with 16.04 and the latest Linux kernel with support for the 6lowpan_bluetooth kernel module for this setup.
Here are the steps to get your Ubuntu system ready for Contiki OS app development:
Install the nRF5 IOT SDK (https://developer.nordicsemi.com)
Download the nRF5 IOT SDK, extract it to a folder of your choice, and point NRF52_SDK_ROOT
environmental variable to it, for example:
wget ttps://developer.nordicsemi.com/nRF5_IoT_SDK/nRF5_IoT_SDK_v0.9.x/nrf5_iot_sdk_3288530.zipunzip nrf5_iot_sdk_3288530.zip -d $HOME/nrf5x-sdk export NRF52_SDK_ROOT=$HOME/nrf5x-sdk
Get the Segger JLink Software for Linux (https://www.segger.com/jlink-software.html)
Download and install “Software and documentation pack” using a deb package appropriate for your machine (32bit or 64bit). In order to use the nRF52 DK as a regular Linux user, edit (using sudo) the /etc/udev/rules.d/99-jlink.rules
file and replace the ATTR keyword with ATTRS.
c) The remaining tools can be installed using Ubuntu’s packet manager:
sudo apt-get install gcc-arm-none-eabi make bluez libcap-ng0 radvd picocom git
d) Clone the Contiki OS repository:
cd $HOME
git clone
This will get the Contiki folder in your home folder.
Modifying the mqtt-demo codeProceed to the contiki folder and cd to the example/nrf52dk/mqtt-demo folder
We need a few information before we edit the code. We need to run a mosquitto broker on the ubuntu machine. Steps to get that up and running is present here:
Thanks to the guys @ digital ocrean for the wonderful step-by-step instruction.
Start the Mosquitto service by running:
sudo service mosquitto start
Now note down the global Ipv6 address of the interface on which your mosquitto has started, usually it is eth0 (sometime also called eno1) or on your wlan0 interface.
ifconfig -a
you will see an output like so:
Note down the line that ends with the <global> notation. This is the global ipv6 address of the interface on which your broker is running on.
Ok lets edit the project-conf.h and change the line
#define MQTT_DEMO_BROKER_IP_ADDR "fd00::215:83ff:fed2:dbd7"
and add the ip you noted above in between the double quotes. Your done and now lets compile !!!
Ready to rumble !!!Your sample is ready to be flashed on to the board. As usual with the iTracker connect the SWD interface to the jLink programmer and make sure that the Jlink programmer is connected via USB to your ubuntu machine.
Compile the program:
make TARGET=nrf52dk
If the compilation completes without errors, then it’s time to connect the DK to your PC. If you haven't used the device with Contiki before, erase the device and flash it with a SoftDevice:
make TARGET=nrf52dk erase
make TARGET=nrf52dk softdevice.flash
Finally, the application can be flashed to the device:
make TARGET=nrf52dk mqtt-demo.flash
The Demo is now flashed into the device and needs you to connect the device to the 6lowpan network. Follow the steps below:
The device will start advertising BLE as soon as it's been initialized, which is indicated by the blinking of LED1. To verify that the device is indeed advertising, run the following commands in a shell:
sudo hcitool lescan
The output should be similar to the one shown on the figure below.
Take down the device’s Bluetooth address of the Contiki nrf52dk, because you're going to need it later on.
Before we can connect to the device, we need to load and enable the bluetooth_6lowpan kernel module:
# Log in as a root user.sudo su# Load 6LoWPAN module.modprobe bluetooth_6lowpan# Enable the bluetooth 6lowpan module.echo 1 > /sys/kernel/debug/bluetooth/6lowpan_enable
Finally, we can make the connection by running the following command (replace 00:AA:BB:CC:DD:EE with your device’s Bluetooth address):
echo "connect 00:AA:BB:CC:DD:EE 1" > /sys/kernel/debug/bluetooth/6lowpan_control
If the command above is completed successfully, LED1 will stop blinking and LED2 will switch on. You can check that by pinging the device using its link-local address. The address has the form of fe80::2aa:bbff:fecc:ddee, where the aa:bb:cc:dd:ee is the device's Bluetooth address. To ping the device, run the following command:
ping6 -I bt0 fe80::2aa:bbff:fecc:ddee
Remember that you’ll have to connect to the device every time you reset it or flash a new firmware. The kernel module will stay loaded until you switch off your PC.
Distributing routable IPv6 prefixBefore we can try a more complex example that is described in the next section, we need to make sure that the device has a routable IPv6 address. In Linux, this is done by using the Router Advertisement Daemon (RADVD). To configure RADVD, create a /etc/radvd.conf
file (as root) and edit it, so that it has the following content:
interface bt0{
AdvSendAdvert on;
prefix 2001:db8::/64
{
AdvOnLink off;
AdvAutonomous on;
AdvRouterAddr on;
};
};
Next, start the RADVD daemon:
# Set IPv6 forwarding (must be present).
sudo echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
# Run radvd daemon.sudo
service radvd restart
# make sure the bt0 interface has the prefix assigned.
ifconfig bt0 add 2001:db8::1/64
If successful, all devices connected to the host will receive a routable 2001:db8
prefix. This can be verified by sending an echo request to the full address:
ping6 -I bt0 2001:db8::2aa:bbff:fecc:ddee
Here aa:bb:cc:dd:ee should be replaced by the corresponding values from your ble address. this is IMPORTANT !!!!
Now if things are well. and you have suceeded in pinging the prefixed IP of your itracker. Then lets subscribe to one of the topics to which it publishes:
mosquitto_sub -h fe80::922b:34ff:fedd:1c93%eno1 -t iot-2/evt/status/fmt/json
If you dont have the mosquitto_sub command in your ubuntu, just make sure you install mosquitto_tools from the apt-get repository.
Here -h is the host ip of the interface on which the broker is running. in my case its the eno1/eth0 interface, but in your case it may vary (wlan0 for ex). Please check accordingly.
After every 30 seconds , you should see the board sending the mqtt message over the topic mentioned in the command above like so:
You have successfully connected your BLE iTracker to a mqtt broker without having to get wifi connectivity by just using 6lowpan. COOLLL..
The possibilities are endless:
- 1) make a mesh of itracker modules over 6lowpan
- 2) connect OpenHAB endpoints to your itracker module and send and control home appliances over mqtt
- 3) connect relays over mqtt with just bluetooth and have a client over android or on your tablets (iOS and android) send the mqtt commands to switch on/off
- 4) send your itracker sensor data to your ubuntu machine over mqtt
We will try the same mechanism to connect to cloud broker in the next tutorial.
Comments
Please log in or sign up to comment.