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:
17:3215f12051f9
Parent:
16:915815632c1f
Child:
18:9ac71c0eb70d
--- a/app/single_us915_main.cpp	Thu Jul 27 17:53:37 2017 +0000
+++ b/app/single_us915_main.cpp	Fri Jul 28 22:18:46 2017 +0000
@@ -134,7 +134,7 @@
 /*!
  * Timer to handle the application data transmission duty cycle
  */
-LowPowerTimeout TxNextPacketTimer;
+LowPowerTimeout TxNextPacketTimeout;
 
 volatile bool send_at_beacon;
 volatile bool awaiting_mcps_indic;
@@ -144,18 +144,12 @@
 static bool AppLedStateOn = false;
 
 /*!
- * Indicates if a new packet can be sent
- */
-static bool NextTx = true;
-
-/*!
  * Device states
  */
 static enum eDeviceState
 {
     DEVICE_STATE_INIT,
     DEVICE_STATE_JOIN,
-    DEVICE_STATE_SEND,
     DEVICE_STATE_CYCLE,
     DEVICE_STATE_SLEEP,
     DEVICE_STATE_REJOIN
@@ -295,51 +289,14 @@
         strcpy(dst, ptr);
 }
 
-/*!
- * \brief Function executed on TxNextPacket Timeout event
- */
-static void OnTxNextPacketTimerEvent( void )
-{
-    MibRequestConfirm_t mibReq;
-    LoRaMacStatus_t status;
-    
-    if (DeviceState == DEVICE_STATE_REJOIN) {
-        isr_printf("unjoin\r\n");
-        mibReq.Type = MIB_NETWORK_JOINED;
-        mibReq.Param.IsNetworkJoined = false;
-        if (LoRaMacMibSetRequestConfirm( &mibReq ) != LORAMAC_STATUS_OK) {
-            TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, 500000);
-            return;
-        }
-    }
-
-    mibReq.Type = MIB_NETWORK_JOINED;
-    status = LoRaMacMibGetRequestConfirm( &mibReq );
-
-    isr_printf("OnTxNextPacketTimerEvent() ");
-    if( status == LORAMAC_STATUS_OK )
-    {
-        NextTx = true;
-        if( mibReq.Param.IsNetworkJoined == true )
-        {
-            DeviceState = DEVICE_STATE_SEND;
-            isr_printf("send");
-        }
-        else
-        {
-            isr_printf("join");
-            DeviceState = DEVICE_STATE_JOIN;
-        }
-    }
-    isr_printf("\r\n");
-}
+static void OnTxNextPacketTimerEvent( void );
 
 /*!
  * \brief   Prepares the payload of the frame
  *
  * \retval  [0: frame could be send, 1: error]
  */
-static bool SendFrame( uint8_t port )
+static bool SendFrame(uint8_t port)
 {
     McpsReq_t mcpsReq;
     LoRaMacTxInfo_t txInfo;
@@ -393,18 +350,61 @@
     LoRaMacStatus_to_string(status, str);
     isr_printf("send failed:%s\r\n", str);
 
-    AppPort = port;
-    TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);        
+    if (status == LORAMAC_STATUS_NO_NETWORK_JOINED)
+        DeviceState = DEVICE_STATE_JOIN;
+    else {
+        AppPort = port;
+        TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);      
+    }
 
     return true;
 }
 
+/*!
+ * \brief Function executed on TxNextPacket Timeout event
+ */
+static void OnTxNextPacketTimerEvent( void )
+{
+    MibRequestConfirm_t mibReq;
+    LoRaMacStatus_t status;
+    
+    if (DeviceState == DEVICE_STATE_REJOIN) {
+        isr_printf("unjoin\r\n");
+        mibReq.Type = MIB_NETWORK_JOINED;
+        mibReq.Param.IsNetworkJoined = false;
+        if (LoRaMacMibSetRequestConfirm( &mibReq ) != LORAMAC_STATUS_OK) {
+            TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, 500000);
+            return;
+        }
+    }
+
+    mibReq.Type = MIB_NETWORK_JOINED;
+    status = LoRaMacMibGetRequestConfirm( &mibReq );
+
+    isr_printf("OnTxNextPacketTimerEvent() ");
+    if( status == LORAMAC_STATUS_OK )
+    {
+        if( mibReq.Param.IsNetworkJoined == true )
+        {
+            isr_printf("send");
+            PrepareTxFrame(AppPort);
+            SendFrame(AppPort);
+        }
+        else
+        {
+            isr_printf("join");
+            DeviceState = DEVICE_STATE_JOIN;
+        }
+    }
+    isr_printf("\r\n");
+}
+
 void
 send_uplink()
 {
     AppDataSize = uplink_length;
-    NextTx = true;
-    DeviceState = DEVICE_STATE_SEND;
+    PrepareTxFrame(AppPort);
+    SendFrame(AppPort);    
 }
 
 /*!
@@ -458,7 +458,7 @@
 
         /* mcpsIndication may not come. last uplink done, send another uplink */
         if (jumper_in.read()) {   /* jumper installed: auto uplink */
-            TxNextPacketTimer.attach_us(&send_uplink, 100000);
+            TxNextPacketTimeout.attach_us(&send_uplink, 100000);
         }
 
         if (++fail_count > 10) {
@@ -471,7 +471,6 @@
         }
     }
 
