Scaling an IoT deployment? Join our webinar on May 28th where we dive into real-world scaling pain points and how to overcome them.

Blues Developers
What’s New
Resources
Blog
Technical articles for developers
Newsletter
The monthly Blues developer newsletter
Terminal
Connect to a Notecard in your browser
Developer Certification
Get certified on wireless connectivity with Blues
Webinars
Listing of Blues technical webinars
Blues.comNotehub.io
Shop
Docs
Button IconHelp
Notehub StatusVisit our Forum
Button IconSign In
Sign In
Sign In
Docs Home
What’s New
Resources
Blog
Technical articles for developers
Newsletter
The monthly Blues developer newsletter
Terminal
Connect to a Notecard in your browser
Developer Certification
Get certified on wireless connectivity with Blues
Webinars
Listing of Blues technical webinars
Blues.comNotehub.io
Shop
Docs
Guides & Tutorials
Collecting Sensor Data
IntroductionSet up HardwareCreate a Notehub ProjectWrite FirmwareView Data in NotehubUse Environment VariablesNext Steps
Routing Data to Cloud
Building Edge ML Applications
Best Practices for Production-Ready Projects
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
Diagnosing GPS Issues
Encrypting and Decrypting Data with the Notecard
Feather MCU Low Power Management
Minimizing Latency
Notecard Communication Without a Library
Recovering a Bricked Notecard
Remote Command and Control
Sending and Receiving Large Binary Objects
Serial-Over-I2C Protocol
Understanding Environment Variables
Understanding Notecard Penalty Boxes
Updating ESP32 Host Firmware
Using External SIM Cards
Using JSONata to Transform JSON
homechevron_rightDocschevron_rightGuides & Tutorialschevron_rightSensor Tutorial with Python, Raspberry Pi, and Notecarrier Pi

Collecting Sensor Data
- Python, Raspberry Pi, and Notecarrier Pi

Get started with:
Python
PythonC/C++ (Arduino/Wiring)C/C++ (STM32Cube)CircuitPython
and
Raspberry Pi
Raspberry PiAdafruit Feather M4 ExpressAdafruit HUZZAH32Arduino Nano 33 BLE SenseArtemis Thing PlusBlues SwanSparkFun MicroMod STM32 ProcessorSTM32 DiscoverySTM32 Nucleo
and
Notecarrier Pi
Notecarrier PiNotecarrier ANotecarrier FSparkFun 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 Notehub. You'll use Python running on a Raspberry Pi wired up to Notecarrier Pi hardware. If you would like to use a different language, board, or Notecarrier, modify the dropdowns at the top of this guide. And if you get stuck at any point, the full source code for each example is available on GitHub .

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

Set up Hardware

First, you'll need to get all of your hardware connected. Follow the instructions below to connect your Raspberry Pi and Notecard mounted on a Notecarrier Pi.

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

  • Notecard mounted to Notecarrier Pi. Be sure to use standoffs to secure the Notecarrier Pi to your Raspberry Pi to prevent a short between the external SIM slot on the Notecarrier Pi and the HDMI out port on the Raspberry Pi.

  • A Raspberry Pi. Any Single-Board Computer with a Raspberry Pi-compatible 40-pin connector, along with Pip and Python 3.x.x will do. If you plan to use a Raspberry Pi-compatible device, see the Notecarrier Pi Datasheet for pin configuration info.

  • A text editor for writing a Python script on the Raspberry Pi. For the sake of this tutorial, nano is sufficient and is installed by default on Raspberry Pi OS. For a better experience, you can use VSCode with the "Remote - SSH" and "Python" extensions installed.

warning

Due to the power requirements of the Notecard, some Raspberry Pi 2 and 3 models include a current-limiting fuse that will power-cycle the device when the Notecard's modem is on and transmitting. To avoid these issues, we recommend using only Raspberry Pi 4 or 5 devices with the Notecard and a Notecarrier.

Connect the Raspberry Pi to the Notecarrier

  1. Plug the Notecarrier Pi into the 40-pin GPIO header of your Raspberry Pi.

And you're done!

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".

  3. 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 build your app. Before writing code, you'll need to ssh into your Raspberry PI from a terminal program and configure I2C communications.

Configure the Raspberry Pi

Configure I2C for the Notecard and Raspberry Pi

The Notecarrier Pi uses I2C for communication from the Raspberry Pi. To enable I2C, use the raspi-config utility.

  1. From a Pi terminal session, launch the "Raspberry Pi Software Configuration Tool (raspi-config)."
