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:
12:ed33c53afcaf
Parent:
11:4c337f5bbe4c
Child:
14:7ac0add1123e
diff -r 4c337f5bbe4c -r ed33c53afcaf app/single_us915_main.cpp
--- a/app/single_us915_main.cpp	Mon Jun 19 23:49:42 2017 +0000
+++ b/app/single_us915_main.cpp	Wed Jul 05 18:50:32 2017 +0000
@@ -20,20 +20,22 @@
 #include "Commissioning.h"
 #include "commands.h"
 
-DigitalOut grove_led(D6);
-uint32_t grove_cnt;
 #if defined(ENABLE_SX1272) && defined(SENSORS)
-    #include "Air_Quality.h"
-    AirQuality airqualitysensor;
-    PinName analogPin = A1;
-    #define SENSOR_PORT     10
-    InterruptIn button(D8);
+    #include "ChainableLED.h"
+    #include "TSL2561.h"
+    InterruptIn button(D8); // https://developer.mbed.org/teams/Seeed/wiki/Button
     #define RED_LED_RATE_US     333333  /* third of second */
     uint32_t button_cnt;
-    //AnalogIn water(A3);
-    DigitalIn water(A3);
+    ChainableLED rgb(D6, D7, 1); // https://developer.mbed.org/components/Grove-Chainable-RGB-LED/
+    AnalogIn a1(A1); // https://developer.mbed.org/teams/Seeed/wiki/Potentiometer
+    AnalogIn a3(A3); // https://developer.mbed.org/teams/Seeed/wiki/Potentiometer
+    DigitalIn pc8_in(PC_8);
+    DigitalOut pc6_out(PC_6);   
+    //TSL2561 tsl2561(TSL2561_ADDR_FLOAT); // https://developer.mbed.org/components/Grove-Digital-Light-Sensor/ 
+    TSL2561 tsl2561(TSL2561_ADDR_LOW);
 #endif
 
+
 DigitalOut jumper_out(PC_10);
 InterruptIn jumper_in(PC_12);
 
@@ -210,13 +212,26 @@
         break;
 #if defined(SENSORS) && defined(ENABLE_SX1272)
     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);
+        {
+            uint16_t x = tsl2561.getLuminosity(TSL2561_VISIBLE);
+            uint16_t a_1 = a1.read_u16();
+            uint16_t a_3 = a3.read_u16();
+            uint8_t status;
+            status = pc8_in.read();
+            status <<= 1;
+            status |= pc6_out.read();
+            AppData[0] = a_1 >> 8;
+            AppData[1] = a_1 & 0xff;
+            AppData[2] = a_3 >> 8;
+            AppData[3] = a_3 & 0xff;
+            AppData[4] = x >> 8;
+            AppData[5] = x & 0xff;
+            AppData[6] = status;
+            AppDataSize = 7;
+            //printf("visible:%u\r\n", x);
+        }
         break;
-#endif /* SENSORS */
+#endif /* SENSORS && ENABLE_SX1272 */
     default:
         break;
     }
@@ -306,7 +321,7 @@
  *
  * \retval  [0: frame could be send, 1: error]
  */
