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:
65:3061ea235a0c
Parent:
64:85fa08519e1e
Child:
66:a11c79073f1f
--- a/main.cpp	Sat Jan 11 10:36:24 2020 +0000
+++ b/main.cpp	Sat Jan 11 11:35:11 2020 +0000
@@ -14,91 +14,36 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <stdio.h>
-
-#include "lorawan/LoRaWANInterface.h"
-#include "lorawan/system/lorawan_data_structures.h"
-#include "events/EventQueue.h"
-
-// Application helpers
-#include "DummySensor.h"
+ 
+#include "mbed.h"
+#include "Lora.h"
+#include "Fahrrad.h"
 #include "trace_helper.h"
-#include "lora_radio_helper.h"
-
-#include <mbed.h>
-
-using namespace events;
-
-// Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
-// This example only communicates with much shorter messages (<30 bytes).
-// If longer messages are used, these buffers must be changed accordingly.
-uint8_t tx_buffer[30];
-uint8_t rx_buffer[30];
-
+#include <stdio.h>
+#include "mbed_stats.h"
 
 float Home_Longitude;
 float Home_Latitude;
-
 float Home_Distance;
 float Home_Distance_max;
 bool GPS_activ,LORA_activ,AtHome;
-
 int LiveTick;
-
 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
-
-/**
- * Maximum number of retries for CONFIRMED messages before giving up
- */
-#define CONFIRMED_MSG_RETRY_COUNTER     3
+struct TX_Position
+{
+uint8_t TX_Precode;
+uint8_t TX_Zeitstempel[sizeof(float)];
+uint8_t TX_Longitude[sizeof(float)];
+uint8_t TX_Latitude[sizeof(float)];
+};
 
 
-  DigitalOut led(LED2);
-
-#include "Fahrrad.h"
-
-/**
-* This event queue is the global event queue for both the
-* application and stack. To conserve memory, the stack is designed to run
-* in the same thread as the application and the application is responsible for
-* providing an event queue to the stack that will be used for ISR deferment as
-* well as application information event queuing.
-*/
-static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
+DigitalOut led(LED2);
 
-/**
- * Event handler.
- *
- * This will be passed to the LoRaWAN stack to queue events for the
- * application which in turn drive the application.
- */
-static void lora_event_handler(lorawan_event_t event);
-
-/**
- * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
- */
-static LoRaWANInterface lorawan(radio);
-
-/**
- * Application specific callbacks
- */
-static lorawan_app_callbacks_t callbacks;
-
+//Livetick Warteschlange Events
+static EventQueue ev_queue(10 *EVENTS_EVENT_SIZE);
 
 
 static void LifeTicker()
@@ -107,13 +52,14 @@
     LiveTick=LiveTick+1;
     printf("\n\t\t---LiveTick--- [%i]\n\n", LiveTick);
     printf("Ladewert: %3.3f%%\n", LadeSpannung.read()*100.0f);
-    if(LadeSpannung.read() > 0.05f) {
-            if (not IsLoading) {Licht_hell();}
-            IsLoading = 1;
-        } else {
-            if (IsLoading) {ev_queue.call_in(3000, Licht_dunkel);}
-            IsLoading = 0;
-        }        
+
+//if(LadeSpannung.read() > 0.05f) {
+//            if (not IsLoading) {Licht_hell();}
+//            IsLoading = 1;
+//        } else {
+//            if (IsLoading) {ev_queue.call_in(3000, Licht_dunkel);}
+//            IsLoading = 0;
+//        }        
         
     if(LORA_activ)
     {
@@ -124,82 +70,30 @@
     printf("\n\tKein LORA Signal\n");        
     }
     
-    
-    
     ev_queue.call(GPS_aktiv); 
-  
-  
-  
-  
    // ev_queue.call_in(5000, LifeTicker);
+   
+   
+       mbed_stats_cpu_t stats;
+       mbed_stats_cpu_get(&stats);
+       printf("Uptime: %llu ", stats.uptime / 1000);
+       printf("Sleep time: %llu ", stats.sleep_time / 1000);
+       printf("Deep Sleep: %llu\n", stats.deep_sleep_time / 1000);
+       
 }
 
