application layer with: button, LED, pot, tempSense

Dependencies:   mbed SX1272Lib

Fork of LoRaWAN-demo-72-bootcamp by Semtech

Use with sx1272 shield with grove peripherals connected:

D8 D9: ButtonRX TXA3 A4: TempSense
D6 D7:SCL SDA : LEDA1 A2: Pot

Button

Sends to different payloads: short press (under 1 sec)
long press: held down > 1 sec.

serial console keys

115200bps, 8N1
Enter key not used
Keys '0' to '3': cayenne channel number
'0': pot (rotary sensor)
'1': temperature
' 2': digital out
'3': analog out

Revision:
12:662ff73ce484
Parent:
11:018e7e28161d
Child:
13:f88dd77772b6
--- a/app/main.cpp	Sat Feb 03 00:44:16 2018 +0000
+++ b/app/main.cpp	Mon Feb 12 11:25:50 2018 -0800
@@ -305,8 +305,17 @@
     vt.SetCursorPos( 44, 1 );
     vt.printf("%u sendFrame() %s\e[K", sendCnt, str);
     return true;
+} // ..SendFrame()
+
+TimerEvent_t event;
+#define TX_INTERVAL_MS      15000
+void autoUplink()
+{
+    DeviceState = DEVICE_STATE_SEND;
 }
 
+char statusTxt[64];
+
 uint8_t c_ch;
 
 void SerialRxProcess( void )
@@ -333,6 +342,14 @@
                 if (status == LORAMAC_STATUS_OK)
                     SendFrame(0, false);
                 break;
+            case 'j':
+                DeviceState = DEVICE_STATE_JOIN;
+                break;
+            case 'u':
+                TimerInit(&event, autoUplink);
+                TimerSetValue(&event, TX_INTERVAL_MS);
+                TimerStart(&event);
+                sprintf(statusTxt, "timer on %u", TX_INTERVAL_MS);
             default:
                 break;
         }
@@ -382,6 +399,7 @@
 
 volatile TimerTime_t buttonStartAt;
 DigitalIn d8(D8);
+DigitalIn userBut(USER_BUTTON);
 DigitalOut extLed(D15);
 PwmOut pwm(PB_11);
 volatile int cayenne_ack_ch;
@@ -507,7 +525,7 @@
         default:
             break;
     }
-}
+} // ..PrepareTxFrame()
 
 
 void LoRaMacEventInfoStatus_toString(LoRaMacEventInfoStatus_t s, char* out)
@@ -554,6 +572,7 @@
     }
 }
 
+
 /*!
  * \brief   MCPS-Confirm event function
  *
@@ -588,14 +607,13 @@
     } else {
         char str[48];
         LoRaMacEventInfoStatus_toString(mcpsConfirm->Status, str);
-        vt.SetCursorPos( 44, 1 );
-        vt.printf("mcpsConf %s", str);
+        strcpy(statusTxt, "mcpsConf ");
+        strcat(statusTxt, str);
 
     }
 
     DeviceState = DEVICE_STATE_TRIGGER;
-}
-
+} // ..McpsConfirm()
 
 /*!
  * \brief   MCPS-Indication event function
@@ -608,8 +626,8 @@
     if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) {
         char str[48];
         LoRaMacEventInfoStatus_toString(mcpsIndication->Status, str);
-        vt.SetCursorPos( 44, 1 );
-        vt.printf("mcpsInd %s\e[K", str);
+        strcpy(statusTxt, "mcpsInd ");
+        strcat(statusTxt, str);
         return;
     }
 
@@ -823,28 +841,28 @@
  */
 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
 {
-    vt.SetCursorPos( 45, 1 );
-    vt.printf("MlmeConfirm() ");
+    char str[48];
+
+    strcpy(statusTxt, "MlmeConfirm() ");
+
     switch( mlmeConfirm->MlmeRequest ) {
         case MLME_JOIN: {
-            vt.printf("MLME_JOIN ");
+            strcat(statusTxt, "MLME_JOIN ");
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) {
-                vt.printf("OK");
+                strcat(statusTxt, "OK");
                 DeviceState = DEVICE_STATE_JOIN_OK;
                 extLed = 0;
             } else {
-                char str[48];
                 LoRaMacEventInfoStatus_toString(mlmeConfirm->Status, str);
-                vt.printf(str);
-                // Join was not successful. Try to join again
+                strcat(statusTxt, str);
                 DeviceState = DEVICE_STATE_JOIN;
             }
             break;
         }
         case MLME_LINK_CHECK: {
-            vt.printf("MLME_LINK_CHECK");
+            strcat(statusTxt, "MLME_LINK_CHECK ");
             if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) {
-                vt.printf("OK");
+                strcat(statusTxt, "OK");
                 // Check DemodMargin
                 // Check NbGateways
                 if( ComplianceTest.Running == true ) {
@@ -856,16 +874,17 @@
             break;
         }
         default:
-            vt.printf("<%d>", mlmeConfirm->MlmeRequest);
+            sprintf(str, "<%d>", mlmeConfirm->MlmeRequest);
+            strcat(statusTxt, str);
             break;
     }
-    vt.printf("\e[K");
-}
+} // ..MlmeConfirm()
 
