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: Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM
Fork of LRAT-example-lorawan by
main.cpp
- Committer:
- Huot87
- Date:
- 2018-08-08
- Revision:
- 32:f211b8c28273
- Parent:
- 31:f03c183e2bf6
File content as of revision 32:f211b8c28273:
/**
* 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 <stdio.h>
#include "mbed.h"
#include "lorawan/LoRaWANInterface.h"
#include "lorawan/system/lorawan_data_structures.h"
#include "events/EventQueue.h"
// Application helpers
#include "DummySensor.h"
#include "trace_helper.h"
#include "lora_radio_helper.h"
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "MAIN"
// Huot Added LSM303AGR_ACC
#include "LSM303AGRAccSensor.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];
// ACCELEROMETER AXES VARIABLE INITIALIZATION
int x = 0;
bool interruptOccured = false;
int32_t axes[3];
/*
* Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
*/
#define TX_TIMER 10000
/**
* 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 15
/**
* Dummy pin for dummy sensor
*/
#define PC_9 0
/**
* Dummy sensor class object
*/
DS1820 ds1820(PC_9);
/**
* 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);
/**
* 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 down the radio object.
*/
static LoRaWANInterface lorawan(radio);
/**
* Application specific callbacks
*/
static lorawan_app_callbacks_t callbacks;
int mytime;
int mybatt;
double mylat;
double mylon;
#define NEOM8M_ADR_GPS 0x42
#define LSM303_ADR_ACC 0x32
#define LSM303_ADR_MAG 0x3C
// ------ HUOT Change
// Had to change I2C to use DevI2C to get below LSM303AGRAccSensor Constructor to function
static DevI2C i2c(PB_9, PB_8);
static LSM303AGRAccSensor accel(&i2c, LSM303_ADR_ACC, PB_14);
InterruptIn accelPin(PB_14);
void accelRunning();
void accelSetup();
/**
* Entry point for application
*/
int main (void)
{
// setup tracing
setup_trace();
// stores the status of a call to LoRaWAN protocol
lorawan_status_t retcode;
printf("\r\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n");
// HUOT -- ACCCELEROMETER SENSOR SETUP
accelSetup();
while (true) {
if (interruptOccured) {
printf("+++++++ INTERRUPT COUNTER: %d +++++++ \r\n", x);
// Write to all accel regs to configure interrupt.
accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x2A);
printf("Enable X and Y Axes High Interrupt Events. \r\n");
// READ SOURCE DATA
uint8_t data = 0;
accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &data);
printf("DATA = %02x \r\n", data);
interruptOccured = false;
} else {
printf("========= No Interrupt on this pass... ========= \r\n");
}
wait(4);
}
mytime = 0;
mybatt = 15;
mylat = 51.06509;
mylon = -114.08895;
// 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");
//printf("MBED_CONF_LORA_APP_PORT: %d", MBED_CONF_LORA_APP_PORT);
// 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();
/*if (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
printf("Retcode = Connecting.");
}*/
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();
return 0;
}
/**
* Sends a message to the Network Server
*/
static void send_message()
{
uint16_t packet_len;
int16_t retcode;
float sensor_value;
if (ds1820.begin()) {
ds1820.startConversion();
sensor_value = ds1820.read();
printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value);
ds1820.startConversion();
} else {
printf("\r\n No sensor found \r\n");
return;
}
//packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f",
// sensor_value);
//packet_len = sprintf((char*) tx_buffer, "VAL=%3.1f", sensor_value);
//packet_len = sprintf((char*) tx_buffer, "%d,%d,%f,%f", mytime, mybatt, mylat, mylon);
//printf("BUF: |%s|", tx_buffer);
time_t tNow = time(NULL);
printf("Clock: %d\r\n", tNow);
mytime = tNow;
char cmd[2];
//char buf[1024];
// ---------- HUOT COMMENT BLOCK OUT ------
/*
char buf;
//cmd[0] = 0xFF;
//i2c.write(NEOM8M_ADR_GPS, cmd, 1);
//i2c.read(NEOM8M_ADR_GPS, buf, 1024);
cmd[0] = 0x20;
cmd[1] = 0x57;
i2c.write(LSM303_ADR_ACC, cmd, 2);
cmd[0] = 0x20;
//i2c.write(LSM303_ADR_ACC, cmd, 1);
i2c.read(LSM303_ADR_ACC, &buf, 1);
//i2c.read(LSM303_ADR_ACC, buf, 1);
printf("Return Value: %02x", buf);
*/
/* HUOT -- TEST OF ACCELEROMETER GET DATA FROM AXES
if (x % 2 == 0) {
accel.get_x_axes(axes);
printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
}
*/
int ilat = (int)(mylat * 100000);
int ilon = (int)(mylon * 100000);
packet_len = 11;
tx_buffer[0] = (mytime >> 24) & 0xFF;
tx_buffer[1] = (mytime >> 16) & 0xFF;
tx_buffer[2] = (mytime >> 8) & 0xFF;
tx_buffer[3] = (mytime >> 0) & 0xFF;
tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F);
tx_buffer[5] = (ilat >> 14) & 0xFF;
tx_buffer[6] = (ilat >> 6) & 0xFF;
tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03);
tx_buffer[8] = (ilon >> 16) & 0xFF;
tx_buffer[9] = (ilon >> 8) & 0xFF;
tx_buffer[10] = (ilon >> 0) & 0xFF;
printf("\r\nBUF: |");
int i;
for (i = 0; i < packet_len; i++) { printf("%02x", tx_buffer[i]); }
printf("|\r\n");
mytime++;
if (mybatt == 0) { mybatt = 15; } else { mybatt--; }
retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
MSG_CONFIRMED_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) {
ev_queue.call_in(3000, send_message);
}
}
return;
}
printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
x += 1;
memset(tx_buffer, 0, sizeof(tx_buffer));
}
/**
* Receive a message from the Network Server
*/
static void receive_message()
{
int16_t retcode;
retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
sizeof(rx_buffer),
MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG);
if (retcode < 0) {
printf("\r\n receive() - Error code %d \r\n", retcode);
return;
}
printf(" Data:");
for (uint8_t i = 0; i < retcode; i++) {
printf("%x", rx_buffer[i]);
}
printf("\r\n Data Length: %d\r\n", retcode);
memset(rx_buffer, 0, sizeof(rx_buffer));
}
/**
* Event handler
*/
static void lora_event_handler(lorawan_event_t event)
{
tr_debug("In lora_event_handler(%d)...", event);
switch (event) {
case CONNECTED:
printf("\r\n Connection - Successful \r\n");
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
send_message();
} else {
ev_queue.call_every(TX_TIMER, 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:
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");
}
}
void accelRunning()
{
interruptOccured = true;
x++;
// printf("++++++++++++++ Interrupt Called! Get Drunk! +++++++++++++++++++ \r\n");
// printf("COUNTER: ---- %d \r\n", x++);
// uint8_t srcData;
// accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &srcData);
// printf("SRC DATA = %02x \r\n", srcData);
// accel.get_x_axes(axes);
// printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
}
// ---- HUOT ACCEL SETUP
void accelSetup()
{
int initCode = accel.init(NULL);
printf("INIT CODE = %d \r\n", initCode);
int enableCode = accel.enable();
printf("ENABLE CODE = %d \r\n", enableCode);
/* HP FILTER BYPASS
// SETUP INTERRUPT
// ODR = 100 Hz
accel.write_reg(LSM303AGR_ACC_CTRL_REG1, 0x67);
printf("ODR Set \r\n");
// High-Pass filter Disabled
accel.write_reg(LSM303AGR_ACC_CTRL_REG2, 0x00);
printf("HP Filter Disabled \r\n");
// Interrupt Driven to INT1 pad
accel.write_reg(LSM303AGR_ACC_CTRL_REG3, 0x40);
printf("INT1 Pad \r\n");
// Full Scale = 2 g
accel.write_reg(LSM303AGR_ACC_CTRL_REG4, 0x00);
printf("Full Scale set at 2G's \r\n");
// Interrupt Latched
accel.write_reg(LSM303AGR_ACC_CTRL_REG5, 0x08);
printf("Interrupt Latched \r\n");
// Threshold = 250 mg
accel.write_reg(LSM303AGR_ACC_INT1_THS, 0x01);
printf("Threshold set at XXX mg's \r\n");
// Duration Set
accel.write_reg(LSM303AGR_ACC_INT1_DURATION, 0x00);
printf("Duration set to 0");
// Enable X-High and Y-High Interrupt Generation
accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x05);
printf("Enable X and Y Axes High Interrupt Events. \r\n");
*/
// SETUP INTERRUPT
// ODR = 100 Hz
accel.write_reg(LSM303AGR_ACC_CTRL_REG1, 0x67);
printf("ODR Set \r\n");
// High-Pass filter Enabled
accel.write_reg(LSM303AGR_ACC_CTRL_REG2, 0x09);
printf("HP Filter Enabled \r\n");
// Interrupt Driven to INT1 pad
accel.write_reg(LSM303AGR_ACC_CTRL_REG3, 0x40);
printf("INT1 Pad \r\n");
// Full Scale = 2 g
accel.write_reg(LSM303AGR_ACC_CTRL_REG4, 0x00);
printf("Full Scale set at 2G's \r\n");
// Interrupt Latched
accel.write_reg(LSM303AGR_ACC_CTRL_REG5, 0x08);
printf("Interrupt Latched \r\n");
accel.write_reg(LSM303AGR_ACC_INT1_THS, 0x01);
printf("Threshold set at XXX mg's \r\n");
// Duration Set
accel.write_reg(LSM303AGR_ACC_INT1_DURATION, 0x00);
printf("Duration set to 0");
// DUMMY READ TO FORCE HP FILTER TO CURRENT ACCELERATION VALUE
uint8_t read_ref;
accel.read_reg(LSM303AGR_ACC_REFERENCE, &read_ref);
// Enable X-High, Y-High and Z-High Interrupt Generation
accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x2A);
printf("Enable X and Y Axes High Interrupt Events. \r\n");
accelPin.rise(&accelRunning);
// ATTEMPT TO READ SOURCE DATA
uint8_t data = 0;
accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &data);
printf("DATA = %02x \r\n", data);
// uint8_t data = 0;
//
// accel.read_reg(, &data);
// printf("DATA = %02x \r\n", data);
//
// uint8_t srcData;
// accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &srcData);
// printf("SRC DATA = %02x \r\n", srcData);
}
// EOF