$
sudo raspi-config
  1. Select Interface Options.

  2. Select P5 I2C.

  3. For "Would you like the ARM I2C interface to be enabled?", select <Yes>.

  4. The next screen will confirm your selection, and should read, "The ARM I2C interface is enabled". Select <OK>.

  5. Right arrow twice, and select the <Finish>.

  6. If prompted, "Would you like to reboot now?". Select <Yes>.

Install I2C Helper Tools

  1. Once your Pi comes back online, SSH back into the device.

  2. Install the i2c-tools package, so you can confirm connections to I2C devices.

$
sudo apt-get install -y i2c-tools
  1. Run i2cdetect to ensure the Notecard is detected.
$
sudo i2cdetect -y 1

You should see 17 in the output. This means a device responded at 0x17, which is the I2C address of the Notecard.

Write Code

warning

By default, the Notecarrier Pi provides only an I2C connection between the Pi host and the Notecard. To enable Serial communication, the SERIAL TXRX dip switch must be toggled to the ON position.

To communicate with the Notecard over I2C, you'll need to install the note-python and python-periphery libraries. And to generate mocked sensor readings, you'll need to install the notecard-pseudo-sensor library.

  1. To use the note-python, notecard-pseudo-sensor, and python-periphery libraries you'll need to install them using pip3.
$
pip3 install note-python notecard-pseudo-sensor python-periphery
  1. In your project directory, create a new file called sensors.py.
$
nano sensors.py
  1. Add the following imports to the top of the file:
import json
import notecard
import notecard_pseudo_sensor
from periphery import I2C
import time
  1. Next, add a variable for your ProductUID using the value you specified when creating your Notehub project:
productUID = "com.your-company.your-name:your_product"
  1. Configure the I2C connection to your Notecard, and initialize it using the OpenI2C function:
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port, 0, 0)
  1. Now, we'll configure the Notecard. 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).
req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
 
print(json.dumps(req))
 
rsp = card.Transaction(req)
print(rsp)

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

  1. Save and exit the sensors.py file. From the same directory, use the python3 interpreter to run the program.
$
python3 sensors.py
{"req": "hub.set", "product": "com.your-company.your-name:your_product", "mode": "continuous"}
{}

If your terminal output looks similar to that above, then you are talking to your Notecard and all of your hardware is configured correctly!

Read from the Sensor

Now that you've configured your Raspberry Pi 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. Add the line below to instantiate a new instance of the sensor class for generating mock values.
sensor = notecard_pseudo_sensor.NotecardPseudoSensor(card)
  1. Add a while True block to the bottom of your sensors.py file that uses the code below. Now the program will take a mocked temperature and humidity reading, and print those values to the console before sleeping for 15 seconds and repeating.
while True:
  temp = sensor.temp()
  humidity = sensor.humidity()
  print('Temperature: {} degrees C'.format(temp))
  print('Humidity: {}%'.format(humidity))
 
  time.sleep(15)
  1. Save and exit the sensors.py file. From the same directory, use the python3 interpreter to run the program. Only this time, after the Notecard request and response statements, you'll see the temperature and humidity values logged to the terminal every 15 seconds.
$
python3 sensors.py
{"req": "hub.set", "product": "com.your-company.your-name:your_product", "mode": "continuous"}
{}
Temperature: 33.875 degrees C
Humidity: 49.9511%
...

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 while True: loop, after the print() statements used to print out the readings.
req = {"req": "note.add"}
req["file"] = "sensors.qo"
req["sync"] = True
req["body"] = { "temp": temp, "humidity": humidity }
  1. To send the request to your Notecard and read the result, add the following just before the time.sleep(15) statement:
rsp = card.Transaction(req)
print(rsp)
  1. Save and exit the sensors.py file. From the same directory, use the python3 interpreter to run the program. As it executes, the program output 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.
$
python3 sensors.py
{"req": "hub.set", "product": "com.your-company.your-name:your_product", "mode": "continuous"}
{}
Temperature: 24.431171875 degrees C
Humidity: 50.35774813702876%
{"total": 1}
Temperature: 24.4348828125 degrees C
Humidity: 50.418771430827704%
{"total": 2}
...

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

Use Environment Variables

Environment variables are a Notehub state and settings management feature that allow you to set variables in key-value pairs, and intelligently synchronize those values across devices and fleets of devices.

In this section you'll learn how environment variables work by creating a variable that determines how often your firmware should take sensor readings.

