dragon innovation_relayr+arduino
0 Comments

WunderBar and Grove Part 1: From Arduino Through The Heart of the Cloud

The following post was authored by Peter Dwersteg, Developer Expert at relayr (a Dragon customer doing big things in hardware and IOT).

The Bridge, or Grove, module is easily the most misunderstood component of the WunderBar. It doesn’t come with pre-packaged sensors. The other modules display clear, live data on the developer dashboard, but the Bridge’s section there is conspicuously blank. It’s a bit of an outlier in a product that prides itself on out-of-the-box functionality.

However, the Bridge is quietly the most interesting and powerful (at least, in my opinion) piece of the WunderBar. With the Bridge, you’ve got a tool for expanding your hardware in any way you can imagine. Connect an Arduino or Raspberry Pi to your software applications through the relayr cloud, complete with two-way communication? Want to use data from a Grove soil moisture sensor, breathalyzer, or any other sensor in your app? The Bridge has you covered.

The Idea

Before I suffered a season-ending toe injury, I had an awesome idea to carry an air pressure sensor (barometer) on my body while snowboarding to keep track of my altitude change over the course of the day. With a high enough sample rate, I could draw a detailed picture of my path down the mountain. Analysis, gamification, or bragging could follow. Swings in air pressure also indicate impending changes in the weather, so my sensor could also function as a mountain safety device!

In this series of articles, we’re going to explore what the Bridge module can do by implementing my altimeter idea! With the right equipment – listed later in the article – you can follow along at home!

Let’s build it

Since the WunderBar doesn’t have a barometer sensor, we’ll need to use the Bridge to hook one up! There’s two ways of doing this. The first is to use an Arduino or other board as an intermediary between the sensor and Bridge. The second is to actually modify the firmware on the bridge to interface with the sensor directly. There are pros and cons to both:

Method Pros Cons
Arduino
  • Simple, well documented, no special equipment beyond an Arduino.
  • Communicates with Master Module and the relayr cloud in the current version of the Master Module firmware.
  • Physically larger
  • More power intensive
  • More technology to possibly go wrong
Direct connection to Bridge (custom module firmware)
  • Physically smaller
  • Less power intensive
  • Need to wait for the next version of the Master Module firmware to communicate with the relayr cloud.
  • Currently requires special equipment like a SWD programmer.
  • Requires knowledge of C for embedded systems.

 

In this article (part one of two), we’re going to use an Arduino to integrate a barometer. In the second part, we’ll tackle the more complex firmware solution.

We’ll start with an intro to the hardware and firmware, then jump into a sweet example application.

The Bridge Module Hardware

The bridge is physically different from other WunderBar modules. As you’ve probably noticed, it’s got a white, four-pin socket on one end and no battery bracket on the bottom. The socket is in the standard Grove connector cable format and the pins are simply exposed from the on-board Nordic chip, so they can be used however you like. However, you’ll generally be using them to perform serial communication over various protocols which include UART, I2C, and SPI. Which protocol depends on the firmware that’s currently flashed to the Bridge. The battery slot is omitted because a coin cell battery can’t be expected to power both the module and an attached sensor simultaneously for long periods of time. Therefore, once it’s been broken off of the WunderBar, you’ll need to supply the Bridge with 5V through the exposed Vin and Gnd sockets (the two golden circles in the lower left of the illustration).

dragon innovation_relayr+arduino

The Bridge Module Firmware

Fresh out of the box, the Bridge module can’t actually do a whole lot. You’ll need to flash some firmware to it before it starts getting interesting. Luckily this is really simple, and we’ve got extensive documentation on the subject available on the relayr developer dashboard.

In this example, we’ll be flashing the UART firmware to the Bridge. This firmware allows it to communicate naturally with devices that also make use of the UART protocol, in our case an Arduino using that board’s hardware serial pins. You could sub in any appropriate device, including, but not limited to, the Raspberry Pi.

