/* * Barometer_Sensor_nenei.ino * Example sketch for barometer. * Measure pressure and temperature, calculate time variation of pressure and display them. * * Website : masserv.utcluj.ro/~florind/microcontrollers/ * Author : Florin Dragan, UTC-N * Create Time: 12 february 2015 * Change Log : * * Based on libraries created on 2012 by seeed technology inc. Copyright (c) * Website : www.seeed.cc * Author : Jim Lindblom, LG * Create Time: 2012 * Change Log : * * The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #undef max #undef min #include #include #include "time.h" #include //BMP280 #include #include #include #include #define P_scale 5 // pressure graphical display scale #define dP_scale 2 // pressure variation graphical display scale #define FONT_SPACE 7 const char *ssid[] = {"Acasuca", "Acasuca_2.4G", "masserv-1"}; const char *password[]={"pavlov61ap7", "kNY2ph3mmrnT6zk4", "protocoale"}; //const char* ntpServer = "pool.ntp.org"; const unsigned long GMT_Offset = 7200; // in seconds const unsigned long daylightOffset = 3600; // in seconds // You can specify the time server pool and the offset (in seconds, can be // changed later with setTimeOffset() ). Additionaly you can specify the // update interval (in milliseconds, can be changed using setUpdateInterval() ). // Array of days char *daysOfWeek[] = { "Dum", "Lun", "Mar", "Mie", "Joi", "Vin", "Sam" }; unsigned char ss, ss_0 = 1, mm, hh, d, wd, m ; //DATE/TIME variable unsigned int y ; //DATE/TIME variable boolean start_flag = 1, un_minut_flag = 1, sase_minute_flag = 1; float temperature = 0, temperature_0; unsigned long pressure, pressure_0, pressure_init; float atm, atm_0; unsigned long m_pressure = 0; unsigned long ts; // unsigned char T_min_0 = 0, T_min = 0; //used as "time period" to calculate dP/dt int delta_P; long buff_P[240]; // pressure buffer, 240 samples lengh BMP280 bmp280; struct tm timeinfo; void setup(){ Serial.begin(115200); // Connect to Wi-Fi connectToWiFi(); // Init and get the NTP time adjustTime(); ReadDateTime(); // Wire.begin(); // Not neede because next function do it if (!bmp280.init()) { Serial.println("Device not connected or broken!"); } unsigned char samples = 32; // number of reading samples while (samples--){ temperature += bmp280.getTemperature(); //Get the temperature, MUST be called first } temperature = temperature / samples; delay(10); samples = 32; // number of reading samples while (samples--){ pressure = bmp280.getPressure(); //Get 32 pressure samples m_pressure = m_pressure + pressure; // add 32 samples } pressure_init = m_pressure >> 5; // divide by 2^5 = 32 number of samples pressure = pressure_init; for (unsigned char j=0; j<240; j++){ // populate the buffer with initial read pressure value buff_P[j] = pressure_init; } Tft.TFTinit(); // init TFT library and SPI Tft.drawString("Soare+", 0, 0, 2, YELLOW); // draw fixed display strings Tft.drawString("Ploaie-", 0, 230, 2, GRAY2); Tft.drawString("Pres:", 0, 255, 2, GRAY1); Tft.drawString("hPa", 172, 250, 2, CYAN); Tft.drawString("atm", 172, 268, 2, CYAN); Tft.drawString("Temp:", 0, 304, 2, GRAY1); Tft.drawString("'C", 172, 304, 2, CYAN); Tft.drawString("dP/dt:", 0, 286, 2, GRAY1); Tft.drawString("hPa/h", 172, 286, 2, CYAN); Tft.drawFloat(float(pressure_init)/100, 2, 0, 80, 1, GRAY1); // draw the initial value as a reference axis Tft.drawString("dP", 0, 170, 1, GRAY1); } void loop() { // Get Date/Time to be used to run each minute and each 6 minutes getLocalTime(&timeinfo); // Run each minute if (((timeinfo.tm_sec == 0) && (ss_0 != 0)) || start_flag==1) { // ss_0 = timeinfo.tm_sec; temperature_0 = temperature; temperature = bmp280.getTemperature(); //Get the temperature, MUST be called first pressure_0 = pressure; m_pressure = 0; unsigned char samples = 32; while (samples--){ pressure = bmp280.getPressure();//Get the pressure m_pressure = m_pressure + pressure; } pressure = m_pressure >> 5; atm_0 = atm; // pressure on "atm" units atm = float(pressure) / 101325; // 1 atm = 101325 Pa unsigned char Pos_X = 100, Char_size = 1; // Clear previously displayed values Tft.drawFloat(float(pressure_0)/100, 2, 80, 250,2, BLACK); // Tft.drawNumber(T_min_0, 204, 0, 2, BLACK); // Clear Date/Time // Date Tft.drawString(daysOfWeek[wd], Pos_X, 0, Char_size, BLACK); Pos_X += 4*FONT_SPACE*Char_size ; if (d<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(d, Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(d, Pos_X, 0, Char_size, BLACK); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar('-', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; if (m<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(m, Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(m, Pos_X, 0, Char_size, BLACK); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar('-', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; if (y<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(y, Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(y, Pos_X, 0, Char_size, BLACK); Pos_X += 4*FONT_SPACE*Char_size ; } Pos_X += 4*Char_size ; // Time if (hh<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(hh, Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(hh, Pos_X, 0, Char_size, BLACK); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar(':', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; if (mm<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(mm, Pos_X, 0, Char_size, BLACK); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(mm, Pos_X, 0, Char_size, BLACK); Pos_X += 2*FONT_SPACE*Char_size ; } /* Tft.drawChar(':', Pos_X, 0, Char_size, BLACK); Pos_X += 8*Char_size ; if (ss<10) { Tft.drawChar('0', Pos_X, 0, Char_size, BLACK); Pos_X += 8*Char_size ; Tft.drawNumber(ss, Pos_X, 0, Char_size, BLACK); Pos_X += 8*Char_size ; } else { Tft.drawNumber(ss, Pos_X, 0, Char_size, BLACK); } */ Tft.drawFloat(atm_0,4, 80, 268, 2, BLACK); Tft.drawFloat(temperature_0, 2, 80, 304, 2, BLACK); // Read DATE/TIME from NTP Server if ((&timeinfo, "%H:%M:%S") == "14:40:00" ) { adjustTime(); } ReadDateTime(); // Display last calculated values Tft.drawFloat(float(pressure)/100, 2, 80, 250, 2, GREEN); // Tft.drawNumber(T_min, 204, 0, 2, GREEN); // Displays Date/Time // Date Pos_X = 100 ; Tft.drawString(daysOfWeek[wd], Pos_X, 0, Char_size, GREEN); Pos_X += 4*FONT_SPACE*Char_size ; if (d<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(d, Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(d, Pos_X, 0, Char_size, GREEN); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar('-', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; if (m<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(m, Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(m, Pos_X, 0, Char_size, GREEN); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar('-', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; if (y<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(y, Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(y, Pos_X, 0, Char_size, GREEN); Pos_X += 4*FONT_SPACE*Char_size ; } Pos_X += 4*Char_size ; // Time if (hh<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(hh, Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(hh, Pos_X, 0, Char_size, GREEN); Pos_X += 2*FONT_SPACE*Char_size ; } Tft.drawChar(':', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; if (mm<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; Tft.drawNumber(mm, Pos_X, 0, Char_size, GREEN); Pos_X += FONT_SPACE*Char_size ; } else { Tft.drawNumber(mm, Pos_X, 0, Char_size, GREEN); Pos_X += 2*FONT_SPACE*Char_size ; } /* Tft.drawChar(':', Pos_X, 0, Char_size, GREEN); Pos_X += 8*Char_size ; if (ss<10) { Tft.drawChar('0', Pos_X, 0, Char_size, GREEN); Pos_X += 8*Char_size ; Tft.drawNumber(ss, Pos_X, 0, Char_size, GREEN); Pos_X += 8*Char_size ; } else { Tft.drawNumber(ss, Pos_X, 0, Char_size, GREEN); } */ Tft.drawFloat(atm,4, 80, 268, 2, GREEN); Tft.drawFloat(temperature, 2, 80, 304, 2, BRIGHT_RED); // Run each 6 minutes if ((mm %6 == 0) && (timeinfo.tm_sec == 0) || start_flag == 1 ) { Serial.println(&timeinfo, "%T"); int dP; // Clear previously displayed pressure P values and points for (unsigned char j=0; j<240; j++){ Tft.setPixel(239-j,90 - ((buff_P[j]-pressure_init)/P_scale), BLACK); // Clear previously displayed pressure dP/dt values and points dP = buff_P[j] - buff_P[j+1]; if (dP < 2 && dP > -2) dP = 0; Tft.setPixel(239-j, 180 - (dP/dP_scale), BLACK); } Tft.drawFloat(float(delta_P)/100,2, 80, 286, 2, BLACK); // Shift right 1 location each sample value, to create a FIFO queue for (unsigned char j=239; j>0; j--){ buff_P[j] = buff_P[j-1] ; //deplaseaza elementele cu 1 pozitie spre dreapta } buff_P[0] = pressure; delta_P = buff_P[0] - buff_P[1]; //calculate dP from two consecutive values for (unsigned char j=0; j<240; j++){ // Displays P(t) pressure time variation graph Tft.setPixel(239-j, 90 - ((buff_P[j]-pressure_init)/P_scale), GREEN); dP = buff_P[j] - buff_P[j+1]; if (dP < 2 && dP > -2) dP = 0; Tft.setPixel(239-j, 180 - (dP/dP_scale), YELLOW); //afiseaza semnalul in timp } Tft.drawFloat(float(delta_P)/100,2, 80, 286, 2, YELLOW); } //end run each 6 minutes // T_min_0 = T_min; // T_min = T_min+1; start_flag = 0; } //end run each minute ss_0 = timeinfo.tm_sec; } //end loop /* Functions and Macros */ void connectToWiFi() { Serial.println("Connectare la WiFi:"); for (byte s=0; s