-static bool SendFrame( void )
+static bool SendFrame( uint8_t port )
 {
     McpsReq_t mcpsReq;
     LoRaMacTxInfo_t txInfo;
@@ -319,7 +334,6 @@
         mcpsReq.Type = MCPS_UNCONFIRMED;
         mcpsReq.Req.Unconfirmed.fBuffer = NULL;
         mcpsReq.Req.Unconfirmed.fBufferSize = 0;
-        //mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
 
         LoRaMacUplinkStatus.Acked = false;
         LoRaMacUplinkStatus.Port = 0;
@@ -329,14 +343,14 @@
     else
     {
         LoRaMacUplinkStatus.Acked = false;
-        LoRaMacUplinkStatus.Port = AppPort;
+        LoRaMacUplinkStatus.Port = port;
         LoRaMacUplinkStatus.Buffer = AppData;
         LoRaMacUplinkStatus.BufferSize = AppDataSize;
 
         if( IsTxConfirmed == false )
         {
             mcpsReq.Type = MCPS_UNCONFIRMED;
-            mcpsReq.Req.Unconfirmed.fPort = AppPort;
+            mcpsReq.Req.Unconfirmed.fPort = port;
             mcpsReq.Req.Unconfirmed.fBuffer = AppData;
             mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
             //mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
@@ -344,7 +358,7 @@
         else
         {
             mcpsReq.Type = MCPS_CONFIRMED;
-            mcpsReq.Req.Confirmed.fPort = AppPort;
+            mcpsReq.Req.Confirmed.fPort = port;
             mcpsReq.Req.Confirmed.fBuffer = AppData;
             mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
             mcpsReq.Req.Confirmed.NbTrials = 8;
@@ -361,13 +375,13 @@
     LoRaMacStatus_to_string(status, str);
     isr_printf("send failed:%s\r\n", str);
     if (status == LORAMAC_STATUS_NO_NETWORK_JOINED) {
+        AppPort = port;
         TxNextPacketTimer.attach_us(&OnTxNextPacketTimerEvent, randr(1000000, 5000000) + 1000000);        
     }
     send_at_beacon = true;
     return true;
 }
 
-
 void
 send_uplink()
 {
@@ -376,7 +390,6 @@
     DeviceState = DEVICE_STATE_SEND;
 }
 
-
 /*!
  * \brief   MCPS-Confirm event function
  *
@@ -428,8 +441,9 @@
 
 //#ifndef MANUAL_UPLINK
         /* mcpsIndication may not come. last uplink done, send another uplink */
-        if (jumper_in.read())   /* jumper installe: auto uplink */
+        if (jumper_in.read()) {   /* jumper installed: auto uplink */
             TxNextPacketTimer.attach_us(&send_uplink, 100000);
+        }
 //#endif /* !MANUAL_UPLINK */
         if (++fail_count > 10) {
             /* cause re-join */
@@ -444,19 +458,6 @@
     isr_printf("\r\n");
 }
 
-#if defined(SENSORS) && defined(ENABLE_SX1272)
-void grove_led_cb()
-{
-    grove_led = !grove_led;
-    
-    if (grove_cnt > 0 || grove_led.read()) {
-        TxNextPacketTimer.attach_us(&grove_led_cb, RED_LED_RATE_US);
-        if (grove_led.read()) /* only decrement on falling edge */
-            grove_cnt--;
-    }
-}
-#endif /* SENSORS */
-
 /*!
  * \brief   MCPS-Indication event function
  *
@@ -470,8 +471,9 @@
 
 //#ifndef MANUAL_UPLINK
     /* last uplink done, send another uplink */
-    if (jumper_in.read())   /* jumper installed: auto 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 )
@@ -482,9 +484,6 @@
     }
 
     awaiting_mcps_indic = false;
-#if defined(SENSORS) && defined(ENABLE_SX1272)
-    grove_led = 0;
-#endif /* SENSORS */
 
     switch( mcpsIndication->McpsIndication )
     {
@@ -507,22 +506,8 @@
         case MCPS_MULTICAST:
         {
             isr_printf("MCPS_MULTICAST ");
-            if (mcpsIndication->RxData) {
-                switch (mcpsIndication->Buffer[3]) {
-                    default:
-                    case BCAST_CMD_NONE: break;
-                    case BCAST_CMD_LED_OFF: grove_led = 0; isr_printf("LED_OFF "); break;
-                    case BCAST_CMD_LED_ON: grove_led = 1; isr_printf("LED_ON "); break;
-                    case BCAST_CMD_LED_COUNT:
-                        isr_printf("LED_BLINK ");
-#if defined(SENSORS) && defined(ENABLE_SX1272)
-                        grove_cnt = mcpsIndication->Buffer[0] - 1;
-                        grove_led = 1;
-                        TxNextPacketTimer.attach_us(&grove_led_cb, RED_LED_RATE_US);
-#endif /* SENSORS */                        
-                        break;
-                } // ..switch (mcpsIndication->Buffer[3])
-            }
+            /*if (mcpsIndication->RxData) {
+            }*/
             break;
         }
         default:
@@ -564,6 +549,23 @@
             isr_printf("%02x ", mcpsIndication->Buffer[i]);
         }
         isr_printf("\r\n");
+        
+#if defined(SENSORS) && defined(ENABLE_SX1272)        
+        switch (mcpsIndication->Buffer[0]) {
+            default:
+            case CMD_NONE: break;
+            case CMD_LED_RGB:
+                rgb.setColorRGB(0, 
+                    mcpsIndication->Buffer[1],  // R
+                    mcpsIndication->Buffer[2],  // G
+                    mcpsIndication->Buffer[3]   // B
+                );
+                break;
+            case CMD_GPIO_OUT:
+                pc6_out = mcpsIndication->Buffer[1];
+                break;
+        } // ..switch (mcpsIndication->Buffer[3])        
+#endif /* SENSORS && ENABLE_SX1272 */
 
         switch( mcpsIndication->Port )
         {
@@ -597,9 +599,6 @@
         case MLME_JOIN:
         {
             isr_printf("MLME_JOIN ");
-#if defined(SENSORS) && defined(ENABLE_SX1272)
-            grove_led = 0;
-#endif /* SENSORS */
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
             {
                 // Status is OK, node has joined the network
@@ -670,9 +669,6 @@
         }
         case MLME_TXDONE:
             isr_printf("MLME_TXDONE ");
-#if defined(SENSORS) && defined(ENABLE_SX1272)
-            grove_led = 1;
-#endif /* SENSORS */
             break;
         default:
             isr_printf("<%d> ", MlmeIndication->MlmeIndication);
@@ -689,7 +685,7 @@
     memcpy(AppData, pcbuf, pcbuf_len);
     AppDataSize = pcbuf_len;
 
-    ret = SendFrame( );
+    ret = SendFrame(TEXT_PORT);
     isr_printf("%d = SendFrame()\r\n", ret);
 }
 
@@ -706,12 +702,13 @@
     isr_printf(", DevAddr:%x\r\n", mibReq.Param.DevAddr);
     
 #if defined(SENSORS) && defined(ENABLE_SX1272)
-    //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());
+    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("awaiting_mcps_indic:%d\r\n", awaiting_mcps_indic);
+    
     loramac_print_status();
 
 }
@@ -725,56 +722,12 @@
 }
 
 #if defined(SENSORS) && defined(ENABLE_SX1272)
-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)
-{
-    grove_led = !grove_led;
-    printf("red_led:%u\r\n", grove_led.read());
-}
-
-void cmd_water_pull(uint8_t idx)
+void cmd_rgb(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", &grove_cnt);
-        if (grove_cnt > 0) {
-            grove_cnt--;
-            grove_led = 1;
-            TxNextPacketTimer.attach_us(&grove_led_cb, RED_LED_RATE_US);
-        }
-    }    
+    int r, g, b;
+    sscanf(pcbuf+idx, "%d %d %d", &r, &g, &b);
+    rgb.setColorRGB(0, r, g, b);
+    printf("\r\nrgb: %d %d %d\r\n", r, g, b);
 }
 #endif /* SENSORS */
 
