Example Arduino code of using an Adafruit Feather M0 LoRa module to send sensor data.
This code has been tested with KotahiNet in New Zealand. It should be able to be used on any LoraWAN network with a little modification to the frequency plan.
Where possible I have included links to New Zealand suppliers of components. My go to is nicegear, followed by MindKits. Jaycar Electronics have a range of sensors but they're more expensive.
- Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz [nicegear]
- (optional) BMP085/BMP180/BMP280/BME280 Barometric pressure sensor
- (optional) DHT11/DHT21/DHT22 Humidity and Temperature sensor
- (optional) TSL2561 Light Intensity sensor
- (optional) BH1750FVI Light Intensity sensor
- Download and Install the Arduino IDE
- Import the Adafruit boards
- Install the elapsedMillis Library (Adafruit All About Arduino Libraries)
- Install my fork of the LoRa-LMIC-1.51 Library
To send the most basic "Hello World!" message you need to add two connections on the Feather board, and add an antenna. A straight 87mm long wire makes a perfect antenna to get started. Below are the connections needed:
Open Feather_M0_LoRa.ino in the Arduino IDE and find the LoRaWAN Config section in the code.
Enter the Device Address, Network Session Key, and Application Session Key you recieved from KotahiNet.
eg If you recieved
Device address: 01234567
Network Session Key: 0123456789ABCDEF0123456789ABCDEF
Application Session Key: 0123456789ABCDEF0123456789ABCDEF
You would enter it as
// LoRaWAN Config
// Device Address
devaddr_t DevAddr = 0x01234567;
// Network Session Key
unsigned char NwkSkey[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
// Application Session Key
unsigned char AppSkey[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
Then click the Upload button in the Arduino IDE to compile the code and send it to your Feather. It will upload the code and it will start running. If you've configured the code correctly and you're in range of a KotahiNet receiver then you will have sucessfully sent your first messages.
To change the startup message, edit the STARTUP_MESSAGE
value near the top of the code. This must be LESS THAN 40 CHARACTERS.
/**
* Startup message to send
*/
#define STARTUP_MESSAGE "Hello World!"
Then Upload the code to the Feather again.
Easiest is to get a BMP085/BMP180/BMP280/BME280 breakbout board
- Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor [nicegear]
- Adafruit BMP280 I2C or SPI Barometric Pressure & Altitude Sensor [nicegear]
- Adafruit BMP180 Barometric Pressure/Temperature/Altitude Sensor
- or SparkFun Barometric Pressure Sensor Breakout [nicegear]
- or Generic breakout [Mindkits]
- Install the Adafruit Unified Sensor Library library
- Install the Adafruit_BME280 library
- Install the Adafruit_BMP280 library
- Install the Adafruit_BMP085_Unified library
Remove the comments in front of the sensor you have, eg
//#define SENSOR_BMP085 // BMP085
//#define SENSOR_BMP180 // BMP180
//#define SENSOR_BMP280 // BMP280
#define SENSOR_BME280 // BME280
Then Upload the code to the Feather again.
Or a breakout board that has the resistor included
- Install the Adafruit Unified Sensor Library library
- Install the Adafruit DHT Humidity & Temperature Unified Sensor Library
- Install the Adafruit DHT Sensor Library
Remove the comments in front of the sensor you have, eg
//#define SENSOR_DHT11 // DHT 11
//#define SENSOR_DHT21 // DHT 21 (AM2301)
#define SENSOR_DHT22 // DHT 22 (AM2302)
Then Upload the code to the Feather again.
- Adafruit TSL2561 Digital Luminosity/Lux/Light Sensor Breakout
- or SparkFun Luminosity Sensor Breakout - TSL2561 [nicegear] [MindKits]
- or Generic breakout [MindKits]
- Install the Adafruit Unified Sensor Library library
- Install the Adafruit TSL2561 Light Sensor Driver Library
Remove the comments in front of the sensor you have, eg
#define SENSOR_TSL2561 // TLS2561
Then Upload the code to the Feather again.
- GY-302 Digital Light Intensity Sensor Module
- or Generic breakout [MindKits]
- Install the BH1750 library
Remove the comments in front of the sensor you have, eg
#define SENSOR_BH1750 // BH1750FVI
Then Upload the code to the Feather again.
- DS18B20 [Adafruit] [SparkFun] [nicegear] [MindKits]
- Waterproof DS18B20 Digital temperature sensor [Adafruit] [SparkFun] [nicegear] [MindKits]
- Adafruit High Temp Waterproof DS18B20 Digital temperature sensor [nicegear]
- and 4.7k Resistor
Or a breakout board that has the resistor included
- Install the OneWire library
- Install the Arduino Temperature Control library
Remove the comments in front of the sensor you have, eg
#define SENSOR_DS18B20 // DS18B20
Then Upload the code to the Feather again.