😌 Learn How to Simplify Host Firmware Updates with the Notecard on February 2nd !

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)CircuitPythonC/C++ (STM32Cube)Python
and
Adafruit Feather M4 ExpressBlues Wireless SwanAdafruit HUZZAH32Arduino Nano 33 BLE SenseArtemis Thing PlusRaspberry PiSparkFun MicroMod STM32 ProcessorSTM32 DiscoverySTM32 Nucleo
and
Notecarrier-ANotecarrier-FNotecarrier-PiSparkFun MicroMod Cellular Function BoardSparkfun Qwiic Cellular

note

Using a Notecarrier-AF? The AF is no longer available for purchase, but we still support it. You can complete the Notecarrier-F version of this tutorial with that board.

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 CircuitPython running on a Adafruit Feather M4 Express wired up to Notecarrier-F 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 Feather M4 Express and Notecard mounted on a Notecarrier-F.

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

  • Notecard mounted to Notecarrier-F.
  • Any CircuitPython-capable Microcontroller (MCU) with Feather headers. We'll be using the Adafruit Feather M4 Express , but any MCU with a CircuitPython bootloader and binary will do. Be sure to follow the instructions for burning the bootloader and flashing the CircuitPython binary to your device. And if you're using the Adafruit Feather M4 Express, make sure you update your board to the lastest version of CircuitPython .

  • A text editor or IDE that works well with CircuitPython, like Mu or Visual Studio Code .

  • Micro USB to USB-A cable.

Connect the sensor and MCU to the Notecarrier

  1. Plug your Feather-compatible Adafruit Feather M4 Express device into the Feather headers on the Notecarrier-F.

  2. Attach your microcontroller to your computer with a Micro USB to USB-A cable, using the Micro USB port on the microcontroller.

    The MCU connected to the Notecarrier-F

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

Now you're ready to write some firmware. When communicating with the Notecard, you can manually send requests using the Serial write function and passing-in JSON objects, or use the note-python library. The code snippets below show both, so feel free to use the approach that works for you.

note

The rest of this tutorial assumes you have already burned the bootloader and flashed the CircuitPython binary to your MCU.

If you haven't, please consult Adafruit's guide for Installing CircuitPython .

Configure your Notecard

Install the Notecard Python Library

  1. To use the note-python library , you'll first need to download or clone it from the GitHub repo .

  2. Unzip the archive and copy the notecard directory into the lib directory of your CIRCUITPY mount.

  3. Add an import for the library at the top of your code.py file.

    import notecard

Set up Your Notecard

  1. Add some additional imports to the top of your code.py file:

    import board
    import busio
    import time
    import json
  2. Add a definition for your ProductUID using the value you specified when creating your Notehub project.

    productUID = "com.your-company.your-name:your_product"
  3. Configure the I2C Bus and connection to your Notecard, and initialize the connection to the Notecard using the OpenI2C function. Be sure to set the debug parameter to True to see Notecard requests and responses.

    port = busio.I2C(board.SCL, board.SDA)
    card = notecard.OpenI2C(port, 0, 0, debug = True)
  4. 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.

    req = {"req": "hub.set"}
    req["product"] = productUID
    req["mode"] = "continuous"
    rsp = card.Transaction(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 Transaction function.

  5. Save the code.py file to flash this code to your device.

  6. Using your IDE or tool of choice, open a Serial monitor to your CircuitPython device. 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

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.

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

  1. To generate psuedo sensor readings you'll use the notecard-pseudo-sensor library. Start by downloading or cloning the library from its GitHub repo .

    The location of the download button in GitHub

  2. Next, unzip the archive and copy the notecard_pseudo_library directory into the lib directory of the CIRCUITPY mount.

    How to move the sensor library to the device

  3. Add an import for the library at the top of your code.py file.

    import notecard_pseudo_sensor
  4. Configure the pseudo sensor with a reference to the Notecard you created earlier.

    sensor = notecard_pseudo_sensor.NotecardPseudoSensor(card)
  5. Add the following code block to the bottom of your code.py file. This takes a mock temperature and humidity reading before sleeping for 15 seconds and repeating the process.

    while True:
       temp = sensor.temp()
       humidity = sensor.humidity()
       print("\nTemperature: %0.1f C" % temp)
       print("Humidity: %0.1f %%" % humidity)
    
       time.sleep(15)
  6. Save code.py and reopen the Serial monitor. Every 15 seconds, you'll see new readings.

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 sensor temperature and humidity. Add the following in while loop right after the print commands to print out readings.

    req = {"req": "note.add"}
    req["file"] = "sensors.qo"
    req["sync"] = True
    req["body"] = { "temp": temp, "humidity": humidity}
    rsp = card.Transaction(req)
    print(rsp)
  2. Save this code to your device. After restart, 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 Feather M4 Express 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