LoRaWAN demo.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
21:f0aecd41db08
Parent:
20:49a8ecd1dda3
--- a/main.cpp	Wed Jan 27 14:45:50 2021 +0000
+++ b/main.cpp	Fri Feb 19 14:58:25 2021 +0000
@@ -3,7 +3,6 @@
 
 #include "DebouncedInterrupt.h"
 #include "modem_d7a.h"
-#include "modem_lwan.h"
 #include "d7a_callbacks.h"
 #include "lwan_callbacks.h"
 #include "files.h"
@@ -13,20 +12,12 @@
 #define ALARM_COOLDOWN_TIME 10000 // ms
 #define MIN_REPORT_PERIOD   (10) // Seconds
 
-enum {
-    MODEM_RESP_NO,
-    MODEM_RESP_TERMINAL,
-    MODEM_RESP_DONE,
-};
-
-Semaphore modem_ready[MAX_USER_NB];
 Semaphore modem_urc(0);
 Semaphore button_user(0);
 sensor_config_t g_light_config;
 Queue<uint8_t, 8> g_file_modified;
-Queue<void, 8> modem_resp;
 Queue<uint32_t, 8> g_urc;
-uint32_t itf_busy;
+int itf_busy;
 Timer busy_tim;
 
 bool alarm_ready = false;
@@ -122,22 +113,6 @@
     }
 }
 
-// Callback for id User
-void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
-{
-    (void)id;
-    
-    if (ALP_ERR_NONE != err)
-    {
-        modem_print_error(ALP_ITF_TYPE_D7A, err);
-    }
-    
-    if (terminal)
-    {
-        modem_ready[id].release();
-    }
-}
-
 static bool report_ok(uint32_t last_report_time)
 {
     // Do not send a report if it's been less than MIN_REPORT_PERIOD since the last report
@@ -219,7 +194,7 @@
 {
     light_value_t light_level;
     light_value_t light_level_old = 0;
-    alp_pub_payload_t* alp = NULL;
+    alp_payload_t* alp = NULL;
     revision_t rev;
     
     // To force a first report
@@ -235,6 +210,7 @@
     modem_declare_file(FID_ALARM, (alp_file_header_t*)&h_alarm);
     
     PRINT("D7A: Notify Revision\n");
+    modem_d7a_enable_itf();
     
     // Host revision file is in the modem. Update it.
     modem_write_file(FID_HOST_REV, &f_rev, 0, sizeof(revision_t));
@@ -248,7 +224,7 @@
     alp = alp_payload_rsp_f_data(alp, FID_WM_REV, &rev, 0, sizeof(revision_t));
     alp = alp_payload_rsp_f_data(alp, FID_HOST_REV, &f_rev, 0, sizeof(revision_t));
     // Send
-    modem_remote_raw_alp(&report_itf, NULL, alp);
+    modem_remote_raw_alp((void*)&report_itf, alp, NULL, 10000);
     
     // Get the sensor configuration
     ram_fs_read(FID_SENSOR_CONFIG, (uint8_t*)&g_light_config, 0, SIZE_SENSOR_CONFIG);
@@ -262,9 +238,12 @@
         if (report_needed(&g_light_config, light_level, light_level_old, last_report_time))
         {
             PRINT("D7A: Light report %d\r\n", light_level);
-        
-            // Send notification
-            modem_write_file(FID_SENSOR_LIGHT, &light_level, 0, SIZE_SENSOR_LIGHT);
+                    
+            // Build payload
+            alp = NULL;
+            alp = alp_payload_rsp_f_data(alp, FID_SENSOR_LIGHT, &light_level, 0, SIZE_SENSOR_LIGHT);
+            // Send
+            modem_remote_raw_alp((void*)&report_itf, alp, NULL, 1000);
         
             // Update 
             light_level_old = light_level;
@@ -281,7 +260,7 @@
 void lwan_thread()
 {
     alarm_t alarm;
-    alp_pub_payload_t* alp = NULL;
+    alp_payload_t* alp = NULL;
         
     DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
     user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true);
@@ -310,6 +289,27 @@
         {
             PRINT("LoRaWAN: Still busy for %ds.\r\n", itf_busy);
             busy_tim.reset();
+            
+            lwan_status_t lwan;
+            
+            modem_lwan_get_status(&lwan);
+            
+            PRINT(
+                "LoRaWAN: Joined            :%d\r\n"
+                "         NetID             :%d\r\n"
+                "         IsmBand           :%d\r\n"
+                "         PublicNetwork     :%d\r\n"
+                "         UpLinkCounter     :%d\r\n"
+                "         DownLinkCounter   :%d\r\n"
+                "         TxDr              :%d\r\n",
+                lwan.IsNetworkJoined,
+                lwan.NetID,
+                lwan.IsmBand,
+                lwan.PublicNetwork,
+                lwan.UpLinkCounter,
+                lwan.DownLinkCounter,
+                lwan.TxDr
+            );
         }
         else
         {