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.
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 #include "mbed.h" 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 DigitalOut myled1(LED1); 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 9000 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 printf("\r\n Mbed LoRaWANStack initialized \r\n"); 00107 00108 // prepare application callbacks 00109 callbacks.events = mbed::callback(lora_event_handler); 00110 lorawan.add_app_callbacks(&callbacks); 00111 00112 // Set number of retries in case of CONFIRMED messages 00113 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) 00114 != LORAWAN_STATUS_OK) { 00115 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); 00116 return -1; 00117 } 00118 00119 printf("\r\n CONFIRMED message retries : %d \r\n", 00120 CONFIRMED_MSG_RETRY_COUNTER); 00121 00122 // Enable adaptive data rate 00123 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { 00124 printf("\r\n enable_adaptive_datarate failed! \r\n"); 00125 return -1; 00126 } 00127 00128 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); 00129 00130 retcode = lorawan.connect(); 00131 00132 if (retcode == LORAWAN_STATUS_OK || 00133 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00134 } else { 00135 printf("\r\n Connection error, code = %d \r\n", retcode); 00136 return -1; 00137 } 00138 00139 printf("\r\n Connection - In Progress ...\r\n"); 00140 00141 // make your event queue dispatching events forever 00142 ev_queue.dispatch_forever(); 00143 00144 return 0; 00145 } 00146 00147 /** 00148 * Sends a message to the Network Server 00149 */ 00150 static void send_message() 00151 { 00152 uint16_t packet_len; 00153 int16_t retcode; 00154 int32_t sensor_value; 00155 00156 if (ds1820.begin()) { 00157 ds1820.startConversion(); 00158 sensor_value = ds1820.read(); 00159 printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value); 00160 ds1820.startConversion(); 00161 } else { 00162 printf("\r\n No sensor found \r\n"); 00163 return; 00164 } 00165 00166 packet_len = sprintf((char *) tx_buffer, "Sensor Value is %d", 00167 sensor_value); 00168 00169 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, 00170 MSG_CONFIRMED_FLAG); 00171 00172 if (retcode < 0) { 00173 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") 00174 : printf("\r\n send() - Error code %d \r\n", retcode); 00175 00176 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { 00177 //retry in 3 seconds 00178 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00179 ev_queue.call_in(3000, send_message); 00180 } 00181 } 00182 return; 00183 } 00184 00185 printf("\r\n %d bytes scheduled for transmission \r\n", retcode); 00186 memset(tx_buffer, 0, sizeof(tx_buffer)); 00187 } 00188 00189 /** 00190 * Receive a message from the Network Server 00191 */ 00192 static void receive_message() 00193 { 00194 uint8_t port; 00195 int flags; 00196 int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); 00197 00198 if (retcode < 0) { 00199 printf("\r\n receive() - Error code %d \r\n", retcode); 00200 return; 00201 } 00202 00203 printf(" RX Data on port %u (%d bytes): ", port, retcode); 00204 for (uint8_t i = 0; i < retcode; i++) { 00205 printf("%02x ", rx_buffer[i]); 00206 } 00207 printf("\r\n"); 00208 00209 memset(rx_buffer, 0, sizeof(rx_buffer)); 00210 } 00211 00212 /** 00213 * Event handler 00214 */ 00215 static void lora_event_handler(lorawan_event_t event) 00216 { 00217 switch (event) { 00218 case CONNECTED: 00219 printf("\r\n Connection - Successful \r\n"); 00220 myled1 = 1; 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
Generated on Wed Jul 20 2022 07:29:46 by
