poc lorawan using disco_l475vg and sx1272mb2xas

Files at this revision

API Documentation at this revision

Comitter:
fabio_gatti
Date:
Thu Mar 14 22:05:11 2019 +0000
Parent:
48:dfce67eca082
Commit message:
initial relase.; LORAWAN poc using disco_l475vg and sx1272mb2xas

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-lora-radio-drv.lib 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 dfce67eca082 -r bf339fabb590 main.cpp
--- a/main.cpp	Wed Feb 13 18:37:07 2019 +0000
+++ b/main.cpp	Thu Mar 14 22:05:11 2019 +0000
@@ -45,10 +45,21 @@
  */
 #define MAX_NUMBER_OF_EVENTS            10
 
-/**
- * Maximum number of retries for CONFIRMED messages before giving up
- */
-#define CONFIRMED_MSG_RETRY_COUNTER     3
+
+// Maximum number of retries for CONFIRMED messages before giving up
+#define LORAWAN_CONFIRMED_MSG_RETRY_COUNTER  3 
+//DR_5=SF_7; DR_4=SF_8; DR_3=SF_9; DR_2=SF_10; DR_1=SF_11; DR_0=SF_12
+#define LORAWAN_DATA_RATE                    DR_5 
+// app port
+#define LORAWAN_APP_PORT                     15
+// tx message type 
+#define LORAWAN_TX_MSG_TYPE                  MSG_UNCONFIRMED_FLAG
+// number of channel 
+#define LORAWAN_CHANNEL_NBR                  3
+// timeout retry when channel is block in msec 
+#define LORAWAN_CHANNEL_RETRY                3000
+
+
 
 /**
  * Dummy pin for dummy sensor
@@ -87,57 +98,103 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
+
+static void LoRa_PrintChannels() {
+    /* print list of all channel frequencies */
+    lorawan_channelplan_t channelPlan {};
+    static loramac_channel_t channelbuf[10];
+    
+    channelPlan.channels = channelbuf;
+    if (lorawan.get_channel_plan(channelPlan) == LORAWAN_STATUS_OK) {
+        for (uint8_t i = 0; i < channelPlan.nb_channels; i++) {
+            loramac_channel_t chan = channelPlan.channels[i];
+            printf(" CHAN %d ID %d FREQ %lu RX1FREQ %lu Band %d DR min %d max %d\n",
+                   (int) i, (int) chan.id, chan.ch_param.frequency,
+                   chan.ch_param.rx1_frequency, (int) chan.ch_param.band,
+                   (int) chan.ch_param.dr_range.fields.min,
+                   (int) chan.ch_param.dr_range.fields.max);
+        }
+    } else {
+        printf(" COULD NOT GET CHANNEL PLAN\n");
+    }
+}
+
 /**
  * Entry point for application
  */
 int main(void)
 {
+    static loramac_channel_t ttnChannels[] = {
+        {0, {868100000, 0, {(DR_5 << 4) | DR_0}, 1}},
+        {1, {868300000, 0, {(DR_5 << 4) | DR_0}, 1}},
+        {2, {868500000, 0, {(DR_5 << 4) | DR_0}, 1}},
+        {3, {867100000, 0, {(DR_5 << 4) | DR_0}, 0}},
+        {4, {867300000, 0, {(DR_5 << 4) | DR_0}, 0}},
+        {5, {867500000, 0, {(DR_5 << 4) | DR_0}, 0}},
+        {6, {867700000, 0, {(DR_5 << 4) | DR_0}, 0}},
+        {7, {867900000, 0, {(DR_5 << 4) | DR_0}, 0}}
+};
+    lorawan_channelplan_t channelPlan {};
     // setup tracing
     setup_trace();
 
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
-
-    // Initialize LoRaWAN stack
+    printf("---------------------------- \n");
+// LORAWAN: Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
-        printf("\r\n LoRa initialization failed! \r\n");
+        printf(" LoRa initialization failed! \n");
         return -1;
     }
+    printf(" Mbed LoRaWANStack initialized \n");
 
-    printf("\r\n Mbed LoRaWANStack initialized \r\n");
-
-    // prepare application callbacks
+// LORAWAN: prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
     lorawan.add_app_callbacks(&callbacks);
 
-    // Set number of retries in case of CONFIRMED messages
-    if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
+// LORAWAN: Set number of retries in case of CONFIRMED messages
+    if (lorawan.set_confirmed_msg_retries(LORAWAN_CONFIRMED_MSG_RETRY_COUNTER)
             != LORAWAN_STATUS_OK) {
-        printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
+        printf(" Set_confirmed_msg_retries failed! \n");
         return -1;
     }
