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: mbed-os-example-mbed5-lorawan
Fork of mbed-os-example-mbed5-lorawan by
main.cpp
- Committer:
- Ranyd04
- Date:
- 2018-06-08
- Revision:
- 23:3dfaed332d89
- Parent:
- 18:1720bc3831ea
File content as of revision 23:3dfaed332d89:
/**
* 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 "Sensors.h"
#include "qsd.h"
#include "lorawan/LoRaWANInterface.h"
#include "lorawan/system/lorawan_data_structures.h"
#include "events/EventQueue.h"
// Application helpers
#include "trace_helper.h"
#include "lora_radio_helper.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];
/*
* 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 3
/**
* Dummy pin for dummy sensor
*/
#define PC_9 0
/**
* Dummy sensor class object
*/
/**
* 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;
/**
* Entry point for application
*/
// Masque selection de bits
#define MSK1 0xFF
#define MSK2 0xFF00
#define MSK3 0xFF0000
#define MSK4 0xFF000000
uint8_t MSGtosend[30];
int pos, pos_1,pos_2;
SENSORS capteur;
QSD qsd;
int QSD_value;
int QSD_VAL1,QSD_VAL2,QSD_VAL3,QSD_VAL4;
int courant , courant_1,courant_2 ;
int tension_bat;
int main (void)
{
tension_bat = capteur.vbat_sensor();
MSGtosend[0]=0x07;
MSGtosend[1]=0x01;
MSGtosend[2]= tension_bat ;
/*
courant = capteur.current_sensor();
courant_1 = ((courant) & MSK1) >>0;
courant_2 = ((courant) & MSK2) >>8;
MSGtosend[0]=0x06;
MSGtosend[1]=0x02;
MSGtosend[2]=courant_2;
MSGtosend[3]=courant_1;
*/
/*
pos = capteur.transducer_sensor();
pos_1 = (pos & MSK1) >>0;
pos_2 = (pos & MSK2) >>8;
MSGtosend[0]=0x04;
MSGtosend[1]=0x02;
MSGtosend[2]=0x00;
MSGtosend[3]=pos;
*/
/*
QSD_value = qsd.read();
//printf("\r\n QSD Sensor Value = %d \r\n", QSD_value);
QSD_VAL1 = (QSD_value & MSK1)>>0;
QSD_VAL2 = (QSD_value & MSK2)>>8;
QSD_VAL3 = (QSD_value & MSK3)>>16;
QSD_VAL4 = (QSD_value & MSK4)>>24;
MSGtosend[0]=QSD_VAL4;
MSGtosend[1]=QSD_VAL3;
MSGtosend[2]=QSD_VAL2;
MSGtosend[3]=QSD_VAL1;
*/
// setup tracing
setup_trace();
// stores the status of a call to LoRaWAN protocol
lorawan_status_t retcode;
// 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");
// 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_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()
{
uint8_t packet_len;
int8_t retcode;
packet_len = 4;
retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, MSGtosend, 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);
return;
}
printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
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)
{
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 TX_DONE:
printf("\r\n Message Sent to Network Server \r\n");
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
ev_queue.break_dispatch();
//timer.reset();
}
}
}
// EOF