-    NextTx = true;
     isr_printf("\r\n");
 }
 
@@ -489,7 +488,7 @@
     /* last uplink done, send another uplink */
     if (jumper_in.read() && mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_STOP) {
         /* jumper installed: auto uplink */
-        TxNextPacketTimer.attach_us(&send_uplink, 100000);
+        TxNextPacketTimeout.attach_us(&send_uplink, 100000);
     }
 
     if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
@@ -614,13 +613,14 @@
             break;
         }
     }
-
-    isr_printf("\r\n");
     
     if (mcpsIndication->Status == LORAMAC_EVENT_INFO_STATUS_STOP) {
-        TxNextPacketTimer.detach();
+        isr_printf(" STOP-SCC");
+        TxNextPacketTimeout.detach();
         DeviceState = DEVICE_STATE_SLEEP;
     }
+    
+    isr_printf("\r\n");
 }
 
 /*!
@@ -646,7 +646,7 @@
                 DeviceState = DEVICE_STATE_SLEEP;
                 missed_count = 0;
                 if (jumper_in.read())  /* jumper installed: auto uplink */
-                    TxNextPacketTimer.attach_us(&send_uplink, 100000);                
+                    TxNextPacketTimeout.attach_us(&send_uplink, 100000);            
             }
             else
             {
@@ -670,7 +670,7 @@
         default:
             break;
     }
-    NextTx = true;
+
     isr_printf("\r\n");
 }
 
@@ -689,7 +689,7 @@
             isr_printf("%s ", str);
 
             if (send_at_beacon) {
-                TxNextPacketTimer.attach_us(&send_uplink, 100000);
+                TxNextPacketTimeout.attach_us(&send_uplink, 100000);
                 send_at_beacon = false;
             }
 
@@ -708,7 +708,7 @@
     
                     LoRaMacStatus_to_string(status, str);
                     isr_printf("app-rejoin %s\r\n", str);
-                    TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);
+                    TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);
                 }
             }
             break;
@@ -752,8 +752,14 @@
     isr_printf("a1:%u a3:%u\r\n", a1.read_u16(), a3.read_u16());
     isr_printf("button:%d,%u ", button.read(), button_cnt);
 #endif
-    isr_printf("DeviceState:%d\r\n", DeviceState);
-    isr_printf("send_at_beacon:%d\r\n", send_at_beacon);
+    isr_printf("DEVICE_STATE_");
+    switch (DeviceState) {
+        case DEVICE_STATE_JOIN: printf("JOIN"); break;
+        case DEVICE_STATE_CYCLE: printf("CYCLE"); break;
+        case DEVICE_STATE_SLEEP: printf("SLEEP"); break;
+        case DEVICE_STATE_REJOIN: printf("REJOIN"); break;
+    }
+    isr_printf(" send_at_beacon:%d\r\n", send_at_beacon);
     isr_printf("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
     
     loramac_print_status();
@@ -1075,31 +1081,15 @@
                 mibReq.Type = MIB_NETWORK_JOINED;
                 mibReq.Param.IsNetworkJoined = true;
                 LoRaMacMibSetRequestConfirm( &mibReq );
-
-                DeviceState = DEVICE_STATE_SEND;
 #endif
                 break;
             }
-            case DEVICE_STATE_SEND:
-            {
-                if( NextTx == true )
-                {
-                    PrepareTxFrame(AppPort);
-
-                    NextTx = SendFrame(AppPort);
-                }
-              
-                // Schedule next packet transmission
-                TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
-                DeviceState = DEVICE_STATE_SLEEP;
-                break;
-            }
             case DEVICE_STATE_CYCLE:
             {
                 DeviceState = DEVICE_STATE_SLEEP;
 
                 // Schedule next packet transmission
-                TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, TxDutyCycleTime * 1000);
+                TxNextPacketTimeout.attach_us(&OnTxNextPacketTimerEvent, TxDutyCycleTime * 1000);
                 break;
             }
             case DEVICE_STATE_SLEEP: