Example Sketches

From Lorauino Wiki
Jump to: navigation, search

Reading an Adafruit Sensor via I2C and sending data via LoRaWAN in Class A

In this sketch we are reading data from a HMC5883L sensor, see https://www.adafruit.com/products/1746, and trasmitting it via LoRaWAN

#include <Wire.h>
 
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
 
#include <lorauino.h>
 
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
 
// every 60 seconds send data
long int time_period_to_tx = 60000;  
long int timer_millis = 0;
 
void setup() {
 
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  sensor_t sensor;
  mag.getSensor(&sensor);
 
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
 
  LoRaWan_init();
 
  LoRaWan_join ("0200000000eeffc0", "ff000012ff000012", "ab89efcd2301674554761032dcfe98ba");
 
  // Remember that if the library will remain in the Join procedure until the Lorauino manages to Join the network
  // if there are problems in Joining or if there is no network available the main loop will never start
 
  timer_millis = millis();
 
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  long int delta;
  long int magnetic;
  String hex_data;
 
  sensors_event_t event; 
  mag.getEvent(&event);
 
  delta = millis() - timer_millis;
 
  if ( delta > time_period_to_tx ) {
 
    timer_millis = millis();
 
    // read magnetic z sensor
    magnetic = event.magnetic.z * 100;
 
    hex_data =  String(magnetic, HEX);
 
    // pad the hex_data to 4 bytes
    while (hex_data.length()  < 8) hex_data = "0" + hex_data;  
 
    // transmit
    LoRaWan_send(hex_data, 1, true);
 
  }
 
 
}

For more information on the Adafruit Sensor library, which is very useful, see here https://github.com/adafruit/Adafruit_Sensor.


Receiving data in Class C

This is an example sketch on how to handle receiving data in Class C

#include <lorauino.h>
 
void setup() {
 
  Serial.begin(9600);
 
  // Uncomment this to suppress traces
  // LoRaWan_verbose = false;
 
  // Initialize communication with LoRaWan modem
  LoRaWan_init();
 
  // Uncomment to set the device to Class C. By default the modem is class A
  LoRaWan_class("C");
 
  // Start joining procedure to register on the network
  LoRaWan_join ("0200000000EEFFC0", "020000FF020000FF", "AB89EFCD2301674554761032DCFE98BA");
 
  // Alternative registration procedure: Activation by personalization
  //LoRaWan_personalization("FF000002","00112233445566778899AABBCCDDEEFF", "000102030405060708090A0B0C0D0E0F");
 
  // Remember that if the library will remain in the Join procedure until the Lorauino manages to Join the network
  // if there are problems in Joining or if there is no network available the main loop will never start
 
 
}
 
void loop () {
 
   byte rx_check;
   byte rx_len;
   short i;
 
  // Check 10 times if a message is received
  rx_check = 10;
 
 while (rx_check --) {
    rx_len = LoRaWan_get_rx();
 
    if (rx_len) {
      Serial.print("Data len:");
      Serial.println(rx_len);
      // Process data from buffer LoRaWan_last_rx[i]
      // Just print it in this example
      for (i=0; i<rx_len; i++){
        Serial.println(LoRaWan_last_rx[i], HEX);
      }
 
      // here you will have the LoRaWan_last_rx with the received data
      // the following is just an example of a possible handling of the data
 
      if (LoRaWan_last_rx[5] == 0x00) {
        Serial.println("OFF request"); 
      } else if(LoRaWan_last_rx[5] == 0x01) {
        Serial.println("ON request");
      } 
 
      } 
      delay(  1000);      
    }
 
    delay(  1000);
 
}


Receiving data in Class A

Here the example to read received data in Class A. In this mode it is useless to check RX periodically because the device can only receive after sending a frame.

#include <lorauino.h>
 
void setup() {
 
  Serial.begin(9600);
 
 
  // Uncomment this to suppress traces
  // LoRaWan_verbose = false;
 
  // Initialize communication with LoRaWan modem
  LoRaWan_init();
 
  // Uncomment to set the device to Class C. By default the modem is class A
  // LoRaWan_class("C");
 
  // Start joining procedure to register on the network
  LoRaWan_join ("0200000000EEFFC0", "020000FF020000FF", "AB89EFCD2301674554761032DCFE98BA");
 
  // Alternative registration procedure: Activation by personalization
  //LoRaWan_personalization("FF000002","00112233445566778899AABBCCDDEEFF", "000102030405060708090A0B0C0D0E0F");
 
 
  // Remember that if the library will remain in the Join procedure until the Lorauino manages to Join the network
  // if there are problems in Joining or if there is no network available the main loop will never start
 
}
 
 
// Class A example
void loop () {
   byte buf[] = {0,1,2,3};
   byte rx_len;
   short i;
 
  // Send data buffer
  LoRaWan_send(buf,  1, false);
 
  // Check if RX received
  rx_len = LoRaWan_get_rx();
  if (rx_len) {
      // Process data from buffer LoRaWan_last_rx[i]
      // Just print it in this example
      for (i=0; i<rx_len; i++){
        Serial.println(LoRaWan_last_rx[i], HEX);
      }
  }
  // Wait 20 seconds
  delay(20000);
}