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:
10:00997daeb0c0
Parent:
7:e238827f0e47
Child:
11:4c337f5bbe4c
--- a/app/single_us915_main.cpp	Mon Jun 12 19:43:20 2017 +0000
+++ b/app/single_us915_main.cpp	Sat Jun 17 00:04:30 2017 +0000
@@ -19,6 +19,23 @@
 #include "LoRaMac.h"
 #include "Commissioning.h"
 
+#if defined(ENABLE_SX1272) && defined(SENSORS)
+    #include "Air_Quality.h"
+    AirQuality airqualitysensor;
+    PinName analogPin = A1;
+    #define SENSOR_PORT     10
+    InterruptIn button(D8);
+    DigitalOut red_led(D6);
+    uint32_t red_cnt;
+    #define RED_LED_RATE_US     333333  /* third of second */
+    uint32_t button_cnt;
+    //AnalogIn water(A3);
+    DigitalIn water(A3);
+#endif
+
+DigitalOut jumper_out(PC_10);
+InterruptIn jumper_in(PC_12);
+
 char pcbuf[128];
 int pcbuf_len;
 
@@ -78,7 +95,11 @@
 /*!
  * Application port
  */
-static uint8_t AppPort = LORAWAN_APP_PORT;
+#ifdef SENSORS
+    static uint8_t AppPort = SENSOR_PORT;
+#else
+    static uint8_t AppPort = LORAWAN_APP_PORT;
+#endif
 
 /*!
  * User application data size
@@ -186,6 +207,15 @@
             }
         }
         break;
+#ifdef SENSORS
+    case SENSOR_PORT:
+        airqualitysensor.slope();
+        AppData[0] = airqualitysensor.first_vol >> 8;
+        AppData[1] = airqualitysensor.first_vol & 0xff;
+        AppDataSize = 2;
+        isr_printf("first_vol:%d last_vol:%d\r\n", airqualitysensor.first_vol, airqualitysensor.last_vol);
+        break;
+#endif /* SENSORS */
     default:
         break;
     }
@@ -395,10 +425,11 @@
         LoRaMacEventInfoStatus_to_string(mcpsConfirm->Status, str);
         isr_printf("%s ", str);
 
-#ifndef MANUAL_UPLINK
+//#ifndef MANUAL_UPLINK
         /* mcpsIndication may not come. last uplink done, send another uplink */
-        TxNextPacketTimer.attach_us(&send_uplink, 100000);
-#endif /* !MANUAL_UPLINK */
+        if (jumper_in.read())   /* jumper installe: auto uplink */
+            TxNextPacketTimer.attach_us(&send_uplink, 100000);
+//#endif /* !MANUAL_UPLINK */
         if (++fail_count > 10) {
             /* cause re-join */
             MibRequestConfirm_t mibReq;
@@ -412,6 +443,19 @@
     isr_printf("\r\n");
 }
 
+#ifdef SENSORS
+void red_led_cb()
+{
+    red_led = !red_led;
+    
+    if (red_cnt > 0 || red_led.read()) {
+        TxNextPacketTimer.attach_us(&red_led_cb, RED_LED_RATE_US);
+        if (red_led.read()) /* only decrement on falling edge */
+            red_cnt--;
+    }
+}
+#endif /* SENSORS */
+
 /*!
  * \brief   MCPS-Indication event function
  *
@@ -422,18 +466,22 @@
 {
     isr_printf("McpsIndication ");
 
-#ifndef MANUAL_UPLINK
+//#ifndef MANUAL_UPLINK
     /* last uplink done, send another uplink */
-    TxNextPacketTimer.attach_us(&send_uplink, 100000);
-#endif /* !MANUAL_UPLINK */
+    if (jumper_in.read())   /* jumper installed: auto uplink */
+        TxNextPacketTimer.attach_us(&send_uplink, 100000);
+//#endif /* !MANUAL_UPLINK */
 
     if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
     {
-        isr_printf("\r\n");
+        isr_printf("!ok\r\n");
         return;
     }
 
     awaiting_mcps_indic = false;
+#ifdef SENSORS
+    red_led = 0;
+#endif /* SENSORS */
 
     switch( mcpsIndication->McpsIndication )
     {
@@ -455,6 +503,15 @@
         }
         case MCPS_MULTICAST:
         {
+            isr_printf("MCPS_MULTICAST ");
+            if (mcpsIndication->RxData) {
+                isr_printf("beacon-rxdata");
+#ifdef SENSORS
+                red_cnt = mcpsIndication->Buffer[0] - 1;
+                red_led = 1;
+                TxNextPacketTimer.attach_us(&red_led_cb, RED_LED_RATE_US);
+#endif /* SENSORS */
+            }
             break;
         }
         default:
@@ -529,6 +586,9 @@
         case MLME_JOIN:
         {
             isr_printf("MLME_JOIN ");
+#ifdef SENSORS
+            red_led = 0;
+#endif /* SENSORS */
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Status is OK, node has joined the network
@@ -578,12 +638,12 @@
             isr_printf("missed:%u BEACON ", missed_count);
             LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str);
             isr_printf("%s ", str);
-#ifndef MANUAL_UPLINK
+//#ifndef MANUAL_UPLINK
             if (send_at_beacon) {
                 TxNextPacketTimer.attach_us(&send_uplink, 100000);
                 send_at_beacon = false;
             }
-#endif /* !MANUAL_UPLINK */
+//#endif /* !MANUAL_UPLINK */
             if (LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED == MlmeIndication->Status)
                 missed_count = 0;
             else if (++missed_count > 4) {
@@ -597,6 +657,12 @@
             break;
 
         }
