B-L072 ST Board BMP280 LoRaWAN end node
Dependencies: BME280
main.cpp
00001 #include <stdio.h> 00002 #include "BME280.h" 00003 00004 #include "lorawan/LoRaWANInterface.h" 00005 #include "lorawan/system/lorawan_data_structures.h" 00006 #include "events/EventQueue.h" 00007 00008 #include "lora_radio_helper.h" 00009 00010 using namespace events; 00011 00012 /* 00013 * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing 00014 */ 00015 #define TX_TIMER 10000 00016 00017 /** 00018 * Maximum number of events for the event queue. 00019 * 10 is the safe number for the stack events, however, if application 00020 * also uses the queue for whatever purposes, this number should be increased. 00021 */ 00022 #define MAX_NUMBER_OF_EVENTS 10 00023 00024 /** 00025 * Maximum number of retries for CONFIRMED messages before giving up 00026 */ 00027 #define CONFIRMED_MSG_RETRY_COUNTER 3 00028 00029 #define DEBUG 1 00030 #define BUFFER_SIZE 8 00031 00032 00033 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. 00034 // Use set_max_payload_length() function to change maximum payload capacity. 00035 00036 uint8_t tx_buffer[BUFFER_SIZE]; 00037 00038 BME280 sensor(PB_9, PB_8, 0x77); 00039 00040 uint16_t packet_len = BUFFER_SIZE; 00041 float sensor_values[2]; 00042 00043 00044 /** 00045 * This event queue is the global event queue for both the 00046 * application and stack. To conserve memory, the stack is designed to run 00047 * in the same thread as the application and the application is responsible for 00048 * providing an event queue to the stack that will be used for ISR deferment as 00049 * well as application information event queuing. 00050 */ 00051 00052 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); 00053 00054 /** 00055 * Event handler. 00056 * 00057 * This will be passed to the LoRaWAN stack to queue events for the 00058 * application which in turn drive the application. 00059 */ 00060 static void lora_event_handler(lorawan_event_t event); 00061 00062 /** 00063 * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. 00064 */ 00065 static LoRaWANInterface lorawan(radio); 00066 00067 /** 00068 * Application specific callbacks 00069 */ 00070 static lorawan_app_callbacks_t callbacks; 00071 static void send_message(); 00072 00073 00074 int main(void) 00075 { 00076 00077 // stores the status of a call to LoRaWAN protocol 00078 lorawan_status_t retcode; 00079 00080 // Initialize LoRaWAN stack 00081 if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { 00082 if(DEBUG) 00083 printf("\r\n LoRa initialization failed! \r\n"); 00084 return -1; 00085 } 00086 00087 if(DEBUG) 00088 printf("\r\n Mbed LoRaWANStack initialized \r\n"); 00089 00090 // prepare application callbacks 00091 callbacks.events = mbed::callback(lora_event_handler); 00092 lorawan.add_app_callbacks(&callbacks); 00093 00094 // Set number of retries in case of CONFIRMED messages 00095 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) 00096 != LORAWAN_STATUS_OK) { 00097 if(DEBUG) 00098 printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); 00099 return -1; 00100 } 00101 00102 if(DEBUG) 00103 printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER); 00104 00105 // Enable adaptive data rate 00106 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { 00107 if(DEBUG) 00108 printf("\r\n enable_adaptive_datarate failed! \r\n"); 00109 return -1; 00110 } 00111 00112 if(DEBUG) 00113 printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); 00114 00115 retcode = lorawan.connect(); 00116 00117 if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00118 } else { 00119 if(DEBUG) 00120 printf("\r\n Connection error, code = %d \r\n", retcode); 00121 return -1; 00122 } 00123 00124 if(DEBUG) 00125 printf("\r\n Connection - In Progress ...\r\n"); 00126 00127 00128 // make your event queue dispatching events forever 00129 ev_queue.dispatch_forever(); 00130 00131 return 0; 00132 } 00133 00134 /** 00135 * Sends a message to the Network Server 00136 */ 00137 static void send_message() 00138 { 00139 int16_t retcode; 00140 00141 sensor.trigger(); 00142 sensor_values[0] = sensor.getTemperature(); 00143 sensor_values[1] = sensor.getPressure(); 00144 //sensor_values[2] = sensor.getHumidity(); 00145 00146 memcpy(tx_buffer, sensor_values, packet_len); 00147 00148 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); 00149 00150 if (retcode < 0) { 00151 00152 if(DEBUG) 00153 retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") 00154 : printf("\r\n send() - Error code %d \r\n", retcode); 00155 00156 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { 00157 //retry in 3 seconds 00158 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00159 ev_queue.call_in(3000, send_message); 00160 } 00161 } 00162 return; 00163 } 00164 00165 if(DEBUG) 00166 printf("\r\n Temp: %f \r\n", sensor_values[0]); 00167 printf("\r\n Pressure: %f \r\n", sensor_values[1]); 00168 printf("\r\n txBuffer: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X \r\n", tx_buffer[0], tx_buffer[1], tx_buffer[2], tx_buffer[3], tx_buffer[4], tx_buffer[5], tx_buffer[6], tx_buffer[7]); 00169 printf("\r\n %d bytes scheduled for transmission \r\n", retcode); 00170 00171 //Check if radio wakes up automatically. If not, then call radio.TX(); 00172 radio.sleep(); 00173 } 00174 00175 /** 00176 * Receive a message from the Network Server 00177 */ 00178 static void receive_message() 00179 { 00180 00181 } 00182 00183 /** 00184 * Event handler 00185 */ 00186 static void lora_event_handler(lorawan_event_t event) 00187 { 00188 switch (event) { 00189 case CONNECTED: 00190 if(DEBUG) 00191 printf("\r\n Connection - Successful \r\n"); 00192 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00193 send_message(); 00194 } else { 00195 ev_queue.call_every(TX_TIMER, send_message); 00196 } 00197 00198 break; 00199 case DISCONNECTED: 00200 ev_queue.break_dispatch(); 00201 if(DEBUG) 00202 printf("\r\n Disconnected Successfully \r\n"); 00203 break; 00204 case TX_DONE: 00205 if(DEBUG) 00206 printf("\r\n Message Sent to Network Server \r\n"); 00207 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00208 send_message(); 00209 } 00210 break; 00211 case TX_TIMEOUT: 00212 case TX_ERROR: 00213 case TX_CRYPTO_ERROR: 00214 case TX_SCHEDULING_ERROR: 00215 if(DEBUG) 00216 printf("\r\n Transmission Error - EventCode = %d \r\n", event); 00217 // try again 00218 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00219 send_message(); 00220 } 00221 break; 00222 case RX_DONE: 00223 if(DEBUG) 00224 printf("\r\n Received message from Network Server \r\n"); 00225 receive_message(); 00226 break; 00227 case RX_TIMEOUT: 00228 case RX_ERROR: 00229 if(DEBUG) 00230 printf("\r\n Error in reception - Code = %d \r\n", event); 00231 break; 00232 case JOIN_FAILURE: 00233 if(DEBUG) 00234 printf("\r\n OTAA Failed - Check Keys \r\n"); 00235 break; 00236 case UPLINK_REQUIRED: 00237 if(DEBUG) 00238 printf("\r\n Uplink required by NS \r\n"); 00239 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00240 send_message(); 00241 } 00242 break; 00243 default: 00244 MBED_ASSERT("Unknown Event"); 00245 } 00246 } 00247 00248 // EOF
Generated on Wed Jul 13 2022 10:16:34 by 1.7.2