With the proliferation of cellular networks, public Wi-Fi access points, and even LoRaWAN gateways, there is still no “one size fits all” when it comes to wireless IoT deployments. Unfortunately, when designing a product, you often need to choose a single radio access technology (RAT), such as one of the above, before you start doing anything else.
Why does this matter?
- we have to design the product Regarding the form factor of specific RAT components (PCB, physical enclosure, etc.).
- we must choose Appropriate battery technology Optimize performance with that RAT.
- And most importantly, we must. program firmware Target specific RATs.
At the end of the day, I would argue that it doesn’t matter if you’re using LoRa, Cellular, Wi-Fi, or Zigbee. maybe. We just want to connect.
Wireless harmonization with bluesFor this reason, blues note cards It’s such a breath of fresh air. By using Single JSON-based API Additionally, by building around one form factor, you can design products without fear of switching from Cellular to Wi-Fi to LoRa (and vice versa).you can too Deploying the same product with different RATswithout changing a single line of code.
But let’s be crazyYou can freely choose between Cellular, Wi-Fi, and LoRa. But what if you want to use them all?
Today I would like to introduce you to a scenario where you build a single product with the following features:
- First, try connecting to the cloud via LoRa/LoRaWAN.
- If LoRa fails, try using Wi-Fi.
- And what if both fail? Fallback to cellular.
Although this example uses a Raspberry Pi with Python, I want to be doubly clear that the same thing can be 100% achieved using an ESP32, STM32, Nordic (etc.) host with Arduino/C. I would like to go there. Same API command.
Use the following hardware:
- Blues Note Card LoRa (Automatically connect to available networks) network of things gateway).
- Blue Note Card Cell + WiFi (Prepaid cell phone and Wi-Fi on one board).
- blues note carrier pie hat (Two of these are each used as carrier boards for the notecards above).
- Raspberry Pi 5 (also works with 4B and Zero!).
The notecard is inserted into the note carrier.
…to…
Notecarriers can also be stacked on top of each other on the Pi.
How does Notecard sync cloud data?By default, notecards are low power and “sometimes connected” devices. Notecard has up to 1MB of user-addressable flash, allowing you to store large amounts of data before Notecard needs to sync your data to the cloud. This enables battery-powered asset tracking and condition monitoring solutions that cannot be continuously connected.
To be clear, Notecard is not intended to be a drop-in replacement for Wi-Fi.This applies to individual JSON-based events (or Note Contains key-value pairs (in Blues terminology). for example:
{
"event": "a4489717-3ece-4fb6-beab-b40275c8c089",
"device": "dev:864622044598304",
"product": "product:com.blues.rlauer:aaa",
"received": 1704988174.993267,
"req": "note.add",
"file": "fallback.qo",
"body": {
"voltage": 4.9140625
},
"best_location_type": "tower",
"best_location_when": 1704988171,
"best_lat": 43.0738875,
"best_lon": -89.44357812499999,
"best_location": "Shorewood Hills WI",
"best_country": "US",
"best_timezone": "America/Chicago",
"fleets": [
"fleet:4d7ccbcb-a410-4730-9ee2-b483b3523b17"
]
}
Notecard knows exactly where to go as soon as you turn it on. You don’t need to manage security or rotate keys using Notecard. Everything is handled automatically and your data is securely streamed from your device to the cloud.
On the cloud side, blues note hub Acts as a data “hub”. What you can do from Notehub Route your data to any cloud platform Such as AWS, Azure, Datacake, Ubidots, or your own custom HTTPS or MQTT endpoint.
Notehub handles everything. It takes all the pain out of coordinating your own infrastructure to integrate wireless and cloud connectivity.
It is important to note Communication with notecards is bidirectional Essentially, you can start an event on the cloud and use it to distribute it to Notehub. Notehub API, Notehub securely sends its events to one or more Notecard devices (enabling remote control or remote update scenarios).
Python codeWith these high-level concepts in mind, let’s take a look at how to actually build this. LoRa -> Wi-Fi -> Mobile phone Communication device.
When building with Notecard LoRa, it’s important to consider it as the “least common denominator” of Notecards. Everything you can do with Notecard LoRa can also be done with Notecard Cell+WiFI. However, the opposite may not be true. Higher bandwidth activities and location tracking scenarios can easily be handled by Cellular or Wi-Fi, but not as much by LoRa. Let me tell you, it makes sense to start with LoRa.
Step 1: Change the I2C addressThe complete source code for this project is below. Available in this gist.
Since we are connecting to both Notecards via I2C, both Notecards cannot have the same I2C address. 0x17
(This is the default for Notecard). This can be easily handled using: Card.io API, This allows you to set one of them to a new address. 0x18
:
{
"req": "card.io",
"i2c": 24
}
Step 2: Open I2C connectionLearn how to connect Notecard to your in-browser terminal to send API requests. Blues quick start.
Once the command is sent to one of your notecards, you can start programming your Python script.For example, create a new file main.py
on a Raspberry Pi.
Since both Notecards share the same I2C bus, you only need to pass the I2C address of each Notecard in your code.
port = I2C("/dev/i2c-1")nCardLoRa = notecard.OpenI2C(port, 0, 0, True)
nCardCellWiFi = notecard.OpenI2C(port, 0x18, 0, True)
Step 3: Link notecards to Notehub
Next, you need to issue a command to Notecard telling it which project in Notehub to connect to. A “project” is exactly what you think it is: a collection of one or more Notecard devices that provides a logical way to organize and route events to cloud endpoints.
Here’s an example of a Notehub project from my account.
back in main.py
use Hub.set command Each notecard specifies a different notecard sn
(“Serial Number”) This allows you to identify the two Notecards when you start syncing data with Notehub.
rsp = hub.set(nCardLoRa, product=productUID, mode="periodic", sn="lora")
rsp = hub.set(nCardCellWiFi, product=productUID, mode="periodic", sn="cell+wifi")
You must specify your own productUID
(Unique identifier for your Notehub project).
The next setup task is to ensure that the proper Wi-Fi credentials are applied to Notecard Cell+WiFi.Use of card.wifi API, You can specify the required SSID and password.
# update the wifi credentials on the notecard cell+wifi
req = {"req": "card.wifi"}
req["ssid"] = "your-ssid"
req["password"] = "your-password"
rsp = nCardCellWiFi.Transaction(req)
For reference, see this article by TJ VanToll as an example of how to use it. Multiple Wi-Fi access points in one project.
Step 5: Create a memo templatea memo template This acts as a hint to Notecard, allowing it to store data internally as fixed-length records rather than as flexible JSON objects. JSON objects tend to be large. Not only this, save storage Not just note cards saves bandwidth When you sync your data with the cloud.template too Required When using Notecard LoRa.
Again, make sure you create the same template for each notecard. In this project, we’re just sending an arbitrary value, such as the voltage registered on the notecard, but this could be anything.
this is a good point Check out all the great projects created by the blues community Use Notecard and Notehub – from machine learning to asset tracking, condition monitoring, and more
Note The code to create the template is as follows:
# create note template on each notecard
req = {"req": "note.template"}
req["file"] = "fallback.qo"
req["port"] = 10
req["body"] = {"voltage": 12.1}
rsp = nCardLoRa.Transaction(req)
rsp = nCardCellWiFi.Transaction(req)
Step 6: Get any datalook This document section Note Use this to understand what values you should use for different data types when creating templates.
Again, this project keeps things very simple.And to extend it you need the following Several Since we have data to send to the cloud, let’s query Notecard to get the voltage value from the power supply.
# query one of the Notecards for power supply voltage
# this just gives us some arbitrary data to send!
req = {"req": "card.voltage"}
req["mode"] = "?"
rsp = nCardLoRa.Transaction(req)voltage = rsp["value"]
Step 7: Send data in your notes using Notecard LoRa
Recall that the order of connection settings is as follows:
Let’s create your first note and send it with Notecard LoRa. To send this request to Notecard LoRa, note.add API:
# create the outbound note
# arbitrary data based on the voltage reported on the Notecard LoRa
reqNote = {"req": "note.add"}
reqNote["file"] = "fallback.qo"
reqNote["body"] = {"voltage": voltage}
reqNote["sync"] = True# first, try the Notecard LoRa
rsp = nCardLoRa.Transaction(reqNote)
Now comes the difficult part. If Notecard is an “occasionally connected” device, at what point do you consent to whether data has been sent? In this scenario, use (optional). sync
argument. This tells Notecard to try the following: Sync this data now.
after that, Hub.sync.status API After the above note.add
Your request has been sent! This API allows you to check the status of the last sent note.
# monitor a series of requests to hub.sync.status for one minute
t_end = time.time() + 60
didLoRaComplete = Falsewhile time.time() < t_end and didLoRaComplete is False:
req = {"req": "hub.sync.status"}
rsp = nCardLoRa.Transaction(req)
if "completed" in rsp and rsp["completed"] < 3:
# "completed" tells us how many seconds since the last sync completion
# by looking for 3, we are asking if the sync was completed in the last 3 secs
didLoRaComplete = True
time.sleep(1)
Did it work? You can check Notehub to see if that voltage data appears.
wonderful! Project completed!
Now let’s disconnect my personal LoRaWAN gateway and see what happens next. hub.sync.status
request:
# request:
{"req":"hub.sync.status"}
# response:
{'sync': True, 'requested': 59, 'status': 'sync in progress {sync}'}
Um oh. Unfortunately, we are impatient. Since the “sync in progress” is still going on for more than a minute, it’s time to fall back to Wi-Fi.
Step 9: LoRa to Wi-Fi to CellularSince you are migrating from one Notecard device to another, first do the following: Notecard Deleting LoRa notesBecause we don’t want it to be sent later if the LoRaWAN gateway comes back online.
Use of file.delete API, You can clear notes from Notecard LoRa.
req = {"req": "file.delete"}
req["files"] = ["fallback.qo"]
rsp = nCardLoRa.Transaction(req)
Next, let’s create a new note for Notecard Cell+WiFi (same as the original note) note.add
request), wait a few minutes before connecting again. Here is the complete code block:
if didLoRaComplete is False:
# no luck? now try Notecard Cell+WiFi!
# this will default to Wi-Fi, but fallback on Cellular AUTOMATICALLY!
rsp = nCardCellWiFi.Transaction(reqNote)
# while this is running, delete the original note on the Notecard LoRa
# just in case it comes back online
req = {"req": "file.delete"}
req["files"] = ["fallback.qo"]
rsp = nCardLoRa.Transaction(req) # monitor hub.sync.status for a couple of minutes, just for kicks
t_end = time.time() + 120
didCellWiFiComplete = False
while time.time() < t_end and didCellWiFiComplete is False:
req = {"req": "hub.sync.status"}
rsp = nCardCellWiFi.Transaction(req)
if "completed" in rsp and rsp["completed"] < 3:
# "completed" tells us how many seconds since the last sync completion
# by looking for "3", we are asking if the sync was completed in the last 3 secs
didCellWiFiComplete = True
time.sleep(1)
By default, Notecard Cell+WiFi attempts to use Wi-Fi first. Use it again hub.sync.status
You can use the API to check whether a note has been delivered.
{"status":"wifi disconnected {wifi-disconnected}","mode":"{modem-off}","time":1704984927,"sync":true,"completed":50}
Oh no, I don’t have Wi-Fi!
But here are the benefits of using Notecard Cell+WiFi. Fallback feature to mobile phone is automatic. You don’t need to do anything. The Notecard detects that your Wi-Fi is not working and within about a minute turns on your cellular radio to connect to the nearest base station and deliver your data.
resultWhen you return to Notehub, you can look at one of the event details to see how the data is transmitted (this is whether the data was delivered over LoRa, Wi-Fi, or cellular. , are effectively the same).
{
"event": "8dffcd30-0ab2-4f2e-a7bd-95d26bdb2f1f",
"session": "1a1dec6c-b2ee-4226-9758-e7a6399f860f",
"best_id": "cell+wifi",
"device": "dev:860322068073292",
"sn": "cell+wifi",
"product": "product:com.blues.rlauer:aaa",
"app": "app:9a442885-d04d-4f5d-bca5-94eb07f7a27f",
"received": 1704985303.747774,
"req": "note.add",
"file": "fallback.qo",
"body": {
"voltage": 5.1328125
},
"best_location_type": "triangulated",
"best_location_when": 1704984697,
"best_lat": 43.071268,
"best_lon": -89.432769,
"best_location": "Shorewood Hills WI",
"best_country": "US",
"best_timezone": "America/Chicago",
"tri_when": 1704984697,
"tri_lat": 43.071268,
"tri_lon": -89.432769,
"tri_location": "Shorewood Hills WI",
"tri_country": "US",
"tri_timezone": "America/Chicago",
"tri_points": 6,
"fleets": [
"fleet:4d7ccbcb-a410-4730-9ee2-b483b3523b17"
]
}
In fact, you can drill down. session Referring to data from Notecard events, mouse This was used (unfortunately, this information is not available in Notecard LoRa).
So? Once your data is synced with the cloud, the next obvious step is to route that data to your cloud endpoints. Your solution will likely use one of the cloud providers mentioned above.and luckily There’s probably a guide for you!
next stepLearn more about the Notecard API and find other Blues technical resources. Bruce.Development. Otherwise, if you’re ready to get started, get yours blues starter kit Or if you want to do exactly this project: note carrier pie hat together Note card cell + WiFi, and LoRa notecard.
Happy hacking with Bruce! 💙