🚀 Browse our open source reference applications to accelerate your IoT project!

Search
Documentation Results
End of results
Community Results
End of results
Support
Blues.io
Notehub.io
Shop
Sign In
Search
Documentation Results
End of results
Community Results
End of results
Support
Blues.io
Notehub.io
Shop
×
HomeGuides & Tutorials
Welcome
Collecting Sensor DataIntroductionSet up HardwareCreate a Notehub ProjectWrite FirmwareView Data in Notehub
Routing Data to Cloud
Building Edge ML Applications
Twilio SMS Guide
Fleet Admin Guide
Using the Notehub API
Notecard Guides
Guide Listing
Asset Tracking
Attention Pin Guide
Connecting to a Wi-Fi Access Point
Debugging with the FTDI Debug Cable
Diagnosing Cellular Connectivity Issues
Encrypting Data With the Notecard
Minimizing Latency
Notecard Outboard Firmware Update
Remote Command and Control
Serial-Over-I2C Protocol
Understanding Environment Variables
Understanding Notecard Penalty Boxes
Updating ESP32 Host Firmware
Using External SIM Cards
Using JSONata to Transform JSON
Rate this page  
  • ★
    ★
  • ★
    ★
  • ★
    ★
  • ★
    ★
  • ★
    ★

Sensor Tutorial

Get started with:
C/C++ (Arduino/Wiring)C/C++ (STM32Cube)CircuitPythonPython
and
Adafruit Feather M4 ExpressAdafruit HUZZAH32Arduino Nano 33 BLE SenseArtemis Thing PlusBlues Wireless SwanRaspberry PiSparkFun MicroMod STM32 ProcessorSTM32 DiscoverySTM32 Nucleo
and
Notecarrier-ANotecarrier-FNotecarrier-PiSparkFun MicroMod Cellular Function BoardSparkfun Qwiic Cellular

Don't see your favorite hardware here? Rest assured the Notecard works with virtually every MCU and SBC available. If you can't figure out how to complete this tutorial let us know in our forum and we can help you out.

Introduction

This tutorial should take approximately 40-50 minutes to complete.

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 C/C++ (Arduino/Wiring) running on a Adafruit HUZZAH32 wired up to Notecarrier-A hardware. If you would like to use a different language, board, or Notecarrier, modify the dropdowns at the top of this guide.

The tutorial uses mock sensor readings for simplicity, but feel free to hook up a physical sensor of your choice and use that instead.

note

If you get stuck at any point during this tutorial, the full source for each example is available on GitHub:

  • View this example's source code on GitHub .

Set up Hardware

First, you’ll need to get all of your hardware connected. Follow the instructions below to connect your Adafruit HUZZAH32 and Notecard mounted on a Notecarrier-A.

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

  • Notecard mounted to Notecarrier-A.
  • Any ESP32-compatible Microcontroller (MCU). We'll be using the Adafruit HUZZAH32 , but any MCU that can be programmed with the Arduino IDE will do.

  • Micro USB to USB-A cable.

NOTE: For this tutorial, you'll be powering the Notecard through the MicroUSB connection of your Adafruit HUZZAH32. Some Feather-compatible devices cannot handle 2 Amp pulses from the Notecard when connected to GSM, so if you experience resets or other power-related issues, we suggest powering your Notecard separately through the USB or LiPo connector on the Notecarrier.

Connect the Adafruit HUZZAH32 to your Notecard

Now, let's connect your Notecard using a Serial connection.

  1. Connect V+ from the Notecarrier-A to the USB pin on your Adafruit HUZZAH32.

  2. Connect GND from the Notecarrier-A to a GND pin on your Adafruit HUZZAH32.

  3. Connect TX from the Notecarrier-A to the RX pin on your Adafruit HUZZAH32.

  4. Connect RX from the Notecarrier-A to the TX pin on your Adafruit HUZZAH32.

  5. Plug the USB cable directly into your Adafruit HUZZAH32, which will allow you to flash firmware to your device.

NOTE: Make extra sure you've plugged in the jumper wires next to the TX and RX pins on the Feather. The pin names are actually printed underneath the pin they're referring to, and mis-wiring will cause a serial error of notecard not responding without much else to help debug the actual issue.

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.

    How to create a new Notehub project

