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: X_NUCLEO_IKS01A2
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 #include "XNucleoIKS01A2.h" 00023 // Application helpers 00024 #include "trace_helper.h" 00025 #include "lora_radio_helper.h" 00026 00027 using namespace events; 00028 00029 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. 00030 // This example only communicates with much shorter messages (<30 bytes). 00031 // If longer messages are used, these buffers must be changed accordingly. 00032 uint8_t tx_buffer[256]; 00033 uint8_t rx_buffer[256]; 00034 00035 /* 00036 * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing 00037 */ 00038 #define TX_TIMER 10000 00039 00040 /** 00041 * Maximum number of events for the event queue. 00042 * 10 is the safe number for the stack events, however, if application 00043 * also uses the queue for whatever purposes, this number should be increased. 00044 */ 00045 #define MAX_NUMBER_OF_EVENTS 10 00046 00047 /** 00048 * Maximum number of retries for CONFIRMED messages before giving up 00049 */ 00050 #define CONFIRMED_MSG_RETRY_COUNTER 3 00051 00052 00053 /** 00054 * This event queue is the global event queue for both the 00055 * application and stack. To conserve memory, the stack is designed to run 00056 * in the same thread as the application and the application is responsible for 00057 * providing an event queue to the stack that will be used for ISR deferment as 00058 * well as application information event queuing. 00059 */ 00060 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); 00061 00062 /** 00063 * Event handler. 00064 * 00065 * This will be passed to the LoRaWAN stack to queue events for the 00066 * application which in turn drive the application. 00067 */ 00068 static void lora_event_handler(lorawan_event_t event); 00069 00070 /** 00071 * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. 00072 */ 00073 static LoRaWANInterface lorawan(radio); 00074 00075 /** 00076 * Application specific callbacks 00077 */ 00078 static lorawan_app_callbacks_t callbacks; 00079 00080 00081 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(); 00082 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; 00083 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; 00084 /** 00085 * Entry point for application 00086 */ 00087 int main(void) 00088 { 00089 acc_gyro->enable_x(); 00090 acc_gyro->enable_g(); 00091 00092 // setup tracing 00093 setup_trace(); 00094 00095 // stores the status of a call to LoRaWAN protocol 00096 lorawan_status_t retcode; 00097 00098 // Initialize LoRaWAN stack 00099 if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { 00100 printf("\r\n LoRa initialization failed! \r\n"); 00101 return -1; 00102 } 00103 00104 printf("\r\n Mbed LoRaWANStack initialized \r\n"); 00105 00106 // prepare application callbacks 00107 callbacks.events = mbed::callback(lora_event_handler); 00108 lorawan.add_app_callbacks(&callbacks); 00109 00110 // Set number of retries in case of CONFIRMED messages 00111 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) 00112 != LORAWAN_STATUS_OK) { 00113 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); 00114 return -1; 00115 } 00116 00117 printf("\r\n CONFIRMED message retries : %d \r\n", 00118 CONFIRMED_MSG_RETRY_COUNTER); 00119 00120 // Enable adaptive data rate 00121 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { 00122 printf("\r\n enable_adaptive_datarate failed! \r\n"); 00123 return -1; 00124 } 00125 00126 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); 00127 00128 retcode = lorawan.connect(); 00129 00130 if (retcode == LORAWAN_STATUS_OK || 00131 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00132 } else { 00133 printf("\r\n Connection error, code = %d \r\n", retcode); 00134 return -1; 00135 } 00136 00137 printf("\r\n Connection - In Progress ...\r\n"); 00138 00139 // make your event queue dispatching events forever 00140 ev_queue.dispatch_forever(); 00141 00142 return 0; 00143 } 00144 00145 /** 00146 * Sends a message to the Network Server 00147 */ 00148 static void send_message() 00149 { 00150 uint16_t packet_len; 00151 int16_t retcode; 00152 float value; 00153 int32_t axes[3]; 00154 acc_gyro->get_x_axes(axes); 00155 printf("\n\rgyro:%d %d %d\n\r",axes[0],axes[1], axes[2]); 00156 hum_temp->get_temperature(&value); 00157 printf("\n\rtemp:%f\n\r",value); 00158 00159 00160 packet_len = sprintf((char *) tx_buffer, "\n\rgyro:%d %d %d\n\rn\rtemp:%f\n\r",axes[0],axes[1], axes[2], value); 00161 00162 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, 00163 MSG_UNCONFIRMED_FLAG); 00164 00165 if (retcode < 0) { 00166 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") 00167 : printf("\r\n send() - Error code %d \r\n", retcode); 00168 00169 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { 00170 //retry in 3 seconds 00171 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00172 ev_queue.call_in(3000, send_message); 00173 } 00174 } 00175 return; 00176 } 00177 00178 printf("\r\n %d bytes scheduled for transmission \r\n", retcode); 00179 memset(tx_buffer, 0, sizeof(tx_buffer)); 00180 } 00181 00182 /** 00183 * Event handler 00184 */ 00185 static void lora_event_handler(lorawan_event_t event) 00186 { 00187 switch (event) { 00188 case CONNECTED: 00189 printf("\r\n Connection - Successful \r\n"); 00190 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00191 send_message(); 00192 } else { 00193 ev_queue.call_every(TX_TIMER, send_message); 00194 } 00195 00196 break; 00197 case DISCONNECTED: 00198 ev_queue.break_dispatch(); 00199 printf("\r\n Disconnected Successfully \r\n"); 00200 break; 00201 case TX_DONE: 00202 printf("\r\n Message Sent to Network Server \r\n"); 00203 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00204 send_message(); 00205 } 00206 break; 00207 case TX_TIMEOUT: 00208 case TX_ERROR: 00209 case TX_CRYPTO_ERROR: 00210 case TX_SCHEDULING_ERROR: 00211 printf("\r\n Transmission Error - EventCode = %d \r\n", event); 00212 // try again 00213 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00214 send_message(); 00215 } 00216 break; 00217 case RX_DONE: 00218 printf("\r\n Received message from Network Server \r\n"); 00219 break; 00220 case RX_TIMEOUT: 00221 case RX_ERROR: 00222 printf("\r\n Error in reception - Code = %d \r\n", event); 00223 break; 00224 case JOIN_FAILURE: 00225 printf("\r\n OTAA Failed - Check Keys \r\n"); 00226 break; 00227 case UPLINK_REQUIRED: 00228 printf("\r\n Uplink required by NS \r\n"); 00229 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00230 send_message(); 00231 } 00232 break; 00233 default: 00234 MBED_ASSERT("Unknown Event"); 00235 } 00236 } 00237 00238 // EOF
Generated on Sat Jul 23 2022 16:20:49 by
1.7.2