-
-
-
-/**
- * Entry point for application
- */
- 
- 
-
 int main(void)
 { 
- 
-    ev_queue.call_every(5000,LifeTicker);
-    
+    printf("----- MAIN() ------");
+
     // setup tracing
     setup_trace();
     
-    //Starte_Fahrrad_Eventmanager(); //schonmal die Fahrrad Events Initialisieren falls wir hier auch ohne lorawan empfang was machen wollen
-
-    // stores the status of a call to LoRaWAN protocol
-    lorawan_status_t retcode;
+    Lora_init();
     
-
-    // Initialize LoRaWAN stack
-    if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
-        printf("\r\n LoRa initialization failed! \r\n");
-        return -1;
-    }
-
-    printf("\r\n Mbed LoRaWANStack initialized \r\n");
-
-    // prepare application callbacks
-    callbacks.events = mbed::callback(lora_event_handler);
-    lorawan.add_app_callbacks(&callbacks);
-
-    // Set number of retries in case of CONFIRMED messages
-    if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
-            != LORAWAN_STATUS_OK) {
-        printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
-        return -1;
-    }
-
-    printf("\r\n CONFIRMED message retries : %d \r\n",
-           CONFIRMED_MSG_RETRY_COUNTER);
-
-    // Enable adaptive data rate
-    if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
-        printf("\r\n enable_adaptive_datarate failed! \r\n");
-        return -1;
-    }
-
-    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
-
-    retcode = lorawan.connect();
+    ev_queue.call_every(5000,LifeTicker);
     
-    if (retcode == LORAWAN_STATUS_OK ||
-            retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
-    } else {
-        printf("\r\n Connection error, code = %d \r\n", retcode);
-        return -1;
-    }
-
-    printf("\r\n Connection - In Progress ...\r\n");
-
-    // make your event queue dispatching events forever
-    ev_queue.dispatch_forever();
+    //Starte_Fahrrad_Eventmanager(); //schonmal die Fahrrad Events Initialisieren falls wir hier auch ohne lorawan empfang was machen wollen
 
     return 0;
 }
@@ -208,140 +102,3 @@
 
 
 
-
-/**
- * Sends a message to the Network Server
- */
-static void send_message()
-{
-    uint16_t packet_len;
-    int16_t retcode;
-    int32_t sensor_value;
-    
-    
-
-    //if (ds1820.begin()) {
-//        ds1820.startConversion();
-//        sensor_value = ds1820.read();
-//        printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
-//        ds1820.startConversion();
-//    } else {
-//        printf("\r\n No sensor found \r\n");
-//        return;
-//    }
-
-   
-
-    if (retcode < 0) {
-        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
-        : printf("\r\n send() - Error code %d \r\n", retcode);
-
-        if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
-            //retry in 3 seconds
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                ev_queue.call_in(3000, send_message);
-            }
-        }
-        return;
-    }
-
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
-    memset(tx_buffer, 0, sizeof(tx_buffer));
-}
-
-/**
- * Receive a message from the Network Server
- */
-static void receive_message()
-{
-    uint8_t port;
-    int flags;
-    int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
-
-    if (retcode < 0) {
-        printf("\r\n receive() - Error code %d \r\n", retcode);
-        return;
-    }
-
-    printf(" RX Data on port %u (%d bytes): ", port, retcode);
-    for (uint8_t i = 0; i < retcode; i++) {
-        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] == 0xc1)GPS_anfordern();    
-    
-    printf("\r\n");
-    
-    memset(rx_buffer, 0, sizeof(rx_buffer));
-}
-
-/**
- * Event handler
- */
-static void lora_event_handler(lorawan_event_t event)
-{
-    switch (event) {
-        case CONNECTED:
-            LORA_activ=1;
-            printf("\r\n Connection - Successful \r\n");
-
-            //ev_queue.call_every(5000, send_message);
-          
-
-            break;
-        case DISCONNECTED:
-            ev_queue.break_dispatch();
-            printf("\r\n Disconnected Successfully \r\n");
-            break;
-        case TX_DONE:
-            printf("\r\n Message Sent to Network Server \r\n");
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                //send_message();
-            }
-            break;
-        case TX_TIMEOUT:
-        
-            LORA_activ=0;
-        case TX_ERROR:
-        case TX_CRYPTO_ERROR:
-        case TX_SCHEDULING_ERROR:
-            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
-            // try again
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
-            }
-            break;
-        case RX_DONE:
-            printf("\r\n Received message from Network Server \r\n");
-            receive_message();
-            break;
-        case RX_TIMEOUT:
-        case RX_ERROR:
-            printf("\r\n Error in reception - Code = %d \r\n", event);
-            break;
-        case JOIN_FAILURE:
-            printf("\r\n OTAA Failed - Check Keys \r\n");
-            break;
-        case UPLINK_REQUIRED:
-            printf("\r\n Uplink required by NS \r\n");
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
-            }
-            break;
-        default:
-            MBED_ASSERT("Unknown Event");
-    }
-}
-
-
-
-
-
-// EOF