Navigation Menu

Skip to content

Commit

Permalink
configuration information
Browse files Browse the repository at this point in the history
  • Loading branch information
ciiram committed Aug 5, 2019
1 parent caec774 commit df9b0bc
Show file tree
Hide file tree
Showing 9 changed files with 3,360 additions and 4 deletions.
6 changes: 3 additions & 3 deletions device_addresses.h
Expand Up @@ -14,9 +14,9 @@ limitations under the License.
#ifndef _DEVICE_ADDRESSES_H
#define _DEVICE_ADDRESSES_H

static uint32_t DEVADDR = 0x0;
static uint8_t NWKSKEY[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
static uint8_t APPSKEY[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
static uint32_t DEVADDR = 0x260118E5;
static uint8_t NWKSKEY[] = { 0x07, 0x87, 0x15, 0xAF, 0x1B, 0x8F, 0x82, 0x5C, 0x95, 0x49, 0xC1, 0x2C, 0x32, 0x2E, 0x08, 0xF7 };
static uint8_t APPSKEY[] = { 0xEF, 0xD1, 0xB3, 0xAC, 0xAA, 0xB7, 0xBE, 0xAC, 0xC4, 0xE3, 0x7D, 0x56, 0x22, 0x7D, 0x55, 0x42 };



Expand Down
76 changes: 76 additions & 0 deletions inc/lora_radio_helper.h
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "lorawan/LoRaRadio.h"

#ifndef APP_LORA_RADIO_HELPER_H_
#define APP_LORA_RADIO_HELPER_H_

#include "SX1272_LoRaRadio.h"
#include "SX1276_LoRaRadio.h"

#define SX1272 0xFF
#define SX1276 0xEE

#if (MBED_CONF_APP_LORA_RADIO == SX1272)

SX1272_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI,
MBED_CONF_APP_LORA_SPI_MISO,
MBED_CONF_APP_LORA_SPI_SCLK,
MBED_CONF_APP_LORA_CS,
MBED_CONF_APP_LORA_RESET,
MBED_CONF_APP_LORA_DIO0,
MBED_CONF_APP_LORA_DIO1,
MBED_CONF_APP_LORA_DIO2,
MBED_CONF_APP_LORA_DIO3,
MBED_CONF_APP_LORA_DIO4,
MBED_CONF_APP_LORA_DIO5,
MBED_CONF_APP_LORA_RF_SWITCH_CTL1,
MBED_CONF_APP_LORA_RF_SWITCH_CTL2,
MBED_CONF_APP_LORA_TXCTL,
MBED_CONF_APP_LORA_RXCTL,
MBED_CONF_APP_LORA_ANT_SWITCH,
MBED_CONF_APP_LORA_PWR_AMP_CTL,
MBED_CONF_APP_LORA_TCXO);

#elif (MBED_CONF_APP_LORA_RADIO == SX1276)

SX1276_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI,
MBED_CONF_APP_LORA_SPI_MISO,
MBED_CONF_APP_LORA_SPI_SCLK,
MBED_CONF_APP_LORA_CS,
MBED_CONF_APP_LORA_RESET,
MBED_CONF_APP_LORA_DIO0,
MBED_CONF_APP_LORA_DIO1,
MBED_CONF_APP_LORA_DIO2,
MBED_CONF_APP_LORA_DIO3,
MBED_CONF_APP_LORA_DIO4,
MBED_CONF_APP_LORA_DIO5,
MBED_CONF_APP_LORA_RF_SWITCH_CTL1,
MBED_CONF_APP_LORA_RF_SWITCH_CTL2,
MBED_CONF_APP_LORA_TXCTL,
MBED_CONF_APP_LORA_RXCTL,
MBED_CONF_APP_LORA_ANT_SWITCH,
MBED_CONF_APP_LORA_PWR_AMP_CTL,
MBED_CONF_APP_LORA_TCXO);

