Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Lorawan_Version_0_1
Dependents: Lorawan_Version_0_1
Lora/Lora.cpp
- Committer:
- jacktractive
- Date:
- 2020-01-21
- Revision:
- 70:65b2f1cc2859
- Parent:
- 69:316fee01f5d9
- Child:
- 71:ca2425c0a864
File content as of revision 70:65b2f1cc2859:
#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 connected;
Lora::Lora(EventQueue *q, Light *l, GPS *gps)
{
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;
packet_len = sizeof(tx_buffer);
retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
MSG_UNCONFIRMED_FLAG);
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) {
//es kann wieder gesendet werden q->call_in(3000, send_message);
}
}
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)
{
//1Byte Opcode 0x01
//4Byte Timestemp
//4Byte Longitude
//4Byte Latitude
float testtime = 141516.00;
float testHomelat = 52.143734;
float testHomelon = 7.323368;
time=testtime;
longitude=testHomelon;
latitude = testHomelat;
if (connected)
{
uint8_t tmpbytes[sizeof(float)];
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];
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]);
send_message();
}
printf("\n[LORA] not connected\n");
}
/**
* Sends a message to the Network Server
*/
int Lora::Lora_init()
{
// 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]);
}
//if (rx_buffer[0] == 0xa0) Licht_aus();
// if (rx_buffer[0] == 0xa1)Licht_an();
if (rx_buffer[0] == 0xb1){Ref_Light->Blinken_ein(10000);}
if (rx_buffer[0] == 0xc1){Ref_GPS->set_manual_tracking(true); }
if (rx_buffer[0] == 0xc0){Ref_GPS->set_manual_tracking(false); }
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:
connected=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) {
//send_message();
}
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