Using Environment Variables in Firmware

The Notecard provides a set of requests for working with environment variables. The most common of these requests is env.get, which allows you to retrieve the value of an environment variable.

Complete the steps below to use the env.get request to retrieve and use the reading_interval environment variable.

  1. First, adjust your existing hub.set configuration to set the inbound argument to 5. This tells your Notecard to look for inbound changes from Notehub every 5 minutes.

    req = {"req": "hub.set"}
    req["product"] = productUID
    req["mode"] = "continuous"
    req["inbound"] = 5 # add this line
    rsp = card.Transaction(req)
  2. Next, place the following new function before the existing while True loop.

    # This function assumes you’ll set the reading_interval environment variable to
    # a positive integer. If the variable is not set, set to 0, or set to an invalid
    # type, this function returns a default value of 60.
    def get_sensor_interval():
       sensor_interval_seconds = 60
       req = {"req": "env.get"}
       req["name"] = "reading_interval"
       rsp = card.Transaction(req)
       if rsp and rsp["text"] and int(rsp["text"]) > 0:
          sensor_interval_seconds = int(rsp["text"])
       return sensor_interval_seconds
  3. Finally, find the existing time.sleep(15) line in your while True loop, and replace it with the code below.

    sensor_interval_seconds = get_sensor_interval()
    print(f"Delaying {sensor_interval_seconds} seconds")
    time.sleep(sensor_interval_seconds)

Your firmware now uses the reading_interval environment variable to determine how many seconds to delay in between sensor readings.

Save and exit the sensors.py file. From the same directory, use the python3 interpreter to run the program. You should see your device using the default reading_interval value of 60 seconds.

Setting an Environment Variable

Now we have our device programmed to retrieve an Environment Variable from Notehub, we will create that variable. Environment variables can be set in the Notehub UI or through the Notehub API. In this tutorial you'll learn how to set the values through the Notehub UI. If you'd like to instead set environment variables through the Notehub API, refer to environment variable requests in the Project API.

  1. Return to your Notehub project, go the the Devices page, and double click your device. You should see a screen that looks like this.

    The Notehub device screen

  2. Click the Environment tab.

  3. Under the Device environment variables header, define a new environment variable named reading_interval and set its value to 30.

    The environment screen with a new value set

Now that you have an environment variable set, you'll see it reflected on your device after your configured inbound interval has passed.

$
python3 sensors.py
...
Temperature: 19.9 C
Humidity: 49.3 %
{"req": "note.add", "body": {"humidity": 49.3467, "temp": 19.875}, "sync": true, "file": "sensors.qo"}
{"total":1}
{'total': 1}
{"name": "reading_interval", "req": "env.get"}
{"text":"30"}
Delaying 30 seconds
note

On cellular and Wi-Fi-based Notecards you can use the hub.set request's sync argument to immediately receive inbound updates instead of relying on the inbound interval.

And with that, you've used your first environment variable on your Notecard! To see the real power of environment variables in action, try returning to Notehub and updating your device's reading_interval with your serial monitor open.

note

This tutorial had you use several configuration settings that are best used when you have your Notecard connected to mains power.

  • In the hub.set request, setting mode to "continuous" tells the Notecard to maintain an active network connection.

  • In the hub.set request, setting sync to true tells the Notecard to immediately synchronize inbound Notes and environment variables from Notehub.

  • In the note.add request, setting sync to true tells the Notecard to immediately synchronize all outbound Notes to Notehub.

Because each of these settings cause the Notecard to use more power, you may wish to disable them if you plan to transition your project to battery power. You can run the command below to put your Notecard into periodic mode with the sync argument turned off.

{
 "req": "hub.set",
 "mode": "periodic",
 "sync": false,
 "outbound": 60,
 "inbound": 120
}

Learn more about optimizing the Notecard for low-power scenarios in Low Power Design.

Next Steps

Congratulations! You've successfully connected your Raspberry Pi to your Notecard and built a basic IoT project.

If you're following the Blues Quickstart, next we recommend learning how to send (and visualize) your data in a cloud application:

  1. Use the Notecard to Send Data
  2. Set Up Your Microcontroller
  3. Build Your First IoT App With Blues
  4. Send Data to Your Cloud

At any time, if you find yourself stuck, please reach out on the community forum .

Can we improve this page? Send us feedback
© 2025 Blues Inc.
© 2025 Blues Inc.
TermsPrivacy
Notecard Disconnected
Having trouble connecting?

Try changing your 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