Support
Blues.io
Notehub.io
Shop
Support
Blues.io
Notehub.io
Shop
×
HomeBuild
Hookup Guide
Quickstart
Tutorials
Sensor TutorialIntroductionSet up HardwareCreate a Notehub ProjectWrite FirmwareView Data in Notehub
Route Tutorial
Notecard Guides
Asset Tracking
Serial-Over-I2C Protocol
Updating ESP32 Host Firmware
Configuring ESP32 Attention Pin
Understanding Environment Variables
Routing Guides
Twilio Route
MQTT Route
Azure Function Route
ThingWorx Route
Get started with
Arduino/WiringC/C++CircuitPythonPython
and
ArduinoESP32FeatherSTM32 DiscoverySTM32 Nucleo
and
Notecarrier-AF

Sensor Tutorial

Introduction

In this tutorial, you’ll learn how to take sensor readings from a Device and send readings to your Notecard and the Blues Wireless Notehub. You'll use Arduino/Wiring running on a STM32 Nucleo wired up to Notecarrier-AF hardware. If you would like to use a different language, board, or Notecarrier, modify the dropdowns at the top of this guide.

If you get stuck at any point during this tutorial, the full source for each example is available in the note-tutorials GitHub repo.

Set up Hardware

First, you’ll need to get all of your hardware connected. Follow the instructions below to connect your STM32 Nucleo, BME680 sensor, and Notecard mounted on a Notecarrier-AF.

In order to complete this guide, you’ll need the following:

  • An Adafruit Feather STM32F405 Express.

  • Micro USB to USB-A cable.

  • A Grove I2C Sensor of your choosing. We’ll be using the Grove BME680 from Seeed, but you’re welcome to use any Grove I2C sensor and adapt the code in this guide to read from it instead.

Connect the sensor and MCU to the Notecarrier-AF

  1. Plug your STM32F405 Express into the Feather headers on the Notecarrier-AF.

  2. Plug your Grove BME680 into one of the I2C ports on the Notecarrier-AF.

Create a Notehub Project

Now that your hardware is all connected, let’s create a new Notehub project to receive sensor readings from your Notecard.

  1. Navigate to notehub.io and log-in, or create a new account.

  2. Using the New Project card, give your project a name and ProductUID.

NOTE: The ProductUID must be globally unique, so we recommend a namespaced name like "org.coca-cola.soda.vending-machine.v2".

  1. Take note of your ProductUID. This identifier is used by Notehub to associate your Notecard to your project.

Write Firmware

Configure the Arduino IDE

For this portion of the guide, we’ll be using the Arduino IDE, so be sure to install version 1.8+ if you haven’t already.

Once installed, we’ll need to add support for your STM32 Nucleo Board.

  1. Start the Arduino IDE and open the File > Preferences menu.

  2. Copy the following path https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json into the "Additional Board Manager URLs" field. If there’s already something in the box, then add a comma to separate the URLs.

  3. Click OK, then open the Boards Manager from the Tools > Board: [board name] > Boards Manager... menu.

  4. Search for "STM32" and click the Install button to add STM32 board support to the Arduino IDE.

  5. Once the installation is complete, click the Close button.

  6. Select Generic STM32F4 from the Tools > Board menu.

  7. Select Adafruit Feather STM32F405 from the Tools > Board part number menu, and select the appropriate port for your device.

  8. Make sure the remaining Tools > Board settings match the image below.

Note: Flashing firmware to the STM32F4 Feather currently requires you do manually enter bootloader mode each time you want to update firmware on the device. You can do this by holding a wire between the B0 and 3V3 pins on the board, or on the Notecarrier-AF edge connector and pressing reset on the STM32F4 Feather. Once done, you can flash firmware using the Arduino upload button.

Great job, now you’re ready to write some firmware!

Arduino Sketch

When communicating with the Notecard over I2C, you'll want to use the note-arduino library. The code snippets below provide everything you need to talk to the Notecard over I2C in Arduino.

warning

The Notecarrier-AF provides only an I2C connection between a Feather MCU host and the Notecard; it does not facilitate Serial communication between devices.

Configure your Notecard

Install the Notecard Arduino Library

  1. To use the note-arduino library, you’ll need to add it to the Arduino IDE.

  2. Click on Tools > Manage Libraries...

  3. Search for "Blues" in the input box and click the "Install" button next to the "Blues Wireless Notecard" result.

    Installing the Blues Wireless Notecard library.

  4. Create a new sketch and select the Sketch > Include Library > Contributed Libraries > Blues Wireless Notecard menu option, to add the following include to your sketch:

#include <Notecard.h>

Set-up Your Notecard

  1. Now, configure a Serial interface. Serial will be used to log information to the Serial terminal of the Arduino IDE.
#define serialDebug Serial
  1. Next, add a definition for your ProductUID using the value you specified when creating your Notehub project.
#define productUID "<com.your.product.uid>"
  1. In the setup() function, Initialize the serialDebug object and tell the Notecard library to use this serial object for sending debug output.
serialDebug.begin(115200);
while (!serialDebug) {
  ; // wait for serial port to connect. Needed for native USB
}
  1. Initialize an instance of the Notecard class and initialize an I2C connection to the Notecard using the notecard.begin() function. Then, use setDebugOutputStream() link the debug output to serialDebug with the following code:
Notecard notecard;

