Forest fire prediction using sensors and LoRa communications

Dependencies:   X_NUCLEO_IKS01A2

Files at this revision

API Documentation at this revision

Comitter:
spadala
Date:
Mon Jun 03 10:26:44 2019 +0000
Parent:
50:6e615eea1e6f
Commit message:
final

Changed in this revision

OLD_mbed_app.json Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_IKS01A2.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r 6e615eea1e6f -r 925c07d0d7cf OLD_mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OLD_mbed_app.json	Mon Jun 03 10:26:44 2019 +0000
@@ -0,0 +1,241 @@
+{
+    "config": {
+        "lora-radio": {
+            "help": "Which radio to use (options: SX1272,SX1276)",
+            "value": "SX1276"
+        },
+        "main_stack_size":     { "value": 4096 },
+
+        "lora-spi-mosi":       { "value": "NC" },
+        "lora-spi-miso":       { "value": "NC" },
+        "lora-spi-sclk":       { "value": "NC" },
+        "lora-cs":             { "value": "NC" },
+        "lora-reset":          { "value": "NC" },
+        "lora-dio0":           { "value": "NC" },
+        "lora-dio1":           { "value": "NC" },
+        "lora-dio2":           { "value": "NC" },
+        "lora-dio3":           { "value": "NC" },
+        "lora-dio4":           { "value": "NC" },
+        "lora-dio5":           { "value": "NC" },
+        "lora-rf-switch-ctl1": { "value": "NC" },
+        "lora-rf-switch-ctl2": { "value": "NC" },
+        "lora-txctl":          { "value": "NC" },
+        "lora-rxctl":          { "value": "NC" },
+        "lora-ant-switch":     { "value": "NC" },
+        "lora-pwr-amp-ctl":    { "value": "NC" },
+        "lora-tcxo":           { "value": "NC" }
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": false,
+            "platform.stdio-baud-rate": 115200,
+            "platform.default-serial-baud-rate": 115200,
+            "lora.over-the-air-activation": false,
+            "lora.duty-cycle-on": false,
+            "lora.phy": "EU868",
+            "lora.appskey": "{ 0xCD, 0xA9, 0x0D, 0x58, 0x4A, 0x45, 0x47, 0xE7, 0x77, 0x06, 0x63, 0x7E, 0x7B, 0x0D, 0x88, 0xC1 }",
+            "lora.nwkskey": "{ 0x30, 0xFD, 0x2C, 0xD2, 0x46, 0x06, 0xE5, 0xAE, 0x21, 0x8E, 0xCF, 0x29, 0xCE, 0xFB, 0xC1, 0xA1 }",
+            "lora.device-address": "0x26011BF1" 
+        },
+
+        "K64F": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "A4",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
+
+        "DISCO_L072CZ_LRWAN1": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "PA_7",
+            "lora-spi-miso":       "PA_6",
+            "lora-spi-sclk":       "PB_3",
+            "lora-cs":             "PA_15",
+            "lora-reset":          "PC_0",
+            "lora-dio0":           "PB_4",
+            "lora-dio1":           "PB_1",
+            "lora-dio2":           "PB_0",
+            "lora-dio3":           "PC_13",
+            "lora-dio4":           "NC",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "PC_2",
+            "lora-rxctl":          "PA_1",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "PC_1",
+            "lora-tcxo":           "PA_12"
+        },
+
+        "MTB_MURATA_ABZ": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "PA_7",
+            "lora-spi-miso":       "PA_6",
+            "lora-spi-sclk":       "PB_3",
+            "lora-cs":             "PA_15",
+            "lora-reset":          "PC_0",
+            "lora-dio0":           "PB_4",
+            "lora-dio1":           "PB_1",
+            "lora-dio2":           "PB_0",
+            "lora-dio3":           "PC_13",
+            "lora-dio4":           "NC",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "PC_2",
+            "lora-rxctl":          "PA_1",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "PC_1",
+            "lora-tcxo":           "PA_12"
+        },
+
+        "XDOT_L151CC": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_MTS_XDOT": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "FF1705_L151CC": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTS_MDOT_F411RE": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "LORA_DIO5",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "LORA_TXCTL",
+            "lora-rxctl":           "LORA_RXCTL",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_ADV_WISE_1510": {
+            "lora-radio":           "SX1276",
+            "lora-spi-mosi":        "SPI_RF_MOSI",
+            "lora-spi-miso":        "SPI_RF_MISO",
+            "lora-spi-sclk":        "SPI_RF_SCK",
+            "lora-cs":              "SPI_RF_CS",
+            "lora-reset":           "SPI_RF_RESET",
+            "lora-dio0":            "DIO0",
+            "lora-dio1":            "DIO1",
+            "lora-dio2":            "DIO2",
+            "lora-dio3":            "DIO3",
+            "lora-dio4":            "DIO4",
+            "lora-dio5":            "DIO5",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "ANT_SWITCH",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_RAK811": {
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "SPI_RF_MOSI",
+            "lora-spi-miso":       "SPI_RF_MISO",
+            "lora-spi-sclk":       "SPI_RF_SCK",
+            "lora-cs":             "SPI_RF_CS",
+            "lora-reset":          "SPI_RF_RESET",
+            "lora-dio0":           "DIO0",
+            "lora-dio1":           "DIO1",
+            "lora-dio2":           "DIO2",
+            "lora-dio3":           "DIO3",
+            "lora-dio4":           "DIO4",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "ANT_CTX_PA",
+            "lora-rxctl":          "ANT_CRX_RX",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "RF_TCXO_EN"
+        }
+    },
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]
+}
diff -r 6e615eea1e6f -r 925c07d0d7cf X_NUCLEO_IKS01A2.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_IKS01A2.lib	Mon Jun 03 10:26:44 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/salvatoregulfo/code/X_NUCLEO_IKS01A2/#98c301bb7d1b
diff -r 6e615eea1e6f -r 925c07d0d7cf main.cpp
--- a/main.cpp	Wed Mar 13 17:30:20 2019 +0000
+++ b/main.cpp	Mon Jun 03 10:26:44 2019 +0000
@@ -20,22 +20,42 @@
 #include "lorawan/system/lorawan_data_structures.h"
 #include "events/EventQueue.h"
 
