end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Revision:
29:ad409c68c0a6
Parent:
28:36be305a5e5e
Child:
31:6ed03f61af64
--- a/app/single_us915_main.cpp	Mon Aug 28 13:13:56 2017 -0700
+++ b/app/single_us915_main.cpp	Fri Dec 07 17:57:41 2018 -0800
@@ -9,12 +9,9 @@
 Description: LoRaMac classA device implementation
 
 License: Revised BSD License, see LICENSE.TXT file include in the project
-
-Maintainer: Miguel Luis and Gregory Cristian
 */
 #include "mbed.h"
 #include "board.h"
-#include "radio.h"
 
 #include "LoRaMac.h"
 #include "Commissioning.h"
@@ -85,7 +82,7 @@
     LowPowerTicker sensor_ticker;
 #endif /* SENSORS */
 
-DigitalOut pc2(PC_2);
+//DigitalOut pc2(PC_2);
 
 #if defined(TARGET_DISCO_L072CZ_LRWAN1) || ( defined(TARGET_NUCLEO_L073RZ) && defined(TYPE_ABZ) )
     DigitalOut jumper_out(PA_11);
@@ -266,7 +263,7 @@
             x = tsl2561.getLuminosity(TSL2561_VISIBLE);
 #endif
             if (sensor_overflow) {
-                isr_printf("sensor_overflow\r\n");
+                app_printf("sensor_overflow\r\n");
                 sensor_overflow = false;
             }
             AppDataSize = 0;
@@ -400,10 +397,11 @@
     if( status == LORAMAC_STATUS_OK )
     {
         awaiting_mcps_indic = true;
+        app_printf("send ok\r\n");
         return false;
     }
     LoRaMacStatus_to_string(status, str);