+        case MLME_TXDONE:
+            isr_printf("MLME_TXDONE ");
+#ifdef SENSORS
+            red_led = 1;
+#endif /* SENSORS */
+            break;
         default:
             isr_printf("<%d> ", MlmeIndication->MlmeIndication);
             break;
@@ -616,6 +682,7 @@
     isr_printf("%d = SendFrame()\r\n", ret);
 }
 
+
 void cmd_status(uint8_t idx)
 {
     MibRequestConfirm_t mibReq;
@@ -627,10 +694,15 @@
     LoRaMacMibGetRequestConfirm( &mibReq );         
     isr_printf(", DevAddr:%x\r\n", mibReq.Param.DevAddr);
     
+#ifdef SENSORS
+    //printf("button:%d,%u  water:%f ", button.read(), button_cnt, water.read());
+    printf("button:%d,%u  water:%d jumper:%d ", button.read(), button_cnt, water.read(), jumper_in.read());
+#endif
     isr_printf("DeviceState:%d\r\n", DeviceState);
     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();
+
 }
 
 void cmd_uplink_length(uint8_t idx)
@@ -641,6 +713,60 @@
     isr_printf("uplink_length:%u\r\n", uplink_length);
 }
 
+#ifdef SENSORS
+void cmd_sensor(uint8_t idx)
+{
+   int current_quality = airqualitysensor.slope();
+    if (current_quality >= 0) { // if a valid data returned.
+        if (current_quality == 0)
+            isr_printf("High pollution! Force signal active\n\r");
+        else if (current_quality == 1)
+            isr_printf("High pollution!\n\r");
+        else if (current_quality == 2)
+            isr_printf("Low pollution!\n\r");
+        else if (current_quality == 3)
+            isr_printf("Fresh air\n\r");    
+    }
+}
+
+void cmd_red_led(uint8_t idx)
+{
+    red_led = !red_led;
+    printf("red_led:%u\r\n", red_led.read());
+}
+
+void cmd_water_pull(uint8_t idx)
+{
+    printf("water pull:");
+    if (pcbuf[idx] == 'u') {
+        water.mode(PullUp);
+        printf("up");
+    }else if (pcbuf[idx] == 'n') {
+        water.mode(PullNone);
+        printf("none");
+    } else if (pcbuf[idx] == 'd') {
+        water.mode(PullDown);
+        printf("down");
+    } else if (pcbuf[idx] == 'o') {
+        water.mode(OpenDrain);
+        printf("od");
+    }     
+    printf("\r\n");
+}
+
+void cmd_led_flash(uint8_t idx)
+{
+    if (pcbuf[idx] >= '0' && pcbuf[idx] <= '9') {
+        sscanf(pcbuf+idx, "%u", &red_cnt);
+        if (red_cnt > 0) {
+            red_cnt--;
+            red_led = 1;
+            TxNextPacketTimer.attach_us(&red_led_cb, RED_LED_RATE_US);
+        }
+    }    
+}
+#endif /* SENSORS */
+
 typedef struct {
     const char* const cmd;
     void (*handler)(uint8_t args_at);
@@ -654,7 +780,13 @@
 {   /* after first character, command names must be [A-Za-z] */
     { "?", cmd_help, "","show available commands"},
     { ".", cmd_status, "","print status"}, 
-    { "ul", cmd_uplink_length, "","set uplink payload length"}, 
+    { "ul", cmd_uplink_length, "%u","set uplink payload length"}, 
+#ifdef SENSORS
+    { "s", cmd_sensor, "","read sensor"}, 
+    { "l", cmd_red_led, "","toggle red LED"},
+    { "w", cmd_water_pull, "%c","config water pull: u,d,n,o"},
+    { "f", cmd_led_flash, "%d","flash LED n times"},
+#endif
     { NULL, NULL, NULL, NULL }
 };
 
@@ -744,6 +876,28 @@
     }
 }
 
+#ifdef SENSORS
+// Interrupt Handler
+void AirQualityInterrupt()
+{
+    AnalogIn sensor(analogPin);
+    airqualitysensor.last_vol = airqualitysensor.first_vol;
+    airqualitysensor.first_vol = sensor.read()*1000;
+    airqualitysensor.timer_index = 1;
+}
+
+void button_isr()
+{
+    button_cnt++;
+    isr_printf("button_isr\r\n");
+    if (!jumper_in.read()) {
+        /* jumper not installed: manual uplink */
+        send_uplink();
+    }
+}
+#endif /* SENSORS */
+
+
 /**
  * Main application entry point.
  */
@@ -753,11 +907,29 @@
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
 
-    BoardInit( );
     pc.baud(38400);
     pc.attach(&rx_callback);
     isr_printf("\r\nreset\r\n");
+    
+    BoardInit( );
+
     DeviceState = DEVICE_STATE_INIT;
+    
+#ifdef SENSORS
+    airqualitysensor.init(analogPin, AirQualityInterrupt);
+    if (button) {
+        button.rise(&button_isr);
+        printf("button-rise\r\n");
+    } else {
+        button.rise(&button_isr);
+        printf("button-fall\r\n");
+    }
+    //water.mode(PullNone);
+    water.mode(PullUp);
+#endif
+
+    jumper_out = 1;
+    jumper_in.mode(PullDown);
 
     while( 1 )
     {
@@ -791,7 +963,8 @@
             }
             case DEVICE_STATE_JOIN:
             {
-                send_at_beacon = true;
+                if (jumper_in.read())   /* if jumper installed: auto uplink */
+                    send_at_beacon = true;
 #if( OVER_THE_AIR_ACTIVATION != 0 )
                 MlmeReq_t mlmeReq;
                 // override software definition with hardware value