+// Sensor headers
+#include "mbed.h"
+#include "XNucleoIKS01A2.h"
+
 // Application helpers
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
 using namespace events;
 
+//#define WITH_SENSORS
+
+#ifdef WITH_SENSORS
+// Instantiate the expansion board
+static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+
+// Retrieve the composing elements of the expansion board
+static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
+static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
+#endif
+
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 // This example only communicates with much shorter messages (<30 bytes).
 // If longer messages are used, these buffers must be changed accordingly.
-uint8_t tx_buffer[30];
-uint8_t rx_buffer[30];
+uint8_t tx_buffer[100];
+uint8_t rx_buffer[100];
+
+// Buffers needed to read values from the sensors
+float value_temp, value_hum;
+#define SENSOR_BUFFER_SIZE 32
+char buffer_temp[SENSOR_BUFFER_SIZE], buffer_hum[SENSOR_BUFFER_SIZE];
 
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
  */
-#define TX_TIMER                        10000
+#define TX_TIMER                        11000
 
 /**
  * Maximum number of events for the event queue.
@@ -77,11 +97,27 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
+
+static char *print_double(char* str, double v);
+
 /**
  * Entry point for application
  */
 int main(void)
 {
+    printf("\r\n Hello, starting...");
+    
+    #ifdef WITH_SENSORS
+    // setup sensor stack
+    hum_temp->enable();
+    press_temp->enable();
+    
+    // testing sensors
+    hum_temp->get_temperature(&value_temp);
+    hum_temp->get_humidity(&value_hum);
+    printf("\r\n Sensors test:\r\n HTS221: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer_temp, value_temp), print_double(buffer_hum, value_hum));
+    #endif
+    
     // setup tracing
     setup_trace();
 
@@ -135,26 +171,31 @@
     return 0;
 }
 
-/**
+#ifndef WITH_SENSORS
+
+/*
  * Sends a message to the Network Server
  */
 static void send_message()
 {
     uint16_t packet_len;
     int16_t retcode;
-    printf("\n\rHello world");    
+    printf("\n\rSending message");    
 
-    packet_len = sprintf((char *) tx_buffer, "Hello world");
+    packet_len = sprintf((char *) tx_buffer, "This is a test. LoRa Works! <3");
 
     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
                            MSG_UNCONFIRMED_FLAG);
 
     if (retcode < 0) {
+        printf("retcode < 0 in send\r\n");
+        
         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
         : printf("\r\n send() - Error code %d \r\n", retcode);
 
         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
             //retry in 3 seconds
+            printf("LORAWAN_STATUS_WOULD_BLOCK verified, retrying");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 ev_queue.call_in(3000, send_message);
             }
@@ -166,6 +207,97 @@
     memset(tx_buffer, 0, sizeof(tx_buffer));
 }
 