-    isr_printf("send failed:%s\r\n", str);
+    app_printf("send failed:%s\r\n", str);
 
     if (status == LORAMAC_STATUS_NO_NETWORK_JOINED) {
         join_retry = true;
@@ -429,7 +427,7 @@
     LoRaMacStatus_t status;
     
     if (DeviceState == DEVICE_STATE_REJOIN) {
-        isr_printf("unjoin\r\n");
+        app_printf("unjoin\r\n");
         mibReq.Type = MIB_NETWORK_JOINED;
         mibReq.Param.IsNetworkJoined = false;
 #ifdef SENSORS
@@ -444,21 +442,21 @@
     mibReq.Type = MIB_NETWORK_JOINED;
     status = LoRaMacMibGetRequestConfirm( &mibReq );
 
-    isr_printf("OnTxNextPacketTimerEvent() ");
+    app_printf("OnTxNextPacketTimerEvent() ");
     if( status == LORAMAC_STATUS_OK )
     {
         if( mibReq.Param.IsNetworkJoined == true )
         {
-            isr_printf("send");
+            app_printf("send");
             DeviceState = DEVICE_STATE_UPLINK;
         }
         else
         {
-            isr_printf("join");
+            app_printf("join");
             DeviceState = DEVICE_STATE_JOIN;
         }
     }
-    isr_printf("\r\n");
+    app_printf("\r\n");
 }
 
 void
@@ -479,23 +477,23 @@
     static uint8_t fail_count = 0;
     char str[64];
     
-    isr_printf("McpsConfirm ");
+    app_printf("McpsConfirm ");
     if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
     {
         fail_count = 0;
-        isr_printf("OK ");
+        app_printf("OK ");
         switch( mcpsConfirm->McpsRequest )
         {
             case MCPS_UNCONFIRMED:
             {
-                isr_printf("UNCONFIRMED");
+                app_printf("UNCONFIRMED");
                 // Check Datarate
                 // Check TxPower
                 break;
             }
             case MCPS_CONFIRMED:
             {
-                isr_printf("CONFIRMED");
+                app_printf("CONFIRMED");
                 // Check Datarate
                 // Check TxPower
                 // Check AckReceived
@@ -515,7 +513,7 @@
 
     } else {
         LoRaMacEventInfoStatus_to_string(mcpsConfirm->Status, str);
-        isr_printf("%s ", str);
+        app_printf("%s ", str);
 
         /* mcpsIndication may not come. last uplink done, send another uplink */
         if (jumper_in.read()) {   /* jumper installed: auto uplink */
@@ -535,7 +533,7 @@
         }
     }
 
-    isr_printf("\r\n");
+    app_printf("\r\n");
 }
 
 /*!
@@ -549,7 +547,7 @@
     char str[64];
     MibRequestConfirm_t mibReq;
 
-    isr_printf("McpsIndication ");
+    app_printf("McpsIndication ");
 
     /* last uplink done, send another uplink */
     if (jumper_in.read() && mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_STOP) {
@@ -560,7 +558,7 @@
     if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
     {
         LoRaMacEventInfoStatus_to_string(mcpsIndication->Status, str);
-        isr_printf("%s\r\n", str);
+        app_printf("%s\r\n", str);
         return;
     }
 
@@ -571,13 +569,13 @@
         /* this refers to the downlink from gateway, not the uplink that was sent to it */
         case MCPS_UNCONFIRMED:
         {
-            isr_printf("UNCONFIRMED ");
+            app_printf("UNCONFIRMED ");
             break;
         }
         case MCPS_CONFIRMED:
         {
             /* downlink has requested Ack */
-            isr_printf("CONFIRMED ");
+            app_printf("CONFIRMED ");
             break;
         }
         case MCPS_PROPRIETARY:
@@ -586,7 +584,7 @@
         }
         case MCPS_MULTICAST:
         {
-            isr_printf("MCPS_MULTICAST ");
+            app_printf("MCPS_MULTICAST ");
             /*if (mcpsIndication->RxData) {
             }*/
             break;
@@ -625,11 +623,11 @@
     if( mcpsIndication->RxData == true )
     {
         int i;
-        isr_printf("RxData %u ", mcpsIndication->BufferSize);
+        app_printf("RxData %u ", mcpsIndication->BufferSize);
         for (i = 0; i < mcpsIndication->BufferSize; i++) {
-            isr_printf("%02x ", mcpsIndication->Buffer[i]);
+            app_printf("%02x ", mcpsIndication->Buffer[i]);
         }
-        isr_printf("\r\n");
+        app_printf("\r\n");
         
         switch (mcpsIndication->Buffer[0]) {
             default:
@@ -641,7 +639,7 @@
                     mcpsIndication->Buffer[2],  // G
                     mcpsIndication->Buffer[3]   // B
                 );
-                isr_printf("rgb %u %u %u\r\n",
+                app_printf("rgb %u %u %u\r\n",
                     mcpsIndication->Buffer[1],
                     mcpsIndication->Buffer[2],
                     mcpsIndication->Buffer[3]
@@ -651,16 +649,16 @@
 #if defined(SENSORS)
             case CMD_GPIO_OUT:
                 pc6_out = mcpsIndication->Buffer[1];
-                isr_printf("gpo %d\r\n", mcpsIndication->Buffer[1]);
+                app_printf("gpo %d\r\n", mcpsIndication->Buffer[1]);
                 break;
             case CMD_PWM:
                 pwm.period(1.0 / mcpsIndication->Buffer[1]);
                 pwm.write(mcpsIndication->Buffer[2] / 255.0);
-                isr_printf("pwm %u %u\r\n", mcpsIndication->Buffer[1], mcpsIndication->Buffer[2]);
+                app_printf("pwm %u %u\r\n", mcpsIndication->Buffer[1], mcpsIndication->Buffer[2]);
                 break;
 #endif /* SENSORS */
             case CMD_TX_POWER:
-                isr_printf("txp %u\r\n", mcpsIndication->Buffer[1]);
+                app_printf("txp %u\r\n", mcpsIndication->Buffer[1]);
                 mibReq.Type = MIB_CHANNELS_TX_POWER;
                 mibReq.Param.ChannelsTxPower = mcpsIndication->Buffer[1];
                 LoRaMacMibSetRequestConfirm( &mibReq );        
@@ -683,12 +681,12 @@
     }
     
     if (mcpsIndication->Status == LORAMAC_EVENT_INFO_STATUS_STOP) {
-        isr_printf(" STOP-SCC");
+        app_printf(" STOP-SCC");
         TxNextPacketTimeout.detach();
         DeviceState = DEVICE_STATE_SLEEP;
     }
     
-    isr_printf("\r\n");
+    app_printf("\r\n");
 }
 
 /*!
@@ -701,13 +699,13 @@
 {
     char str[64];
     LoRaMacEventInfoStatus_to_string(mlmeConfirm->Status, str);
-    isr_printf("MlmeConfirm %s ", str);
+    app_printf("MlmeConfirm %s ", str);
 
     switch( mlmeConfirm->MlmeRequest )
     {
         case MLME_JOIN:
         {
-            isr_printf("MLME_JOIN ");
+            app_printf("MLME_JOIN ");
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Status is OK, node has joined the network
@@ -731,7 +729,7 @@
         }
         case MLME_LINK_CHECK:
         {
-            isr_printf("LINK_CHECK");
+            app_printf("LINK_CHECK");
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Check DemodMargin
@@ -745,7 +743,7 @@
             break;
     }
 
-    isr_printf("\r\n");
+    app_printf("\r\n");
 }
 
 
@@ -753,14 +751,17 @@
 {
     char str[64];
 
-    isr_printf("MlmeIndication ");
+    app_printf("MlmeIndication ");
     switch( MlmeIndication->MlmeIndication )
     {
         case MLME_BEACON:
         {
-            isr_printf("missed:%u BEACON ", missed_count);
+            app_printf("BEACON ", missed_count);
+            if (missed_count)
+                app_printf("missed:%u ", missed_count);
+
             LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str);
-            isr_printf("%s ", str);
+            app_printf("%s ", str);
 
             if (send_at_beacon) {
                 TxNextPacketTimeout.attach_us(&send_uplink, 100000);
@@ -790,7 +791,7 @@
                         DeviceState = DEVICE_STATE_REJOIN;
     
                     LoRaMacStatus_to_string(status, str);
-                    isr_printf("app-rejoin %s\r\n", str);
+                    app_printf("app-rejoin %s\r\n", str);
                     TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);
                 }
             }
@@ -798,14 +799,14 @@
 
         }
         case MLME_TXDONE:
-            isr_printf("MLME_TXDONE ");
+            app_printf("MLME_TXDONE ");
             break;
         default:
-            isr_printf("<%d> ", MlmeIndication->MlmeIndication);
+            app_printf("<%d> ", MlmeIndication->MlmeIndication);
             break;
     }
 
-    isr_printf("\r\n");
+    app_printf("\r\n");
 }
 
 void
@@ -816,7 +817,7 @@
     AppDataSize = pcbuf_len;
 
     ret = SendFrame(TEXT_PORT);
-    isr_printf("%d = SendFrame()\r\n", ret);
+    app_printf("%d = SendFrame()\r\n", ret);
 }
 
 
@@ -824,22 +825,22 @@
 {
     MibRequestConfirm_t mibReq;
     
-    isr_printf("DevEUI ");
+    app_printf("DevEUI ");
     for (int i = 0; i < 8; i++)
-        isr_printf("%02x ", DevEui[i]);
+        app_printf("%02x ", DevEui[i]);
     mibReq.Type = MIB_DEV_ADDR;
     LoRaMacMibGetRequestConfirm( &mibReq );         
-    isr_printf(", DevAddr:%x\r\n", mibReq.Param.DevAddr);
+    app_printf(", DevAddr:%x\r\n", mibReq.Param.DevAddr);
     
 #if defined(SENSORS)
     #ifdef TYPE_ABZ
-        isr_printf("a0:%u a2:%u\r\n", a0.read_u16(), a2.read_u16());
+        app_printf("a0:%u a2:%u\r\n", a0.read_u16(), a2.read_u16());
     #else
-        isr_printf("a1:%u a3:%u\r\n", a1.read_u16(), a3.read_u16());
+        app_printf("a1:%u a3:%u\r\n", a1.read_u16(), a3.read_u16());
     #endif
-    isr_printf("button:%d ", user_button.read());
+    app_printf("button:%d ", user_button.read());
 #endif
-    isr_printf("DEVICE_STATE_");
+    app_printf("DEVICE_STATE_");
     switch (DeviceState) {
         case DEVICE_STATE_JOIN: printf("JOIN"); break;
         case DEVICE_STATE_CYCLE: printf("CYCLE"); break;
@@ -848,8 +849,8 @@
         case DEVICE_STATE_INIT: printf("INIT"); break;
         case DEVICE_STATE_UPLINK: printf("UPLINK"); break;
     }
-    isr_printf(" send_at_beacon:%d\r\n", send_at_beacon);
-    isr_printf("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
+    app_printf(" send_at_beacon:%d\r\n", send_at_beacon);
+    app_printf("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
     
     loramac_print_status();
 
@@ -860,7 +861,7 @@
     if (pcbuf[idx] >= '0' && pcbuf[idx] <= '9') {
         sscanf(pcbuf+idx, "%u", &uplink_length);
     }
-    isr_printf("uplink_length:%u\r\n", uplink_length);
+    app_printf("uplink_length:%u\r\n", uplink_length);
 }
 
 #if defined(ENABLE_RGB)
@@ -869,7 +870,7 @@
     int r, g, b;
     sscanf(pcbuf+idx, "%d %d %d", &r, &g, &b);
     rgb.setColorRGB(0, r, g, b);
-    isr_printf("\r\nrgb: %d %d %d\r\n", r, g, b);
+    app_printf("\r\nrgb: %d %d %d\r\n", r, g, b);
 }
 #endif /* ENABLE_RGB */
 
@@ -905,7 +906,7 @@
     
     mibReq.Type = MIB_CHANNELS_TX_POWER;
     LoRaMacMibGetRequestConfirm( &mibReq );
-    isr_printf("ChannelsTxPower:%u\r\n", mibReq.Param.ChannelsTxPower);
+    app_printf("ChannelsTxPower:%u\r\n", mibReq.Param.ChannelsTxPower);
 }
 
 typedef struct {
@@ -937,7 +938,7 @@
     int i;
     
     for (i = 0; menu_items[i].cmd != NULL ; i++) {
-        isr_printf("%s%s\t%s\r\n", menu_items[i].cmd, menu_items[i].arg_descr, menu_items[i].description);
+        app_printf("%s%s\t%s\r\n", menu_items[i].cmd, menu_items[i].arg_descr, menu_items[i].description);
     }
     
 }
@@ -955,7 +956,7 @@
     if (pcbuf_len == 0)
         return;
         
-    isr_printf("\r\n");
+    app_printf("\r\n");
         
     /* get end of user-entered command */
     user_cmd_len = 1;   // first character can be any character
@@ -983,7 +984,7 @@
         send_pcbuf_uplink();
    
     pcbuf_len = 0;
-    isr_printf("> ");
+    app_printf("> ");
     fflush(stdout); 
 }
 
@@ -1019,7 +1020,7 @@
 
 void button_isr()
 {
-    isr_printf("button_isr\r\n");
+    app_printf("button_isr\r\n");
 
     AppPort = SENSOR_PORT;
     send_uplink();
@@ -1034,9 +1035,9 @@
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
 
-    pc.baud(38400);
+    pc.baud(115200);
     pc.attach(&rx_callback);
-    isr_printf("\r\nreset %s\r\n", FW_VERS);
+    app_printf("\r\nreset %s\r\n", FW_VERS);
     
     BoardInit( );
 
@@ -1049,11 +1050,11 @@
     user_button.fall(&button_isr);
     
 #ifdef ENABLE_LUMINOSITY
-    isr_printf("TSL2561 Sensor ");
+    app_printf("TSL2561 Sensor ");
     if (tsl2561.begin()) {    
-        isr_printf("Found\r\n");        
+        app_printf("Found\r\n");        
     } else {    
-        isr_printf("not-found\r\n");   
+        app_printf("not-found\r\n");   
     }
     
     // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
@@ -1069,7 +1070,7 @@
 
     jumper_out = 1;
     jumper_in.mode(PullDown);
-    pc2 = 0;
+    //pc2 = 0;
 
     while( 1 )
     {
@@ -1079,7 +1080,7 @@
         {
             case DEVICE_STATE_INIT:
             {
-                isr_printf("DEVICE_STATE_INIT\r\n");
+                app_printf("DEVICE_STATE_INIT\r\n");
 #if defined(SENSORS)
                 uplink_length = 7;
 #else                
@@ -1091,10 +1092,10 @@
                 LoRaMacPrimitives.MacMlmeIndication = MlmeIndication;
                 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
                 if (LORAMAC_STATUS_OK != LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks )) {
-                    isr_printf("LoRaMacInitialization() failed\r\n");
+                    app_printf("LoRaMacInitialization() failed\r\n");
                     for (;;) ;
                 }
-                isr_printf("INIT-ok\r\n");
+                app_printf("INIT-ok\r\n");
 
                 mibReq.Type = MIB_PUBLIC_NETWORK;
                 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
@@ -1113,10 +1114,10 @@
                 MlmeReq_t mlmeReq;
                 // override software definition with hardware value
                 BoardGetUniqueId(DevEui);
-                isr_printf("DevEUI ");
+                app_printf("DevEUI ");
                 for (int i = 0; i < 8; i++)
-                    isr_printf("%02x ", DevEui[i]);
-                isr_printf("\r\n");
+                    app_printf("%02x ", DevEui[i]);
+                app_printf("\r\n");
 
                 mlmeReq.Type = MLME_JOIN;
 
@@ -1129,7 +1130,7 @@
                 if (status != LORAMAC_STATUS_OK) {
                     char str[48];
                     LoRaMacStatus_to_string(status, str);
-                    isr_printf("join-req-failed:%s\r\n", str);
+                    app_printf("join-req-failed:%s\r\n", str);
                 }
                 DeviceState = DEVICE_STATE_SLEEP;
 #else
@@ -1166,15 +1167,16 @@
             }
             case DEVICE_STATE_SLEEP:
             {
-                bottom_half();
+                //bottom_half();
                 LoRaMacBottomHalf();
                 // Wake up through events
-                pc2 = 1;
+                //pc2 = 1;
                 sleep();
-                pc2 = 0;
+                //pc2 = 0;
                 break;
             }
             case DEVICE_STATE_UPLINK:
+                app_printf("DEVICE_STATE_UPLINK\r\n");
                 PrepareTxFrame(AppPort);
                 SendFrame(AppPort);
                 DeviceState = DEVICE_STATE_SLEEP;
@@ -1186,11 +1188,11 @@
             }
         }
 
-        bottom_half();
+        //bottom_half();
         LoRaMacBottomHalf();
 
         if (join_retry) {
-            isr_printf("join_retry\r\n");
+            app_printf("join_retry\r\n");
             DeviceState = DEVICE_STATE_JOIN;
             join_retry = false;
         }