mirror of
https://github.com/lupyuen/lupyuen.github.io.git
synced 2025-01-13 09:08:30 +08:00
546 lines
No EOL
33 KiB
HTML
546 lines
No EOL
33 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="generator" content="rustdoc">
|
||
<title>The Things Network on PineDio Stack BL604 RISC-V Board</title>
|
||
|
||
|
||
<!-- Begin scripts/articles/*-header.html: Article Header for Custom Markdown files processed by rustdoc, like chip8.md -->
|
||
<meta property="og:title"
|
||
content="The Things Network on PineDio Stack BL604 RISC-V Board"
|
||
data-rh="true">
|
||
<meta property="og:description"
|
||
content="How we join The Things Network and send data to the cloud on the new PineDio Stack BL604 RISC-V Board"
|
||
data-rh="true">
|
||
<meta property="og:image"
|
||
content="https://lupyuen.github.io/images/ttn-title.jpg">
|
||
<meta property="og:type"
|
||
content="article" data-rh="true">
|
||
<link rel="canonical" href="https://lupyuen.org/articles/ttn.html" />
|
||
<!-- End scripts/articles/*-header.html -->
|
||
<!-- Begin scripts/rustdoc-header.html: Header for Custom Markdown files processed by rustdoc, like chip8.md -->
|
||
<link rel="alternate" type="application/rss+xml" title="RSS Feed for lupyuen" href="/rss.xml" />
|
||
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
|
||
<link rel="stylesheet" type="text/css" href="../dark.css">
|
||
<link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle">
|
||
<link rel="stylesheet" type="text/css" href="../prism.css">
|
||
<script src="../storage.js"></script><noscript>
|
||
<link rel="stylesheet" href="../noscript.css"></noscript>
|
||
<link rel="shortcut icon" href="../favicon.ico">
|
||
<style type="text/css">
|
||
#crate-search {
|
||
background-image: url("../down-arrow.svg");
|
||
}
|
||
</style>
|
||
<!-- End scripts/rustdoc-header.html -->
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
<!-- Begin scripts/rustdoc-before.html: Pre-HTML for Custom Markdown files processed by rustdoc, like chip8.md -->
|
||
|
||
<!-- Begin Theme Picker -->
|
||
<div class="theme-picker" style="left: 0"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg"
|
||
width="18" alt="Pick another theme!"></button>
|
||
<div id="theme-choices"></div>
|
||
</div>
|
||
<!-- Theme Picker -->
|
||
|
||
<!-- End scripts/rustdoc-before.html -->
|
||
|
||
|
||
<h1 class="title">The Things Network on PineDio Stack BL604 RISC-V Board</h1>
|
||
<nav id="rustdoc"><ul>
|
||
<li><a href="#add-gateway-to-the-things-network" title="Add Gateway to The Things Network">1 Add Gateway to The Things Network</a><ul>
|
||
<li><a href="#configure-gateway" title="Configure Gateway">1.1 Configure Gateway</a><ul></ul></li>
|
||
<li><a href="#gateway-is-up" title="Gateway Is Up!">1.2 Gateway Is Up!</a><ul></ul></li></ul></li>
|
||
<li><a href="#add-device-to-the-things-network" title="Add Device to The Things Network">2 Add Device to The Things Network</a><ul></ul></li>
|
||
<li><a href="#run-the-lorawan-firmware" title="Run the LoRaWAN Firmware">3 Run the LoRaWAN Firmware</a><ul></ul></li>
|
||
<li><a href="#join-device-to-the-things-network" title="Join Device to The Things Network">4 Join Device to The Things Network</a><ul>
|
||
<li><a href="#join-commands" title="Join Commands">4.1 Join Commands</a><ul></ul></li>
|
||
<li><a href="#we-are-in" title="We Are In!">4.2 We Are In!</a><ul></ul></li></ul></li>
|
||
<li><a href="#send-data-to-the-things-network" title="Send Data to The Things Network">5 Send Data to The Things Network</a><ul>
|
||
<li><a href="#doing-better" title="Doing Better">5.1 Doing Better</a><ul></ul></li></ul></li>
|
||
<li><a href="#the-things-network-coverage" title="The Things Network Coverage">6 The Things Network Coverage</a><ul></ul></li>
|
||
<li><a href="#fair-use-of-the-things-network" title="Fair Use of The Things Network">7 Fair Use of The Things Network</a><ul></ul></li>
|
||
<li><a href="#whats-next" title="What’s Next">8 What’s Next</a><ul></ul></li>
|
||
<li><a href="#notes" title="Notes">9 Notes</a><ul></ul></li></ul></nav><p><img src="https://lupyuen.github.io/images/ttn-title.jpg" alt="PineDio Stack BL604 RISC-V Board (foreground) talking to The Things Network via RAKWireless RAK7248 LoRaWAN Gateway (background)" /></p>
|
||
<p><em>PineDio Stack BL604 RISC-V Board (foreground) talking to The Things Network via RAKWireless RAK7248 LoRaWAN Gateway (background)</em></p>
|
||
<p>📝 <em>21 Sep 2021</em></p>
|
||
<p><em>What is The Things Network?</em></p>
|
||
<p><a href="https://www.thethingsnetwork.org/"><strong>The Things Network</strong></a> is a <strong>crowd-sourced wireless network</strong>. And it works <strong>worldwide</strong>!</p>
|
||
<p>Our <strong>IoT Devices</strong> may connect to The Things Network and <strong>transmit Sensor Data</strong> to the Cloud.</p>
|
||
<p><em>How much does it cost?</em></p>
|
||
<p>Nothing! The public community network is <strong>Free for Fair Use</strong>.</p>
|
||
<p>(The network has been free since its launch in 2015)</p>
|
||
<p><em>Totally free! What’s the catch?</em></p>
|
||
<p>Here’s what we need…</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>LoRa Wireless Module</strong>: We’ll use the <strong>Semtech SX1232 LoRa Transceiver</strong> (Transmitter + Receiver) that’s bundled with our PineDio Stack Board.</p>
|
||
<p>(More about this in a while)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>Network Coverage</strong>: Check whether our area is covered by the network…</p>
|
||
<p><a href="https://www.thethingsnetwork.org/map"><strong>The Things Network Global Coverage</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><strong>Fair Use</strong>: Because it’s a free network for Sensor Data, we can’t spam it with messages.</p>
|
||
<p>Each device may transmit roughly <strong>10 tiny messages per hour</strong>.</p>
|
||
<p>(Assuming 12 bytes per message)</p>
|
||
<p>This varies by region, message size and data rate.</p>
|
||
<p>(More about this in a while)</p>
|
||
</li>
|
||
</ol>
|
||
<p><em>Darn no coverage here. What now?</em></p>
|
||
<p>Everyone is welcome to join The Things Network and <strong>grow the network</strong>!</p>
|
||
<p>In a while I’ll explain how I <strong>added my LoRaWAN Gateway</strong> to The Things Network.</p>
|
||
<p><a href="https://docs.rakwireless.com/Product-Categories/WisGate/RAK7248/Overview/">(I bought my RAKwireless RAK7248 Gateway for $280)</a></p>
|
||
<p><em>What is PineDio Stack?</em></p>
|
||
<p><strong>PineDio Stack</strong> is a 32-bit RISC-V Microcontroller board…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/pinedio2"><strong>“PineDio Stack BL604 runs Apache NuttX RTOS”</strong></a></li>
|
||
</ul>
|
||
<p>Which has an onboard LoRa SX1262 Transceiver…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/lorawan2"><strong>“LoRaWAN on PineDio Stack BL604 RISC-V Board”</strong></a></li>
|
||
</ul>
|
||
<p>Today we’ll walk through the steps for <strong>connecting PineDio Stack to The Things Network</strong>…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-flow.jpg" alt="PineDio Stack BL604 talking to The Things Network via LoRaWAN Gateway" /></p>
|
||
<h1 id="add-gateway-to-the-things-network"><a class="doc-anchor" href="#add-gateway-to-the-things-network">§</a>1 Add Gateway to The Things Network</h1>
|
||
<p>(<strong>Skip this chapter</strong> if you have The Things Network coverage… You’re so lucky! 👍)</p>
|
||
<p>Sadly there’s no The Things Network coverage in my area. Lemme explain how I <strong>added my LoRaWAN Gateway</strong> (RAKWireless RAK7248) to The Things Network.</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-wisgate.png" alt="RAKwireless docs for The Things Network" /></p>
|
||
<p>This is the official doc for adding <strong>RAKWireless RAK7248</strong> (and similar gateways) to The Things Network…</p>
|
||
<ul>
|
||
<li><a href="https://docs.rakwireless.com/Product-Categories/WisGate/RAK7244C/Quickstart/#connecting-to-the-things-network-ttn"><strong>“Connecting to The Things Network”</strong></a></li>
|
||
</ul>
|
||
<p>Run <strong>“<code>sudo gateway-config</code>”</strong> as described to <strong>configure the gateway</strong> for The Things Network. (Instead of ChirpStack)</p>
|
||
<p>We create a <strong>free account</strong> on The Things Network…</p>
|
||
<ul>
|
||
<li><a href="https://www.thethingsnetwork.org/"><strong>“The Things Network: Sign Up”</strong></a></li>
|
||
</ul>
|
||
<p>Log in and select the nearest region. (Either US, Europe or Australia)</p>
|
||
<p>Click <strong><code>Gateways</code></strong> and <strong><code>Add Gateway</code></strong>…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-gateway.jpg" alt="Add Gateway" /></p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>Gateway ID</strong> needs to be globally unique.</p>
|
||
<p>(Choose wisely!)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>Gateway EUI</strong> (Extended Unique Identifier) comes from our LoRaWAN Gateway.</p>
|
||
<p>On our RAKwireless Gateway, run this command to get the EUI…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>gateway-version</code></pre></div></li>
|
||
<li>
|
||
<p><strong>Frequency Plan</strong>: See this…</p>
|
||
<p><a href="https://www.thethingsnetwork.org/docs/lorawan/frequencies-by-country.html"><strong>“Frequency Plans by Country”</strong></a></p>
|
||
</li>
|
||
</ol>
|
||
<p>Fill in the fields and click <strong>“<code>Create Gateway</code>”</strong></p>
|
||
<h2 id="configure-gateway"><a class="doc-anchor" href="#configure-gateway">§</a>1.1 Configure Gateway</h2>
|
||
<p>Take Note: This is missing from the RAKwireless docs…</p>
|
||
<p>The Things Network has been upgraded recently and there’s no longer the option for <strong>“Legacy Packet Forwarder”</strong>.</p>
|
||
<p>Instead we set the <strong>Server Address</strong> like so…</p>
|
||
<ol>
|
||
<li>
|
||
<p>Browse to the Gateway that we have added</p>
|
||
</li>
|
||
<li>
|
||
<p>Click <strong>“<code>Download global_conf.json</code>”</strong></p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-wisgate3.png" alt="Our Gateway in The Things Network" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Open the Downloaded <strong><code>global_conf.json</code></strong> with a text editor.</p>
|
||
<p>It should look like this…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-wisgate2.png" alt="Gateway Config" /></p>
|
||
</li>
|
||
<li>
|
||
<p>On our RAKwireless Gateway, run this…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>sudo gateway-config</code></pre></div></li>
|
||
<li>
|
||
<p>Select <strong>“Edit Packet Forwarder Config”</strong></p>
|
||
</li>
|
||
<li>
|
||
<p>Look for the <strong><code>gateway_conf</code></strong> section…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-gateway2.png" alt="Edit Packet Forwarder Config" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Replace these values from the Downloaded <strong><code>global_conf.json</code></strong>…</p>
|
||
<div class="example-wrap"><pre class="language-json"><code>"gateway_conf": {
|
||
"gateway_ID": ...,
|
||
"server_address": ...,
|
||
"serv_port_up": ...,
|
||
"serv_port_down": ...,</code></pre></div></li>
|
||
<li>
|
||
<p>Scroll down and look for the end of the <strong><code>gateway_conf</code></strong> section (just after <strong><code>beacon_power</code></strong>)…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-gateway3.png" alt="Edit Packet Forwarder Config" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Insert the entire <strong><code>servers</code></strong> section from the Downloaded <strong><code>global_conf.json</code></strong>…</p>
|
||
<div class="example-wrap"><pre class="language-json"><code>"servers": [ {
|
||
"gateway_ID": ...,
|
||
"server_address": ...,
|
||
"serv_port_up": ...,
|
||
"serv_port_down": ...,
|
||
} ]</code></pre></div>
|
||
<p>(Check the trailing commas, especially after <strong><code>beacon_power</code></strong>!)</p>
|
||
</li>
|
||
<li>
|
||
<p>Our updated file should look like this…</p>
|
||
<p><img src="https://lupyuen.github.io/images/gateway-confg.png" alt="Packet Forwarded Config" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Save the file.</p>
|
||
<p>Select <strong>“Restart Packet Forwarder”</strong></p>
|
||
</li>
|
||
</ol>
|
||
<p><a href="https://www.thethingsnetwork.org/docs/the-things-stack/migrate-to-v3/migrate-gateways/">(More about Server Address)</a></p>
|
||
<h2 id="gateway-is-up"><a class="doc-anchor" href="#gateway-is-up">§</a>1.2 Gateway Is Up!</h2>
|
||
<p><em>How will we know if our Gateway is connected?</em></p>
|
||
<p>In The Things Network, browse to our Gateway and click <strong>“<code>Live Data</code>”</strong> (in the left bar)</p>
|
||
<p>We should see the <strong>Heartbeat Messages</strong> (Gateway Status) received from our Gateway…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-wisgate4.png" alt="Gateway Live Data" /></p>
|
||
<p><em>What are the Uplink Messages?</em></p>
|
||
<p>These are LoRa Messages from <strong>nearby devices</strong> that our Gateway has helpfully relayed to The Things Network.</p>
|
||
<p>Yep we’re <strong>officially a contributor</strong> to the globally-connected The Things Network!</p>
|
||
<p>In case of problems, check the <strong>Packet Forwarder Log</strong> on our Gateway…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>sudo tail /var/log/daemon.log</code></pre></div>
|
||
<p><a href="https://lupyuen.github.io/articles/gateway#appendix-packet-forwarder-log">(See sample Packet Forwarder Log)</a></p>
|
||
<h1 id="add-device-to-the-things-network"><a class="doc-anchor" href="#add-device-to-the-things-network">§</a>2 Add Device to The Things Network</h1>
|
||
<p>(If you skipped the previous chapter: Welcome back! We’ll need a <strong>free account</strong> on The Things Network: <a href="https://www.thethingsnetwork.org/"><strong>Click “Sign Up” here</strong></a>)</p>
|
||
<p>Before sending data to The Things Network, we need to <strong>add a device</strong>…</p>
|
||
<ol>
|
||
<li>
|
||
<p>Log in to <a href="https://www.thethingsnetwork.org/"><strong>The Things Network</strong></a>.</p>
|
||
<p>Select the nearest region.</p>
|
||
<p>(Either US, Europe or Australia)</p>
|
||
</li>
|
||
<li>
|
||
<p>Click <strong><code>Applications</code></strong> and <strong><code>Add Application</code></strong>…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-app.png" alt="Add Application" /></p>
|
||
<p>Our devices shall be registered under this Application.</p>
|
||
<p>Fill in any name for the <strong>Application ID</strong>. (Needs to be globally unique)</p>
|
||
<p>Click <strong>“<code>Create Application</code>”</strong></p>
|
||
</li>
|
||
<li>
|
||
<p>In the Application, click <strong><code>"End Devices"</code></strong> (in the left bar)</p>
|
||
<p>Click <strong>“<code>Add End Device</code>”</strong></p>
|
||
<p>Click <strong>“<code>Manually</code>”</strong></p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-device3.png" alt="Register End Device" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Fill in these fields…</p>
|
||
<p><strong>LoRaWAN Version:</strong> MAC V1.0.2</p>
|
||
<p><strong>Regional Parameters Version:</strong> PHY V1.0.2 REV B</p>
|
||
<p><strong>Frequency Plan:</strong> See this…</p>
|
||
<p><a href="https://www.thethingsnetwork.org/docs/lorawan/frequencies-by-country.html"><strong>“Frequency Plans by Country”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p>Click <strong>“<code>Show Advanced Activation</code>”</strong></p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-device5.png" alt="Register End Device" /></p>
|
||
<p><strong>Activation Mode</strong> should be <strong>Over The Air Activation</strong></p>
|
||
</li>
|
||
<li>
|
||
<p>For <strong>DevEUI, JoinEUI, AppKey and NwkKey</strong>:</p>
|
||
<p>Click the buttons for <strong>“<code>Generate</code>”</strong> and <strong>“<code>Fill With Zeros</code>”</strong>…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-device.png" alt="Register End Device" /></p>
|
||
</li>
|
||
<li>
|
||
<p>Click <strong>“<code>Register End Device</code>”</strong></p>
|
||
</li>
|
||
</ol>
|
||
<p><em>Why did we select LoRaWAN Version 1.0.2 Rev B?</em></p>
|
||
<p>This is the version of LoRaWAN that’s supported by our firmware for PineDio Stack.</p>
|
||
<p>(Our firmware is older than the upgraded version of The Things Network)</p>
|
||
<p>If you see <strong>“Message Integrity Code” Errors</strong> later, check the settings above for <strong>LoRaWAN Version</strong>.</p>
|
||
<p><a href="https://www.thethingsindustries.com/docs/getting-started/migrating/major-changes/#lorawan-support">(More about legacy LoRaWAN support)</a></p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-device4.png" alt="Legacy LoRaWAN Support" /></p>
|
||
<h1 id="run-the-lorawan-firmware"><a class="doc-anchor" href="#run-the-lorawan-firmware">§</a>3 Run the LoRaWAN Firmware</h1>
|
||
<p>Now we build, flash and run the <strong>LoRaWAN Firmware</strong> for PineDio Stack!</p>
|
||
<p>This is the <strong>Source Code</strong> for our LoRaWAN Firmware…</p>
|
||
<ul>
|
||
<li><a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/pinedio_lorawan"><strong><code>pinedio_lorawan</code> Firmware</strong></a></li>
|
||
</ul>
|
||
<p>Which calls the following <strong>LoRaWAN and SX1262 Drivers</strong>…</p>
|
||
<ul>
|
||
<li>
|
||
<p><a href="https://github.com/lupyuen/lorawan"><strong><code>lorawan</code> Driver</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://github.com/lupyuen/lora-sx1262"><strong><code>lora-sx1262</code> Driver</strong></a></p>
|
||
</li>
|
||
</ul>
|
||
<p>Follow these instructions to <strong>build, flash and run</strong> the firmware…</p>
|
||
<ol>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/tsen#build-lorawan-firmware"><strong>“Build LoRaWAN Firmware”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/tsen#flash-lorawan-firmware"><strong>“Flash LoRaWAN Firmware”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/tsen#run-lorawan-firmware"><strong>“Run LoRaWAN Firmware”</strong></a></p>
|
||
</li>
|
||
</ol>
|
||
<p>We’re ready to…</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>Join PineDio Stack</strong> to The Things Network</p>
|
||
<p>(Because we need to join the network before sending data)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>Send data from PineDio Stack</strong> to The Things Network</p>
|
||
<p>(And observe the data received by The Things Network!)</p>
|
||
</li>
|
||
</ol>
|
||
<p><a href="https://lupyuen.github.io/articles/lorawan">(Yep this is the same LoRaWAN Firmware that we ported from Apache Mynewt OS to BL602!)</a></p>
|
||
<p><img src="https://lupyuen.github.io/images/lorawan2-title.jpg" alt="Tiny tasty treat… PineDio Stack BL604 RISC-V Board" /></p>
|
||
<h1 id="join-device-to-the-things-network"><a class="doc-anchor" href="#join-device-to-the-things-network">§</a>4 Join Device to The Things Network</h1>
|
||
<p>Let’s <strong>join our PineDio Stack</strong> device to The Things Network!</p>
|
||
<p>Because we’re doing <strong>Over-The-Air Activation</strong>, we need to join the network every time we boot our device.</p>
|
||
<p>In The Things Network, browse to our <strong>Device</strong> and <strong>copy these values</strong> (needed for network activation)…</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>JoinEUI</strong> (Join Extended Unique Identifier)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>DevEUI</strong> (Device Extended Unique Identifier)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>AppKey</strong> (Application Key)</p>
|
||
</li>
|
||
</ol>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-device2.png" alt="Device Overview" /></p>
|
||
<p>Click the icons shown above to <strong>reveal, format and copy</strong> the values.</p>
|
||
<p>Note that the copied values are formatted as…</p>
|
||
<div class="example-wrap"><pre class="language-text"><code>0xAB, 0xBA, 0xDA, 0xBA, ...</code></pre></div>
|
||
<p>Later we shall convert the comma-delimited values to <strong>colon-separated</strong> values…</p>
|
||
<div class="example-wrap"><pre class="language-text"><code>0xAB:0xBA:0xDA:0xBA:...</code></pre></div><h2 id="join-commands"><a class="doc-anchor" href="#join-commands">§</a>4.1 Join Commands</h2>
|
||
<p>Head over to the <strong>Serial Terminal</strong> for PineDio Stack.</p>
|
||
<p>At the PineDio Stack Command Prompt, enter these commands to <strong>join PineDio Stack to The Things Network</strong>…</p>
|
||
<ol>
|
||
<li>
|
||
<p>First we start the <strong>Background Task</strong> that will handle LoRa packets…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>create_task</code></pre></div>
|
||
<p><a href="https://lupyuen.github.io/articles/lora2#event-queue">(<code>create_task</code> is explained here)</a></p>
|
||
</li>
|
||
<li>
|
||
<p>Next we initialise the <strong>LoRa SX1262 and LoRaWAN Drivers</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>init_lorawan</code></pre></div>
|
||
<p><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/pinedio_lorawan/pinedio_lorawan/lorawan.c#L168-L174">(<code>init_lorawan</code> is defined here)</a></p>
|
||
</li>
|
||
<li>
|
||
<p>Set the <strong>DevEUI</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_wr_dev_eui 0xAB:0xBA:0xDA:0xBA:0xAB:0xBA:0xDA:0xBA</code></pre></div>
|
||
<p>Change “<code>0xAB:0xBA:...</code>” to your <strong>DevEUI</strong></p>
|
||
<p>(Remember to change the <strong>“<code>,</code>”</strong> delimiter to <strong>“<code>:</code>”</strong>)</p>
|
||
</li>
|
||
<li>
|
||
<p>Set the <strong>JoinEUI</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_wr_app_eui 0x00:0x00:0x00:0x00:0x00:0x00:0x00:0x00</code></pre></div>
|
||
<p>Change “<code>0x00:0x00:...</code>” to your <strong>JoinEUI</strong></p>
|
||
<p>(Yep change the <strong>“<code>,</code>”</strong> delimiter to <strong>“<code>:</code>”</strong>)</p>
|
||
</li>
|
||
<li>
|
||
<p>Set the <strong>AppKey</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_wr_app_key 0xAB:0xBA:0xDA:0xBA:0xAB:0xBA:0xDA:0xBA0xAB:0xBA:0xDA:0xBA:0xAB:0xBA:0xDA:0xBA</code></pre></div>
|
||
<p>Change “<code>0xAB:0xBA:...</code>” to your <strong>AppKey</strong></p>
|
||
<p>(Again change <strong>“<code>,</code>”</strong> to <strong>“<code>:</code>”</strong>)</p>
|
||
</li>
|
||
<li>
|
||
<p>Finally we send a request to <strong>join The Things Network</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_join 1</code></pre></div>
|
||
<p>“<code>1</code>” means try only once.</p>
|
||
<p><a href="https://lupyuen.github.io/articles/lorawan#join-network-request">(<code>las_join</code> is explained here)</a></p>
|
||
</li>
|
||
</ol>
|
||
<p><a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/pinedio_lorawan#lorawan-commands-for-the-things-network">(Source)</a></p>
|
||
<h2 id="we-are-in"><a class="doc-anchor" href="#we-are-in">§</a>4.2 We Are In!</h2>
|
||
<p>Head back to The Things Network. Browse to our <strong>Application</strong> and click <strong>“<code>Live Data</code>”</strong> (in the left bar)</p>
|
||
<p>We should see <strong>“Successfully Processed Join Request”</strong>…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-join2.png" alt="Application Live Data" /></p>
|
||
<p>Yep our PineDio Stack has successfully joined The Things Network!</p>
|
||
<p>If we see <strong>“Message Integrity Code” Errors</strong>, check the Device Settings. The <strong>LoRaWAN Version</strong> should be <strong>1.0.2 Rev B</strong>.</p>
|
||
<h1 id="send-data-to-the-things-network"><a class="doc-anchor" href="#send-data-to-the-things-network">§</a>5 Send Data to The Things Network</h1>
|
||
<p>Finally we’re ready to send data from PineDio Stack to The Things Network!</p>
|
||
<p>At the PineDio Stack Command Prompt, enter these commands…</p>
|
||
<ol>
|
||
<li>
|
||
<p>We open an <strong>Application Port</strong> that will connect to The Things Network…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_app_port open 2</code></pre></div>
|
||
<p>“<code>2</code>” is the Application Port Number</p>
|
||
<p><a href="https://lupyuen.github.io/articles/lorawan#open-lorawan-port">(<code>las_app_port</code> is explained here)</a></p>
|
||
</li>
|
||
<li>
|
||
<p>Then we <strong>send a Data Packet</strong> containing 5 bytes of data (<code>0x00</code>) to The Things Network at Port 2…</p>
|
||
<div class="example-wrap"><pre class="language-bash"><code>las_app_tx 2 5 0</code></pre></div>
|
||
<p>(“<code>0</code>” means that this is an Unconfirmed Message, we’re not expecting an acknowledgement from The Things Network)</p>
|
||
<p><a href="https://youtu.be/BMMIIiZG6G0"><strong>Watch the demo video on YouTube</strong></a></p>
|
||
<p><a href="https://github.com/lupyuen/bl_iot_sdk/blob/master/customer_app/sdk_app_lorawan/README.md#output-log"><strong>See the output log</strong></a></p>
|
||
</li>
|
||
</ol>
|
||
<p><a href="https://github.com/lupyuen/bl_iot_sdk/tree/master/customer_app/pinedio_lorawan#lorawan-commands-for-the-things-network">(Source)</a></p>
|
||
<p>Switch back to The Things Network. Browse to our <strong>Application</strong> and click <strong>“<code>Live Data</code>”</strong> (in the left bar)</p>
|
||
<p>We should see <strong>5 bytes of <code>0x00</code></strong> received by The Things Network…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-send2.png" alt="Application Live Data" /></p>
|
||
<p>And we’re done!</p>
|
||
<h2 id="doing-better"><a class="doc-anchor" href="#doing-better">§</a>5.1 Doing Better</h2>
|
||
<p><em>Sending 5 bytes of data to the network doesn’t sound particularly exciting?</em></p>
|
||
<p>Yep we’re just getting started!</p>
|
||
<p>In future articles we shall explore The Thing Network’s <strong>Cloud Integration</strong> features for processing our sensor data: MQTT, Webhooks, Storage, Downlinks, Payload Formatters, …</p>
|
||
<p>We shall visualise our sensor data with <strong>MQTT, Prometheus and Grafana</strong>…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/prometheus"><strong>“Monitor IoT Devices in The Things Network with Prometheus and Grafana”</strong></a></li>
|
||
</ul>
|
||
<p>We will store the sensor data in The Things Network and fetch them with <strong>Roblox over HTTP</strong>…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/roblox"><strong>“IoT Digital Twin with Roblox and The Things Network”</strong></a></li>
|
||
</ul>
|
||
<p>We may decode our sensor data in The Things Network with a <strong>Payload Formatter</strong>…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/payload"><strong>“CBOR Payload Formatter for The Things Network”</strong></a></li>
|
||
</ul>
|
||
<p>The Things Network exposes a HTTP POST API for us to <strong>push Downlink Messages</strong> to our devices…</p>
|
||
<ul>
|
||
<li><a href="https://www.thethingsindustries.com/docs/integrations/webhooks/scheduling-downlinks/"><strong>“The Things Network: Scheduling Downlinks”</strong></a></li>
|
||
</ul>
|
||
<p>Which will be useful for <strong>Remote Actuation</strong> of our devices.</p>
|
||
<p>Check this doc for the complete list of Cloud Integration features (including IFTTT and Node-RED)…</p>
|
||
<ul>
|
||
<li><a href="https://www.thethingsnetwork.org/docs/applications-and-integrations/"><strong>“The Things Network: Applications & Integrations”</strong></a></li>
|
||
</ul>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-flow2.jpg" alt="Sending messages for free to The Things Network" /></p>
|
||
<h1 id="the-things-network-coverage"><a class="doc-anchor" href="#the-things-network-coverage">§</a>6 The Things Network Coverage</h1>
|
||
<p>Thanks to The Things Network, we’ve just sent a tiny message to the Cloud… <strong>For Free</strong>!</p>
|
||
<p>(Assuming we have The Things Network coverage)</p>
|
||
<p><em>How’s the coverage for The Things Network worldwide?</em></p>
|
||
<p>Depends on the region.</p>
|
||
<p>According to the <a href="https://www.thethingsnetwork.org/map"><strong>The Things Network Coverage Map</strong></a>, coverage in Singapore is really spotty…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-coverage.jpg" alt="The Things Network coverage in Singapore" /></p>
|
||
<p><em>Can we extend The Things Network coverage?</em></p>
|
||
<p>We can install our own LoRaWAN Gateways and <strong>join them to The Things Network!</strong></p>
|
||
<p><strong>Schools could install gateways</strong> for The Things Network…</p>
|
||
<p>And share free access to The Things Network with <strong>homes, workplaces and devices nearby!</strong></p>
|
||
<p>Hopefully with <strong>affordable, open-source gateways</strong> (like <strong>Pine64’s PineDio Gateway</strong>) we’ll grow The Things Network substantially…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/gateway"><strong>“PineDio LoRa Gateway: Testing The Prototype”</strong></a></li>
|
||
</ul>
|
||
<p><img src="https://lupyuen.github.io/images/lorawan2-pine64.jpg" alt="PineDio Gateway, PinePhone Backplate and USB Adapter" /></p>
|
||
<p><em>PineDio Gateway, PinePhone LoRa Backplate and LoRa USB Adapter</em></p>
|
||
<h1 id="fair-use-of-the-things-network"><a class="doc-anchor" href="#fair-use-of-the-things-network">§</a>7 Fair Use of The Things Network</h1>
|
||
<p><em>The Things Network is Free for Fair Use…</em></p>
|
||
<p><em>How many messages can we send in an hour?</em></p>
|
||
<p>Each device may transmit roughly <strong>10 tiny messages per hour</strong>.</p>
|
||
<p>(Assuming 12 bytes per message)</p>
|
||
<p>This varies by <strong>region, message size and data rate</strong>, as explained here…</p>
|
||
<ul>
|
||
<li><a href="https://www.thethingsnetwork.org/forum/t/fair-use-policy-explained/1300"><strong>“Fair Use Policy Explained”</strong></a></li>
|
||
</ul>
|
||
<p>TLDR: We can <strong>send more messages</strong> to the network if we…</p>
|
||
<ol>
|
||
<li>
|
||
<p><strong>Reduce the Message Size</strong></p>
|
||
<p>(Payload should be <strong>12 bytes</strong> or smaller)</p>
|
||
</li>
|
||
<li>
|
||
<p><strong>Select a Higher Data Rate</strong></p>
|
||
<p>(Our LoRaWAN Driver uses <strong>DR2</strong>, which is 125 kbps)</p>
|
||
</li>
|
||
</ol>
|
||
<p><em>Why does the message rate vary by region?</em></p>
|
||
<p>The Things Network operates on <a href="https://en.wikipedia.org/wiki/ISM_radio_band"><strong>ISM Radio Bands</strong></a>, which are regulated differently across regions.</p>
|
||
<p>To comply with Local Regulations, each device is allowed to <strong>transmit data for up to X seconds</strong> per day. (Where X depends on the region)</p>
|
||
<p>This daily limit is known as the <strong>Duty Cycle</strong>, as explained here…</p>
|
||
<ul>
|
||
<li><a href="https://www.thethingsnetwork.org/docs/lorawan/duty-cycle/"><strong>“The Things Network: Duty Cycle”</strong></a></li>
|
||
</ul>
|
||
<p><em>How can we optimise our messages?</em></p>
|
||
<p>Encode our message payload with <a href="https://en.wikipedia.org/wiki/CBOR"><strong>CBOR (Concise Binary Object Representation)</strong></a> instead of JSON.</p>
|
||
<p>(CBOR works like a compressed, binary version of JSON)</p>
|
||
<p>This JSON Payload occupies <strong>10 bytes</strong>…</p>
|
||
<div class="example-wrap"><pre class="language-json"><code>{ "t": 1745 }</code></pre></div>
|
||
<p>While the CBOR version needs only <strong>6 bytes</strong>!</p>
|
||
<p>To learn more about CBOR…</p>
|
||
<ul>
|
||
<li><a href="https://lupyuen.github.io/articles/cbor"><strong>“Encode Sensor Data with CBOR on BL602”</strong></a></li>
|
||
</ul>
|
||
<p><em>Wow… Fair Use sounds complicated!</em></p>
|
||
<p>This <strong>Airtime Calculator</strong> tells us how many messages we can send in an hour…</p>
|
||
<ul>
|
||
<li><a href="https://avbentem.github.io/airtime-calculator/ttn/us915"><strong>“Airtime Calculator for LoRaWAN”</strong></a></li>
|
||
</ul>
|
||
<p>Select the <strong>Region</strong> (like US915), enter the <strong>Message Payload Size</strong> (say 12 bytes), look up the <strong>Data Rate</strong> (usually DR2) and our answer magically appears…</p>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-airtime.png" alt="Airtime Calculator" /></p>
|
||
<h1 id="whats-next"><a class="doc-anchor" href="#whats-next">§</a>8 What’s Next</h1>
|
||
<p>In the next article, PineDio Stack shall transmit <strong>Real-Time Sensor Data</strong> from a Temperature Sensor to The Things Network…</p>
|
||
<ul>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/cbor"><strong>“Encode Sensor Data with CBOR on BL602”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/tsen"><strong>“Internal Temperature Sensor on BL602”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/gateway"><strong>“PineDio LoRa Gateway: Testing The Prototype”</strong></a></p>
|
||
</li>
|
||
</ul>
|
||
<p>And we shall <strong>visualise the Sensor Data</strong> with <strong>Prometheus and Grafana</strong>…</p>
|
||
<ul>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/grafana"><strong>“Grafana Data Source for The Things Network”</strong></a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/payload"><strong>“CBOR Payload Formatter for The Things Network”</strong></a></p>
|
||
</li>
|
||
</ul>
|
||
<p>Stay Tuned!</p>
|
||
<ul>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/sponsor">Sponsor me a coffee</a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://www.reddit.com/r/RISCV/comments/ps9l4w/the_things_network_on_pinedio_stack_bl604_riscv/">Discuss this article on Reddit</a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/articles/book">Read “The RISC-V BL602 / BL604 Book”</a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io">Check out my articles</a></p>
|
||
</li>
|
||
<li>
|
||
<p><a href="https://lupyuen.github.io/rss.xml">RSS Feed</a></p>
|
||
</li>
|
||
</ul>
|
||
<p><em>Got a question, comment or suggestion? Create an Issue or submit a Pull Request here…</em></p>
|
||
<p><a href="https://github.com/lupyuen/lupyuen.github.io/blob/master/src/ttn.md"><code>lupyuen.github.io/src/ttn.md</code></a></p>
|
||
<h1 id="notes"><a class="doc-anchor" href="#notes">§</a>9 Notes</h1>
|
||
<ol>
|
||
<li>This article is the expanded version of <a href="https://twitter.com/MisterTechBlog/status/1438673926721134596">this Twitter Thread</a></li>
|
||
</ol>
|
||
<p><img src="https://lupyuen.github.io/images/ttn-pinedio.jpg" alt="PineDio Stack BL604 RISC-V Board (foreground) talking to The Things Network via RAKWireless RAK7248 LoRaWAN Gateway (background)" /></p>
|
||
|
||
|
||
<!-- Begin scripts/rustdoc-after.html: Post-HTML for Custom Markdown files processed by rustdoc, like chip8.md -->
|
||
|
||
<!-- Begin Theme Picker and Prism Theme -->
|
||
<script src="../theme.js"></script>
|
||
<script src="../prism.js"></script>
|
||
<!-- Theme Picker and Prism Theme -->
|
||
|
||
<!-- End scripts/rustdoc-after.html -->
|
||
|
||
|
||
</body>
|
||
</html> |