Arduino LoRa simple temperature sensor

Code example

Many of the parameters used in this example are same as explained in Arduino LoRa demo sensor. We'll explain the additional few parameters being used here to build an operational IoT device with periodic sensing and low-power mode to be able to run on regular batteries for years. You can access the full working example on Arduino LoRa examples github page.

In the sketch named "Arduino_LoRa_simple_temp.ino" .

#include< SPI.h>
// Include the SX1272
#include "SX1272.h"
#include "my_temp_sensor_code.h"

The time taken in minutes between two readings and transmission

unsigned int idlePeriodInMin = 10;

Next we define the low power consumption, the most important setting of the simple temperature sensor

#define LOW_POWER

If low-power mode, then we include the low-power library

#ifdef LOW_POWER
 // you need the LowPower library from RocketScream
 #include "LowPower.h"

LoRa Mode and transmission parameters

unsigned int nCycle = idlePeriodInMin*60/LOW_POWER_PERIOD;
unsigned long nextTransmissionTime=0L;
int loraMode=LORAMODE;

In the setup() function

Initialization of the temperature sensor


If low power mode, power the temperature sensor

#ifdef LOW_POWER

Open serial communications and switch the LoRa radio module ON

  sx1272.ON();   // Power ON the module

Then, set transmission mode and enable carrier sense mechanism

  e = sx1272.setMode(loraMode);
  sx1272._enableCarrierSense= true;

Select the frequency channel and select amplifier line; PABOOST or RFO

  e = sx1272.setChannel(DEFAULT_CHANNEL);
#ifdef PABOOST

Set transmission power and the node address

  e = sx1272.setPowerDBM((uint8_t)MAX_DBM);
  e = sx1272.setNodeAddress(node_addr);

Then we move on to the second function of the Arduino sketch the loop() function

Powering up the sensor

#ifdef LOW_POWER

Get multiple values from the sensor, to calculate a mean value

 for (int i=0; i<5; i++) {
    temp += sensor_temp_getValue();  

Power down the sensor

#ifdef LOW_POWER

Message format

 uint8_t r_size;
 // the recommended format  \!TC/22.5
 char float_str[10];
 int pl=r_size+app_key_offset;

Send message to the gateway after performing carrier sense mechanism.


 // a simple data packet
 // Send message to the gateway           
 e = sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, pl); 

Switch to power saving mode

 #if defined LOW_POWER
  // Switch to power saving mode 
 e = sx1272.setSleepMode();
 nCycle = idlePeriodInMin*60/LOW_POWER_PERIOD; 
 for (int i=0; i < nCycle; i++) {  
 LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                                     

The raw source of the sketch example is visible here.

You can look at the Arduino_LoRa_Simple_DHT example on our github which replaces the simple analog temperature sensor with the digital DHT22 sensor.

You can have a look at the following video that further shows how to reduce the power consumption by removing the power led and the voltage regulator.

For advanced users

For the most courageous of you, you can go further and have a look at the Arduino_LoRa_Generic_DHT example on our github repository which shows a more "generic" approach to handle physical sensors: all physical sensors must be derived from a base Sensor class (defined in Sensor.cpp and Sensor.h) and should provide a get_value() and a get_nomenclature() function that will be called in the main program loop.

Then you can look at the Arduino_LoRa_Generic_Simple_MultiSensors example on our github repository which shows how to handle multiple physical in the same "generic" manner than previously. The example drives 5 physical sensors for 7 logical sensors as 2 physical sensors provide both temperature and humidity. The example also includes sensor classes for:

  • very simple LM35DZ analog temperature sensor
  • very simple TMP36 analog temperature sensor
  • digital DHT22 (AM2302) temperature and humidity sensor (work also with the AM2305)
  • digital SHT1x and SHT2x temperature and humidity sensor from Sensirion
  • digital DS18B20 temperature sensor
  • ultra-sonic HC-SR04 distance sensor
  • Davies Leaf Wetness sensor
  • general raw analog sensor

This example runs the sensor node in Congduc Pham's office in University of Pau, France which drives several temperature and humidity physical sensors (the test is also for comparison purpose). The associated gateway pushes data to ThingSpeak channel 66583 and WAZIUP platform as UPPA-TESTS-Sensor3. To visualize on WAZIUP platform, you may need to login as guest (password is also guest) first.

We are reaching the end of this online tutorial. If you made your way to these finals steps and everything is working fine then CONGRATULATIONS from the WAZIUP/WAZIHUB team!