Untitled
unknown
plain_text
3 years ago
4.4 kB
3
Indexable
Never
/* Javier Ferreiros López 2021 Grado en Ingeniería y Sistemas de Datos SISTEMAS DE ADQUISICIÓN DE DATOS Practise 1 - Sensors BMP280 ATMOSPHERIC PRESSURE AND TEMPERATURE SENSOR SENSORS - READING AND PROCESSING DATA Departamento de Ingeniería Electrónica E.T.S.I. TELECOMUNICACIÓN UNIVERSIDAD POLITÉCNICA DE MADRID */ #include <Arduino.h> #include <Adafruit_BMP280.h> /* The Adafruit BMP280 library is originally prepared for Arduino boards. In the following definitions we specify the exact lines used by our board for I2C communications. As originally prepared, the definitions match those for STM32 NUCLEO boards. If you use ESP32 boards, comment STM32 lines and uncomment ESP32 definitions. If you use Arduino boards, comment all these lines. */ // FOR ESP32 and AVR based boards //#define BMP_SCL 22 //#define BMP_SDA 21 //#define USER_BTN 18 // FOR STM32 boards #define BMP_SCL D15 #define BMP_SDA D14 Adafruit_BMP280 bmp; // Sensor object char text[200], text2[12]; int counter=0; float pressure, temperature, qnh=1013.25; /*We start with standard QNH*/ #define TEMPERATURE_FILTER_SIZE 5 float temperature_filter[TEMPERATURE_FILTER_SIZE]; float average_temperature; float average_temperature_fahrenheit; int n_temperature_data=0, index_temperature_data=0; #define QNH_FILTER_SIZE 30 float qnh_filter[QNH_FILTER_SIZE]; float average_qnh; float average_qnh_inHg; int n_qnh_data=0,index_qnh_data=0; float altitude=600; // <---- MODIFY to the actual one (for ETSIT, 670m) void setup() { Serial.begin(9600); delay(100); /*To assure everything initialices properly*/ Serial.println("Starting!"); if (!bmp.begin(0x76)) { Serial.println("ERROR: BMP280 does not initialize"); while (1); } Serial.println("BMP280 correctly initialized"); } void read_sensor (void) { pressure=bmp.readPressure()/100.0; /*Convert from Pa to hPa*/ temperature=bmp.readTemperature(); } void obtain_qnh (void) { float alt; /*Move QNH value until we match our actual altitude*/ alt=bmp.readAltitude(qnh); while (abs(altitude-alt)>0.25) { qnh+=(altitude-alt)/10.0; alt=bmp.readAltitude(qnh); } /*Some sensors have an offset error, we correct it here*/ qnh-=3.85; } void filter_temperature_data (void) { int i; /*First, we save current temperature sample*/ if (n_temperature_data<TEMPERATURE_FILTER_SIZE) temperature_filter[n_temperature_data++]=temperature; else { if (index_temperature_data==TEMPERATURE_FILTER_SIZE) index_temperature_data=0; temperature_filter[index_temperature_data++]=temperature; } /*Then, we calculate average temperature*/ average_temperature=0.0; for (i=0;i<n_temperature_data;i++) average_temperature+=temperature_filter[i]; average_temperature/=n_temperature_data; average_temperature_fahrenheit = (9/5)*average_temperature + 32; } void filter_qnh_data (void) { int i; /*First, we save current QNH sample*/ if (n_qnh_data<QNH_FILTER_SIZE) qnh_filter[n_qnh_data++]=qnh; else { if (index_qnh_data==QNH_FILTER_SIZE) index_qnh_data=0; qnh_filter[index_qnh_data++]=qnh; } /*Then, we calculate average QNH*/ average_qnh=0.0; for (i=0;i<n_qnh_data;i++) average_qnh+=qnh_filter[i]; average_qnh/=n_qnh_data; average_qnh_inHg = 0.02953*average_qnh; } void send_data (void) { /*Write reading number*/ sprintf(text,"%d:",counter++); /*Add actual Pressure*/ dtostrf(pressure,0,2,text2); sprintf(text,"%s actual pressure=%s hPa,",text,text2); /*Add filtered QNH in hPa and inHg*/ dtostrf(average_qnh,0,2,text2); sprintf(text,"%s filtered QNH=%s hPa,",text,text2); dtostfr(average_qnh_inHg,0,2,text2); sprintf(text,"%s filtered QNH=%s Hg,",text,text2); /*Add filtered temperature in ºC and ºF*/ dtostrf(average_temperature,0,1,text2); sprintf(text,"%s filtered temp=%s ºC",text,text2); dtostrf(average_temperature_fahrenheit,0,1,text2); sprintf(text,"%s filtereed temp?%s ºF",text,text2); /*Send the whole text via serial USB to the host computer*/ Serial.println(text); } void loop() { read_sensor(); obtain_qnh(); filter_temperature_data(); filter_qnh_data(); send_data(); delay(1000); }