![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Program to satellite, send variables of temperature, pressure, RH, GPS
Dependencies: mbed nRF24L01P BMP180 DHT11 GPS
Sensores.cpp@1:b91db48be151, 2019-11-30 (annotated)
- Committer:
- JorgeGtz
- Date:
- Sat Nov 30 18:32:58 2019 +0000
- Revision:
- 1:b91db48be151
- Parent:
- 0:e8e530360fe5
Satelite
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JorgeGtz | 0:e8e530360fe5 | 1 | #include "Sensores.h" |
JorgeGtz | 0:e8e530360fe5 | 2 | #include "mbed.h" |
JorgeGtz | 0:e8e530360fe5 | 3 | #include "Dht11.h" |
JorgeGtz | 0:e8e530360fe5 | 4 | #include "BMP180.h" |
JorgeGtz | 0:e8e530360fe5 | 5 | #include "GPS.h" |
JorgeGtz | 0:e8e530360fe5 | 6 | #include "nRF24L01P.h" |
JorgeGtz | 0:e8e530360fe5 | 7 | |
JorgeGtz | 0:e8e530360fe5 | 8 | |
JorgeGtz | 0:e8e530360fe5 | 9 | Serial BT(Rx,Tx); |
JorgeGtz | 0:e8e530360fe5 | 10 | Dht11 sensor(SENSOR); |
JorgeGtz | 0:e8e530360fe5 | 11 | I2C i2c(SDA, SCL); //SDA,SCL |
JorgeGtz | 0:e8e530360fe5 | 12 | BMP180 bmp180(&i2c); |
JorgeGtz | 0:e8e530360fe5 | 13 | GPS ark(ARK_Rx, ARK_Tx); |
JorgeGtz | 0:e8e530360fe5 | 14 | DigitalOut myled(LED); |
JorgeGtz | 0:e8e530360fe5 | 15 | Ticker mTicker; |
JorgeGtz | 0:e8e530360fe5 | 16 | nRF24L01P my_nrf24l01p(MOSI, MISO, SCK, CSN, CE, IRQ); // mosi, miso, sck, csn, ce, irq |
JorgeGtz | 0:e8e530360fe5 | 17 | |
JorgeGtz | 0:e8e530360fe5 | 18 | |
JorgeGtz | 0:e8e530360fe5 | 19 | int Temperatura,Humedad, Presion, Altura, Lat, Long; |
JorgeGtz | 0:e8e530360fe5 | 20 | float Longitud, Latitud, temp; |
JorgeGtz | 0:e8e530360fe5 | 21 | long UTC,muestreo_sensores=0;; |
JorgeGtz | 0:e8e530360fe5 | 22 | char hora,minuto,segundo; |
JorgeGtz | 0:e8e530360fe5 | 23 | int count=0; |
JorgeGtz | 0:e8e530360fe5 | 24 | int count_enviar=0; |
JorgeGtz | 0:e8e530360fe5 | 25 | |
JorgeGtz | 0:e8e530360fe5 | 26 | |
JorgeGtz | 0:e8e530360fe5 | 27 | |
JorgeGtz | 0:e8e530360fe5 | 28 | char tab_control[TRANSFER_SIZE]={'T','H','P','A','L','D','U'}; |
JorgeGtz | 0:e8e530360fe5 | 29 | char valor[TRANSFER_SIZE]; |
JorgeGtz | 0:e8e530360fe5 | 30 | int variables[7]; |
JorgeGtz | 0:e8e530360fe5 | 31 | |
JorgeGtz | 0:e8e530360fe5 | 32 | |
JorgeGtz | 0:e8e530360fe5 | 33 | void ticker_callback(){ |
JorgeGtz | 0:e8e530360fe5 | 34 | count++; |
JorgeGtz | 0:e8e530360fe5 | 35 | muestreo_sensores++; |
JorgeGtz | 0:e8e530360fe5 | 36 | count_enviar++; |
JorgeGtz | 0:e8e530360fe5 | 37 | |
JorgeGtz | 0:e8e530360fe5 | 38 | if(count>10){ |
JorgeGtz | 0:e8e530360fe5 | 39 | // Temperatura=sensor.getFahrenheit(); |
JorgeGtz | 0:e8e530360fe5 | 40 | if( ark.sample() == 1) { |
JorgeGtz | 0:e8e530360fe5 | 41 | // myled=0; |
JorgeGtz | 0:e8e530360fe5 | 42 | Latitud=ark.latitude; |
JorgeGtz | 0:e8e530360fe5 | 43 | Longitud=ark.longitude; |
JorgeGtz | 0:e8e530360fe5 | 44 | UTC=ark.utc+50000; |
JorgeGtz | 0:e8e530360fe5 | 45 | Altura=ark.alt; |
JorgeGtz | 0:e8e530360fe5 | 46 | |
JorgeGtz | 0:e8e530360fe5 | 47 | } |
JorgeGtz | 0:e8e530360fe5 | 48 | // else myled=1; |
JorgeGtz | 0:e8e530360fe5 | 49 | count=0; |
JorgeGtz | 0:e8e530360fe5 | 50 | } |
JorgeGtz | 0:e8e530360fe5 | 51 | |
JorgeGtz | 0:e8e530360fe5 | 52 | if(muestreo_sensores>200) { |
JorgeGtz | 0:e8e530360fe5 | 53 | sensor.read(); |
JorgeGtz | 0:e8e530360fe5 | 54 | Humedad=sensor.getHumidity(); |
JorgeGtz | 0:e8e530360fe5 | 55 | muestreo_sensores=0; |
JorgeGtz | 0:e8e530360fe5 | 56 | } |
JorgeGtz | 0:e8e530360fe5 | 57 | |
JorgeGtz | 0:e8e530360fe5 | 58 | if(count_enviar>5) { |
JorgeGtz | 0:e8e530360fe5 | 59 | enviar_datos(); |
JorgeGtz | 0:e8e530360fe5 | 60 | myled=!myled; |
JorgeGtz | 0:e8e530360fe5 | 61 | count_enviar=0; |
JorgeGtz | 0:e8e530360fe5 | 62 | } |
JorgeGtz | 0:e8e530360fe5 | 63 | |
JorgeGtz | 0:e8e530360fe5 | 64 | } |
JorgeGtz | 0:e8e530360fe5 | 65 | |
JorgeGtz | 0:e8e530360fe5 | 66 | //////////////////////////////////////////////////////////////////////////////// |
JorgeGtz | 0:e8e530360fe5 | 67 | void init_serial(void){ |
JorgeGtz | 0:e8e530360fe5 | 68 | BT.baud(115200); |
JorgeGtz | 0:e8e530360fe5 | 69 | } |
JorgeGtz | 0:e8e530360fe5 | 70 | |
JorgeGtz | 0:e8e530360fe5 | 71 | void init_transmisor(void){ |
JorgeGtz | 0:e8e530360fe5 | 72 | |
JorgeGtz | 0:e8e530360fe5 | 73 | my_nrf24l01p.powerUp(); |
JorgeGtz | 0:e8e530360fe5 | 74 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
JorgeGtz | 0:e8e530360fe5 | 75 | my_nrf24l01p.setTransmitMode(); |
JorgeGtz | 0:e8e530360fe5 | 76 | my_nrf24l01p.enable(); |
JorgeGtz | 0:e8e530360fe5 | 77 | } |
JorgeGtz | 0:e8e530360fe5 | 78 | |
JorgeGtz | 0:e8e530360fe5 | 79 | void init_sensores(void){ |
JorgeGtz | 0:e8e530360fe5 | 80 | mTicker.attach_us(&ticker_callback,10000); //(funtion(),tiempo de interrupcion(seg)) |
JorgeGtz | 0:e8e530360fe5 | 81 | bmp180.init(); |
JorgeGtz | 0:e8e530360fe5 | 82 | } |
JorgeGtz | 0:e8e530360fe5 | 83 | |
JorgeGtz | 0:e8e530360fe5 | 84 | void actualizar(void){ |
JorgeGtz | 0:e8e530360fe5 | 85 | |
JorgeGtz | 0:e8e530360fe5 | 86 | bmp180.startTemperature(); |
JorgeGtz | 0:e8e530360fe5 | 87 | wait_ms(5); |
JorgeGtz | 0:e8e530360fe5 | 88 | if(bmp180.getTemperature(&temp) != 0) { |
JorgeGtz | 0:e8e530360fe5 | 89 | // BT.printf("Error getting temperature\n"); |
JorgeGtz | 0:e8e530360fe5 | 90 | } |
JorgeGtz | 0:e8e530360fe5 | 91 | |
JorgeGtz | 0:e8e530360fe5 | 92 | bmp180.startPressure(BMP180::ULTRA_LOW_POWER); |
JorgeGtz | 0:e8e530360fe5 | 93 | wait_ms(10); // Wait for conversion to complete |
JorgeGtz | 0:e8e530360fe5 | 94 | if(bmp180.getPressure(&Presion) != 0) { |
JorgeGtz | 0:e8e530360fe5 | 95 | // BT.printf("Error getting pressure\n"); |
JorgeGtz | 0:e8e530360fe5 | 96 | } |
JorgeGtz | 0:e8e530360fe5 | 97 | |
JorgeGtz | 0:e8e530360fe5 | 98 | |
JorgeGtz | 0:e8e530360fe5 | 99 | |
JorgeGtz | 0:e8e530360fe5 | 100 | } |
JorgeGtz | 0:e8e530360fe5 | 101 | |
JorgeGtz | 0:e8e530360fe5 | 102 | void enviar_datos(void){ |
JorgeGtz | 0:e8e530360fe5 | 103 | presion(); |
JorgeGtz | 0:e8e530360fe5 | 104 | sensor_dht11(); |
JorgeGtz | 0:e8e530360fe5 | 105 | global(); |
JorgeGtz | 0:e8e530360fe5 | 106 | transmitir(); |
JorgeGtz | 0:e8e530360fe5 | 107 | } |
JorgeGtz | 0:e8e530360fe5 | 108 | |
JorgeGtz | 0:e8e530360fe5 | 109 | ////////////////////////////////////////////////////////////////////////////////Enviar data |
JorgeGtz | 0:e8e530360fe5 | 110 | |
JorgeGtz | 0:e8e530360fe5 | 111 | void presion(void){ |
JorgeGtz | 0:e8e530360fe5 | 112 | |
JorgeGtz | 0:e8e530360fe5 | 113 | |
JorgeGtz | 0:e8e530360fe5 | 114 | Temperatura=temp*100; |
JorgeGtz | 0:e8e530360fe5 | 115 | variables[0]=Temperatura; |
JorgeGtz | 0:e8e530360fe5 | 116 | variables[2]=Presion; |
JorgeGtz | 0:e8e530360fe5 | 117 | /* |
JorgeGtz | 0:e8e530360fe5 | 118 | sprintf(valor,"T%06d",Temperatura); |
JorgeGtz | 0:e8e530360fe5 | 119 | |
JorgeGtz | 0:e8e530360fe5 | 120 | BT.printf(valor); |
JorgeGtz | 0:e8e530360fe5 | 121 | BT.printf("\n");*/ |
JorgeGtz | 0:e8e530360fe5 | 122 | } |
JorgeGtz | 0:e8e530360fe5 | 123 | |
JorgeGtz | 0:e8e530360fe5 | 124 | void sensor_dht11(void){ |
JorgeGtz | 0:e8e530360fe5 | 125 | variables[1]=Humedad; |
JorgeGtz | 0:e8e530360fe5 | 126 | } |
JorgeGtz | 0:e8e530360fe5 | 127 | |
JorgeGtz | 0:e8e530360fe5 | 128 | void global(void){ |
JorgeGtz | 0:e8e530360fe5 | 129 | |
JorgeGtz | 0:e8e530360fe5 | 130 | Lat=Latitud*1000; |
JorgeGtz | 0:e8e530360fe5 | 131 | Long=Longitud*100; |
JorgeGtz | 0:e8e530360fe5 | 132 | |
JorgeGtz | 0:e8e530360fe5 | 133 | variables[3]=Altura; |
JorgeGtz | 0:e8e530360fe5 | 134 | variables[4]=Lat; |
JorgeGtz | 0:e8e530360fe5 | 135 | variables[5]=Long; |
JorgeGtz | 0:e8e530360fe5 | 136 | variables[6]=UTC; |
JorgeGtz | 0:e8e530360fe5 | 137 | |
JorgeGtz | 0:e8e530360fe5 | 138 | } |
JorgeGtz | 0:e8e530360fe5 | 139 | |
JorgeGtz | 0:e8e530360fe5 | 140 | //////////////////////////////////////////////////////////////////////////////// |
JorgeGtz | 0:e8e530360fe5 | 141 | void transmitir(void){ |
JorgeGtz | 0:e8e530360fe5 | 142 | for(int i=0; i<=6; i++){ |
JorgeGtz | 0:e8e530360fe5 | 143 | |
JorgeGtz | 0:e8e530360fe5 | 144 | sprintf(valor,"%c%6d",tab_control[i],variables[i]); |
JorgeGtz | 0:e8e530360fe5 | 145 | |
JorgeGtz | 0:e8e530360fe5 | 146 | // If the transmit buffer is full |
JorgeGtz | 0:e8e530360fe5 | 147 | if ( TRANSFER_SIZE >= sizeof( valor) ) { |
JorgeGtz | 0:e8e530360fe5 | 148 | BT.printf("Sending Message... "); |
JorgeGtz | 0:e8e530360fe5 | 149 | // Send the transmitbuffer via the nRF24L01+ |
JorgeGtz | 0:e8e530360fe5 | 150 | my_nrf24l01p.write( NRF24L01P_PIPE_P0,valor, TRANSFER_SIZE ); |
JorgeGtz | 0:e8e530360fe5 | 151 | BT.printf("Message Sent\n"); |
JorgeGtz | 0:e8e530360fe5 | 152 | } |
JorgeGtz | 0:e8e530360fe5 | 153 | } |
JorgeGtz | 0:e8e530360fe5 | 154 | } |
JorgeGtz | 0:e8e530360fe5 | 155 | |
JorgeGtz | 0:e8e530360fe5 | 156 | |
JorgeGtz | 0:e8e530360fe5 | 157 | |
JorgeGtz | 0:e8e530360fe5 | 158 |