+#else
+
+
+static void send_message()
+{
+    printf("\n\rSending message");
+    uint16_t packet_len;
+    int16_t retcode;
+    
+    // reading from sensors
+    memset(buffer_temp, 0, sizeof(buffer_temp));
+    memset(buffer_hum, 0, sizeof(buffer_hum));
+    hum_temp->get_temperature(&value_temp);
+    hum_temp->get_humidity(&value_hum);
+    printf("\r\nSensors reading: [temp] %7s C, [hum] %s%%\r\n", 
+        print_double(buffer_temp, value_temp), print_double(buffer_hum, value_hum));
+
+    // format data
+    uint16_t temp_len = sprintf((char *) tx_buffer, buffer_temp);
+    tx_buffer[temp_len] = ':';
+    uint16_t hum_len = sprintf((char *) &tx_buffer[temp_len+1], buffer_hum);
+    
+    // set final payload and send
+    packet_len = temp_len + hum_len + 1;    
+    
+    printf("\r\n Sending to server %s", tx_buffer);
+    
+    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
+                           MSG_UNCONFIRMED_FLAG);
+                           
+    
+
+    if (retcode < 0) {
+        printf("retcode < 0 in send\r\n");
+        
+        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
+        : printf("\r\n send() - Error code %d \r\n", retcode);
+
+        if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
+            //retry in 3 seconds
+            printf("LORAWAN_STATUS_WOULD_BLOCK verified, retrying");
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                ev_queue.call_in(3000, send_message);
+            }
+        }
+        return;
+    }
+
+    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+    memset(tx_buffer, 0, sizeof(tx_buffer));
+}
+
+#endif
+
+/* Helper function for printing floats & doubles */
+static char *print_double(char* str, double v)
+{
+  int decimalDigits=2;
+  int i = 1;
+  int intPart, fractPart;
+  int len;
+  char *ptr;
+
+  /* prepare decimal digits multiplicator */
+  for (;decimalDigits!=0; i*=10, decimalDigits--);
+
+  /* calculate integer & fractinal parts */
+  intPart = (int)v;
+  fractPart = (int)((v-(double)(int)v)*i);
+
+  /* fill in integer part */
+  sprintf(str, "%i.", intPart);
+
+  /* prepare fill in of fractional part */
+  len = strlen(str);
+  ptr = &str[len];
+
+  /* fill in leading fractional zeros */
+  for (i/=10;i>1; i/=10, ptr++) {
+    if (fractPart >= i) {
+      break;
+    }
+    *ptr = '0';
+  }
+
+  /* fill in (rest of) fractional part */
+  sprintf(ptr, "%i", fractPart);
+
+  return str;
+}
+
 /**
  * Event handler
  */
diff -r 6e615eea1e6f -r 925c07d0d7cf mbed-os.lib
--- a/mbed-os.lib	Wed Mar 13 17:30:20 2019 +0000
+++ b/mbed-os.lib	Mon Jun 03 10:26:44 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#51d55508e8400b60af467005646c4e2164738d48
+https://github.com/ARMmbed/mbed-os/#73f096399b4cda1f780b140c87afad9446047432
diff -r 6e615eea1e6f -r 925c07d0d7cf mbed_app.json
--- a/mbed_app.json	Wed Mar 13 17:30:20 2019 +0000
+++ b/mbed_app.json	Mon Jun 03 10:26:44 2019 +0000
@@ -28,14 +28,17 @@
     "target_overrides": {
         "*": {
             "platform.stdio-convert-newlines": false,
-            "platform.stdio-baud-rate": 115200,
-            "platform.default-serial-baud-rate": 115200,
-            "lora.over-the-air-activation": false,
+            "platform.stdio-baud-rate": 9600,
+            "platform.default-serial-baud-rate": 9600,
+            "lora.over-the-air-activation": true,
             "lora.duty-cycle-on": false,
             "lora.phy": "EU868",
-            "lora.appskey": "{ 0xCD, 0xA9, 0x0D, 0x58, 0x4A, 0x45, 0x47, 0xE7, 0x77, 0x06, 0x63, 0x7E, 0x7B, 0x0D, 0x88, 0xC1 }",
-            "lora.nwkskey": "{ 0x30, 0xFD, 0x2C, 0xD2, 0x46, 0x06, 0xE5, 0xAE, 0x21, 0x8E, 0xCF, 0x29, 0xCE, 0xFB, 0xC1, 0xA1 }",
-            "lora.device-address": "0x26011BF1" 
+            "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xC2, 0x3D }",
+            "lora.device-eui": "{ 0x00, 0x96, 0x41, 0xD2, 0x40, 0xD7, 0x23, 0x22 }",
+            "lora.application-key": "{ 0xE6, 0x8B, 0xA4, 0xD7, 0x53, 0x53, 0x95, 0x0A, 0xBB, 0x38, 0xB6, 0x5C, 0xD8, 0x28, 0xB7, 0x6E }",
+            "lora.nwkskey": "{ 0x23, 0xC4, 0xDD, 0x61, 0xE0, 0x91, 0x01, 0x7D, 0x11, 0x4B, 0xEF, 0x27, 0x9C, 0xFE, 0xD9, 0x78 }",
+            "lora.appskey": "{ 0x93, 0xB6, 0x6C, 0xE5, 0x59, 0x4F, 0x21, 0x0F, 0xA4, 0x5E, 0xA6, 0x48, 0x58, 0xBE, 0x0C, 0x37 }",
+            "lora.device-address": "0x26011082" 
         },
 
         "K64F": {