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:
73:974c1df98553
Parent:
72:67c5bce77999
--- a/main.cpp	Tue Jan 28 14:22:48 2020 +0000
+++ b/main.cpp	Fri Jan 31 08:58:37 2020 +0000
@@ -16,8 +16,10 @@
 uint32_t TickTime=3000;
 uint32_t LoraTickCounter;
 uint32_t LoraTickTime=15000; //15000
-uint32_t LoraTickTimeSleep=7200000; // in sleep mode only every 2 hours to receive commandos
-int mode; //1 = normal , 2 = sleep every but check 2 hour , 3 = sleep Lora turned completly off -> no remote accessibility
+uint32_t LoraTickTimeSleep=21600000; // in sleep mode only every 6 hours to receive commandos
+
+
+int mode;
 
 bool IsLoading;
 AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo
@@ -74,32 +76,36 @@
    printf("\n[SYSTEM] Uptime: %llu Idle: %llu Sleep: %llu Deep: %llu", stats.uptime / 1000 , stats.idle_time / 1000, stats.sleep_time / 1000,stats.deep_sleep_time / 1000);
         
 //Dynamo - bike in motion - adjust light
-   if(LadeSpannung.read()*3.3f*11 > 5) {   
+   if(LadeSpannung.read()*3.3f*11 > 2) {   
         TimeWithoutMoving =0 ;
         printf("\n[SYSTEM] Bike moving: %fV", LadeSpannung.read()*3.3f*11);
+
+//GPS , Lora wake up communication when moving
+        if (GPS_modul.mode==2 && GPS_modul.is_idle){
+            GPS_modul.idle(false); 
+            printf("\n[GPS] Wake up due to Moving");
+        }
+        if (GPS_modul.mode==3){
+            change_mode(2); // eco mode after moving
+        }
     }else {        
         TimeWithoutMoving=TimeWithoutMoving+TickTime;
         printf("\n[SYSTEM] Bike still for: %is", TimeWithoutMoving/1000);
+        
+//GPS turn off when bike is not moved for a while (3min)
+        if (GPS_modul.mode==2 && TimeWithoutMoving>180000){ 
+        GPS_modul.idle(true); //GPS to sleep, Lora needs to stay active for blinkrequests
+        printf("\n[GPS] Sleep due to Standing still");
+        }       
      }        
      
 // Adjust the light according to the movement of the bike
     light.adjust(TimeWithoutMoving);
 
-//GPS , Lora wake up communication when moving
-if (mode>1 && TimeWithoutMoving==0){
-    GPS_modul.idle(false); 
-    printf("\n[GPS] Wake up due to Moving");
-    change_mode(1); // full communication
-}
 
-//GPS turn off when bike is not moved for a while (15min)
-if (mode==1 && TimeWithoutMoving>900000){ 
-    GPS_modul.idle(true); //GPS to sleep, Lora needs to stay active for blinkrequests
-    printf("\n[GPS] Sleep due to Standing still");
-    }       
-
-// User Button - change Mode 
-     if (!USERButton) change_mode(mode+1);
+// change Mode 
+if (GPS_modul.mode!= mode) change_mode(GPS_modul.mode);
+if (!USERButton) change_mode(mode+1);
 }
 
   
@@ -121,6 +127,7 @@
     lora.Lora_init(); // Lora initialisieren
     
     mode=1; // always starting in normal mode
+    GPS_modul.mode = mode;
     
     ev_queue.dispatch_forever();// make your event queue dispatching events forever
     return 0;
@@ -134,11 +141,10 @@
                  LoraTickID=0;
                  }        
                  
- //1 = normal , 2 = sleep every but check 2 hour , 3 = sleep Lora turned completly off -> no remote accessibility
+ //1 = normal , 2 = Eco-mode GPS only while moving , 3 = sleep Lora only every few hours for remote accessibility
          mode=modeid;
          if (mode>3) mode=1; 
-                   
-    printf("\n[MODE] --- [%i] ", mode);
+         GPS_modul.mode=mode;           
     
  //Init new LoraTick         
  
@@ -146,15 +152,18 @@
              GPS_modul.idle(false); 
              LoraTickID = ev_queue.call_every(LoraTickTime,LoraTicker); //Zyklus zum senden über Lora    
              light.Blinken_ein(500); //blink 1x
+             printf("\n[MODE][1][Normal-Mode]");
              }
          if (mode == 2) {    
-             GPS_modul.idle(true);    
-             LoraTickID = ev_queue.call_every(LoraTickTimeSleep,LoraTicker); //Zyklus zum senden über Lora    
+             LoraTickID = ev_queue.call_every(LoraTickTime,LoraTicker); //Zyklus zum senden über Lora, GPS only when moving       
              light.Blinken_ein(1000);//blink 2x
+             printf("\n[MODE][2][Eco-Mode]");
              }             
          if (mode == 3) {    
+             LoraTickID = ev_queue.call_every(LoraTickTimeSleep,LoraTicker); // Lora Ticker every few hours te receive messages   
              GPS_modul.idle(true);  
              light.Blinken_ein(1500);//blink 3x
+             printf("\n[MODE][3][Sleep-Mode]");
              }
      }