This is an example application based on Mbed-OS LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification.

Dependencies:   Lorawan_Version_0_1

Dependents:   Lorawan_Version_0_1

Revision:
68:41fff9c3fb4f
Parent:
67:7bb75ed97500
Child:
69:316fee01f5d9
--- a/main.cpp	Wed Jan 15 08:37:29 2020 +0000
+++ b/main.cpp	Fri Jan 17 13:46:31 2020 +0000
@@ -22,16 +22,15 @@
 #include "events/EventQueue.h"
 
 // Application helpers
-#include "DummySensor.h"
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
 #include <mbed.h>
 #include "mbed_stats.h"
 
-#include "Watchdog.h"
+using namespace events;
 
-using namespace events;
+DigitalIn USERButton(PB_15);
 
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 uint8_t tx_buffer[13];
@@ -47,30 +46,24 @@
 
 
 DigitalOut GPSdisable(PC_6);
-int LiveTick;
+uint32_t TickCounter;
+uint32_t TickTime;
+
+uint32_t TimeWithoutMoving;
 
 bool IsLoading;
 AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo
 
-/*
- * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
- */
-#define TX_TIMER                        100000
 
-/**
- * Maximum number of events for the event queue.
- * 10 is the safe number for the stack events, however, if application
- * also uses the queue for whatever purposes, this number should be increased.
- */
-#define MAX_NUMBER_OF_EVENTS            10
+#define MAX_NUMBER_OF_EVENTS            20
 
 /**
  * Maximum number of retries for CONFIRMED messages before giving up
  */
-#define CONFIRMED_MSG_RETRY_COUNTER     3
+#define CONFIRMED_MSG_RETRY_COUNTER     1
 
 
-  DigitalOut led(LED2);
+DigitalOut StatusLED(PB_8);
 
 #include "Fahrrad.h"
 
@@ -84,7 +77,7 @@
 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
 
 static void send_message();
-
+static int Lora_init();
 /**
  * Event handler.
  *
@@ -111,54 +104,66 @@
 
 static void LifeTicker()
 {
+//Watchdog
     Watchdog &watchdog = Watchdog::get_instance();    
     Watchdog::get_instance().kick(); // kick the Watchdog before the timeout
         
-    LiveTick=LiveTick+1;
-    printf("\n[LiveTick] --- [%i]\n\n", LiveTick);
+//Lifetick
+    TickCounter=TickCounter+1;
+    printf("------------------------------\n");
+    printf("\n[LiveTick] --- [%i]\n\n", TickCounter);
     
-       mbed_stats_cpu_t stats;
-       mbed_stats_cpu_get(&stats);
-       printf("[SYSTEM] Uptime: %llu ", stats.uptime / 1000);
-       printf("Idle: %llu ", stats.idle_time / 1000);       
-       printf("Sleep: %llu ", stats.sleep_time / 1000);
-       printf("Deep: %llu\n", stats.deep_sleep_time / 1000);
+//Sleep Statistik
+   mbed_stats_cpu_t stats;
+   mbed_stats_cpu_get(&stats);
+   printf("[SYSTEM] Uptime: %llu ", stats.uptime / 1000);
+   printf("Idle: %llu ", stats.idle_time / 1000);       
+   printf("Sleep: %llu ", stats.sleep_time / 1000);
+   printf("Deepsleep: %llu\n", stats.deep_sleep_time / 1000);
     
-    
-    printf("[SYSTEM] Ladewert: %3.3f%%\n", LadeSpannung.read()*100.0f);
+//Dynamo - Fahrrad in Bewegung
     if(LadeSpannung.read() > 0.05f) {
             if (not IsLoading) {Licht_hell();}
             IsLoading = 1;
+            TimeWithoutMoving =0 ;
+            printf("[SYSTEM] Akku laden mit: %fV\n", LadeSpannung.read()*3.3f*11);
         } else {
             if (IsLoading) {ev_queue.call_in(3000, Licht_dunkel);}
             IsLoading = 0;
+            TimeWithoutMoving=TimeWithoutMoving+TickTime/1000;
+            printf("[SYSTEM] Fahrrad steht seit: %is\n", TimeWithoutMoving);
         }        
         
-        
-        
-    GPSdisable=0;
-    GPS_Position current_pos;    
-    current_pos = GPS_aktiv(); 
+        if (!USERButton){Blinken_ein(&ev_queue);};
+    
+//GPS
+    GPS_activ = TimeWithoutMoving<300;
+    GPS_Position current_pos; 
     
-    if(LORA_activ)
-    {
-    printf("\n[LORA] Connected\n");    
-    if (current_pos.Longitude < 0.1)
-    {        
-        current_pos.Longitude =  50.123;
-        current_pos.Latitude =  7.456;     
-    }
-    send_Position_to_Lora(0x01, current_pos);
+    if (GPS_activ){
+        GPSdisable=0;
+        current_pos = GPS_aktiv();   
+    }  
+    else{    
+        GPSdisable=1;
+        printf("\n[GPS] Aus - Energie sparen\n"); 
+    }        
+    
+//LORA
+    if(LORA_activ){
+        StatusLED=1;
+        printf("\n[LORA] Connected\n");   
+        send_Position_to_Lora(0x01, current_pos);
     }
     else 
     {
-    printf("\n[LORA] not Connected\n");        
+    printf("\n[LORA] not Connected\n");    
+    StatusLED=0;    
     }                
 }
 
 
 
-
 /**
  * Entry point for application
  */
@@ -169,18 +174,69 @@
     watchdog.start(30000);
     uint32_t watchdog_timeout = watchdog.get_timeout();
     printf("Watchdog initialized to %iu ms.\r\n", watchdog_timeout);