-
-    printf("\r\n CONFIRMED message retries : %d \r\n",
-           CONFIRMED_MSG_RETRY_COUNTER);
-
+    printf(" CONFIRMED message retries : %d \n",
+           LORAWAN_CONFIRMED_MSG_RETRY_COUNTER);
+ 
+ // LORAWAN: settaggio canali   
+    channelPlan.channels = (loramac_channel_t*) ttnChannels;
+    channelPlan.nb_channels = LORAWAN_CHANNEL_NBR;
+    if (lorawan.set_channel_plan(channelPlan) == LORAWAN_STATUS_OK) {
+        printf(" [+] Setting TTN channels\n");
+    } else {
+        printf(" [-] Failed to set TTN channels! Debug return code.\n");
+    }
+    LoRa_PrintChannels();
+      
+// LORAWAN:  data rate
+//   if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
+//      printf("\r\n enable_adaptive_datarate failed! \r\n");
+//     return -1;
+// }
+    
     // Enable adaptive data rate
-    if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
-        printf("\r\n enable_adaptive_datarate failed! \r\n");
+    if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
+        printf(" disable_adaptive_datarate failed! \r\n");
         return -1;
     }
-
-    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
+    printf(" Adaptive data  rate (ADR) - disabled \r\n");
+    lorawan.set_datarate(LORAWAN_DATA_RATE);
 
     retcode = lorawan.connect();
 
     if (retcode == LORAWAN_STATUS_OK ||
             retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
-        printf("\r\n Connection error, code = %d \r\n", retcode);
+        printf(" Connection error, code = %d \n", retcode);
         return -1;
     }
 
-    printf("\r\n Connection - In Progress ...\r\n");
+    printf(" Connection - In Progress ...\n");
 
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
@@ -153,37 +210,39 @@
     uint16_t packet_len;
     int16_t retcode;
     float sensor_value;
+ 
 
     if (ds1820.begin()) {
         ds1820.startConversion();
         sensor_value = ds1820.read();
-        printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value);
+        printf("\n -------------------------\n");
+        printf(" Dummy Sensor Value = %3.1f \n", sensor_value);
         ds1820.startConversion();
     } else {
-        printf("\r\n No sensor found \r\n");
+        printf(" No sensor found \n");
         return;
     }
-
-    packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %3.1f",
+ 
+    packet_len = sprintf((char *) tx_buffer, " Dummy Sensor Value is %3.1f",
                          sensor_value);
 
-    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
-                           MSG_UNCONFIRMED_FLAG);
+    retcode = lorawan.send(LORAWAN_APP_PORT, tx_buffer, packet_len,
+                           LORAWAN_TX_MSG_TYPE);
 
     if (retcode < 0) {
-        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
-        : printf("\r\n send() - Error code %d \r\n", retcode);
+        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf(" send - WOULD BLOCK\r\n")
+        : printf(" send() - Error code %d \n", retcode);
 
         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
             //retry in 3 seconds
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                ev_queue.call_in(3000, send_message);
+                ev_queue.call_in(LORAWAN_CHANNEL_RETRY, send_message);
             }
         }
         return;
     }
-
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+    
+    printf(" %d bytes scheduled for transmission \n", retcode);
     memset(tx_buffer, 0, sizeof(tx_buffer));
 }
 
@@ -197,7 +256,7 @@
     int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
 
     if (retcode < 0) {
-        printf("\r\n receive() - Error code %d \r\n", retcode);
+        printf(" receive() - Error code %d \r\n", retcode);
         return;
     }
 
@@ -205,7 +264,7 @@
     for (uint8_t i = 0; i < retcode; i++) {
         printf("%02x ", rx_buffer[i]);
     }