#else
#error "Unknown LoRa radio specified (SX1272,SX1276 are valid)"
#endif


#endif /* APP_LORA_RADIO_HELPER_H_ */
112 changes: 112 additions & 0 deletions inc/lorawan_network.h
@@ -0,0 +1,112 @@
#ifndef _LORAWAN_NETWORK_H_
#define _LORAWAN_NETWORK_H_

#include "mbed.h"
#include "mbed_trace.h"
#include "mbed_events.h"
#include "LoRaWANInterface.h"
#include "SX1276_LoRaRadio.h"
#include "CayenneLPP.h"
#include "lora_radio_helper.h"

#define MBED_CONF_LORA_APP_PORT 15

// EventQueue is required to dispatch events around
EventQueue ev_queue;

// Constructing Mbed LoRaWANInterface and passing it down the radio object.
static LoRaWANInterface lorawan(radio);

// Application specific callbacks
static lorawan_app_callbacks_t callbacks;

// LoRaWAN stack event handler
static void lora_event_handler(lorawan_event_t event);

static void lorawan_setup(uint32_t devAddr, uint8_t nwkSKey[16], uint8_t appSKey[16], Callback<void(lorawan_event_t)> lw_event_handler) {
if (devAddr == 0x0) {
printf("Set your LoRaWAN credentials first!\n");
return;
}

// Enable trace output for this demo, so we can see what the LoRaWAN stack does
mbed_trace_init();

if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
printf("[LNWK][ERR ] LoRa initialization failed!\n");
return;
}

// prepare application callbacks
callbacks.events = lw_event_handler;
lorawan.add_app_callbacks(&callbacks);

// Disable adaptive data rating
if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
printf("[LNWK][ERR ] disable_adaptive_datarate failed!\n");
return;
}

lorawan.set_datarate(0); // SF12BW125

lorawan_connect_t connect_params;
connect_params.connect_type = LORAWAN_CONNECTION_ABP;
// connect_params.connection_u.otaa.dev_eui = DEV_EUI;
// connect_params.connection_u.otaa.app_eui = APP_EUI;
// connect_params.connection_u.otaa.app_key = APP_KEY;
// connect_params.connection_u.otaa.nb_trials = 3;

connect_params.connection_u.abp.dev_addr = devAddr;
connect_params.connection_u.abp.nwk_skey = nwkSKey;
connect_params.connection_u.abp.app_skey = appSKey;

lorawan_status_t retcode = lorawan.connect(connect_params);

if (retcode == LORAWAN_STATUS_OK ||
retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
} else {
printf("[LNWK][ERR ] Connection error, code = %d\n", retcode);
return;
}

printf("[LNWK][INFO] Connection - In Progress...\n");
}

// This is called from RX_DONE, so whenever a message came in
static void receive_message()
{
uint8_t rx_buffer[50] = { 0 };
int16_t retcode;
retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
sizeof(rx_buffer),
MSG_UNCONFIRMED_FLAG);

if (retcode < 0) {
printf("[LNWK][WARN] receive() - Error code %d\n", retcode);
return;
}

printf("[LNWK][INFO] Data received on port %d (length %d): ", MBED_CONF_LORA_APP_PORT, retcode);

for (uint8_t i = 0; i < retcode; i++) {
printf("%02x ", rx_buffer[i]);
}
printf("\n");
}

// Event handler
bool lorawan_send(CayenneLPP *payload) {
int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload->getBuffer(), payload->getSize(), MSG_UNCONFIRMED_FLAG);

// for some reason send() ret\urns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk
if (retcode < 0) {
retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("[LNWK][WARN] send - duty cycle violation\n")
: printf("[LNWK][WARN] send() - Error code %d\n", retcode);
return false;
}

printf("[LNWK][INFO] %d bytes scheduled for transmission\n", retcode);
return true;
}

#endif // _LORAWAN_NETWORK_H_

0 comments on commit df9b0bc

Please sign in to comment.