contain lorawan with serial_rx enabled
Dependencies: pulga-lorawan-drv SPI_MX25R Si1133 BME280
Diff: main.cpp
- Revision:
- 61:65744bc8ab55
- Parent:
- 60:c4f9e9202fb4
- Child:
- 62:89df9529dbb0
--- a/main.cpp Mon Nov 30 19:25:11 2020 +0000 +++ b/main.cpp Fri Jan 08 20:16:58 2021 +0000 @@ -24,14 +24,14 @@ //#include "DummySensor.h" #include "trace_helper.h" #include "lora_radio_helper.h" -#include "BME280.h" +//#include "BME280.h" using namespace events; // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. // This example only communicates with much shorter messages (<30 bytes). // If longer messages are used, these buffers must be changed accordingly. -uint8_t tx_buffer[30]; +uint8_t tx_buffer[256]; uint8_t rx_buffer[30]; /* @@ -65,9 +65,9 @@ /** * Sensors Variables */ - uint32_t lux = 0; - uint32_t amb = 0; - float sensor_get = 0; +// uint32_t lux = 0; +// uint32_t amb = 0; +// float sensor_get = 0; /** * This event queue is the global event queue for both the @@ -102,9 +102,121 @@ mbed::DigitalOut _alive_led(P1_13, 0); mbed::DigitalOut _actuated_led(P1_14,1); +int lat=0; +int lon=0; +int latitude=0; +int longitude=0; + + +//Temperature, Pressure, Humidity Sensor +#include "BME280.txt" +#include "BMX160.txt" +#include "gps.txt" + +void BMX160Read (void) +{ + /*Le os Registradores do Acelerometro*/ + i2c_reg_buffer[0] = 0x12; + i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); + i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false); + + /*Le os Registradores do Giroscopio*/ + i2c_reg_buffer[0] = 0x0C; + i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); + i2c.read(BMI160_ADDR, (char *)&gyr_sample_buffer, sizeof(gyr_sample_buffer), false); + + /*Ajusta dados brutos Acelerometro em unidades de g */ + acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0); + acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0); + acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0); + + /*Ajusta dados Brutos do Giroscopio em unidades de deg/s */ + gyr_result_buffer[0] = (gyr_sample_buffer[0]/131.2); + gyr_result_buffer[1] = (gyr_sample_buffer[1]/131.2); + + /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/ + accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; + accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; + + /*Calcula os Angulos de Rotacao com valor do Giroscopio e aplica filtro complementar realizando a fusao*/ + tiltx = (0.98*(tiltx_prev+(gyr_result_buffer[0]*0.001)))+(0.02*(accel_ang_x)); + tilty = (0.98*(tilty_prev+(gyr_result_buffer[1]*0.001)))+(0.02*(accel_ang_y)); + + tiltx_prev = tiltx; + tilty_prev = tilty; + + /*Imprime os dados ACC pre-formatados*/ + printf("%.3f,%.3f;",tiltx, tilty); + + } + +void GPS_Read(void) +{ + gps_print_local(); + printf ("gps longitude=%d \n",lon); + printf ("gps latitude=%d \n",lat); + if(lat!=0 && lon!=0){ + longitude=lon; + latitude=lat; +// led1 = !led1; + } + } int main(void) { + gps_config(); + gps_leBootMsg(); + gps_config_gnss (); + init(); + + //BMX160 Declaration###################################### + // pc.printf("Teste BMI160\n\r"); +// printf("Configurando BMX160...\n\r"); + wait_ms(250); + + /*Config Freq. I2C Bus*/ + i2c.frequency(20000); + + /*Reset BMI160*/ + i2c_reg_buffer[0] = 0x7E; + i2c_reg_buffer[1] = 0xB6; + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); + wait_ms(200); +// printf("BMI160 Resetado\n\r"); + + /*Habilita o Acelerometro*/ + i2c_reg_buffer[0] = 0x7E; + i2c_reg_buffer[1] = 0x11; //PMU Normal + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); +// printf("Acc Habilitado\n\r"); + + /*Habilita o Giroscopio*/ + i2c_reg_buffer[0] = 0x7E; + i2c_reg_buffer[1] = 0x15; //PMU Normal + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); +// printf("Gyr Habilitado\n\r"); + + /*Config o Data Rate ACC em 1600Hz*/ + i2c_reg_buffer[0] = 0x40; + i2c_reg_buffer[1] = 0x2C; + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); +// printf("Data Rate ACC Selecionado a 1600Hz\n\r"); + + /*Config o Data Rate GYR em 1600Hz*/ + i2c_reg_buffer[0] = 0x42; + i2c_reg_buffer[1] = 0x2C; + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); +// printf("Data Rate GYR Selecionado a 1600Hz\n\r"); + + /*Config o Range GYR em 250º/s*/ + i2c_reg_buffer[0] = 0x43; + i2c_reg_buffer[1] = 0x03; + i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); +// printf("Range GYR Selecionado a 250deg/s\n\r"); + + printf("BMX160 Configurado\n\r"); + + //######################################################## // setup tracing setup_trace(); @@ -167,8 +279,9 @@ uint16_t packet_len; int16_t retcode; int32_t sensor_value; + gps_print_local(); - packet_len = sprintf((char *) tx_buffer, "Sensor Value is 10.0\n"); + packet_len = sprintf((char *) tx_buffer, "%2.2f, %04.2f, %2.2f, %d, %d\n", getTemperature(), getPressure(), getHumidity(), lon, lat); retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); @@ -180,13 +293,17 @@ if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - ev_queue.call_in(3000, send_message); + ev_queue.call_in(10000, send_message); } } return; } - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + printf("%2.2f;%04.2f;%2.2f;", getTemperature(), getPressure(), getHumidity()); + BMX160Read(); + printf ("%d;",lon); + printf ("%d \r\n",lat); +// printf("\r\n %d bytes scheduled for transmission \r\n", retcode); memset(tx_buffer, 0, sizeof(tx_buffer)); } @@ -200,11 +317,11 @@ int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); if (retcode < 0) { - printf("\r\n receive() - Error code %d \r\n", retcode); +// printf("\r\n receive() - Error code %d \r\n", retcode); return; } - printf(" RX Data on port %u (%d bytes): ", port, retcode); +// printf(" RX Data on port %u (%d bytes): ", port, retcode); for (uint8_t i = 0; i < retcode; i++) { printf("%02x ", rx_buffer[i]); } @@ -233,38 +350,38 @@ printf("\r\n Disconnected Successfully \r\n"); break; case TX_DONE: - printf("\r\n Message Sent to Network Server \r\n"); +// printf("\r\n Message Sent to Network Server \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; case TX_TIMEOUT: - printf("\r\n Transmission Error TX_Timeout"); +// printf("\r\n Transmission Error TX_Timeout"); case TX_ERROR: - printf("\r\n Transmission Error TX_Error"); +// printf("\r\n Transmission Error TX_Error"); case TX_CRYPTO_ERROR: - printf("\r\n Transmission Error TX_Crypto_Error"); +// printf("\r\n Transmission Error TX_Crypto_Error"); case TX_SCHEDULING_ERROR: - printf("\r\n Transmission Error - EventCode = %d \r\n", event); +// printf("\r\n Transmission Error - EventCode = %d \r\n", event); // try again if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; case RX_DONE: - printf("\r\n Received message from Network Server \r\n"); +// printf("\r\n Received message from Network Server \r\n"); receive_message(); break; case RX_TIMEOUT: - printf("\r\n Transmission Error RX_Timeout"); +// printf("\r\n Transmission Error RX_Timeout"); case RX_ERROR: - printf("\r\n Error in reception - Code = %d \r\n", event); +// printf("\r\n Error in reception - Code = %d \r\n", event); break; case JOIN_FAILURE: - printf("\r\n OTAA Failed - Check Keys \r\n"); +// printf("\r\n OTAA Failed - Check Keys \r\n"); break; case UPLINK_REQUIRED: - printf("\r\n Uplink required by NS \r\n"); +// printf("\r\n Uplink required by NS \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); }