You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

321 lines
9.1 KiB

#include "CayenneLPP.h"
#include "events/EventQueue.h"
#include "lora_radio_helper.h"
#include "lorawan/LoRaWANInterface.h"
#include "lorawan/system/lorawan_data_structures.h"
#include "mbed.h"
#include "nvstore.h"
#define NVKEY_DEVICE_EUI 1
#define NVKEY_APP_EUI 2
#define NVKEY_APP_KEY 3
#if MBED_CONF_APP_MODE_BOOTSTRAP
const static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI;
const static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI;
const static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY;
int main(void) {
int rc;
uint16_t key;
uint16_t rsize;
uint8_t test[8];
// NVStore is a sigleton, get its instance
NVStore &nvstore = NVStore::get_instance();
rc = nvstore.init();
printf("Init NVStore : %d\n", rc);
rc = nvstore.reset();
printf("Reset NVStore : %d\n", rc);
key = NVKEY_DEVICE_EUI;
nvstore.set(key, sizeof(dev_eui), dev_eui);
key = NVKEY_APP_EUI;
nvstore.set(key, sizeof(app_eui), app_eui);
key = NVKEY_APP_KEY;
nvstore.set(key, sizeof(app_key), app_key);
while(1) {
printf("Bootstrap done\n");
wait(1);
}
}
#else // MBED_CONF_APP_MODE_BOOTSTRAP
static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI;
static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI;
static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY;
#if MBED_CONF_APP_DHT_ENABLED
#include "DHT.h"
#endif
#if MBED_CONF_APP_HX711_ENABLED
#include "HX711.h"
#endif
using namespace events;
uint8_t rx_buffer[LORAMAC_PHY_MAXPAYLOAD];
/**
* Maximum number of events for the event queue.
* 16 is the safe number for the stack events, however, if application
* also uses the queue for whatever purposes, this number should be increased.c
*/
#define MAX_NUMBER_OF_EVENTS 10
/**
* Maximum number of retries for CONFIRMED messages before giving up
*/
#define CONFIRMED_MSG_RETRY_COUNTER 3
static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS* EVENTS_EVENT_SIZE);
static void lora_event_handler(lorawan_event_t event);
static LoRaWANInterface lorawan(radio);
static lorawan_app_callbacks_t callbacks;
static CayenneLPP cayenne(51);
#if MBED_CONF_APP_BATTERY_ENABLED
AnalogIn bat(MBED_CONF_APP_BATTERY_ADC);
AnalogIn vref(ADC_VREF);
static float battery_voltage() {
float vbat = bat.read();
float vrefin = vref.read();
float vrefcal = (float)(*(uint16_t*)VREFINT_CAL_ADDR) * (1.0f / (float)0xFFF);
float vdda = 3.0f * vrefcal / vrefin;
return vbat * vdda * MBED_CONF_APP_BATTERY_RATIO;
}
static uint8_t battery_level() {
float vbat = battery_voltage();
if (vbat >= MBED_CONF_APP_BATTERY_MAX) {
return 254;
} else if (vbat > MBED_CONF_APP_BATTERY_MIN && vbat < MBED_CONF_APP_BATTERY_MAX) {
return ( 253 * ( vbat - MBED_CONF_APP_BATTERY_MIN ) ) / ( MBED_CONF_APP_BATTERY_MAX - MBED_CONF_APP_BATTERY_MIN ) + 1;
} else {
return 1;
}
}
#endif
#if MBED_CONF_APP_DHT_ENABLED
DHT dht(MBED_CONF_APP_DHT_DATA, MBED_CONF_APP_DHT_TYPE);
#endif
#if MBED_CONF_APP_HX711_ENABLED
HX711 loadcell(MBED_CONF_APP_HX711_DATA, MBED_CONF_APP_HX711_CLK);
#endif
int main(void) {
lorawan_status_t retcode;
lorawan_connect_t connect;
int rc;
uint16_t key;
uint16_t readsize;
connect.connect_type = LORAWAN_CONNECTION_OTAA;
// NVStore is a sigleton, get its instance
NVStore &nvstore = NVStore::get_instance();
if (nvstore.init() != NVSTORE_SUCCESS) {
debug("\r\n NVStore initialization failed! \r\n");
return -1;
}
key = NVKEY_DEVICE_EUI;
nvstore.get(key, sizeof(dev_eui), dev_eui, readsize);
key = NVKEY_APP_EUI;
nvstore.get(key, sizeof(app_eui), app_eui, readsize);
key = NVKEY_APP_KEY;
nvstore.get(key, sizeof(app_key), app_key, readsize);
connect.connection_u.otaa.app_eui = const_cast<uint8_t *>(app_eui);
connect.connection_u.otaa.dev_eui = const_cast<uint8_t *>(dev_eui);
connect.connection_u.otaa.app_key = const_cast<uint8_t *>(app_key);
connect.connection_u.otaa.nb_trials = MBED_CONF_LORA_NB_TRIALS;
#if MBED_CONF_APP_HX711_ENABLED
loadcell.powerDown();
loadcell.setScale(MBED_CONF_APP_HX711_SCALE);
loadcell.setOffset(MBED_CONF_APP_HX711_OFFSET);
#endif
// Initialize LoRaWAN stack
if(lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
debug("\r\n LoRa initialization failed! \r\n");
return -1;
}
debug("\r\n Mbed LoRaWANStack initialized \r\n");
callbacks.events = mbed::callback(lora_event_handler);
#if MBED_CONF_APP_BATTERY_ENABLED
callbacks.battery_level = mbed::callback(battery_level);
#endif
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) {
debug("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
return -1;
}
debug("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER);
// Enable adaptive data rate
if(lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
debug("\r\n enable_adaptive_datarate failed! \r\n");
return -1;
}
debug("\r\n Adaptive data rate (ADR) - Enabled \r\n");
retcode = lorawan.connect(connect);
if(retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
} else {
debug("\r\n Connection error, code = %d \r\n", retcode);
return -1;
}
debug("\r\n Connection - In Progress ...\r\n");
// make your event queue dispatching events forever
ev_queue.dispatch_forever();
return 0;
}
/**
* Sends a message to the Network Server
*/
static void send_message() {
int16_t retcode;
int eid;
// setup next transmission
eid = ev_queue.call_in(MBED_CONF_APP_TX_TIMER, send_message);
// check backoff time before sending new frame
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
lorawan_status_t status;
int backoff;
status = lorawan.get_backoff_metadata(backoff);
if (status == LORAWAN_STATUS_OK && backoff > 0) {
debug("\r\n %d ms backoff, delay next send \r\n", backoff);
ev_queue.cancel(eid);
ev_queue.call_in(backoff+MBED_CONF_APP_TX_TIMER, send_message);
return;
}
}
#if MBED_CONF_APP_DHT_ENABLED
int err = dht.read();
if(err == DHT::SUCCESS) {
cayenne.addTemperature(MBED_CONF_APP_DHT_TEMP_LPP_ID, dht.getTemperature());
cayenne.addRelativeHumidity(MBED_CONF_APP_DHT_HUM_LPP_ID, dht.getHumidity());
} else {
debug("Error code : %d\r\n", err);
}
#endif
#if MBED_CONF_APP_HX711_ENABLED
loadcell.powerUp();
if(loadcell.waitReadyRetry(20, 100)) {
cayenne.addAnalogInput(MBED_CONF_APP_HX711_LPP_ID, loadcell.getUnits(5));
}
loadcell.powerDown();
#endif
#if MBED_CONF_APP_BATTERY_ENABLED && MBED_CONF_APP_BATTERY_IN_LPP
cayenne.addAnalogInput(MBED_CONF_APP_BATTERY_LPP_ID, battery_voltage());
#endif
// No data to send
if(cayenne.getSize() == 0) {
return;
}
retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, cayenne.getBuffer(), cayenne.getSize(),
MSG_UNCONFIRMED_FLAG);
if(retcode < 0) {
retcode == LORAWAN_STATUS_WOULD_BLOCK ? debug("send - WOULD BLOCK\r\n")
: debug("\r\n send() - Error code %d \r\n", retcode);
return;
}
debug("\r\n %d bytes scheduled for transmission \r\n", retcode);
cayenne.reset();
}
/**
* Receive a message from the Network Server
*/
static void receive_message() {
int16_t retcode;
retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, LORAMAC_PHY_MAXPAYLOAD,
MSG_CONFIRMED_FLAG | MSG_UNCONFIRMED_FLAG);
if(retcode < 0) {
debug("\r\n receive() - Error code %d \r\n", retcode);
return;
}
debug(" Data:");
for(uint8_t i = 0; i < retcode; i++) {
debug("%x", rx_buffer[i]);
}
debug("\r\n Data Length: %d\r\n", retcode);
memset(rx_buffer, 0, LORAMAC_PHY_MAXPAYLOAD);
}
static void lora_event_handler(lorawan_event_t event) {
switch(event) {
case CONNECTED:
debug("\r\n Connection - Successful \r\n");
send_message();
break;
case DISCONNECTED:
ev_queue.break_dispatch();
debug("\r\n Disconnected Successfully \r\n");
break;
case TX_DONE:
debug("\r\n Message Sent to Network Server \r\n");
break;
case TX_TIMEOUT:
case TX_ERROR:
case TX_CRYPTO_ERROR:
case TX_SCHEDULING_ERROR:
debug("\r\n Transmission Error - EventCode = %d \r\n", event);
break;
case RX_DONE:
debug("\r\n Received message from Network Server \r\n");
receive_message();
break;
case RX_TIMEOUT:
case RX_ERROR:
debug("\r\n Error in reception - Code = %d \r\n", event);
break;
case JOIN_FAILURE:
debug("\r\n OTAA Failed - Check Keys \r\n");
break;
default:
MBED_ASSERT("Unknown Event");
}
}
#endif // MBED_APP_MODE_BOOTSTRAP