@@ -793,10 +746,7 @@
     { ".", cmd_status, "","print status"}, 
     { "ul", cmd_uplink_length, "%u","set uplink payload length"}, 
 #if defined(SENSORS) && defined(ENABLE_SX1272)
-    { "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"},
+    { "l", cmd_rgb, "%d %d %d", "set led R G B"},
 #endif
     { NULL, NULL, NULL, NULL }
 };
@@ -887,33 +837,28 @@
     }
 }
 
+
 #if defined(SENSORS) && defined(ENABLE_SX1272)
-// 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 */
+        AppPort = SENSOR_PORT;
         send_uplink();
     }
 }
 #endif /* SENSORS */
 
-
 /**
  * Main application entry point.
  */
 int main( void )
 {
+#if defined(SENSORS) && defined(ENABLE_SX1272)
+    uint8_t prev_but;
+#endif /* SENSORS && ENABLE_SX1272 */
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
@@ -927,7 +872,6 @@
     DeviceState = DEVICE_STATE_INIT;
     
 #if defined(SENSORS) && defined(ENABLE_SX1272)
-    airqualitysensor.init(analogPin, AirQualityInterrupt);
     if (button) {
         button.rise(&button_isr);
         printf("button-rise\r\n");
@@ -935,9 +879,25 @@
         button.rise(&button_isr);
         printf("button-fall\r\n");
     }
-    //water.mode(PullNone);
-    water.mode(PullUp);
-#endif
+    prev_but = button;
+    
+    printf("TSL2561 Sensor ");
+    if (tsl2561.begin()) {    
+        printf("Found\r\n");        
+    } else {    
+        printf("not-found\r\n");   
+    }
+    
+    // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
+    tsl2561.setGain(TSL2561_GAIN_0X);         // set no gain (for bright situtations)
+    //tsl2561.setGain(TSL2561_GAIN_16X);      // set 16x gain (for dim situations)
+    
+    // Changing the integration time gives you a longer time over which to sense tsl2561
+    // longer timelines are slower, but are good in very low tsl2561 situtations!
+    //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright tsl2561)
+    //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium tsl2561)
+    tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim tsl2561)    
+#endif /* SENSORS && ENABLE_SX1272 */
 
     jumper_out = 1;
     jumper_in.mode(PullDown);
@@ -945,6 +905,15 @@
     while( 1 )
     {
         console();
+        
+#if defined(SENSORS) && defined(ENABLE_SX1272) && defined(TARGET_NUCLEO_L073RZ)
+        if (prev_but != button) {
+            if (button) {
+                button_isr();
+            }
+            prev_but = button;
+        }   
+#endif
 
         switch( DeviceState )
         {
@@ -1026,9 +995,9 @@
             {
                 if( NextTx == true )
                 {
-                    PrepareTxFrame( AppPort );
+                    PrepareTxFrame(AppPort);
 
-                    NextTx = SendFrame( );
+                    NextTx = SendFrame(AppPort);
                 }
               
                 // Schedule next packet transmission