Nuvoton
/
NuMaker-mbed-OS-5-lorawan
NuMaker mbed OS v5.x LoRaWAN
Embed:
(wiki syntax)
Show/hide line numbers
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 #ifdef MBED_MAJOR_VERSION 00096 printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); 00097 #endif 00098 // setup tracing 00099 setup_trace(); 00100 00101 // stores the status of a call to LoRaWAN protocol 00102 lorawan_status_t retcode; 00103 00104 // Initialize LoRaWAN stack 00105 if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { 00106 printf("\r\n LoRa initialization failed! \r\n"); 00107 return -1; 00108 } 00109 00110 printf("\r\n Mbed LoRaWANStack initialized \r\n"); 00111 00112 // prepare application callbacks 00113 callbacks.events = mbed::callback(lora_event_handler); 00114 lorawan.add_app_callbacks(&callbacks); 00115 00116 // Set number of retries in case of CONFIRMED messages 00117 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) 00118 != LORAWAN_STATUS_OK) { 00119 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); 00120 return -1; 00121 } 00122 00123 printf("\r\n CONFIRMED message retries : %d \r\n", 00124 CONFIRMED_MSG_RETRY_COUNTER); 00125 00126 // Enable adaptive data rate 00127 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { 00128 printf("\r\n enable_adaptive_datarate failed! \r\n"); 00129 return -1; 00130 } 00131 00132 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); 00133 00134 retcode = lorawan.connect(); 00135 00136 if (retcode == LORAWAN_STATUS_OK || 00137 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00138 } else { 00139 printf("\r\n Connection error, code = %d \r\n", retcode); 00140 return -1; 00141 } 00142 00143 printf("\r\n Connection - In Progress ...\r\n"); 00144 00145 // make your event queue dispatching events forever 00146 ev_queue.dispatch_forever(); 00147 00148 return 0; 00149 } 00150 00151 /** 00152 * Sends a message to the Network Server 00153 */ 00154 static void send_message() 00155 { 00156 uint16_t packet_len; 00157 int16_t retcode; 00158 int32_t sensor_value; 00159 00160 if (ds1820.begin()) { 00161 ds1820.startConversion(); 00162 sensor_value = ds1820.read(); 00163 printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value); 00164 ds1820.startConversion(); 00165 } else { 00166 printf("\r\n No sensor found \r\n"); 00167 return; 00168 } 00169 00170 packet_len = sprintf((char *) tx_buffer, "Val: %d", 00171 sensor_value); 00172 00173 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, 00174 MSG_UNCONFIRMED_FLAG); 00175 00176 if (retcode < 0) { 00177 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") 00178 : printf("\r\n send() - Error code %d \r\n", retcode); 00179 00180 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { 00181 //retry in 3 seconds 00182 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00183 ev_queue.call_in(3000, send_message); 00184 } 00185 } 00186 return; 00187 } 00188 00189 printf("\r\n %d bytes scheduled for transmission \r\n", retcode); 00190 memset(tx_buffer, 0, sizeof(tx_buffer)); 00191 } 00192 00193 /** 00194 * Receive a message from the Network Server 00195 */ 00196 static void receive_message() 00197 { 00198 uint8_t port; 00199 int flags; 00200 int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); 00201 00202 if (retcode < 0) { 00203 printf("\r\n receive() - Error code %d \r\n", retcode); 00204 return; 00205 } 00206 00207 printf(" RX Data on port %u (%d bytes): ", port, retcode); 00208 for (uint8_t i = 0; i < retcode; i++) { 00209 printf("%02x ", rx_buffer[i]); 00210 } 00211 printf("\r\n"); 00212 00213 memset(rx_buffer, 0, sizeof(rx_buffer)); 00214 } 00215 00216 /** 00217 * Event handler 00218 */ 00219 static void lora_event_handler(lorawan_event_t event) 00220 { 00221 switch (event) { 00222 case CONNECTED: 00223 printf("\r\n Connection - Successful \r\n"); 00224 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00225 send_message(); 00226 } else { 00227 ev_queue.call_every(TX_TIMER, send_message); 00228 } 00229 00230 break; 00231 case DISCONNECTED: 00232 ev_queue.break_dispatch(); 00233 printf("\r\n Disconnected Successfully \r\n"); 00234 break; 00235 case TX_DONE: 00236 printf("\r\n Message Sent to Network Server \r\n"); 00237 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00238 send_message(); 00239 } 00240 break; 00241 case TX_TIMEOUT: 00242 case TX_ERROR: 00243 case TX_CRYPTO_ERROR: 00244 case TX_SCHEDULING_ERROR: 00245 printf("\r\n Transmission Error - EventCode = %d \r\n", event); 00246 // try again 00247 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00248 send_message(); 00249 } 00250 break; 00251 case RX_DONE: 00252 printf("\r\n Received message from Network Server \r\n"); 00253 receive_message(); 00254 break; 00255 case RX_TIMEOUT: 00256 case RX_ERROR: 00257 printf("\r\n Error in reception - Code = %d \r\n", event); 00258 break; 00259 case JOIN_FAILURE: 00260 printf("\r\n OTAA Failed - Check Keys \r\n"); 00261 break; 00262 case UPLINK_REQUIRED: 00263 printf("\r\n Uplink required by NS \r\n"); 00264 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00265 send_message(); 00266 } 00267 break; 00268 default: 00269 MBED_ASSERT("Unknown Event"); 00270 } 00271 } 00272 00273 // EOF
Generated on Thu Jul 14 2022 14:11:55 by 1.7.2