contain lorawan with serial_rx enabled
Dependencies: pulga-lorawan-drv SPI_MX25R Si1133 BME280
main.cpp@61:65744bc8ab55, 2021-01-08 (annotated)
- Committer:
- brunnobbco
- Date:
- Fri Jan 08 20:16:58 2021 +0000
- Revision:
- 61:65744bc8ab55
- Parent:
- 60:c4f9e9202fb4
- Child:
- 62:89df9529dbb0
Lorawan, GPS, Sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7037ed05f54f | 1 | /** |
mbed_official | 0:7037ed05f54f | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
mbed_official | 0:7037ed05f54f | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:7037ed05f54f | 4 | * |
mbed_official | 0:7037ed05f54f | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:7037ed05f54f | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 0:7037ed05f54f | 7 | * You may obtain a copy of the License at |
mbed_official | 0:7037ed05f54f | 8 | * |
mbed_official | 0:7037ed05f54f | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:7037ed05f54f | 10 | * |
mbed_official | 0:7037ed05f54f | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:7037ed05f54f | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:7037ed05f54f | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:7037ed05f54f | 14 | * See the License for the specific language governing permissions and |
mbed_official | 0:7037ed05f54f | 15 | * limitations under the License. |
mbed_official | 0:7037ed05f54f | 16 | */ |
mbed_official | 0:7037ed05f54f | 17 | #include <stdio.h> |
mbed_official | 3:8c7198d1a2a1 | 18 | |
mbed_official | 0:7037ed05f54f | 19 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 20 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 21 | #include "events/EventQueue.h" |
mbed_official | 0:7037ed05f54f | 22 | |
mbed_official | 0:7037ed05f54f | 23 | // Application helpers |
pancotinho | 59:a4fc1efb1569 | 24 | //#include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 25 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 26 | #include "lora_radio_helper.h" |
brunnobbco | 61:65744bc8ab55 | 27 | //#include "BME280.h" |
mbed_official | 0:7037ed05f54f | 28 | |
mbed_official | 0:7037ed05f54f | 29 | using namespace events; |
mbed_official | 0:7037ed05f54f | 30 | |
mbed_official | 12:5015dfead3f2 | 31 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 32 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 33 | // If longer messages are used, these buffers must be changed accordingly. |
brunnobbco | 61:65744bc8ab55 | 34 | uint8_t tx_buffer[256]; |
mbed_official | 12:5015dfead3f2 | 35 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 36 | |
mbed_official | 0:7037ed05f54f | 37 | /* |
mbed_official | 0:7037ed05f54f | 38 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 39 | */ |
mbed_official | 0:7037ed05f54f | 40 | #define TX_TIMER 10000 |
mbed_official | 0:7037ed05f54f | 41 | |
mbed_official | 0:7037ed05f54f | 42 | /** |
mbed_official | 0:7037ed05f54f | 43 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 44 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 45 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 46 | */ |
mbed_official | 12:5015dfead3f2 | 47 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 48 | |
mbed_official | 0:7037ed05f54f | 49 | /** |
mbed_official | 0:7037ed05f54f | 50 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 51 | */ |
mbed_official | 0:7037ed05f54f | 52 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 53 | |
mbed_official | 0:7037ed05f54f | 54 | /** |
mbed_official | 0:7037ed05f54f | 55 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 56 | */ |
pancotinho | 59:a4fc1efb1569 | 57 | //#define PC_9 0 |
mbed_official | 0:7037ed05f54f | 58 | |
mbed_official | 0:7037ed05f54f | 59 | /** |
mbed_official | 0:7037ed05f54f | 60 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 61 | */ |
pancotinho | 59:a4fc1efb1569 | 62 | //DS1820 ds1820(PC_9); |
brunnobbco | 60:c4f9e9202fb4 | 63 | //BME280 sensor_amb(P0_13, P0_15, 0x77 << 1) ; |
pancotinho | 59:a4fc1efb1569 | 64 | |
pancotinho | 59:a4fc1efb1569 | 65 | /** |
pancotinho | 59:a4fc1efb1569 | 66 | * Sensors Variables |
pancotinho | 59:a4fc1efb1569 | 67 | */ |
brunnobbco | 61:65744bc8ab55 | 68 | // uint32_t lux = 0; |
brunnobbco | 61:65744bc8ab55 | 69 | // uint32_t amb = 0; |
brunnobbco | 61:65744bc8ab55 | 70 | // float sensor_get = 0; |
mbed_official | 0:7037ed05f54f | 71 | |
mbed_official | 0:7037ed05f54f | 72 | /** |
mbed_official | 0:7037ed05f54f | 73 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 74 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 75 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 76 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 77 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 78 | */ |
mbed_official | 46:a124538e2891 | 79 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 80 | |
mbed_official | 0:7037ed05f54f | 81 | /** |
mbed_official | 0:7037ed05f54f | 82 | * Event handler. |
mbed_official | 0:7037ed05f54f | 83 | * |
mbed_official | 0:7037ed05f54f | 84 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 85 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 86 | */ |
mbed_official | 0:7037ed05f54f | 87 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 88 | |
mbed_official | 0:7037ed05f54f | 89 | /** |
mbed_official | 46:a124538e2891 | 90 | * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. |
mbed_official | 0:7037ed05f54f | 91 | */ |
mbed_official | 2:dc95ac6d6d4e | 92 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 93 | |
mbed_official | 0:7037ed05f54f | 94 | /** |
mbed_official | 0:7037ed05f54f | 95 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 96 | */ |
mbed_official | 0:7037ed05f54f | 97 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 98 | |
mbed_official | 0:7037ed05f54f | 99 | /** |
mbed_official | 0:7037ed05f54f | 100 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 101 | */ |
pancotinho | 59:a4fc1efb1569 | 102 | |
brunnobbco | 60:c4f9e9202fb4 | 103 | mbed::DigitalOut _alive_led(P1_13, 0); |
brunnobbco | 60:c4f9e9202fb4 | 104 | mbed::DigitalOut _actuated_led(P1_14,1); |
brunnobbco | 61:65744bc8ab55 | 105 | int lat=0; |
brunnobbco | 61:65744bc8ab55 | 106 | int lon=0; |
brunnobbco | 61:65744bc8ab55 | 107 | int latitude=0; |
brunnobbco | 61:65744bc8ab55 | 108 | int longitude=0; |
brunnobbco | 61:65744bc8ab55 | 109 | |
brunnobbco | 61:65744bc8ab55 | 110 | |
brunnobbco | 61:65744bc8ab55 | 111 | //Temperature, Pressure, Humidity Sensor |
brunnobbco | 61:65744bc8ab55 | 112 | #include "BME280.txt" |
brunnobbco | 61:65744bc8ab55 | 113 | #include "BMX160.txt" |
brunnobbco | 61:65744bc8ab55 | 114 | #include "gps.txt" |
brunnobbco | 61:65744bc8ab55 | 115 | |
brunnobbco | 61:65744bc8ab55 | 116 | void BMX160Read (void) |
brunnobbco | 61:65744bc8ab55 | 117 | { |
brunnobbco | 61:65744bc8ab55 | 118 | /*Le os Registradores do Acelerometro*/ |
brunnobbco | 61:65744bc8ab55 | 119 | i2c_reg_buffer[0] = 0x12; |
brunnobbco | 61:65744bc8ab55 | 120 | i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); |
brunnobbco | 61:65744bc8ab55 | 121 | i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 122 | |
brunnobbco | 61:65744bc8ab55 | 123 | /*Le os Registradores do Giroscopio*/ |
brunnobbco | 61:65744bc8ab55 | 124 | i2c_reg_buffer[0] = 0x0C; |
brunnobbco | 61:65744bc8ab55 | 125 | i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); |
brunnobbco | 61:65744bc8ab55 | 126 | i2c.read(BMI160_ADDR, (char *)&gyr_sample_buffer, sizeof(gyr_sample_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 127 | |
brunnobbco | 61:65744bc8ab55 | 128 | /*Ajusta dados brutos Acelerometro em unidades de g */ |
brunnobbco | 61:65744bc8ab55 | 129 | acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0); |
brunnobbco | 61:65744bc8ab55 | 130 | acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0); |
brunnobbco | 61:65744bc8ab55 | 131 | acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0); |
brunnobbco | 61:65744bc8ab55 | 132 | |
brunnobbco | 61:65744bc8ab55 | 133 | /*Ajusta dados Brutos do Giroscopio em unidades de deg/s */ |
brunnobbco | 61:65744bc8ab55 | 134 | gyr_result_buffer[0] = (gyr_sample_buffer[0]/131.2); |
brunnobbco | 61:65744bc8ab55 | 135 | gyr_result_buffer[1] = (gyr_sample_buffer[1]/131.2); |
brunnobbco | 61:65744bc8ab55 | 136 | |
brunnobbco | 61:65744bc8ab55 | 137 | /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/ |
brunnobbco | 61:65744bc8ab55 | 138 | accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; |
brunnobbco | 61:65744bc8ab55 | 139 | accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; |
brunnobbco | 61:65744bc8ab55 | 140 | |
brunnobbco | 61:65744bc8ab55 | 141 | /*Calcula os Angulos de Rotacao com valor do Giroscopio e aplica filtro complementar realizando a fusao*/ |
brunnobbco | 61:65744bc8ab55 | 142 | tiltx = (0.98*(tiltx_prev+(gyr_result_buffer[0]*0.001)))+(0.02*(accel_ang_x)); |
brunnobbco | 61:65744bc8ab55 | 143 | tilty = (0.98*(tilty_prev+(gyr_result_buffer[1]*0.001)))+(0.02*(accel_ang_y)); |
brunnobbco | 61:65744bc8ab55 | 144 | |
brunnobbco | 61:65744bc8ab55 | 145 | tiltx_prev = tiltx; |
brunnobbco | 61:65744bc8ab55 | 146 | tilty_prev = tilty; |
brunnobbco | 61:65744bc8ab55 | 147 | |
brunnobbco | 61:65744bc8ab55 | 148 | /*Imprime os dados ACC pre-formatados*/ |
brunnobbco | 61:65744bc8ab55 | 149 | printf("%.3f,%.3f;",tiltx, tilty); |
brunnobbco | 61:65744bc8ab55 | 150 | |
brunnobbco | 61:65744bc8ab55 | 151 | } |
brunnobbco | 61:65744bc8ab55 | 152 | |
brunnobbco | 61:65744bc8ab55 | 153 | void GPS_Read(void) |
brunnobbco | 61:65744bc8ab55 | 154 | { |
brunnobbco | 61:65744bc8ab55 | 155 | gps_print_local(); |
brunnobbco | 61:65744bc8ab55 | 156 | printf ("gps longitude=%d \n",lon); |
brunnobbco | 61:65744bc8ab55 | 157 | printf ("gps latitude=%d \n",lat); |
brunnobbco | 61:65744bc8ab55 | 158 | if(lat!=0 && lon!=0){ |
brunnobbco | 61:65744bc8ab55 | 159 | longitude=lon; |
brunnobbco | 61:65744bc8ab55 | 160 | latitude=lat; |
brunnobbco | 61:65744bc8ab55 | 161 | // led1 = !led1; |
brunnobbco | 61:65744bc8ab55 | 162 | } |
brunnobbco | 61:65744bc8ab55 | 163 | } |
pancotinho | 59:a4fc1efb1569 | 164 | |
mbed_official | 46:a124538e2891 | 165 | int main(void) |
mbed_official | 0:7037ed05f54f | 166 | { |
brunnobbco | 61:65744bc8ab55 | 167 | gps_config(); |
brunnobbco | 61:65744bc8ab55 | 168 | gps_leBootMsg(); |
brunnobbco | 61:65744bc8ab55 | 169 | gps_config_gnss (); |
brunnobbco | 61:65744bc8ab55 | 170 | init(); |
brunnobbco | 61:65744bc8ab55 | 171 | |
brunnobbco | 61:65744bc8ab55 | 172 | //BMX160 Declaration###################################### |
brunnobbco | 61:65744bc8ab55 | 173 | // pc.printf("Teste BMI160\n\r"); |
brunnobbco | 61:65744bc8ab55 | 174 | // printf("Configurando BMX160...\n\r"); |
brunnobbco | 61:65744bc8ab55 | 175 | wait_ms(250); |
brunnobbco | 61:65744bc8ab55 | 176 | |
brunnobbco | 61:65744bc8ab55 | 177 | /*Config Freq. I2C Bus*/ |
brunnobbco | 61:65744bc8ab55 | 178 | i2c.frequency(20000); |
brunnobbco | 61:65744bc8ab55 | 179 | |
brunnobbco | 61:65744bc8ab55 | 180 | /*Reset BMI160*/ |
brunnobbco | 61:65744bc8ab55 | 181 | i2c_reg_buffer[0] = 0x7E; |
brunnobbco | 61:65744bc8ab55 | 182 | i2c_reg_buffer[1] = 0xB6; |
brunnobbco | 61:65744bc8ab55 | 183 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 184 | wait_ms(200); |
brunnobbco | 61:65744bc8ab55 | 185 | // printf("BMI160 Resetado\n\r"); |
brunnobbco | 61:65744bc8ab55 | 186 | |
brunnobbco | 61:65744bc8ab55 | 187 | /*Habilita o Acelerometro*/ |
brunnobbco | 61:65744bc8ab55 | 188 | i2c_reg_buffer[0] = 0x7E; |
brunnobbco | 61:65744bc8ab55 | 189 | i2c_reg_buffer[1] = 0x11; //PMU Normal |
brunnobbco | 61:65744bc8ab55 | 190 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 191 | // printf("Acc Habilitado\n\r"); |
brunnobbco | 61:65744bc8ab55 | 192 | |
brunnobbco | 61:65744bc8ab55 | 193 | /*Habilita o Giroscopio*/ |
brunnobbco | 61:65744bc8ab55 | 194 | i2c_reg_buffer[0] = 0x7E; |
brunnobbco | 61:65744bc8ab55 | 195 | i2c_reg_buffer[1] = 0x15; //PMU Normal |
brunnobbco | 61:65744bc8ab55 | 196 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 197 | // printf("Gyr Habilitado\n\r"); |
brunnobbco | 61:65744bc8ab55 | 198 | |
brunnobbco | 61:65744bc8ab55 | 199 | /*Config o Data Rate ACC em 1600Hz*/ |
brunnobbco | 61:65744bc8ab55 | 200 | i2c_reg_buffer[0] = 0x40; |
brunnobbco | 61:65744bc8ab55 | 201 | i2c_reg_buffer[1] = 0x2C; |
brunnobbco | 61:65744bc8ab55 | 202 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 203 | // printf("Data Rate ACC Selecionado a 1600Hz\n\r"); |
brunnobbco | 61:65744bc8ab55 | 204 | |
brunnobbco | 61:65744bc8ab55 | 205 | /*Config o Data Rate GYR em 1600Hz*/ |
brunnobbco | 61:65744bc8ab55 | 206 | i2c_reg_buffer[0] = 0x42; |
brunnobbco | 61:65744bc8ab55 | 207 | i2c_reg_buffer[1] = 0x2C; |
brunnobbco | 61:65744bc8ab55 | 208 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 209 | // printf("Data Rate GYR Selecionado a 1600Hz\n\r"); |
brunnobbco | 61:65744bc8ab55 | 210 | |
brunnobbco | 61:65744bc8ab55 | 211 | /*Config o Range GYR em 250º/s*/ |
brunnobbco | 61:65744bc8ab55 | 212 | i2c_reg_buffer[0] = 0x43; |
brunnobbco | 61:65744bc8ab55 | 213 | i2c_reg_buffer[1] = 0x03; |
brunnobbco | 61:65744bc8ab55 | 214 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
brunnobbco | 61:65744bc8ab55 | 215 | // printf("Range GYR Selecionado a 250deg/s\n\r"); |
brunnobbco | 61:65744bc8ab55 | 216 | |
brunnobbco | 61:65744bc8ab55 | 217 | printf("BMX160 Configurado\n\r"); |
brunnobbco | 61:65744bc8ab55 | 218 | |
brunnobbco | 61:65744bc8ab55 | 219 | //######################################################## |
mbed_official | 0:7037ed05f54f | 220 | // setup tracing |
mbed_official | 0:7037ed05f54f | 221 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 222 | |
mbed_official | 0:7037ed05f54f | 223 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 224 | lorawan_status_t retcode; |
mbed_official | 0:7037ed05f54f | 225 | |
mbed_official | 0:7037ed05f54f | 226 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 227 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 228 | printf("\r\n LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 229 | return -1; |
mbed_official | 0:7037ed05f54f | 230 | } |
mbed_official | 0:7037ed05f54f | 231 | |
mbed_official | 0:7037ed05f54f | 232 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
mbed_official | 0:7037ed05f54f | 233 | |
mbed_official | 0:7037ed05f54f | 234 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 235 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 236 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 237 | |
mbed_official | 0:7037ed05f54f | 238 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 239 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 46:a124538e2891 | 240 | != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 241 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 242 | return -1; |
mbed_official | 0:7037ed05f54f | 243 | } |
mbed_official | 0:7037ed05f54f | 244 | |
mbed_official | 0:7037ed05f54f | 245 | printf("\r\n CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 246 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 247 | |
mbed_official | 0:7037ed05f54f | 248 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 249 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 250 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 251 | return -1; |
mbed_official | 0:7037ed05f54f | 252 | } |
mbed_official | 0:7037ed05f54f | 253 | |
mbed_official | 0:7037ed05f54f | 254 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 255 | |
mbed_official | 2:dc95ac6d6d4e | 256 | retcode = lorawan.connect(); |
mbed_official | 0:7037ed05f54f | 257 | |
mbed_official | 0:7037ed05f54f | 258 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 46:a124538e2891 | 259 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 260 | } else { |
mbed_official | 0:7037ed05f54f | 261 | printf("\r\n Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 262 | return -1; |
mbed_official | 0:7037ed05f54f | 263 | } |
mbed_official | 0:7037ed05f54f | 264 | |
mbed_official | 0:7037ed05f54f | 265 | printf("\r\n Connection - In Progress ...\r\n"); |
pancotinho | 59:a4fc1efb1569 | 266 | |
brunnobbco | 60:c4f9e9202fb4 | 267 | _actuated_led =0; |
mbed_official | 0:7037ed05f54f | 268 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 269 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 270 | |
mbed_official | 3:8c7198d1a2a1 | 271 | return 0; |
mbed_official | 0:7037ed05f54f | 272 | } |
mbed_official | 0:7037ed05f54f | 273 | |
mbed_official | 0:7037ed05f54f | 274 | /** |
mbed_official | 0:7037ed05f54f | 275 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 276 | */ |
mbed_official | 0:7037ed05f54f | 277 | static void send_message() |
mbed_official | 0:7037ed05f54f | 278 | { |
mbed_official | 0:7037ed05f54f | 279 | uint16_t packet_len; |
mbed_official | 0:7037ed05f54f | 280 | int16_t retcode; |
mbed_official | 56:39847849d219 | 281 | int32_t sensor_value; |
brunnobbco | 61:65744bc8ab55 | 282 | gps_print_local(); |
pancotinho | 59:a4fc1efb1569 | 283 | |
brunnobbco | 61:65744bc8ab55 | 284 | packet_len = sprintf((char *) tx_buffer, "%2.2f, %04.2f, %2.2f, %d, %d\n", getTemperature(), getPressure(), getHumidity(), lon, lat); |
mbed_official | 0:7037ed05f54f | 285 | |
mbed_official | 2:dc95ac6d6d4e | 286 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, |
mbed_official | 47:b6d132f1079f | 287 | MSG_UNCONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 288 | |
mbed_official | 0:7037ed05f54f | 289 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 290 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
mbed_official | 46:a124538e2891 | 291 | : printf("\r\n send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 292 | |
mbed_official | 26:f07f5febf97f | 293 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 294 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 295 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
brunnobbco | 61:65744bc8ab55 | 296 | ev_queue.call_in(10000, send_message); |
mbed_official | 26:f07f5febf97f | 297 | } |
mbed_official | 26:f07f5febf97f | 298 | } |
mbed_official | 0:7037ed05f54f | 299 | return; |
mbed_official | 0:7037ed05f54f | 300 | } |
mbed_official | 0:7037ed05f54f | 301 | |
brunnobbco | 61:65744bc8ab55 | 302 | printf("%2.2f;%04.2f;%2.2f;", getTemperature(), getPressure(), getHumidity()); |
brunnobbco | 61:65744bc8ab55 | 303 | BMX160Read(); |
brunnobbco | 61:65744bc8ab55 | 304 | printf ("%d;",lon); |
brunnobbco | 61:65744bc8ab55 | 305 | printf ("%d \r\n",lat); |
brunnobbco | 61:65744bc8ab55 | 306 | // printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
mbed_official | 12:5015dfead3f2 | 307 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 308 | } |
mbed_official | 0:7037ed05f54f | 309 | |
mbed_official | 0:7037ed05f54f | 310 | /** |
mbed_official | 0:7037ed05f54f | 311 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 312 | */ |
mbed_official | 0:7037ed05f54f | 313 | static void receive_message() |
mbed_official | 0:7037ed05f54f | 314 | { |
mbed_official | 47:b6d132f1079f | 315 | uint8_t port; |
mbed_official | 47:b6d132f1079f | 316 | int flags; |
mbed_official | 47:b6d132f1079f | 317 | int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); |
mbed_official | 0:7037ed05f54f | 318 | |
mbed_official | 0:7037ed05f54f | 319 | if (retcode < 0) { |
brunnobbco | 61:65744bc8ab55 | 320 | // printf("\r\n receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 321 | return; |
mbed_official | 0:7037ed05f54f | 322 | } |
mbed_official | 0:7037ed05f54f | 323 | |
brunnobbco | 61:65744bc8ab55 | 324 | // printf(" RX Data on port %u (%d bytes): ", port, retcode); |
mbed_official | 0:7037ed05f54f | 325 | for (uint8_t i = 0; i < retcode; i++) { |
mbed_official | 47:b6d132f1079f | 326 | printf("%02x ", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 327 | } |
mbed_official | 47:b6d132f1079f | 328 | printf("\r\n"); |
mbed_official | 47:b6d132f1079f | 329 | |
mbed_official | 12:5015dfead3f2 | 330 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 331 | } |
mbed_official | 0:7037ed05f54f | 332 | |
mbed_official | 0:7037ed05f54f | 333 | /** |
mbed_official | 0:7037ed05f54f | 334 | * Event handler |
mbed_official | 0:7037ed05f54f | 335 | */ |
mbed_official | 0:7037ed05f54f | 336 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 337 | { |
mbed_official | 0:7037ed05f54f | 338 | switch (event) { |
mbed_official | 0:7037ed05f54f | 339 | case CONNECTED: |
mbed_official | 0:7037ed05f54f | 340 | printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 341 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 342 | send_message(); |
mbed_official | 0:7037ed05f54f | 343 | } else { |
mbed_official | 0:7037ed05f54f | 344 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 345 | } |
mbed_official | 0:7037ed05f54f | 346 | |
mbed_official | 0:7037ed05f54f | 347 | break; |
mbed_official | 0:7037ed05f54f | 348 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 349 | ev_queue.break_dispatch(); |
mbed_official | 0:7037ed05f54f | 350 | printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 351 | break; |
mbed_official | 0:7037ed05f54f | 352 | case TX_DONE: |
brunnobbco | 61:65744bc8ab55 | 353 | // printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 354 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 355 | send_message(); |
mbed_official | 0:7037ed05f54f | 356 | } |
mbed_official | 0:7037ed05f54f | 357 | break; |
mbed_official | 0:7037ed05f54f | 358 | case TX_TIMEOUT: |
brunnobbco | 61:65744bc8ab55 | 359 | // printf("\r\n Transmission Error TX_Timeout"); |
mbed_official | 0:7037ed05f54f | 360 | case TX_ERROR: |
brunnobbco | 61:65744bc8ab55 | 361 | // printf("\r\n Transmission Error TX_Error"); |
mbed_official | 0:7037ed05f54f | 362 | case TX_CRYPTO_ERROR: |
brunnobbco | 61:65744bc8ab55 | 363 | // printf("\r\n Transmission Error TX_Crypto_Error"); |
mbed_official | 0:7037ed05f54f | 364 | case TX_SCHEDULING_ERROR: |
brunnobbco | 61:65744bc8ab55 | 365 | // printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 366 | // try again |
mbed_official | 0:7037ed05f54f | 367 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 368 | send_message(); |
mbed_official | 0:7037ed05f54f | 369 | } |
mbed_official | 0:7037ed05f54f | 370 | break; |
mbed_official | 0:7037ed05f54f | 371 | case RX_DONE: |
brunnobbco | 61:65744bc8ab55 | 372 | // printf("\r\n Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 373 | receive_message(); |
mbed_official | 0:7037ed05f54f | 374 | break; |
mbed_official | 0:7037ed05f54f | 375 | case RX_TIMEOUT: |
brunnobbco | 61:65744bc8ab55 | 376 | // printf("\r\n Transmission Error RX_Timeout"); |
mbed_official | 0:7037ed05f54f | 377 | case RX_ERROR: |
brunnobbco | 61:65744bc8ab55 | 378 | // printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 379 | break; |
mbed_official | 0:7037ed05f54f | 380 | case JOIN_FAILURE: |
brunnobbco | 61:65744bc8ab55 | 381 | // printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 382 | break; |
mbed_official | 26:f07f5febf97f | 383 | case UPLINK_REQUIRED: |
brunnobbco | 61:65744bc8ab55 | 384 | // printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 385 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 386 | send_message(); |
mbed_official | 26:f07f5febf97f | 387 | } |
mbed_official | 26:f07f5febf97f | 388 | break; |
mbed_official | 0:7037ed05f54f | 389 | default: |
mbed_official | 0:7037ed05f54f | 390 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 391 | } |
mbed_official | 0:7037ed05f54f | 392 | } |
mbed_official | 0:7037ed05f54f | 393 | |
mbed_official | 0:7037ed05f54f | 394 | // EOF |