Lorawan to Pulga

Dependencies:   pulga-lorawan-drv SPI_MX25R Si1133 BME280

Files at this revision

API Documentation at this revision

Comitter:
brunnobbco
Date:
Fri Jan 08 20:16:58 2021 +0000
Parent:
60:c4f9e9202fb4
Commit message:
Lorawan, GPS, Sensors

Changed in this revision

BME280.lib Show annotated file Show diff for this revision Revisions of this file
BME280.txt Show annotated file Show diff for this revision Revisions of this file
BMX160.txt Show annotated file Show diff for this revision Revisions of this file
SPI_MX25R.lib Show annotated file Show diff for this revision Revisions of this file
gps.txt Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c4f9e9202fb4 -r 65744bc8ab55 BME280.lib
--- a/BME280.lib	Mon Nov 30 19:25:11 2020 +0000
+++ b/BME280.lib	Fri Jan 08 20:16:58 2021 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/MACRUM/code/BME280/#c1f1647004c4
+http://developer.mbed.org/users/MACRUM/code/BME280/#ddcaa259e65b
diff -r c4f9e9202fb4 -r 65744bc8ab55 BME280.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BME280.txt	Fri Jan 08 20:16:58 2021 +0000
@@ -0,0 +1,189 @@
+#include "mbed.h"
+#define _DEBUG
+
+I2C sensor(p13, p15);
+
+#ifdef _DEBUG
+#define DEBUG_PRINT(...) printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+const int BME280_ADDR = (0x77 << 1);
+
+uint16_t dig_T1;
+int16_t dig_T2, dig_T3;
+uint16_t dig_P1;
+int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
+uint16_t dig_H1, dig_H3;
+int16_t dig_H2, dig_H4, dig_H5, dig_H6;
+int32_t t_fine;
+
+void init()
+{
+    char cmd[18];
+
+    sensor.frequency(1000000);
+
+    cmd[0] = 0xf2; // ctrl_hum
+    cmd[1] = 0x01;
+    sensor.write(BME280_ADDR, cmd, 2);
+
+    cmd[0] = 0xf4; // ctrl_meas
+    cmd[1] = 0x27;
+    sensor.write(BME280_ADDR, cmd, 2);
+
+    cmd[0] = 0xf5; // config
+    cmd[1] = 0xa0;
+    sensor.write(BME280_ADDR, cmd, 2);
+
+    cmd[0] = 0x88; // read dig_T regs
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, cmd, 6);
+
+    dig_T1 = (cmd[1] << 8) | cmd[0];
+    dig_T2 = (cmd[3] << 8) | cmd[2];
+    dig_T3 = (cmd[5] << 8) | cmd[4];
+
+//   DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
+
+    cmd[0] = 0x8E; // read dig_P regs
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, cmd, 18);
+
+    dig_P1 = (cmd[ 1] << 8) | cmd[ 0];
+    dig_P2 = (cmd[ 3] << 8) | cmd[ 2];
+    dig_P3 = (cmd[ 5] << 8) | cmd[ 4];
+    dig_P4 = (cmd[ 7] << 8) | cmd[ 6];
+    dig_P5 = (cmd[ 9] << 8) | cmd[ 8];
+    dig_P6 = (cmd[11] << 8) | cmd[10];
+    dig_P7 = (cmd[13] << 8) | cmd[12];
+    dig_P8 = (cmd[15] << 8) | cmd[14];
+    dig_P9 = (cmd[17] << 8) | cmd[16];
+
+//    DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
+
+    cmd[0] = 0xA1; // read dig_H regs
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, cmd, 9);
+
+    dig_H1 = cmd[0];
+    dig_H2 = (cmd[2] << 8) | cmd[1];
+    dig_H3 = cmd[3];
+    dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f);
+    dig_H5 = (cmd[7] << 4) | ((cmd[6]>>4) & 0x0f);
+    dig_H6 = cmd[8];
+
+//    DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
+}
+
+float getTemperature()
+{
+    uint32_t temp_raw;
+    float tempf;
+    char cmd[4];
+
+    cmd[0] = 0xfa; // temp_msb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[1], 1);
+
+    cmd[0] = 0xfb; // temp_lsb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[2], 1);
+
+    cmd[0] = 0xfc; // temp_xlsb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[3], 1);
+
+    temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+
+    int32_t temp;
+
+    temp =
+        (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
+        ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
+
+    t_fine = temp;
+    temp = (temp * 5 + 128) >> 8;
+    tempf = (float)temp;
+
+    return (tempf/100.0f);
+}
+
+float getPressure()
+{
+    uint32_t press_raw;
+    float pressf;
+    char cmd[4];
+
+    cmd[0] = 0xf7; // press_msb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[1], 1);
+
+    cmd[0] = 0xf8; // press_lsb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[2], 1);
+
+    cmd[0] = 0xf9; // press_xlsb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[3], 1);
+
+    press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4);
+
+    int32_t var1, var2;
+    uint32_t press;
+
+    var1 = (t_fine >> 1) - 64000;
+    var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
+    var2 = var2 + ((var1 * dig_P5) << 1);
+    var2 = (var2 >> 2) + (dig_P4 << 16);
+    var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
+    var1 = ((32768 + var1) * dig_P1) >> 15;
+    if (var1 == 0) {
+        return 0;
+    }
+    press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
+    if(press < 0x80000000) {
+        press = (press << 1) / var1;
+    } else {
+        press = (press / var1) * 2;
+    }
+    var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
+    var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
+    press = (press + ((var1 + var2 + dig_P7) >> 4));
+
+    pressf = (float)press;
+    return (pressf/100.0f);
+}
+
+float getHumidity()
+{
+    uint32_t hum_raw;
+    float humf;
+    char cmd[4];
+
+    cmd[0] = 0xfd; // hum_msb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[1], 1);
+
+    cmd[0] = 0xfd; // hum_lsb
+    sensor.write(BME280_ADDR, cmd, 1);
+    sensor.read(BME280_ADDR, &cmd[2], 1);
+
+    hum_raw = (cmd[1] << 8) | cmd[2];
+
+    int32_t v_x1;
+
+    v_x1 = t_fine - 76800;
+    v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
+               ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
+                                            (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
+                                            (int32_t)dig_H2 + 8192) >> 14));
+    v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
+    v_x1 = (v_x1 < 0 ? 0 : v_x1);
+    v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
+
+    humf = (float)(v_x1 >> 12);
+
+    return (humf/1024.0f);
+}
diff -r c4f9e9202fb4 -r 65744bc8ab55 BMX160.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMX160.txt	Fri Jan 08 20:16:58 2021 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+ 
+ 
+/* defines the axis for acc */
+#define ACC_NOOF_AXIS       3
+#define GYR_NOOF_AXIS       2
+ 
+/* bmi160 slave address */
+#define BMI160_ADDR         ((0x69)<<1)
+ 
+#define RAD_DEG           57.29577951
+ 
+ 
+I2C i2c(p13, p15);
+ 
+ 
+/* buffer to store acc samples */
+int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+int16_t gyr_sample_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555};
+ 
+double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+double gyr_result_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555};
+ 
+double accel_ang_x, accel_ang_y;
+double tiltx, tilty;
+double tiltx_prev, tilty_prev;
+ 
+char i2c_reg_buffer[2] = {0};
+ 
\ No newline at end of file
diff -r c4f9e9202fb4 -r 65744bc8ab55 SPI_MX25R.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI_MX25R.lib	Fri Jan 08 20:16:58 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Arkadi/code/SPI_MX25R/#b6bb8d236251
diff -r c4f9e9202fb4 -r 65744bc8ab55 gps.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gps.txt	Fri Jan 08 20:16:58 2021 +0000
@@ -0,0 +1,687 @@
+
+#define  SYNC1       0xB5
+#define  SYNC2       0x62
+
+//GPS DEclaration
+SPI spi_2(P0_5, P0_7, P0_11); // mosi, miso, sclk
+DigitalOut cs(P0_30);
+DigitalOut gps_reset(P1_2);
+DigitalOut gps_int (P0_23);
+DigitalOut gps_DSEL (P0_27);
+
+
+
+
+
+typedef struct
+{
+    uint8_t cls;
+    uint8_t id;
+    uint16_t len;          //Length of the payload. Does not include cls, id, or checksum bytes
+    uint8_t *payload;
+    uint8_t checksumA; //Given to us from module. Checked against the rolling calculated A/B checksums.
+    uint8_t checksumB;
+} gps_ubxPacket;
+
+typedef struct
+{
+    uint32_t iTOW=0;
+    uint16_t year=0;
+    uint8_t month=0;
+    uint8_t day=0;
+    uint8_t hour=0;
+    uint8_t min=0;
+    uint8_t sec=0;
+    int8_t valid=0;
+    uint32_t tAcc=0;
+    int32_t nano=0;
+    uint8_t fixtype=0;
+    int8_t flags=0;
+    int8_t flags2=0;
+    uint8_t numSV=0;
+    int32_t lon=0;
+    int32_t lat=0;
+    int32_t height=0;
+    int32_t hMSL=0;
+    uint32_t hAcc=0;
+    uint32_t vAcc=0;
+    int32_t velN=0;
+    int32_t velE=0;
+    int32_t velD=0;
+    int32_t gSpeed=0;
+    int32_t headMot=0;
+    uint32_t sAcc=0;
+    uint32_t headAcc=0;
+    uint16_t pDOP=0;
+    int32_t headVeh;
+    int16_t magDec;
+    uint16_t magAcc;
+
+    
+} gps_navPVT;
+
+
+void gps_leBootMsg(){
+
+    #define MAXIMUM_PACKET_SIZE 60
+    int packet_size;
+    uint8_t packet[MAXIMUM_PACKET_SIZE];
+    uint8_t value;
+    int cont;
+    value = spi_2.write(0x00);
+
+    while (value  != '$' ){ //wait start boot msg
+        value = spi_2.write(0x00);
+        wait_ms(5);
+        cont++;
+        if (cont > 100){
+            printf("\n no response \n"); 
+            return;
+            }
+    }
+    packet[0] = value;
+        value = spi_2.write(0x00);
+        for (packet_size = 1 ; packet_size <= MAXIMUM_PACKET_SIZE ;packet_size++) {
+            if (value  != '\n' ){
+                packet [packet_size]= value;
+                value = spi_2.write(0x00);
+            }
+            else {
+              //lora_send_packet (packet , (uint8_t) packet_size+1); // manda atraves do lora a mensagem de boot do gps
+              printf("Boot msg: %s \n", packet);
+              return;
+            } 
+        
+        }
+
+}
+
+void gps_le_envia_linha(){
+    
+    uint8_t packet[150];
+    uint8_t value;
+
+    for (int i=0;i < 149; i++){
+        if (value == '\n'){
+            //lora_send_packet (packet , (uint8_t) i);
+            return;
+        }
+        else packet [i] =value;
+    }
+    //lora_send_packet (packet , (uint8_t) 99);
+    
+}
+                               
+
+gps_ubxPacket gps_calcula_check(gps_ubxPacket Packet) {
+ 
+    uint8_t Buffer[Packet.len + 4];
+    uint8_t CK_A=0;
+    uint8_t CK_B=0;
+
+    Buffer[0]= Packet.cls;
+    Buffer[1]= Packet.id;
+    Buffer[2]= Packet.len & 0xFF;
+    Buffer[3]= (Packet.len >> 8)& 0xFF;;
+
+    //send_packet (Buffer, (uint8_t) 4 );
+        
+    for (uint16_t i = 0; i < Packet.len; i++) {
+        Buffer [i+4] = Packet.payload[i] ;
+    }
+    
+    //send_packet (Buffer, (uint8_t) Packet.len + 4 );
+
+    for(int i=0 ; i < Packet.len + 4 ; i++) {
+        CK_A = CK_A + Buffer[i];
+        CK_B = CK_B + CK_A;
+        CK_A = CK_A & 0xFF;
+        CK_B = CK_B & 0xFF;
+        //uint8_t packet_check[5] ={Buffer[i],(uint8_t) (i+1),CK_A,CK_B,'#'};
+//        send_packet (packet_check , (uint8_t) 5);
+    }
+    
+    
+//    uint8_t packet_check[2] ={CK_A, CK_B};
+//    send_packet (packet_check , (uint8_t) 2);
+
+    Packet.checksumA = CK_A;
+    Packet.checksumB = CK_B;
+    
+    return Packet;
+    
+}
+
+void send_gps_packet(gps_ubxPacket packet){
+    
+    spi_2.write(SYNC1);
+    spi_2.write(SYNC2);
+    spi_2.write(packet.cls);
+    spi_2.write(packet.id);
+    spi_2.write(packet.len & 0xFF);
+    spi_2.write((packet.len >> 8)& 0xFF);
+    
+    for (uint16_t i = 0; i < packet.len; i++) {
+        spi_2.write(packet.payload[i]);
+    }
+    spi_2.write(packet.checksumA);
+    spi_2.write(packet.checksumB);
+    
+    //=============imprime resposta
+    wait_ms(50);
+    gps_le_envia_linha();
+    gps_le_envia_linha();
+
+}
+
+gps_navPVT le_nav_pvt () {
+    
+    gps_navPVT Pac;
+    
+    char state = 0;
+    int cont =0;
+    int numb=0;
+    
+    while (1){ //começou mensagem
+        if (cont > 100) {
+           // BMX160_read_acc();
+            return Pac;
+            }
+        if (state == 0 ){
+            
+            if ( spi_2.write(0x00) == 0xB5){
+                state =1;
+                }
+            else {
+                cont++;
+                wait_ms(40);
+            }
+                    
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == 0x62){
+                state =2;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == 0x01){
+                //printf("le_nav_pvt going to state 3");
+                state =3;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x01
+            if (spi_2.write(0x00) == 0x07){
+                state =4;
+                //printf("le_nav_pvt going to state 4");
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x01 0x07
+            if (spi_2.write(0x00) == 0x5c){
+                state =5;
+                //printf("le_nav_pvt going to state 5");
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 5) {// read 0xb5 0x62 0x01 0x07 0x5c
+            if (spi_2.write(0x00) == 0x00){
+                state =6;
+                //printf("le_nav_pvt going to state 6");
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 6){ // read 0xb5 0x62 0x01 0x07 0x5c 0x00
+            uint8_t value1,value2,value3,value4;
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.iTOW =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            Pac.year = (value2 << 8) + value1;
+            
+            Pac.month = 0xff & spi_2.write(0x00);
+            Pac.day = 0xff & spi_2.write(0x00);
+            Pac.hour = 0xff & spi_2.write(0x00);
+            Pac.min = 0xff & spi_2.write(0x00);
+            Pac.sec = 0xff & spi_2.write(0x00);
+            Pac.valid = 0xff & spi_2.write(0x00);
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.tAcc =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.nano =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            Pac.fixtype = 0xff & spi_2.write(0x00);
+            Pac.flags = 0xff & spi_2.write(0x00);
+            Pac.flags2 = 0xff & spi_2.write(0x00);
+            Pac.numSV = 0xff & spi_2.write(0x00);
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.lon =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            lon=  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            //printf("Long Data %d \n ", lon);
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.lat =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            lat =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            //printf("Lat Data %d", lat);
+            
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.height =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.hMSL  =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.hAcc  =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.vAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velN  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velE  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velD  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.gSpeed  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headMot  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.sAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.pDOP  = (value2 << 8) + value1;
+            
+            for (int i=0; i < 6; i++)spi_2.write(0x00); 
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.magDec = (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.magAcc = (value2 << 8) + value1;
+
+                              
+            return Pac;
+        }
+    }
+        wait_ms(100);  
+}
+
+void send_nav_pvt (){
+    uint8_t packet_nav_pvt[] = {    0xB5,   0x62,   0x01,   0x07,   0x00,   0x00,   0x08,   0x19};
+    
+    //=============envia pacote nav pvt
+    for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){
+        spi_2.write(packet_nav_pvt[i]);
+        wait_ms(5);
+    }
+
+    gps_navPVT Data = le_nav_pvt();
+
+    uint8_t packet [100];
+    
+    packet [0]= (Data.lon >> 24)& 0xff;
+    packet [1]= (Data.lon >> 16) & 0xff ;
+    packet [2]= (Data.lon >> 8) & 0xff;
+    packet [3]= Data.lon  & 0xff;
+    packet [4]= (Data.lat >> 24)& 0xff;
+    packet [5]= (Data.lat >> 16) & 0xff ;
+    packet [6]= (Data.lat >> 8) & 0xff;
+    packet [7]= Data.lat  & 0xff;
+    packet [8]= (Data.hMSL >> 24)& 0xff;
+    packet [9]= (Data.hMSL >> 16) & 0xff ;
+    packet [10]= (Data.hMSL >> 8) & 0xff;
+    packet [11]= Data.hMSL  & 0xff;
+    
+    if (Data.lon !=0 || Data.lat !=0  ){
+        //lora_send_packet (packet , (uint8_t) 12);
+    }
+}
+
+void send_gps_data(uint8_t *packet, uint8_t size){
+    uint8_t packet_rec[size]; 
+    for ( int i=0; i< size  ; i++){
+        spi_2.write(packet[i]);
+
+        //wait_ms(5);
+    }
+    //send_packet (packet_rec ,size);
+}
+
+/*
+void wait_packet (uint8_t *header) {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == header[0])
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == header[1]){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == header[2]){
+                state =3;
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x06
+            if (spi_2.write(0x00) == header[3]){
+                state =4;
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x06 0x013
+        
+            uint8_t packet[100];
+            uint8_t value = spi_2.write(0x00);
+            uint8_t packet_size;
+             
+            for (packet_size = 0 ; packet_size < 100 ;packet_size++) {
+                if (value  != '\n' ){
+                    packet [packet_size]= value;
+                    value = spi_2.write(0x00);
+                }
+                else {
+                  lora_send_packet (packet , (uint8_t) packet_size+1);
+                  //packet_size = MAXIMUM_PACKET_SIZE +1;
+                  return;
+                }             
+            }
+        
+        }
+    }
+}*/
+
+void wait_packet_byte (uint8_t *header, uint8_t byte) {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == header[0])
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == header[1]){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == header[2]){
+                state =3;
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x06
+            if (spi_2.write(0x00) == header[3]){
+                state =4;
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x06 0x013
+            
+            if (byte == 0){//mostrar todo o pacote
+                uint8_t packet[100];
+                uint8_t value = spi_2.write(0x00);
+                uint8_t packet_size;
+                 
+                for (packet_size = 0 ; packet_size < 100 ;packet_size++) {
+                    if (value  != '\n' ){
+                        packet [packet_size]= value;
+                        value = spi_2.write(0x00);
+                    }
+                    else {
+                      //lora_send_packet (packet , (uint8_t) packet_size+1);
+                      //packet_size = MAXIMUM_PACKET_SIZE +1;
+                      return;
+                    }             
+                }//fim for
+            }//fim if byte
+            
+            else {//mostrar apenas um byte
+            
+             
+                for (int i = 0 ; i < byte-1 ;i++)
+                    spi_2.write(0x00);
+                    
+                uint8_t packet[]={(uint8_t)99,(uint8_t)spi_2.write(0x00)};            
+                //lora_send_packet (packet , (uint8_t) 2);
+                   
+                    
+                for (int i = 0 ; i < 100 ;i++)
+                    if (spi_2.write(0x00) == '\n') 
+                        return;
+    
+                return;
+                
+            }//fim else byte
+         
+         }
+        
+      }
+    
+}
+
+
+void gps_wait_same_packet () {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == 0xb5)
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == 0x62){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2){
+                uint8_t packet[200];
+                uint8_t value = spi_2.write(0x00);
+                uint8_t packet_size;
+                 
+                for (packet_size = 0 ; packet_size < 200 ;packet_size++) {
+                    if (value  != '\n' ){
+                        packet [packet_size]= value;
+                        value = spi_2.write(0x00);
+                    }
+                    else {
+                      //lora_send_packet (packet , (uint8_t) packet_size+1);
+                      //packet_size = MAXIMUM_PACKET_SIZE +1;
+                      return;
+                    }
+                             
+                }//fim for   
+               // lora_send_packet (packet , 200);       
+        }
+    }
+}
+
+void gps_config_gnss (){
+    
+    uint8_t packet_cfg_gnss[] = {
+      0xB5, 0x62, // Header
+      0x03, 0x3E, // Class ID = CFG, Msg ID = UBX-CFG-GNSS
+      0x3C, 0x00, // Payload Length = 60 bytes
+      0x00, 0x00, 0x20, 0x07, // msgVer=0, numTrkChHw=32, numTrkChUse=32, numConfigBlocks=7
+      0x00, 0x08, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, // gnssId=0 (GPS), resTrkCh=8, maxTrkCh=12, ENABLE
+      0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=1 (SBAS), resTrkCh=1, maxTrkCh=2, ENABLE
+      0x02, 0x04, 0x0A, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=2 (Galileo), resTrkCh=4, maxTrkCh=10, ENABLE 
+      0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=3 (BeiDou), resTrkCh=4, maxTrkCh=8, DISABLE 
+      0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=4 (IMES), resTrkCh=0, maxTrkCh=8, DISABLE
+      0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=5 (QZSS), resTrkCh=0, maxTrkCh=3, DISABLE
+      0x06, 0x04, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=6 (GLONASS), resTrkCh=4, maxTrkCh=8, ENABLE
+      0x00, 0x00 //checksums A &amp; B
+    };
+    uint8_t pos_ck = sizeof(packet_cfg_gnss)-2;
+    printf("gnss config finish");
+    uint8_t ubxi;
+    //calcula checksum
+    for (ubxi=2; ubxi<pos_ck ; ubxi++) {
+        packet_cfg_gnss[pos_ck] = packet_cfg_gnss[pos_ck] + packet_cfg_gnss[ubxi];
+        packet_cfg_gnss[pos_ck+1] = packet_cfg_gnss[pos_ck+1] + packet_cfg_gnss[pos_ck];
+    }
+    
+}
+
+
+void gps_print_local (){
+    uint8_t packet_nav_pvt[] = {    0xB5,   0x62,   0x01,   0x07,   0x00,   0x00,   0x08,   0x19};
+    
+    //=============envia pacote nav pvt
+    for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){
+        spi_2.write(packet_nav_pvt[i]);
+        wait_ms(20);
+    }
+    
+    gps_navPVT Data = le_nav_pvt();  
+   
+    //printf ("gps lat=%d lon=%d \n",Data.lat ,Data.lon );
+    //lat=Data.lat;
+    //lon=Data.lon;
+}
+
+void gps_config (){
+    gps_DSEL = 0;
+    //spi gps configuration
+    spi_2.format(8,0);
+    spi_2.frequency(1000000); //1MHz
+    cs=1;
+    wait(0.1);
+    cs=0;
+    wait(0.1);
+    
+    gps_int = 0;
+    
+    //gps reset
+    gps_reset = 1;    
+    wait_ms(50);
+    //gps_reset = 0;    
+    //wait(1.5);
+}
+
diff -r c4f9e9202fb4 -r 65744bc8ab55 main.cpp
--- 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();
             }