-    
-    
-    ev_queue.call_every(10000,LifeTicker);
+           
+    TickTime=15000;
+    ev_queue.call_every(TickTime,LifeTicker);
  
     
-    
     // setup tracing
     setup_trace();
     
-    //Starte_Fahrrad_Eventmanager(); //schonmal die Fahrrad Events Initialisieren falls wir hier auch ohne lorawan empfang was machen wollen
- 
+    Starte_Fahrrad_Eventmanager(); //schonmal die Fahrrad Events Initialisieren falls wir hier auch ohne lorawan empfang was machen wollen
+
+    Lora_init();
+    // make your event queue dispatching events forever
+    ev_queue.dispatch_forever();
+    
+    return 0;
+}
+
 
+static void send_Position_to_Lora(uint8_t OptCode,GPS_Position current_pos)
+{
+    
+    //1Byte Opcode 0x01
+    //4Byte Timestemp
+    //4Byte Longitude
+    //4Byte Latitude 
+    
+    uint8_t tmpbytes[sizeof(float)];  
+    
+    tx_buffer[0] = OptCode;
+        
+    *((float *)tmpbytes) = current_pos.Zeitstempel; 
+    tx_buffer[1] = tmpbytes[0];
+    tx_buffer[2] = tmpbytes[1];
+    tx_buffer[3] = tmpbytes[2];
+    tx_buffer[4] = tmpbytes[3];
+    
+    *((float *)tmpbytes) = current_pos.Longitude; 
+    tx_buffer[5] = tmpbytes[0];
+    tx_buffer[6] = tmpbytes[1];
+    tx_buffer[7] = tmpbytes[2];
+    tx_buffer[8] = tmpbytes[3];
+    
+    *((float *)tmpbytes) = current_pos.Latitude; 
+    tx_buffer[9]  = tmpbytes[0];
+    tx_buffer[10] = tmpbytes[1];
+    tx_buffer[11] = tmpbytes[2];
+    tx_buffer[12] = tmpbytes[3];
+    
+    printf("[LORA] Send: %02X:%02X%02X%02X%02X:%02X%02X%02X%02X:%02X%02X%02X%02X \n",
+                    tx_buffer[0],
+                    tx_buffer[1],tx_buffer[2],tx_buffer[3],tx_buffer[4],
+                    tx_buffer[5],tx_buffer[6],tx_buffer[7],tx_buffer[8],
+                    tx_buffer[9],tx_buffer[10],tx_buffer[11],tx_buffer[12]);   
+    send_message();
+}
+
+
+/**
+ * Sends a message to the Network Server
+ */
+static int Lora_init()
+{
+    
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
         printf("\r\n LoRa initialization failed! \r\n");
@@ -222,52 +278,9 @@
     }
 
     printf("\r\n Connection - In Progress ...\r\n");
-
-    // make your event queue dispatching events forever
-    ev_queue.dispatch_forever();
-    
     return 0;
 }
 
-
-static void send_Position_to_Lora(uint8_t OptCode,GPS_Position current_pos)
-{
-    
-    //1Byte Opcode 0x01
-    //4Byte Timestemp
-    //4Byte Longitude
-    //4Byte Latitude 
-    
-    uint8_t tmpbytes[sizeof(float)];  
-    
-    tx_buffer[0] = OptCode;
-        
-    *((float *)tmpbytes) = current_pos.Zeitstempel; 
-    tx_buffer[1] = tmpbytes[0];
-    tx_buffer[2] = tmpbytes[1];
-    tx_buffer[3] = tmpbytes[2];
-    tx_buffer[4] = tmpbytes[3];
-    
-    *((float *)tmpbytes) = current_pos.Longitude; 
-    tx_buffer[5] = tmpbytes[0];
-    tx_buffer[6] = tmpbytes[1];
-    tx_buffer[7] = tmpbytes[2];
-    tx_buffer[8] = tmpbytes[3];
-    
-    *((float *)tmpbytes) = current_pos.Latitude; 
-    tx_buffer[9]  = tmpbytes[0];
-    tx_buffer[10] = tmpbytes[1];
-    tx_buffer[11] = tmpbytes[2];
-    tx_buffer[12] = tmpbytes[3];
-    
-    printf("\tSend Message:%02X:%02X%02X%02X%02X:%02X%02X%02X%02X:%02X%02X%02X%02X \n",
-                    tx_buffer[0],
-                    tx_buffer[1],tx_buffer[2],tx_buffer[3],tx_buffer[4],
-                    tx_buffer[5],tx_buffer[6],tx_buffer[7],tx_buffer[8],
-                    tx_buffer[9],tx_buffer[10],tx_buffer[11],tx_buffer[12]);   
-    send_message();
-}
-
 /**
  * Sends a message to the Network Server
  */
@@ -317,10 +330,13 @@
         printf("%02x ", rx_buffer[i]);
     }
         
-    if (rx_buffer[0] == 0xa0) Licht_aus(); 
-    if (rx_buffer[0] == 0xa1)Licht_an();        
-    if (rx_buffer[0] == 0xb1) Blinken_ein();
-    if (rx_buffer[0] == 0xb0) Blinken_aus();  
+    //if (rx_buffer[0] == 0xa0) Licht_aus(); 
+//    if (rx_buffer[0] == 0xa1)Licht_an();        
+    if (rx_buffer[0] == 0xb1)  
+    {
+    Blinken_ein(&ev_queue);    
+    }
+    
     if (rx_buffer[0] == 0xc1)GPS_anfordern();    
     
     printf("\r\n");