frank dunn
/
example-ttn-workshop_c027
example for iot course with c027 and sx1276 module
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 <mbed.h> 00018 #include <stdio.h> 00019 00020 #include "lorawan/LoRaWANInterface.h" 00021 #include "lorawan/system/lorawan_data_structures.h" 00022 #include "events/EventQueue.h" 00023 00024 // Application helpers 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 /** 00055 * This event queue is the global event queue for both the 00056 * application and stack. To conserve memory, the stack is designed to run 00057 * in the same thread as the application and the application is responsible for 00058 * providing an event queue to the stack that will be used for ISR deferment as 00059 * well as application information event queuing. 00060 */ 00061 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); 00062 00063 /** 00064 * Event handler. 00065 * 00066 * This will be passed to the LoRaWAN stack to queue events for the 00067 * application which in turn drive the application. 00068 */ 00069 static void lora_event_handler(lorawan_event_t event); 00070 00071 /** 00072 * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. 00073 */ 00074 static LoRaWANInterface lorawan(radio); 00075 00076 /** 00077 * Application specific callbacks 00078 */ 00079 static lorawan_app_callbacks_t callbacks; 00080 00081 /** 00082 * Entry point for application 00083 */ 00084 int main(void) 00085 { 00086 DigitalOut resetPin(A0); 00087 resetPin = false ; 00088 00089 // setup tracing 00090 setup_trace(); 00091 00092 // stores the status of a call to LoRaWAN protocol 00093 lorawan_status_t retcode; 00094 00095 // Initialize LoRaWAN stack 00096 if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { 00097 printf("\r\n LoRa initialization failed! \r\n"); 00098 return -1; 00099 } 00100 00101 printf("\r\n Mbed LoRaWANStack initialized \r\n"); 00102 00103 // prepare application callbacks 00104 callbacks.events = mbed::callback(lora_event_handler); 00105 lorawan.add_app_callbacks(&callbacks); 00106 00107 // Set number of retries in case of CONFIRMED messages 00108 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) 00109 != LORAWAN_STATUS_OK) { 00110 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); 00111 return -1; 00112 } 00113 00114 printf("\r\n CONFIRMED message retries : %d \r\n", 00115 CONFIRMED_MSG_RETRY_COUNTER); 00116 00117 // Enable adaptive data rate 00118 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { 00119 printf("\r\n enable_adaptive_datarate failed! \r\n"); 00120 return -1; 00121 } 00122 00123 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); 00124 00125 retcode = lorawan.connect(); 00126 00127 if (retcode == LORAWAN_STATUS_OK || 00128 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00129 } else { 00130 printf("\r\n Connection error, code = %d \r\n", retcode); 00131 return -1; 00132 } 00133 00134 printf("\r\n Connection - In Progress ...\r\n"); 00135 00136 // make your event queue dispatching events forever 00137 ev_queue.dispatch_forever(); 00138 00139 return 0; 00140 } 00141 00142 /** 00143 * Sends a message to the Network Server 00144 */ 00145 static void send_message() 00146 { 00147 uint16_t packet_len; 00148 int16_t retcode; 00149 printf("\n\rHello world"); 00150 00151 packet_len = sprintf((char *) tx_buffer, "Hello world"); 00152 00153 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, 00154 MSG_UNCONFIRMED_FLAG); 00155 00156 if (retcode < 0) { 00157 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") 00158 : printf("\r\n send() - Error code %d \r\n", retcode); 00159 00160 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { 00161 //retry in 3 seconds 00162 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00163 ev_queue.call_in(3000, send_message); 00164 } 00165 } 00166 return; 00167 } 00168 00169 printf("\r\n %d bytes scheduled for transmission \r\n", retcode); 00170 memset(tx_buffer, 0, sizeof(tx_buffer)); 00171 } 00172 00173 /** 00174 * Event handler 00175 */ 00176 static void lora_event_handler(lorawan_event_t event) 00177 { 00178 switch (event) { 00179 case CONNECTED: 00180 printf("\r\n Connection - Successful \r\n"); 00181 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00182 send_message(); 00183 } else { 00184 ev_queue.call_every(TX_TIMER, send_message); 00185 } 00186 00187 break; 00188 case DISCONNECTED: 00189 ev_queue.break_dispatch(); 00190 printf("\r\n Disconnected Successfully \r\n"); 00191 break; 00192 case TX_DONE: 00193 printf("\r\n Message Sent to Network Server \r\n"); 00194 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00195 send_message(); 00196 } 00197 break; 00198 case TX_TIMEOUT: 00199 case TX_ERROR: 00200 case TX_CRYPTO_ERROR: 00201 case TX_SCHEDULING_ERROR: 00202 printf("\r\n Transmission Error - EventCode = %d \r\n", event); 00203 // try again 00204 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00205 send_message(); 00206 } 00207 break; 00208 case RX_DONE: 00209 printf("\r\n Received message from Network Server \r\n"); 00210 break; 00211 case RX_TIMEOUT: 00212 case RX_ERROR: 00213 printf("\r\n Error in reception - Code = %d \r\n", event); 00214 break; 00215 case JOIN_FAILURE: 00216 printf("\r\n OTAA Failed - Check Keys \r\n"); 00217 break; 00218 case UPLINK_REQUIRED: 00219 printf("\r\n Uplink required by NS \r\n"); 00220 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00221 send_message(); 00222 } 00223 break; 00224 default: 00225 MBED_ASSERT("Unknown Event"); 00226 } 00227 } 00228 00229 // EOF
Generated on Thu Oct 20 2022 00:00:10 by 1.7.2