notecard.begin();
notecard.setDebugOutputStream(serialDebug);
  1. Now, we’ll configure the Notecard. Using the hub.set request, we associate this Notecard with the ProductUID of your project and set the Notecard to operate in continuous mode, which indicates that the device should immediately make a connection to Notehub and keep it active.
J *req = notecard.newRequest("hub.set");
JAddStringToObject(req, "product", productUID);
JAddStringToObject(req, "mode", "continuous");
notecard.sendRequest(req);

The lines above build-up a JSON object by adding two string values for product and mode, and then fires the request off to the Notecard with the sendRequest() function.

  1. Enter booloader mode on your STM32F4 Feather by connecting a wire between B0 and 3v3 and pressing the reset button. Then, click the Upload button (right arrow icon) to flash firmware to your device.

  1. Open the Arduino Serial Monitor. If everything has been connected and configured properly, you’ll see a few debug messages, including the JSON object you sent, as well as the response from the Notecard {}

Read from the sensor

Now that you’ve configured your Arduino to communicate with the Notecard, let’s grab sensor readings from the BME680. First, you’ll need to install the Grove BME680 a library for use in the Arduino IDE.

  1. Navigate to the Seeed Studio GitHub Repo for the BME 680 and download a ZIP archive using the "Clone or download" button.

  2. From the Sketch menu, click Include Library > Add .ZIP Library…

  3. Select the Zip archive you downloaded in Step 1.

  4. Once the installation completes, add an include for the BME680 library header to the top of your project.

#include <seeed_bme680.h>
  1. Outside of the setup and loop functions, declare a global object to represent the BME sensor and pass in the default I2C address of the device.
#define IIC_ADDR  uint8_t(0x76)
Seeed_BME680 bmeSensor(IIC_ADDR);
  1. In setup, add the following to initialize the sensor and log a successful connection or failure to the Serial console.
if (!bmeSensor.init()) {
    serialDebug.println("Could not find a valid BME680 sensor...");
} else {
    serialDebug.println("BME680 Connected...");
}
  1. Now let’s take a reading and print the temperature and humidity to the console. In the loop function, start by calling the BME680 library performReading method:
if (bmeSensor.read_sensor_data()) {
    serialDebug.println("Failed to obtain a reading...");
    delay(15000);
    return;
}
  1. Finally, we’ll output the readings to the console and wait for 15 seconds before exiting the loop.
serialDebug.print("Temperature = ");
serialDebug.print(bmeSensor.sensor_result_value.temperature);
serialDebug.println(" *C");
serialDebug.print("Humidity = ");
serialDebug.print(bmeSensor.sensor_result_value.humidity);
serialDebug.println(" %");
 
delay(15000);
  1. Enter booloader mode again and upload this code to your Arduino. Open the Serial Monitor and you’ll see temp and humidity readings every 15 seconds.

Send Sensor Readings to the Notecard

Now that we’re getting sensor readings, let’s send these to our Notecard.

  1. To send a sensor reading to the Notecard, we’ll need to construct a new JSON request to the note.add API that includes a new Notefile name (sensors.qo), sets the start field to true to instruct the Notecard to sync to Notehub immediately, and finally, sets the body to the sensor temperature and humidity. Add the following in loop right after the serialDebug commands to print out readings.
J *req = notecard.newRequest("note.add");
if (req != NULL) {
    JAddStringToObject(req, "file", "sensors.qo");
    JAddBoolToObject(req, "start", true);
    J *body = JCreateObject();
    if (body != NULL) {
        JAddNumberToObject(body, "temp", bmeSensor.sensor_result_value.temperature);
        JAddNumberToObject(body, "humidity", bmeSensor.sensor_result_value.humidity);
        JAddItemToObject(req, "body", body);
    }
    notecard.sendRequest(req);
}
  1. Enter booloader mode again and upload this code to your device. After reboot, the Serial monitor will update to display the response from the note.add request (the total number of Notes in the notefile) each time you add a new reading.

View Data in Notehub

Once you start capturing readings, your Notecard will initiate a connection to Notehub and will start transferring Notes. Depending on signal strength and coverage in your area, it may take a few minutes for your Notecard to connect to Notehub and transfer data.

  1. Return to notehub.io and open your project. You should see your notecard in the Devices view.

  2. Now, click on the Events left menu item. Once your sensor Notes start syncing, they’ll show up here.

Congratulations!

You’ve successfully connected your STM32 Nucleo and external sensor to your Notecard!

note

During this tutorial, you set your Notecard into continuous mode, which maintains an active cellular connection and enables faster syncs with Notehub. It doesn't have much impact on data usage, but it will draw more power. If you're connected to battery power, or want to transition your project to battery power, then you can put your Notecard into periodic mode with the following request using the in-browser terminal or directly in your firmware.

{"req":"hub.set","mode":"periodic","outbound":60,"inbound":120}
Can we improve this page? Send us feedbackRate this page
  • ★
    ★
  • ★
    ★
  • ★
    ★
  • ★
    ★
  • ★
    ★
© 2021 Blues Inc.Terms & ConditionsPrivacy
blues.ioTwitterLinkedInGitHubHackster.io
Disconnected
Disconnected
Having trouble connecting?

Try changing your Micro USB cable as some cables do not support transferring data. If that does not solve your problem, contact us at support@blues.com and we will get you set up with another tool to communicate with the Notecard.

Connect a NotecardClick 'Connect' and select a USB-connected Notecard to start issuing requests from the browser.