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

Lora/Lora.cpp

Committer:
jacktractive
Date:
2020-02-04
Revision:
74:b05ae4efbd12
Parent:
73:974c1df98553

File content as of revision 74:b05ae4efbd12:

#include <stdio.h>
#include "lorawan/LoRaWANInterface.h"
#include "lorawan/system/lorawan_data_structures.h"
#include "Event.h"
#include "lora_radio_helper.h"
#include <mbed.h>
#include "Light.h"
#include "GPS.h"
#include "Lora.h"

DigitalOut StatusLED(PB_8);


static EventQueue *Ref_Events;
static GPS *Ref_GPS;
static Light *Ref_Light;

bool still_sending;

bool connected;


Lora::Lora(EventQueue *q, Light *l, GPS *gps)
{
    //wir speichern uns Pointer auf die Objekte die wir in der Klasse verwenden möchten
    Ref_Events=q;
    Ref_GPS= gps;
    Ref_Light = l;
}
using namespace events;

// Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
uint8_t tx_buffer[13];
uint8_t rx_buffer[13];

/**
 * Maximum number of retries for CONFIRMED messages before giving up
 */
#define CONFIRMED_MSG_RETRY_COUNTER     2

/**
 * 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;

/**
 * Sends a message to the Network Server
 */
static void send_message()
{
    uint16_t packet_len;
    int16_t retcode;    
    
    still_sending=true; // wir setzten das Bit zurück wenn Nachricht erfolgreich rausgegangen ist
    
    if (!Ref_GPS->GPS_signal_okay){
            packet_len = sizeof(tx_buffer[0]);
            retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, 1,
                           MSG_UNCONFIRMED_FLAG);            
            printf("\n[LORA] Send: #00 lifetick");
                        
        }
        else{
            packet_len = sizeof(tx_buffer);
            retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
                           MSG_UNCONFIRMED_FLAG);
                           
            printf("\n[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]);   
    
            }
    
    if (retcode < 0) {
        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
        : printf("\r\n send() - Error code %d \r\n", retcode);
        return;
    }

    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
    memset(tx_buffer, 0, sizeof(tx_buffer));
}


void Lora::send_Position_to_Lora(uint8_t OptCode,float time,float longitude,float latitude)
{    
    
    if (latitude == 0){
        OptCode = 0x00 ; // only send x00 as Livebyte when no gps available
        tx_buffer[0] = OptCode;
    }
    else{
        //1Byte Opcode 0x01 4Byte Timestemp  4Byte Longitude  4Byte Latitude                 
        uint8_t tmpbytes[sizeof(float)];  
        
        if (Ref_GPS->mode == 1) OptCode = 0x01 ; 
        if (Ref_GPS->mode == 2) OptCode = 0x02 ; 
        if (Ref_GPS->mode == 3) OptCode = 0x03 ;
         
        tx_buffer[0] = OptCode;
            
        *((float *)tmpbytes) = time; 
        tx_buffer[1] = tmpbytes[0];
        tx_buffer[2] = tmpbytes[1];
        tx_buffer[3] = tmpbytes[2];
        tx_buffer[4] = tmpbytes[3];
        
        *((float *)tmpbytes) = longitude; 
        tx_buffer[5] = tmpbytes[0];
        tx_buffer[6] = tmpbytes[1];
        tx_buffer[7] = tmpbytes[2];
        tx_buffer[8] = tmpbytes[3];
        
        *((float *)tmpbytes) = latitude; 
        tx_buffer[9]  = tmpbytes[0];
        tx_buffer[10] = tmpbytes[1];
        tx_buffer[11] = tmpbytes[2];
        tx_buffer[12] = tmpbytes[3];
    }         
            
    if (connected && !still_sending)
    {
    send_message();
    }
    else{
        if (!connected) printf("\n[LORA] not yet connected\n"); // sonst gibt es natürlich SendeErrors
        if (still_sending) printf("\n[LORA] Still Sending - waiting for reply\n"); // sonst machen wir unseren Eventhandler voll
    }
    
    
}


/**
 * Sends a message to the Network Server
 */
int Lora::Lora_init()
{
    StatusLED.write(0);
       
    // Initialize LoRaWAN stack
    if (lorawan.initialize(Ref_Events) != 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");

    // stores the status of a call to LoRaWAN protocol
    lorawan_status_t retcode;
    retcode = lorawan.connect();    
    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");
    return 0;
}


/**
 * 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]);
    }
              
//Fernsteuerung Blinken
    if (rx_buffer[0] == 0xb1){Ref_Light->Blinken_ein(15000);}

//Fernsteuerung GPS manuel an/aus
    if (rx_buffer[0] == 0xc1)    Ref_GPS->mode=1;
    if (rx_buffer[0] == 0xc2)    Ref_GPS->mode=2;
    if (rx_buffer[0] == 0xc3)    Ref_GPS->mode=3;    
    
//Eingangsfach wieder leer machen
    memset(rx_buffer, 0, sizeof(rx_buffer));
}

/**
 * Event handler
 */
static void lora_event_handler(lorawan_event_t event)
{
    switch (event) {
        case CONNECTED:
            connected=1;
            StatusLED.write(1);
            printf("\r\n Connection - Successful \r\n");
            break;
        case DISCONNECTED:
            connected=0;
            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) {
                still_sending=false;
            }
            break;
        case TX_TIMEOUT:
        
            connected=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