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