NOTE: The ProductUID must be globally unique, so we recommend a namespaced name like "com.your-company.your-name:your_product".

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

    Where to find your product UID

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 2.0+ if you haven't already.

Once installed, we'll need to add support for your Adafruit HUZZAH32 Board.

Configure the Arduino Boards Manager to use the ESP32

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

  2. Copy the following path https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_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 "esp32" and click the Install button to add ESP32 board support to the Arduino IDE.

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

  6. To proceed, you may need to install the SiLabs CP2104 driver. This driver allows the Arduino IDE to communicate with (and flash) your ESP32 DevKit over USB.

    • macOS Big Sur (and later): The necessary driver is already installed.
    • Earlier versions of macOS: Download and install "CP210x VCP Mac OSX Driver" from SiLabs .
    • Windows 10: Download and install "CP210x Universal Windows Driver" from SiLabs .
    • Windows 7 to 8.1: Download and install the "CP210x Windows Drivers" from SiLabs .
    • Linux: Download and install the appropriate CP210x driver for your Linux kernel from SiLabs .

    Linux note: If your kernel version is >= 5, you do not need to install the driver. If you are unsure, you can check your kernel version by running uname -srm from your terminal application.

  7. Next, plug your Arduino device back in, open the Arduino IDE, select Adafruit ESP32 Feather from the Tools > Board menu, and select the appropriate Port for your device.

Additional Linux Setup

In order to support the Adafruit HUZZAH32, you will need to install the following packages:

sudo apt update && sudo apt install \
  python-is-python3 \
  python3-serial
warning

The Arduino IDE MUST NOT be installed from the Ubuntu store. Instead, it must be installed from the ZIP file downloaded from the Arduino downloads page ._

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

Arduino Sketch

