This is an example application based on Mbed-OS LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification.

Dependents:   Projet_de_bachelor_code Projet_de_bachelor_code

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include <stdio.h>
00018 
00019 #include "lorawan/LoRaWANInterface.h"
00020 #include "lorawan/system/lorawan_data_structures.h"
00021 #include "events/EventQueue.h"
00022 
00023 // Application helpers
00024 #include "DummySensor.h"
00025 #include "trace_helper.h"
00026 #include "lora_radio_helper.h"
00027 
00028 using namespace events;
00029 
00030 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
00031 // This example only communicates with much shorter messages (<30 bytes).
00032 // If longer messages are used, these buffers must be changed accordingly.
00033 uint8_t tx_buffer[30];
00034 uint8_t rx_buffer[30];
00035 
00036 /*
00037  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
00038  */
00039 #define TX_TIMER                        10000
00040 
00041 /**
00042  * Maximum number of events for the event queue.
00043  * 10 is the safe number for the stack events, however, if application
00044  * also uses the queue for whatever purposes, this number should be increased.
00045  */
00046 #define MAX_NUMBER_OF_EVENTS            10
00047 
00048 /**
00049  * Maximum number of retries for CONFIRMED messages before giving up
00050  */
00051 #define CONFIRMED_MSG_RETRY_COUNTER     3
00052 
00053 /**
00054  * Dummy pin for dummy sensor
00055  */
00056 #define PC_9                            0
00057 
00058 /**
00059  * Dummy sensor class object
00060  */
00061 DS1820  ds1820(PC_9);
00062 
00063 /**
00064 * This event queue is the global event queue for both the
00065 * application and stack. To conserve memory, the stack is designed to run
00066 * in the same thread as the application and the application is responsible for
00067 * providing an event queue to the stack that will be used for ISR deferment as
00068 * well as application information event queuing.
00069 */
00070 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
00071 
00072 /**
00073  * Event handler.
00074  *
00075  * This will be passed to the LoRaWAN stack to queue events for the
00076  * application which in turn drive the application.
00077  */
00078 static void lora_event_handler(lorawan_event_t event);
00079 
00080 /**
00081  * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
00082  */
00083 static LoRaWANInterface lorawan(radio);
00084 
00085 /**
00086  * Application specific callbacks
00087  */
00088 static lorawan_app_callbacks_t callbacks;
00089 
00090 /**
00091  * Entry point for application
00092  */
00093 int main(void)
00094 {
00095     // setup tracing
00096     setup_trace();
00097 
00098     // stores the status of a call to LoRaWAN protocol
00099     lorawan_status_t retcode;
00100 
00101     // Initialize LoRaWAN stack
00102     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
00103         printf("\r\n LoRa initialization failed! \r\n");
00104         return -1;
00105     }
00106 
00107     printf("\r\n Mbed LoRaWANStack initialized \r\n");
00108 
00109     // prepare application callbacks
00110     callbacks.events = mbed::callback(lora_event_handler);
00111     lorawan.add_app_callbacks(&callbacks);
00112 
00113     // Set number of retries in case of CONFIRMED messages
00114     if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
00115             != LORAWAN_STATUS_OK) {
00116         printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
00117         return -1;
00118     }
00119 
00120     printf("\r\n CONFIRMED message retries : %d \r\n",
00121            CONFIRMED_MSG_RETRY_COUNTER);
00122 
00123     // Enable adaptive data rate
00124     if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
00125         printf("\r\n enable_adaptive_datarate failed! \r\n");
00126         return -1;
00127     }
00128 
00129     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
00130 
00131     retcode = lorawan.connect();
00132 
00133     if (retcode == LORAWAN_STATUS_OK ||
00134             retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
00135     } else {
00136         printf("\r\n Connection error, code = %d \r\n", retcode);
00137         return -1;
00138     }
00139 
00140     printf("\r\n Connection - In Progress ...\r\n");
00141 
00142     // make your event queue dispatching events forever
00143     ev_queue.dispatch_forever();
00144 
00145     return 0;
00146 }
00147 
00148 /**
00149  * Sends a message to the Network Server
00150  */
00151 static void send_message()
00152 {
00153     uint16_t packet_len;
00154     int16_t retcode;
00155     int32_t sensor_value;
00156 
00157     if (ds1820.begin()) {
00158         ds1820.startConversion();
00159         sensor_value = ds1820.read();
00160         printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
00161         ds1820.startConversion();
00162     } else {
00163         printf("\r\n No sensor found \r\n");
00164         return;
00165     }
00166 
00167     packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %d",
00168                          sensor_value);
00169 
00170     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
00171                            MSG_UNCONFIRMED_FLAG);
00172 
00173     if (retcode < 0) {
00174         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
00175         : printf("\r\n send() - Error code %d \r\n", retcode);
00176 
00177         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
00178             //retry in 3 seconds
00179             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00180                 ev_queue.call_in(3000, send_message);
00181             }
00182         }
00183         return;
00184     }
00185 
00186     printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
00187     memset(tx_buffer, 0, sizeof(tx_buffer));
00188 }
00189 
00190 /**
00191  * Receive a message from the Network Server
00192  */
00193 static void receive_message()
00194 {
00195     uint8_t port;
00196     int flags;
00197     int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
00198 
00199     if (retcode < 0) {
00200         printf("\r\n receive() - Error code %d \r\n", retcode);
00201         return;
00202     }
00203 
00204     printf(" RX Data on port %u (%d bytes): ", port, retcode);
00205     for (uint8_t i = 0; i < retcode; i++) {
00206         printf("%02x ", rx_buffer[i]);
00207     }
00208     printf("\r\n");
00209     
00210     memset(rx_buffer, 0, sizeof(rx_buffer));
00211 }
00212 
00213 /**
00214  * Event handler
00215  */
00216 static void lora_event_handler(lorawan_event_t event)
00217 {
00218     switch (event) {
00219         case CONNECTED:
00220             printf("\r\n Connection - Successful \r\n");
00221             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00222                 send_message();
00223             } else {
00224                 ev_queue.call_every(TX_TIMER, send_message);
00225             }
00226 
00227             break;
00228         case DISCONNECTED:
00229             ev_queue.break_dispatch();
00230             printf("\r\n Disconnected Successfully \r\n");
00231             break;
00232         case TX_DONE:
00233             printf("\r\n Message Sent to Network Server \r\n");
00234             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00235                 send_message();
00236             }
00237             break;
00238         case TX_TIMEOUT:
00239         case TX_ERROR:
00240         case TX_CRYPTO_ERROR:
00241         case TX_SCHEDULING_ERROR:
00242             printf("\r\n Transmission Error - EventCode = %d \r\n", event);
00243             // try again
00244             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00245                 send_message();
00246             }
00247             break;
00248         case RX_DONE:
00249             printf("\r\n Received message from Network Server \r\n");
00250             receive_message();
00251             break;
00252         case RX_TIMEOUT:
00253         case RX_ERROR:
00254             printf("\r\n Error in reception - Code = %d \r\n", event);
00255             break;
00256         case JOIN_FAILURE:
00257             printf("\r\n OTAA Failed - Check Keys \r\n");
00258             break;
00259         case UPLINK_REQUIRED:
00260             printf("\r\n Uplink required by NS \r\n");
00261             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00262                 send_message();
00263             }
00264             break;
00265         default:
00266             MBED_ASSERT("Unknown Event");
00267     }
00268 }
00269 
00270 // EOF