-    printf("\r\n");
+    printf("\n");
     
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
@@ -215,9 +274,13 @@
  */
 static void lora_event_handler(lorawan_event_t event)
 {
+    int16_t retcode;
+    lorawan_tx_metadata additional_data; 
+    int backoff_data;
+    
     switch (event) {
         case CONNECTED:
-            printf("\r\n Connection - Successful \r\n");
+            printf(" Connection - Successful \n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             } else {
@@ -227,10 +290,44 @@
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
-            printf("\r\n Disconnected Successfully \r\n");
+            printf(" Disconnected Successfully \n");
             break;
         case TX_DONE:
-            printf("\r\n Message Sent to Network Server \r\n");
+            printf(" Message Sent to Network Server \n");
+            
+            retcode = lorawan.get_tx_metadata(additional_data);
+            switch (retcode)
+            {
+                case LORAWAN_STATUS_NOT_INITIALIZED:
+                  printf(" Lorawan stack not initialized\n");
+                  break;
+              
+                case LORAWAN_STATUS_METADATA_NOT_AVAILABLE:
+                  printf(" Metadata not available\n");
+                  break;
+              
+                case LORAWAN_STATUS_OK :
+                   printf(" TX Channel: %d \n",additional_data.channel);
+                   printf(" TOA (msec): %d \n",additional_data.tx_toa);
+                   printf(" Data rate: %u \n",additional_data.data_rate);
+                   break;
+            }
+            
+            retcode = lorawan.get_backoff_metadata(backoff_data);
+            switch (retcode)
+            {
+                case LORAWAN_STATUS_NOT_INITIALIZED:
+                  printf(" Lorawan stack not initialized\n");
+                  break;
+              
+                case LORAWAN_STATUS_METADATA_NOT_AVAILABLE:
+                  printf(" Backoff not available\n");
+                  break;
+              
+                case LORAWAN_STATUS_OK :
+                   printf(" Backoff: %d \n",backoff_data);
+                   break;
+            }           
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
@@ -239,32 +336,33 @@
         case TX_ERROR:
         case TX_CRYPTO_ERROR:
         case TX_SCHEDULING_ERROR:
-            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
+            printf(" Transmission Error - EventCode = %d \r\n", event);
             // try again
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
             break;
         case RX_DONE:
-            printf("\r\n Received message from Network Server \r\n");
+            printf(" Received message from Network Server \r\n");
             receive_message();
             break;
         case RX_TIMEOUT:
         case RX_ERROR:
-            printf("\r\n Error in reception - Code = %d \r\n", event);
+            printf(" Error in reception - Code = %d \r\n", event);
             break;
         case JOIN_FAILURE:
-            printf("\r\n OTAA Failed - Check Keys \r\n");
+            printf(" OTAA Failed - Check Keys \r\n");
             break;
         case UPLINK_REQUIRED:
-            printf("\r\n Uplink required by NS \r\n");
+            printf(" Uplink required by NS \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
             break;
-        default:
-            MBED_ASSERT("Unknown Event");
+        default: 
+            MBED_ASSERT(" Unknown Event");
     }
 }
 
+
 // EOF
diff -r dfce67eca082 -r bf339fabb590 mbed-lora-radio-drv.lib
--- a/mbed-lora-radio-drv.lib	Wed Feb 13 18:37:07 2019 +0000
+++ b/mbed-lora-radio-drv.lib	Thu Mar 14 22:05:11 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers#16958f814d505cfbbedfa16d9bf8b9dff0e0442b
+https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#16958f814d505cfbbedfa16d9bf8b9dff0e0442b
diff -r dfce67eca082 -r bf339fabb590 mbed-os.lib
--- a/mbed-os.lib	Wed Feb 13 18:37:07 2019 +0000
+++ b/mbed-os.lib	Thu Mar 14 22:05:11 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#ecb3c8c837162c73537bd0f3592c6e2a42994045
+https://github.com/ARMmbed/mbed-os/#51d55508e8400b60af467005646c4e2164738d48
diff -r dfce67eca082 -r bf339fabb590 mbed_app.json
--- a/mbed_app.json	Wed Feb 13 18:37:07 2019 +0000
+++ b/mbed_app.json	Thu Mar 14 22:05:11 2019 +0000
@@ -2,7 +2,7 @@
     "config": {
         "lora-radio": {
             "help": "Which radio to use (options: SX1272,SX1276)",
-            "value": "SX1276"
+            "value": "SX1272"
         },
         "main_stack_size":     { "value": 4096 },
 
@@ -27,15 +27,19 @@
     },
     "target_overrides": {
         "*": {
-            "platform.stdio-convert-newlines": true,
+            "platform.stdio-convert-newlines": false,
             "platform.stdio-baud-rate": 115200,
             "platform.default-serial-baud-rate": 115200,
-            "lora.over-the-air-activation": true,
-            "lora.duty-cycle-on": true,
+            "lora.over-the-air-activation": false,
+            "lora.duty-cycle-on": false,
             "lora.phy": "EU868",
-            "lora.device-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
-            "lora.application-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
-            "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }"
+            "lora.device-eui": "{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }",
+            "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0x6E, 0x3D }",
+            "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.device-address": "0x260113FF",
+            "lora.nwkskey": "{ 0x47, 0x39, 0x2F, 0xC9, 0x71, 0xA3, 0x23, 0x81, 0xD6, 0x82, 0x9D, 0x91, 0xA4, 0xDD, 0x38, 0xEF }",
+            "lora.appskey": "{ 0xDF, 0x75, 0x91, 0x07, 0xC1, 0x2F, 0x49, 0xA3, 0x03, 0xD7, 0x52, 0x94, 0x8B, 0xAD, 0xE1, 0x67 }"
+           
         },
 
         "K64F": {
@@ -58,6 +62,28 @@
             "lora-pwr-amp-ctl":    "NC",
             "lora-tcxo":           "NC"
         },
+       "DISCO_L475VG_IOT01A": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1272",
+            "lora-spi-mosi":       "PA_7",
+            "lora-spi-miso":       "PA_6",
+            "lora-spi-sclk":       "PA_5",
+            "lora-cs":             "PA_2",
+            "lora-reset":          "PC_5",
+            "lora-dio0":           "PD_14",
+            "lora-dio1":           "PB_0",
+            "lora-dio2":           "PA_3",
+            "lora-dio3":           "PB_4",
+            "lora-dio4":           "NC",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "PC_1",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
 
         "DISCO_L072CZ_LRWAN1": {
             "main_stack_size":      1024,