When communicating with the Notecard, you can manually send requests using the Serial.println function and passing-in JSON objects, or use the note-arduino library . Code snippets are provided for both methods, so feel free to use the approach that works best for you.

    warning

    If NOT using a Notecard firmware library, you may unintentionally send requests to the Notecard so fast that you overflow the 1500 byte buffer used to receive data (whether it be I2C, Serial, or UART). The solution is to pause 250 ms after every 250 bytes sent and ensure the total size of each NDJSON object sent is no more than 8KB.

    1. First, let's alias the Serial interfaces at the top of your program in the Arduino IDE. The first, Serial will be used to log information to the Serial Monitor of the Arduino IDE. The second, Serial1 represents the connection between your Arduino and Notecard. Paste the following code at the top of your sketch:

      #define usbSerial Serial
      #define txRxPinsSerial Serial1
    2. Next, add a definition for your ProductUID using the value you specified when creating your Notehub project.

      #define NOTE_PRODUCT_UID "com.your-company.your-name:your_product"
    3. In the setup() function, initialize the usbSerial object.

      usbSerial.begin(115200);
      while (!usbSerial) {
         ; // wait for serial port to connect. Needed for native USB
      }
      usbSerial.println("Starting...");
    4. Also in setup, initialize the txRxPinsSerial object, using a baud rate of 9600, and send a newline (\n) to clear out any pending data on the device.

      txRxPinsSerial.begin(9600);
      txRxPinsSerial.println("\n");
      delay(250);
    5. Now, we'll configure the Notecard. This one-time operation, also belongs in the setup() function. We will use the hub.set request to associate this Notecard with the ProductUID of your project, as well as set the Notecard to operate in continuous mode (which indicates that the device should immediately make a connection to Notehub and keep it active).

      txRxPinsSerial.println("{\"req\":\"hub.set\",\"product\":\"" NOTE_PRODUCT_UID "\",\"mode\":\"continuous\"}");
    6. Finally, we'll add a while loop to setup to read the Notecard's response, and display the result to the Arduino terminal.

      delay(250); // wait for the Notecard to respond
      while (txRxPinsSerial.available() > 0) {
          char incomingByte = txRxPinsSerial.read();
          if (incomingByte != '\r' && incomingByte != '\n') {
             usbSerial.print(incomingByte);
          }
      }
      usbSerial.println();
    7. Now, it's time to watch the logs and validate the firmware. Open the Serial Monitor, by selecting Tools > Serial Monitor from the menu. The default baud rate is 9600 baud, be sure to select 115200 baud from the drop down menu.

    8. Click the upload button (right arrow icon) to save and flash the firmware to your device.

      If you haven't saved your file yet, you will be prompted to do so. Name your sketch (for example, nano-no-library), and click Save. Once your device is flashed and comes back online, check the serial monitor to confirm your Notecard has been properly configured.

      Starting...
      {}

    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>

    Write Code

    1. First, let's alias the Serial interfaces at the top of your program in the Arduino IDE. The first, Serial will be used to log information to the Serial Monitor of the Arduino IDE. The second, Serial1 represents the connection between your Arduino and Notecard. Paste the following code at the top of your sketch:

      #define usbSerial Serial
      #define txRxPinsSerial Serial1
    2. Next, add a definition for your ProductUID using the value you specified when creating your Notehub project.

      #define NOTE_PRODUCT_UID "com.your-company.your-name:your_product"
    3. Above the setup() and loop() functions, declare a global object to represent the Notecard.

      Notecard notecard;
    4. In the setup() function, initialize the usbSerial object.

      usbSerial.begin(115200);
      while (!usbSerial) {
          ; // wait for serial port to connect. Needed for native USB
      }
      usbSerial.println("Starting...");
    5. Next, initialize the serial connection to the Notecard using the begin() function. Then, use setDebugOutputStream() link the debug output to usbSerial with the following code:

      notecard.begin(txRxPinsSerial, 9600);
      notecard.setDebugOutputStream(usbSerial);
    6. Now, we'll configure the Notecard. This one-time operation, also belongs in the setup() function. We will use the hub.set request to associate this Notecard with the ProductUID of your project, as well as set the Notecard to operate in continuous mode (indicating the device should immediately make a connection to Notehub and keep it active).

      J *req = notecard.newRequest("hub.set");
      if (req != NULL) {
          JAddStringToObject(req, "product", NOTE_PRODUCT_UID);
          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. Now, it's time to watch the logs and validate the firmware. Open the Serial Monitor, by selecting Tools > Serial Monitor from the menu. The default baud rate is 9600 baud, be sure to select 115200 baud from the drop down menu.

    2. Click the upload button (right arrow icon) to save and flash the firmware to your device.

      If you haven't saved your file yet, you will be prompted to do so. Name your sketch (for example, nano-with-library), and click Save. Once your device is flashed and comes back online, check the serial monitor to confirm your Notecard has been properly configured.

    Read from the Sensor

    Now that you've configured your Arduino to communicate with the Notecard, let's grab some pseudo sensor readings.

    note

    If you have your own sensor, feel free to hook it up and use your own values instead of this tutorial's mocked ones.

    1. To generate mock sensor readings you'll use the note-arduino library and NotecardPseudoSensor library .

    2. Start by clicking on Tools > Manage Libraries... and search for "Blues" in the input box and click the Install button next to the "Blues Wireless Notecard" result.

    3. Then, search for “NotecardPseudoSensor” in the input box and click the Install button next to the “Blues Wireless Notecard Pseudo Sensor” result.

    4. Add the following include lines to the top of your sketch:

      #include <Notecard.h>
      #include <NotecardPseudoSensor.h>
    5. Next, include the following namespace under your NotecardPseudoSensor includes.

      using namespace blues;
    6. After that, create an instance of Notecard notecard and NotecardPseudoSensor with the lines of code below.

      Notecard notecard; // required to use NotecardPseudoSensor library
      NotecardPseudoSensor sensor(notecard);
    7. Initialize communication with the NotecardPseudoSensor, by adding the following line to setup().

      notecard.begin(txRxPinsSerial);
    8. Finally, place the following code in your loop function, which generates mock temperature and humidity readings, prints them to the console, and then waits 15 seconds before exiting the loop.

      float temperature = sensor.temp();
      float humidity = sensor.humidity();
      
      usbSerial.print("Temperature = ");
      usbSerial.print(temperature);
      usbSerial.println(" *C");
      usbSerial.print("Humidity = ");
      usbSerial.print(humidity);
      usbSerial.println(" %");
      
      delay(15000);
    9. Upload this code to your Adafruit HUZZAH32. Open the Serial Monitor and you'll see temperature and humidity readings every 15 seconds.

    1. To generate mock sensor readings you'll use the NotecardPseudoSensor library .

    2. Start by clicking on Tools > Manage Libraries...

    3. Then, search for “NotecardPseudoSensor” in the input box and click the Install button next to the “Blues Wireless Notecard Pseudo Sensor” result.

    4. Add the following include to the top of your sketch:

      #include <NotecardPseudoSensor.h>
    5. Next, include the following namespace under your NotecardPseudoSensor includes.

      using namespace blues;
    6. After that, create an instance of NotecardPseudoSensor with the lines of code below. Add it right under the Notecard notecard; line of code.

      Notecard notecard;
      NotecardPseudoSensor sensor(notecard);
    7. Finally, place the following code in your loop function, which generates mock temperature and humidity readings, prints them to the console, and then waits 15 seconds before exiting the loop.

      float temperature = sensor.temp();
      float humidity = sensor.humidity();
      
      usbSerial.print("Temperature = ");
      usbSerial.print(temperature);
      usbSerial.println(" *C");
      usbSerial.print("Humidity = ");
      usbSerial.print(humidity);
      usbSerial.println(" %");
      
      delay(15000);
    8. Upload this code to your Adafruit HUZZAH32. Open the Serial Monitor and you'll see temperature 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 sync field to true (to instruct the Notecard to sync to Notehub immediately), and finally, sets the body to the sampled temperature and humidity. Add the following to the loop() function, after the usbSerial commands used to print out the readings.

      char json_request[150];
      snprintf(json_request, sizeof(json_request),
          "{"
            "\"req\":\"note.add\","
            "\"file\":\"sensors.qo\","
            "\"sync\":true,"
            "\"body\":{"
              "\"temp\":%d.%02d,"
              "\"humidity\":%d.%02d"
            "}"
          "}",
          (int)temperature, abs(((int)(temperature*100.0)%100)),
          (int)humidity, (int)(humidity*100.0)%100
      );
    2. Now, send the request to your Notecard and read the result. Add the following code, just before the call to delay:

      txRxPinsSerial.println(json_request);
      delay(250); // wait for the Notecard to respond
      while (txRxPinsSerial.available() > 0) {
          char incomingByte = txRxPinsSerial.read();
          if (incomingByte != '\r' && incomingByte != '\n') {
              usbSerial.print(incomingByte);
          }
      }
      usbSerial.println();
    3. Upload the code to your device. After rebooting, 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.

    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 sync field to true (to instruct the Notecard to sync to Notehub immediately), and finally, sets the body to the sampled temperature and humidity. Add the following to the loop() function, after the usbSerial commands used to print out the readings.

      J *req = notecard.newRequest("note.add");
      if (req != NULL) {
         JAddStringToObject(req, "file", "sensors.qo");
         JAddBoolToObject(req, "sync", true);
         J *body = JAddObjectToObject(req, "body");
         if (body) {
            JAddNumberToObject(body, "temp", temperature);
            JAddNumberToObject(body, "humidity", humidity);
         }
         notecard.sendRequest(req);
      }
    2. Upload the code to your device. After rebooting, 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.

      The new device in Notehub

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

      The event list in Notehub

    Congratulations!

    You’ve successfully connected your Adafruit HUZZAH32 and sensor to your Notecard!

    Now you're ready to connect this Notecard project to a cloud application! Take a look at our routing tutorials, which cover a number of popular cloud applications and data visualization tools.

    note

    During this tutorial, you set your Notecard into continuous mode, which maintains an active network 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
    • ★
      ★
    • ★
      ★
    • ★
      ★
    • ★
      ★
    • ★
      ★
    © 2023 Blues Inc.Terms & ConditionsPrivacy
    blues.ioTwitterLinkedInGitHubHackster.io
    Disconnected
    Notecard 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.

    Advanced Usage

    The help command gives more info.

    Connect a Notecard
    Use USB to connect and start issuing requests from the browser.
    Try Notecard Simulator
    Experiment with Notecard's latest firmware on a Simulator assigned to your free Notehub account.

    Don't have an account? Sign up