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