-void foo()
+void poll()
 {
     float R, temp;
     uint16_t u16;
+    static unsigned prevBut = 1;
     
     vt.SetCursorPos( 43, 1 );
     
@@ -874,7 +893,24 @@
     R = R0 * R;
     temp = 1.0/(log(R/R0)/B+1/298.15)-273.15;
     
-    vt.printf("%u d9:%u (%03x %03x %03x %03x) %.2fC\e[K\r\n", DeviceState, d8.read() >> 4, a1.read_u16() >> 4, a2.read_u16() >> 4, a3.read_u16() >> 4, a4.read_u16(), temp);
+    vt.printf("%u d8:%u,%u (%03x %03x %03x %03x) %.2fC\e[K", DeviceState,
+            d8.read(), userBut.read(),
+            a1.read_u16() >> 4, a2.read_u16() >> 4, a3.read_u16() >> 4, a4.read_u16(), temp);
+
+    if (statusTxt[0] != 0) {
+        vt.SetCursorPos( 45, 1 );
+        vt.printf("%s\e[K", statusTxt);
+        statusTxt[0] = 0;
+    }
+
+    if (prevBut == 0 && userBut.read() == 1) {
+        /* user button release */
+        TimerInit(&event, autoUplink);
+        TimerSetValue(&event, TX_INTERVAL_MS);
+        TimerStart(&event);
+        sprintf(statusTxt, "timer on %u", TX_INTERVAL_MS);
+    }
+    prevBut = userBut.read();
 }
 
 const LoRaMacPrimitives_t LoRaMacPrimitives = {
@@ -911,7 +947,7 @@
     DeviceState = DEVICE_STATE_INIT;
 
     while( 1 ) {
-        foo();
+        poll();
         SerialRxProcess( );
 
         if( DownlinkStatusUpdated == true ) {
@@ -981,6 +1017,7 @@
                 mlmeReq.Req.Join.DevEui = DevEui;
                 mlmeReq.Req.Join.AppEui = AppEui;
                 mlmeReq.Req.Join.AppKey = AppKey;
+                mlmeReq.Req.Join.NbTrials = 1;
 
                 s = LoRaMacMlmeRequest( &mlmeReq );
                 if (s != LORAMAC_STATUS_OK) {
@@ -988,9 +1025,8 @@
                     LoRaMacStatus_toString(s, str);
                     vt.SetCursorPos( 44, 1 );
                     vt.printf("mlmeReq join %s\e[K", str);
-                    return -1;                    
-                } else
-                    DeviceState = DEVICE_STATE_SLEEP;
+                }
+                DeviceState = DEVICE_STATE_SLEEP;
 
                 extLed = 1;
 #else
@@ -1055,12 +1091,11 @@
                     buttonStartAt = TimerGetCurrentTime();
                 }
                 break;
-            default: {
+            default:
                 DeviceState = DEVICE_STATE_INIT;
                 break;
-            }
+
+        } // ..switch( DeviceState )
 
-        }
-
-    }
+    } // ..while( 1 )
 }