Once your Arduino (or other device) sends a packet of bytes to the Bridge, the firmware we’ve flashed will handle all the work of packaging the data and transmitting it up the chain to our servers. It takes all of that nasty cloud-communication work out of your hands and replaces it with a single method call in your Arduino sketch. But we’ll learn about all that in a couple of minutes!

Building an Altimeter using an Arduino and a Grove sensor

Back to the fun stuff. Let’s take a look at some actual hardware and code for my altimeter! Components you’ll need for this project are as follows:

  • 1 WunderBar
  • 1 Arduino (Uno, Mega, or other. For Uno, see extra equipment below)
  • 1 Bosch BMP085 digital pressure sensor (datasheet)
  • Grove connectors and/or a variety of other cables
  • Breadboard (optional)

Each of these steps is documented in more detail on our website, links will be included when appropriate.

Extra Equipment for Arduino Uno Users

If you’re using an Arduino Uno (or another Arduino board that only has a single hardware serial port), you’ll need one more device.

We strongly recommend that you use only hardware serial for communicating with the Bridge, so to debug on your computer you’ll need a UART to USB breakout board like this one. I made use of a Silicon Labs CP2101-based device like the one pictured and the Arduino SoftwareSerial library to develop this dragon innovation_relayr+arduinoapplication. Whenever I wanted to transmit debug information to my computer, I sent it through a SoftwareSerial instance created on Arduino pins 10 and 11, which were connected to Rx and Tx on my UART to USB breakout. Make sure to keep the baud rate on this virtual serial port low to assure proper data transmission. I found the best balance of clarity and speed at 28800 baud.

You’ll also need to disconnect the Bridge from the hardware serial pins each time you flash the Arduino, or you’ll get sync errors.

Connect your BMP085 to your Arduino

Let’s start by making sure your BMP085 works, and investigating the data it provides.

  1. Wire up the following circuit. For the best performance, connect the sensor to your hardware serial ports. Since it’s not visible on the diagram, attach the SCL pin/pad on the BMP085 to the A5 pin on the arduino, and the SDA pin/pad to A4.

    dragon innovation_relayr+arduino

    * Don’t summon the blue smoke: be sure to connect your BMP085 to 3.3V, not 5V!

  2. Adafruit provides a fantastic Arduino library for the BMP085, and you can be sure we’re going to use it. Everybody reading this article, turn to the person on your left and thank them as if they were Adafruit. Download the library and import it into your arduino sketch, along with the Wire library (used for I2C communication in the Adafruit code).
  3. The BMP085 can sense air pressure and temperature, and then calculate altitude using these two values. Read and print all three to a serial monitor using the following code:

 

code1

Since air pressure varies with weather and other conditions, you can get a more accurate reading of the altitude by passing the current sea-level pressure for your location to the readAltitude() method like so:

Serial.print(bmp.readAltitude(102430));

Connect your Arduino to the Bridge Module

  1. Flash the UART firmware .hex file to your Bridge.
  2. Re-onboard just the bridge module by using the “reset module” option in your relayr app.
  3. Wire the bridge to your Arduino with the following circuit.

    dragon innovation_relayr+arduino

    * Notes: The BMP085 needs to be connected to 3.3V, and the bridge module to 5V. If your bridge module is still physically connected to the master module, it is already supplied with power and you may omit its 5V Vin.

  4. Download the Arduino bridge module library and import it into the Arduino IDE.
  5. Check that the bridge is properly connected by adding the following code to your Arduino sketch:

a. Add a reference to the Bridge module library

#include <WunderbarBridge.h> 

For Arduino Uno users, you’ll also need the SoftwareSerial library.

 #include <SoftwareSerial.h>

b. Create a bridge instance before the setup method

// Create bridge with SoftwareSerial debug on pins 10 and 11 at 28800 baud                               Bridge bridge = Bridge(10, 11, 28800);

For Arduino Mega users, the library doesn’t set up a SoftwareSerial, so your declaration will be:

 Bridge bridge = Bridge(115200);

c. Check your connection to the bridge in the setup method

void setup() {

   bridge.begin();                                                                                                                              }

Communicating through the Cloud

