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.
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 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.
The tutorial uses mock sensor readings for simplicity, but feel free to hook up a physical sensor of your choice and use that instead.
If you get stuck at any point during this tutorial, the full source for each example is available on GitHub:
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.
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 devices with the Notecard and a Notecarrier.
- Plug the Notecarrier-Pi into the 40-pin GPIO header of your Raspberry Pi.
And you're done!
Now that your hardware is all connected, let’s create a new Notehub project to receive sensor readings from your Notecard.
Navigate to notehub.io and log-in, or create a new account.
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
"com.your-company.your-name:your_product"
.
Take note of your ProductUID. This identifier is used by Notehub to associate your Notecard to your project.
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.
The Notecarrier-Pi uses I2C for communication
from the Raspberry Pi. To enable I2C, use the raspi-config
utility.
- From a Pi terminal session, launch the "Raspberry Pi Software Configuration Tool (raspi-config)."
sudo raspi-config
Select Interface Options.
Select P5 I2C.
For "Would you like the ARM I2C interface to be enabled?", select <Yes>.
The next screen will confirm your selection, and should read, "The ARM I2C interface is enabled". Select <OK>.
Right arrow twice, and select the <Finish>.
If prompted, "Would you like to reboot now?". Select <Yes>.
Once your Pi comes back online, SSH back into the device.
Install the
i2c-tools
package, so you can confirm connections to I2C devices.
sudo apt-get install -y i2c-tools
- Run
i2cdetect
to ensure the Notecard is detected.
sudo i2cdetect -y 1
You should see
17
in the output. This means a device responded at0x17
, which is the I2C address of the Notecard.
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.
- To use the
note-python
,notecard-pseudo-sensor
, andpython-periphery
libraries you'll need to install them usingpip3
.
pip3 install note-python notecard-pseudo-sensor python-periphery
- In your project directory, create a new file called
sensors.py
.
nano sensors.py
- Add the following imports to the top of the file:
import json
import notecard
import notecard_pseudo_sensor
from periphery import I2C
import time
- 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"
- 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)
- 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 incontinuous
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.
- Save and exit the
sensors.py
file. From the same directory, use thepython3
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!
Now that you've configured your Raspberry Pi to communicate with the Notecard, let's grab some pseudo sensor readings.
If you have your own sensor, feel free to hook it up and use your own values instead of this tutorial's mocked ones.
- Add the line below to instantiate a new instance of the sensor class for generating mock values.
sensor = notecard_pseudo_sensor.NotecardPseudoSensor(card)
- Add a
while True
block to the bottom of yoursensors.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)
- Save and exit the
sensors.py
file. From the same directory, use thepython3
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%
...
Now that we're getting sensor readings, let's send these to our Notecard.
- 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 thesync
field toTrue
(to instruct the Notecard to sync to Notehub immediately), and finally, sets thebody
to the sampled temperature and humidity. Add the following to thewhile True:
loop, after theprint()
statements used to print out the readings.
req = {"req": "note.add"}
req["file"] = "sensors.qo"
req["sync"] = True
req["body"] = { "temp": temp, "humidity": humidity }
- 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)
- Save and exit the
sensors.py
file. From the same directory, use thepython3
interpreter to run the program. As it executes, the program output will update to display the response from thenote.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}
...
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.
Return to notehub.io and open your project. You should see your notecard in the Devices view.
Now, click on the Events left menu item. Once your sensor Notes start syncing, they’ll show up here.
You’ve successfully connected your Raspberry Pi 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.
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
}