<html> <p class=„lead“>This tutorial describes how to setup the Adafruit Feather M0 with RFM95 LoRa Radio for The Things Network and transmit the battery voltage encoded to reduce the duty cycle.</p><div class=„content“ readability=„81“> <p>To work with The Things Network, a wire has to be soldered from DI01 to D6 (blue wire):</p> <p><img src=„https://blog.werktag.io/posts/adafruit-feather-m0-lora-on-ttn/feather-lora-wiring.png“ alt=„feather wiring“/></p> <p>Adafruit <a href=„https://learn.adafruit.com/adafruit-feather-m0-radio-with-lora-radio-module/antenna-options“>recommends</a> to solder an antenna with 8.2 cm length (for 868 MHz) to the antenna pin (yellow wire).</p> <p>Setup the Arduino IDE with the necessary libraries: Follow the <a href=„https://learn.adafruit.com/adafruit-feather-m0-radio-with-lora-radio-module/overview“>tutorial</a> on Adafruit, or if your Arduino IDE (or VS Code with Arduino integration) is already setup, do:</p> <ol><li>Add
https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
to your Additional Boards Manager URLs</li> <li>Install:
Arduino SAMD Boards
in the Boards Manager</li> <li>Install:
Adafruit SAMD
in the Boards Manager</li> <li>Select the Board Type
Feather M0
</li> <li>Under Linux/Ubuntu, you might have to add your user to the dialout group:
sudo usermod -a -G dialout $USER
, and make sure the changes are applied.</li> <li>Select the port for your feather.</li> </ol><p>Clone this Arduino Sketch:
git clone https://github.com/werktag/m0-lorawan-ttn
</p> <p>Clone the lmic library adjusted for the Feather M0 LoRa into your Arduino library folder:
git clone https://github.com/huebe/arduino-lmic
(The Arduino library folder is usually be found in
Documents\Arduino\libraries
in Windows, and
~/Arduino/libraries
in Linux)</p> <p>The changes to the original lmic library are a slower SPI speed and the correct radio settings.</p> <p>Now it’s time to verify the sketch and upload it onto your feather. If the Sketch can’t be uploaded, a double click on the reset button of the feather might help. The blinking red LED indicates that your feather is in bootloader state and ready to be programmed.</p> <p>If you don’t have login for The Things Network (TTN), create one.</p> <p>To adjust the TTN Credentials in the Sketch:</p> <ul><li>In the <a href=„https://console.thethingsnetwork.org/“>TTN Console</a>, create an application.</li> <li>Add a device to the application. Give it a Device ID of you choice, click the “Generate” button for Device EUI, leave App Key empty and press “Register”.</li> <li>In the Device Overview, copy the Application EUI to the APPEUI variable in the Sketch in the C-Style lsb format.</li> <li>In the Device Overview, copy the Device EUI to the DEVEUI variable in the Sketch in the C-Style lsb format.</li> <li>In the Device Overview, copy the App Key to the APPKEY variable in the Sketch in the C-Style msb format.<br/></li> </ul><p><img src=„https://blog.werktag.io/posts/adafruit-feather-m0-lora-on-ttn/ttn-console-format.png“ alt=„ttn console“/></p> <p>As you might notice in the Sketch, the battery voltage gets squeezed into one byte:</p> <div class=„highlight“ readability=„14“> <pre class=„language-c“ data-lang=„c“> Payload format Bytes: [(Bat-Voltage - 2) * 100] double measuredvbat = analogRead(VBATPIN); measuredvbat *= 2; we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage measuredvbat /= 1024; // convert to voltage measuredvbat -= 2; // offset by -2 measuredvbat *= 100; //make it centi volts vals[0] = (unsigned char)measuredvbat; //we're unsigned</pre></div>
<p>As duty cycle is everything when transmitting data over LoRaWan, it’s recommended to compress your data as much as possible. To make the data handling easier on the server, TTN provides a decoder functionality to decode the value into the better readable JSON format. In the <a href=„https://console.thethingsnetwork.org/“>TTN Console</a> -> Application -> [your application] -> Payload Formats, add the following decoder function in the [decoder] tab:</p> <div class=„highlight“ readability=„7“> <pre class=„language-javascript“ data-lang=„javascript“>function Decoder(b, port) {
var bat = b[0] / 100 + 2;
return {
battery: bat
}
} </pre></div> <p>The decoded battery value should now appear in the Application Data Tab: <img src=„https://blog.werktag.io/posts/adafruit-feather-m0-lora-on-ttn/ttn-console-data.png“ alt=„ttn data“/></p> <p>(as you might notice, the battery is charging ;) )</p> <p>That’s it, enjoy LoRaWan with your feather!</p> </div> </html>