Now that everything is operating on the hardware side, it’s time to handle device-to-app communication through the relayr cloud. First though, let’s take a quick break to talk about the communication flow between our devices and services.

dragon innovation_relayr+arduino

The BMP085 will communicate data to the bridge with the I2C protocol. The Bridge then packages this data and transmits it over an encrypted Bluetooth Low Energy connection to the Master Module, which then uses the standard MQTT protocol to publish the data to the cloud. From there, it’s sent to any subscribing apps.

Send Commands to the Arduino

While we aren’t going to use this functionality in our application, it’s worthwhile to see how sending data from an app to the Arduino works. Full documentation of the relayr HTTP API can be found here.

On the app side, it’s as simple as making a POST to the ‘Device Command’ HTTP endpoint in the relayr API. The content of the POST is JSON and contains a unique message id and an array of bytes. For example, POSTing the JSON content:

code 2

would transmit four bytes to your Arduino: 32, 69, 76, and 98. The size of the payload can be anywhere from 1 to 20 bytes. If you want to send more than 20 bytes, you’ll need to break it up into multiple packets and POST each in sequence.

If you don’t want to mess around with an HTTP client like Postman, there’s a simple way to publish commands to your modules through the developer dashboard. Just click on the page icon at the top of any sensor pane in the live data area:

dragon innovation_relayr+arduino

At the bottom of the window, you’ll see a terminal-style device command prompt. Enter your commands in this prompt, and it does all the HTTP heavy lifting for you.

dragon innovation_relayr+arduino

To send our four example bytes, copy and paste this command into the prompt:

code-a

If you want to test your Bridge module’s connection, you can turn on the on-module LED for several seconds with this command:

code_b

On the Arduino side, all you need to do is listen to the serial port connected to the bridge to see if there’s information available, then read it using the following code:

code-c

Publishing Data from the Arduino to your Apps

The final step of our tutorial is to send the data generated by our BMP085 to your connected applications. This is also super simple using our wonderful library.

In this example, I’m naively packaging all three types of data (temperature, pressure, and altitude) into a single twelve-byte array and publishing them simultaneously in a single packet, once per second. Add the following code to your sketch.

1. Declare an array to fill with the data (4 bytes for each long) before the setup() method.

    byte readingsBytes[12];

2. Add a method to package each long into 4 bytes starting at a specific array index

code4

3In the loop method, read the three values, package them, and send ‘em on their way!

code5

Here’s the full code for our example:

code6

code7

It’s that simple! A single call to sendData() launches our values up to the cloud and from there to your applications.

On the application side, once you’ve chosen your platform and SDK of choice, all you need to do is subscribe to data coming from your Bridge by using the exact same steps as you would to connect to any other sensor module. The data will come packaged as JSON, represented as a series of bytes (similar to how it’s structured when you send data)

If you don’t want to wait until after you’ve implemented an app to see what the data looks like (or to check that it’s sending), you can always click this button in the dashboard to see the last 100 packets sent by the Bridge.

dragon innovation_relayr+arduino

And you’re done! Design a nice UI for yourself, store the data for later analysis, a combination of the two, or whatever you want with it!

Next Steps

It’s that simple to connect your hardware to the cloud using the WunderBar. I know this will come in handy (especially for you more advanced hardware hackers) for a ton of your projects. It’s one of the simplest and fastest ways to add reliable connectivity to your project.

However, we’re making a device for the outdoors! I don’t know about you, but I don’t want this monstrosity taped to my snowboard or stuck in my pocket while I’m banging around on the mountain! Especially when there isn’t reliable WiFi (get it together ski areas)!

 

dragon innovation_relayr+arduino

* intentionally janky to generate disgust

In part 2, we’ll talk about miniaturizing our hardware by plugging the BMP085 directly into the Bridge module. This requires knowledge of embedded C and it’s a lot more complicated, but can produce incredible results! Get ready to write and flash our own firmware.

See you in a couple of weeks.

Leave a Reply

Your email address will not be published. Required fields are marked *