/* * Meteostanice v2.2 * MaJkl 2014-2016 + kopice sikovnych lidi na internetu */ #include #include #include #include #include DS3231 rtc(SDA, SCL); #include "SparkFunBME280.h" BME280 BME280; //APDS #define DUMP_REGS #include APDS9930 apds = APDS9930(); uint16_t proximity_data = 0; uint16_t ch1 = 1, lux = 1; #include "fonts\allFonts.h" #include "fonts\Arial34.h" #include "fonts\Arial18.h" #include "fonts\Spec18.h" #define _sclk 52 #define _miso 50 #define _mosi 51 #define TFT_CS 10 #define TFT_DC 9 #define TFT_RST 8 ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC, TFT_RST); #define LCDLED 44 #define sendLED 42 #define recieveLED 43 #define SSID "XXXXXXXXXX" //SSID Wifi site #define PASS "YYYYYYYYYY" //Heslo #define TSKEY1 "xxxxxxxxxxxxxxxx" // Thingspeak Key 1 #define IP "184.106.153.149" // thingspeak.com //Definice pro externi senzory #define DataBits0 4 // Number of data0 bits to expect #define DataBits1 32 // Number of data1 bits to expect #define allDataBits 36 // Number of data sum 0+1 bits to expect // isrFlags bit numbers #define F_HAVE_DATA 0 // 0=Nothing in read buffer, 1=Data in read buffer #define F_GOOD_DATA 0 // 0=Unverified data, 1=Verified (2 consecutive matching reads) #define F_CARRY_BIT 3 // Bit used to carry over bit shift from one long to the other #define F_STATE 1 // 0=Sync mode, 1=Data mode // Constants const unsigned long sync_MIN = 4300; // Nastaveni casovani impulsu externich cidel const unsigned long sync_MAX = 4700; const unsigned long bit1_MIN = 2300; const unsigned long bit1_MAX = 2700; const unsigned long bit0_MIN = 1330; const unsigned long bit0_MAX = 1730; const unsigned long glitch_Length = 300; // Anything below this value is a glitch and will be ignored. // Interrupt variables unsigned long fall_Time = 0; // Placeholder for microsecond time when last falling edge occured. unsigned long rise_Time = 0; // Placeholder for microsecond time when last rising edge occured. byte bit_Count = 0; // Bit counter for received bits. unsigned long build_Buffer[] = {0,0}; // Placeholder last data packet being received. volatile unsigned long read_Buffer[] = {0,0}; // Placeholder last full data packet read. volatile byte isrFlags = 0; // Various flag bits int prijato; bool pripojeno = false; bool wifiok=false; Time cas; short minuta; bool povposlat = true; bool povprijem = false; float Pressure, aPressure; float temp1, humi1, tlak, atlak, vyska; //BME280 float temp1prum, temp12, humi1prum, humi12; int vl; //prumnerovani vlhkosti BME280 - kvuli desetinymu mistu float exttep[4]; short exthum[4], extbat[4], extkan[4], cidlo, extprijato[4]; long extcasprijmu[4]; //promene pro prumery float ptemp[4], phumi[4], ptlak, ptempold1[4], phumiold1[4], ptlakold1, ptempold2[4], phumiold2[4], ptlakold2; short cykprum, cykprepis, prum1, prum2, prum3; long casprum; String baterie[3] = {"G","F","E"}; //Baterie G 0%, F 50%, E 100% long casLCD; int stisk = 0; short LCDrezim = 1, LCDjas = 0, LCDjasreg = 255; bool LCDON = false; short x1, y1; void PinChangeISR0() { // Pin 2 (Interrupt 0) service routine if (povprijem == true) { unsigned long long Time = micros(); // Get current time if (digitalRead(2) == LOW) { // Falling edge if (Time > (rise_Time + glitch_Length)) { // Not a glitch Time = micros() - fall_Time; // Subtract last falling edge to get pulse time. if (bitRead(build_Buffer[1],31) == 1) bitSet(isrFlags, F_CARRY_BIT); else bitClear(isrFlags, F_CARRY_BIT); if (bitRead(isrFlags, F_STATE) == 1) { // Looking for Data if ((Time > bit0_MIN) && (Time < bit0_MAX)) { // 0 bit build_Buffer[1] = build_Buffer[1] << 1; build_Buffer[0] = build_Buffer[0] << 1; if (bitRead(isrFlags,F_CARRY_BIT) == 1) bitSet(build_Buffer[0],0); bit_Count++; } else if ((Time > bit1_MIN) && (Time < bit1_MAX)) { // 1 bit build_Buffer[1] = build_Buffer[1] << 1; bitSet(build_Buffer[1],0); build_Buffer[0] = build_Buffer[0] << 1; if (bitRead(isrFlags,F_CARRY_BIT) == 1) bitSet(build_Buffer[0],0); bit_Count++; } else { // Not a 0 or 1 bit so restart data build and check if it's a sync? bit_Count = 0; build_Buffer[0] = 0; build_Buffer[1] = 0; bitClear(isrFlags, F_GOOD_DATA); // Signal data reads dont' match bitClear(isrFlags, F_STATE); // Set looking for Sync mode if ((Time > sync_MIN) && (Time < sync_MAX)) { // Sync length okay bitSet(isrFlags, F_STATE); // Set data mode } } if (bit_Count >= allDataBits) { // All bits arrived bitClear(isrFlags, F_GOOD_DATA); // Assume data reads don't match if (build_Buffer[0] == read_Buffer[0]) { if (build_Buffer[1] == read_Buffer[1]) bitSet(isrFlags, F_GOOD_DATA); // Set data reads match } read_Buffer[0] = build_Buffer[0]; read_Buffer[1] = build_Buffer[1]; bitSet(isrFlags, F_HAVE_DATA); // Set data available bitClear(isrFlags, F_STATE); // Set looking for Sync mode digitalWrite(13,HIGH); // Used for debugging build_Buffer[0] = 0; build_Buffer[1] = 0; bit_Count = 0; } } else { // Looking for sync if ((Time > sync_MIN) && (Time < sync_MAX)) { // Sync length okay build_Buffer[0] = 0; build_Buffer[1] = 0; bit_Count = 0; bitSet(isrFlags, F_STATE); // Set data mode digitalWrite(13,LOW); // Used for debugging } } fall_Time = micros(); // Store fall time } } else { // Rising edge if (Time > (fall_Time + glitch_Length)) { // Not a glitch rise_Time = Time; // Store rise time } } } } void setup() { pinMode(LCDLED, OUTPUT); pinMode(sendLED, OUTPUT); pinMode(recieveLED, OUTPUT); attachInterrupt(0,PinChangeISR0,CHANGE); short radek=15; bool result = tft.begin(); tft.setRotation(iliRotation270); tft.fillScreen(ILI9341_BLACK); tft.setFont(Arial14); tft.printAligned(F("Meteostanice v2.2"), gTextAlignTopCenter); LEDON(6); vypistext("Inicializace LCD", 0,radek*1,ILI9341_BLUE); vypistext("OK", 200,15,ILI9341_LAWNGREEN); Serial.begin(9600); Serial1.begin(9600); rtc.begin(); //Start RTC modulu Serial1.println("AT"); vypistext("Test spojeni s ESP8266", 0,radek*2,ILI9341_BLUE); delay(2000); if (Serial1.find("OK")) { vypistext("OK", 200,radek*2, ILI9341_LAWNGREEN); vypistext("Pokus pripojeni WIFI", 0,radek*3, ILI9341_BLUE); Serial1.println("AT+CIPCLOSE=0"); delay(1000); pripojitWiFi(); if (pripojeno == true) { vypistext("OK", 200,radek*3, ILI9341_LAWNGREEN); vypistext("Synchronizace casu", 0,radek*4, ILI9341_BLUE); SyncCas(); vypistext("OK", 200,radek*4, ILI9341_LAWNGREEN); delay(1000); vypistext("Meteostanece bezi ONLINE", 0,radek*5, ILI9341_LAWNGREEN); wifiok=true; } else { vypistext("NOK", 200,radek*3, ILI9341_RED); vypistext("Cas nemusi byt spravny!", 0,radek*4, ILI9341_RED); wifiok=false; vypistext("Meteostanice bezi OFFLINE", 0,radek*5, ILI9341_RED); } } else { vypistext("NOK", 200,radek*2, ILI9341_RED); vypistext("ESP8266 nekomunikuje", 0,radek*3, ILI9341_RED); vypistext("Cas nemusi byt spravny!", 0,radek*4, ILI9341_RED); vypistext("Meteostanice pobezi OFFLINE", 0,radek*5, ILI9341_RED); } //----------------------------Start inicializace prox.cidla APDS9930------------------------------- vypistext("Inicializace APDS9930", 0,radek*6, ILI9341_BLUE); if ( apds.init() ) { vypistext("OK", 200,radek*6, ILI9341_LAWNGREEN); } else { vypistext("NOK", 200,radek*6, ILI9341_RED); } vypistext("Inicializace.svet.senz.", 0,radek*7, ILI9341_BLUE); if ( apds.enableLightSensor(false) ) { vypistext("OK", 200,radek*7, ILI9341_LAWNGREEN); } else { vypistext("NOK", 200,radek*7, ILI9341_RED); } vypistext("Inicializace.proximit.senz.", 0,radek*8, ILI9341_BLUE); if ( !apds.setProximityGain(PGAIN_1X) ) { vypistext("NOK", 200,radek*8, ILI9341_RED); } if ( apds.enableProximitySensor(false) ) { vypistext("OK", 200,radek*8, ILI9341_LAWNGREEN); } else { vypistext("NOK", 200,radek*8, ILI9341_RED); } //Start BME280 BME280.settings.commInterface = I2C_MODE; BME280.settings.I2CAddress = 0x76; BME280.settings.runMode = 3; //Normal mode BME280.settings.tStandby = 0; BME280.settings.filter = 0; BME280.settings.tempOverSample = 1; BME280.settings.pressOverSample = 1; BME280.settings.humidOverSample = 1; uint8_t memCounter = 0x80; uint8_t tempReadData; BME280.begin(); #ifdef DUMP_REGS uint8_t reg; uint8_t val; for(reg = 0x00; reg <= 0x19; reg++) { if( (reg != 0x10) && (reg != 0x11) ) { apds.wireReadDataByte(reg, val); } } apds.wireReadDataByte(0x1E, val); #endif delay(500); vypistext("Aktivace zbyvajicich cidel", 0,radek*9, ILI9341_BLUE); vypistext("Startuji Meteostanici", 0,radek*10, ILI9341_LAWNGREEN); delay(2000); tft.fillScreen(ILI9341_BLACK); pinMode(2,INPUT); casLCD = millis(); Serial.end(); } void loop() { //-------------------------Vykresleni zakladnich ramecku a popisku------------------------------ //horizontalni tft.drawRect (0, 0, 320, 240, ILI9341_WHITE); //ramecek displaye tft.drawLine(0, 36, 320, 36, ILI9341_WHITE); //linka pod hodinama tft.drawLine(0, 57, 320, 57, ILI9341_WHITE); //linka pod Doma, Venku tft.drawLine(0, 158, 320, 158, ILI9341_WHITE); //linka nad tlakem tft.setFont(Arial18); tft.setTextColor(ILI9341_GRAY, ILI9341_BLACK); tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); tft.printAt("Doma", 8, 39); tft.printAt("Venku",200,39); tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); //interni x1 = 75; y1 = 60; tft.drawLine(x1-6, y1-3, x1-6, y1+98, ILI9341_WHITE); tft.printAt("CH",x1,y1); tft.setFont(Spec18); tft.printAt("A",x1,y1+20); tft.printAt("B",x1,y1+40); vypistext("I",90,39, ILI9341_YELLOW); tft.setFont(Arial18); tft.printAt("RB",x1,y1+60); tft.printAt("TI",x1,y1+80); tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); tft.printAt("Atm. tlak(hPa):",8,y1+100); tft.printAt("Osvetleni(lux):",8,y1+120); //-------------------------Konec Vykresleni zakladnich ramecku a popisku------------------------------ tft.setFont(Arial34); tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); cas = rtc.getTime(); minuta = (cas.min); tft.printAt(rtc.getTimeStr(), 8, 3); tft.printAt(rtc.getDateStr(), 155, 3); tft.setFont(Arial18); //Resetovani promenych pro prijem 433 pri preteceni micros if ((micros() > 0) && (micros() < 1000000)) { povprijem = false; Resetprijimace(); } else povprijem = true; //Ovladani osvetleni LCD pomoci proximity senzoru !apds.readProximity(proximity_data); if (proximity_data > 1000) { stisk++; if (LCDON == false) LEDON(4); if (stisk > 1) LCDrezim = 2; if (stisk <= 1) LCDrezim = 1; casLCD = millis(); } else stisk=0; //Cast pro ovladani osvetleni tft.setFont(Spec18); if (LCDrezim == 1) vypistext("C",70,39, ILI9341_YELLOW); if (stisk > 2) LCDrezim = 2; if (millis() > (casLCD + 10000) && (LCDrezim == 1)) LEDOFF(4); if (LCDrezim == 2) { vypistext("D",70,39, ILI9341_YELLOW); }; //Nastaveni jasu pomoci senzoru osvetleni !apds.readCh1Light(lux); ch1 = constrain((lux*5)+20,50,255); if (LCDjasreg < ch1) LCDjasreg = LCDjasreg+5; if (LCDjasreg > ch1) LCDjasreg = LCDjasreg-5; if (LCDON == true) analogWrite(LCDLED, LCDjasreg); tft.setFont(Arial18); // Mereni teploty, vlhkosti, tlaku temp1 = ((BME280.readTempC()) - 1.4); //korekce teploty BMP vs DHT22 humi1 = BME280.readFloatHumidity(); atlak = ((BME280.readFloatPressure())/100); vyska = BME280.readFloatAltitudeMeters(); Pressure = ((atlak-2)/pow(1-(408/44330.0),5.255)); //korekce -2 asi+- a s vyskou 408m.n.m - byl rozdil oproti BMP180 { //prumerovani vlhkosti na desetiny mista vl++; temp1prum = temp1prum + temp1; humi1prum = humi1prum + humi1; if (vl==5) { temp12 = temp1prum/5; humi12 = humi1prum/5; temp1prum = 0; humi1prum = 0; vl=0; } } digitalWrite(recieveLED, LOW); //Kod pro externi cidla zacatek unsigned long myData0 = 0; unsigned long myData1 = 0; if (bitRead(isrFlags,F_GOOD_DATA) == 1) { digitalWrite(recieveLED, HIGH); myData0 = read_Buffer[0]; // Read the data spread over 2x 32 variables myData1 = read_Buffer[1]; bitClear(isrFlags,F_HAVE_DATA); // Flag we have read the data dec2binLong(myData0,DataBits0); dec2binLong(myData1,DataBits1); byte H = ((myData1 >> 24) & 0x3) + 1; // Get Channel cidlo = H; if (cidlo==1) { extkan[1]=H; extcasprijmu[1] = millis(); extprijato[1] = 2; tft.setFont(Spec18); vypistext("H",108+35,60, ILI9341_LAWNGREEN); tft.setFont(Arial18); }; if (cidlo==2) { extkan[2]=H; extcasprijmu[2] = millis(); extprijato[2] = 2; tft.setFont(Spec18); vypistext("H",180+35,60, ILI9341_LAWNGREEN); tft.setFont(Arial18); }; if (cidlo==3) { extkan[3]=H; extcasprijmu[3] = millis(); extprijato[3] = 2; tft.setFont(Spec18); vypistext("H",251+35,60, ILI9341_LAWNGREEN); tft.setFont(Arial18); }; H = (myData1 >> 26) & 0x3; // Get Battery if (cidlo==1) extbat[1]=H; if (cidlo==2) extbat[2]=H; if (cidlo==3) extbat[3]=H; byte ML = (myData1 >> 12) & 0xF0; // Get MMMM H = (myData1 >> 12) & 0xF; // Get LLLL ML = ML | H; // OR MMMM & LLLL nibbles together H = (myData1 >> 20) & 0xF; // Get HHHH byte HH = 0; if((myData1 >> 23) & 0x1 == 1) //23 bit HH = 0xF; int Temperature = (H << 8)|(HH << 12) | ML; // Combine HHHH MMMMLLLL // Temperature = Temperature*3; //F // Remove Constant offset if (cidlo==1) exttep[1]=(float) Temperature / 10; if (cidlo==2) exttep[2]=(float) Temperature / 10; if (cidlo==3) exttep[3]=(float) Temperature / 10; H = (myData1 >> 0) & 0xF0; // Get HHHH ML = (myData1 >> 0) & 0xF; // Get LLLL ML = ML | H; // OR HHHH & LLLL nibbles together if (cidlo==1) exthum[1]=(ML); if (cidlo==2) exthum[2]=(ML); if (cidlo==3) exthum[3]=(ML); } //Kod pro externi cidla konec x1=1; y1=80; tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); napistext(String(temp12), x1, y1,5); napistext(String(humi12), x1, y1+20,5); napistext(String(rosnyBod(temp1, humi1)), x1,y1+40,5); napistext(String(teplotniIndex(temp1, humi1)), x1,y1+60,5); tft.setFont(Spec18); if ((ptempold1[0]-ptempold2[0]) > 0.01) tft.printAt("L",x1+53, y1); if (((ptempold1[0]-ptempold2[0]) <= 0.01) && (ptempold1[0]-ptempold2[0]) >= -0.01) tft.printAt("M",x1+53, y1); if ((ptempold1[0]-ptempold2[0]) < -0.01) tft.printAt("N",x1+53, y1); if ((phumiold1[0]-phumiold2[0]) > 0.01) tft.printAt("L",x1+53, y1+20); if (((phumiold1[0]-phumiold2[0]) <= 0.01) && (phumiold1[0]-phumiold2[0]) >= -0.01) tft.printAt("M",x1+53, y1+20); if ((phumiold1[0]-phumiold2[0]) < -0.01) tft.printAt("N",x1+53, y1+20); tft.setFont(Arial18); //vnitrni hodnoty x1=130; y1=160; napistext(String(Pressure), x1, y1, 7); tft.setFont(Spec18); if ((ptlakold1-ptlakold2) > 0.02) tft.printAt("L",x1+73, y1); if (((ptlakold1-ptlakold2) <= 0.02) && (ptlakold1-ptlakold2) >= -0.02) tft.printAt("M",x1+73, y1); if ((ptlakold1-ptlakold2) < -0.02) tft.printAt("N",x1+73, y1); tft.setFont(Arial18); napistext(String(lux), 145, 180, 4 ); //ext.kanal 1 x1=108; y1=80; tft.drawLine(x1-4, y1-23, x1-4, y1+78, ILI9341_WHITE); if (extprijato[1] == 2) tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); if (extprijato[1] == 1) tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); if (extprijato[1] == 0) tft.setTextColor(ILI9341_RED, ILI9341_BLACK); napistext(String(exttep[1]),x1, y1, 5); napistext(String(exthum[1]),x1, y1+20, 5); napistext(String(rosnyBod(exttep[1], exthum[1])), x1, y1+40, 5); tft.printAt(String(extkan[1]),x1+10, y1-20); tft.setFont(Spec18); if ((ptempold1[1]-ptempold2[1]) > 0.01) tft.printAt("L",x1+53, y1); if (((ptempold1[1]-ptempold2[1]) <= 0.01) && (ptempold1[1]-ptempold2[1]) >= -0.01) tft.printAt("M",x1+53, y1); if ((ptempold1[1]-ptempold2[1]) < -0.01) tft.printAt("N",x1+53, y1); if ((phumiold1[1]-phumiold2[1]) > 0.01) tft.printAt("L",x1+53, y1+20); if (((phumiold1[1]-phumiold2[1]) <= 0.01) && (phumiold1[1]-phumiold2[1]) >= -0.01) tft.printAt("M",x1+53, y1+20); if ((phumiold1[1]-phumiold2[1]) < -0.01) tft.printAt("N",x1+53, y1+20); tft.printAt(baterie[extbat[1]],x1+23,y1-20); if ((millis() > (extcasprijmu[1] + 5000))) tft.printAt("I", x1+35, y1-20); tft.setFont(Arial18); //ext.kanal 2 x1=180; y1=80; tft.drawLine(x1-4, y1-23, x1-4, y1+78, ILI9341_WHITE); if (extprijato[2] == 2) tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); if (extprijato[2] == 1) tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); if (extprijato[2] == 0) tft.setTextColor(ILI9341_RED, ILI9341_BLACK); napistext(String(exttep[2]),x1, y1, 5); napistext(String(exthum[2]),x1, y1+20, 5); napistext(String(rosnyBod(exttep[2], exthum[2])), x1, y1+40, 5); tft.printAt(String(extkan[2]),x1+10, y1-20); tft.setFont(Spec18); if ((ptempold1[2]-ptempold2[2]) > 0.01) tft.printAt("L",x1+53, y1); if (((ptempold1[2]-ptempold2[2]) <= 0.01) && (ptempold1[2]-ptempold2[2]) >= -0.01) tft.printAt("M",x1+53, y1); if ((ptempold1[2]-ptempold2[2]) < -0.01) tft.printAt("N",x1+53, y1); if ((phumiold1[2]-phumiold2[2]) > 0.01) tft.printAt("L",x1+53, y1+20); if (((phumiold1[2]-phumiold2[2]) <= 0.01) && (phumiold1[2]-phumiold2[2]) >= -0.01) tft.printAt("M",x1+53, y1+20); if ((phumiold1[2]-phumiold2[2]) < -0.01) tft.printAt("N",x1+53, y1+20); tft.printAt(baterie[extbat[2]],x1+23, y1-20); if ((millis() > (extcasprijmu[2] + 5000))) tft.printAt("I", x1+35, y1-20); tft.setFont(Arial18); //ext.kanal 3 x1=251; y1=80; tft.drawLine(x1-4, y1-23, x1-4, y1+78, ILI9341_WHITE); if (extprijato[3] == 2) tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); if (extprijato[3] == 1) tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); if (extprijato[3] == 0) tft.setTextColor(ILI9341_RED, ILI9341_BLACK); napistext(String(exttep[3]),x1, y1, 5); napistext(String(exthum[3]),x1, y1+20, 5); napistext(String(rosnyBod(exttep[3], exthum[3])), x1, y1+40, 5); tft.printAt(String(extkan[3]),x1+10, y1-20); tft.setFont(Spec18); if ((ptempold1[3]-ptempold2[3]) > 0.01) tft.printAt("L",x1+53, y1); if (((ptempold1[3]-ptempold2[3]) <= 0.01) && (ptempold1[3]-ptempold2[3]) >= -0.01) tft.printAt("M",x1+53, y1); if ((ptempold1[3]-ptempold2[3]) < -0.01) tft.printAt("N",x1+53, y1); if ((phumiold1[3]-phumiold2[3]) > 0.01) tft.printAt("L",x1+53, y1+20); if (((phumiold1[3]-phumiold2[3]) <= 0.01) && (phumiold1[3]-phumiold2[3]) >= -0.01) tft.printAt("M",x1+53, y1+20); if ((phumiold1[3]-phumiold2[3]) < -0.01) tft.printAt("N",x1+53, y1+20); tft.printAt(baterie[extbat[3]],x1+23, y1-20); if ((millis() > (extcasprijmu[3] + 5000))) tft.printAt("I", x1+35, y1-20); tft.setFont(Arial18); //Kontrola prodlevy mezi prijmem dat 433MHz po 90s if ((millis() > (extcasprijmu[1] + 90000)) && (extprijato[1] == 2)) extprijato[1] = 1; if ((millis() > (extcasprijmu[2] + 90000)) && (extprijato[2] == 2)) extprijato[2] = 1; if ((millis() > (extcasprijmu[3] + 90000)) && (extprijato[3] == 2)) extprijato[3] = 1; //Kontrola prodlevy mezi prijmem dat 433MHz po 180s if (millis() > (extcasprijmu[1] + 180000)) { extprijato[1] = 0; extkan[1] = 0; }; if (millis() > (extcasprijmu[2] + 180000)) { extprijato[2] = 0; extkan[2] = 0; }; if (millis() > (extcasprijmu[3] + 180000)) { extprijato[3] = 0; extkan[3] = 0; }; // ------------- Cast pro prumerovani teplot vlhkosti a tlaku { if ((millis() > (casprum + 5000))) { cykprum++; ptemp[1] = ptemp[1] + exttep[1]; ptemp[2] = ptemp[2] + exttep[2]; ptemp[3] = ptemp[3] + exttep[3]; ptemp[0] = ptemp[0] + temp1; phumi[1] = phumi[1] + exthum[1]; phumi[2] = phumi[2] + exthum[2]; phumi[3] = phumi[3] + exthum[3]; phumi[0] = phumi[0] + humi1; ptlak = ptlak + Pressure; if (cykprum == 60) { for (cykprepis=0;cykprepis < 4;cykprepis++) { ptempold2[cykprepis] = ptempold1[cykprepis]; phumiold2[cykprepis] = phumiold1[cykprepis]; ptempold1[cykprepis] = (ptemp[cykprepis])/60; phumiold1[cykprepis] = (phumi[cykprepis])/60; ptemp[cykprepis] = 0; phumi[cykprepis] = 0; } ptlakold2 = ptlakold1; ptlakold1 = ptlak/60; ptlak = 0; cykprum = 0; } tft.setTextColor(ILI9341_LAWNGREEN, ILI9341_BLACK); casprum = millis(); } } //Odesilani dat na TS kazdych 5 minut if ((minuta%5 == 0) && (povposlat == true)) { digitalWrite(sendLED, HIGH); poslidata1(String(temp12),String(humi12),String(Pressure),String(exttep[1]),String(exthum[1]),String(exttep[2]),String(exthum[2]),String(exttep[3])); povposlat = false; } else digitalWrite(sendLED, LOW); if (minuta%5 != 0) povposlat = true; } void poslidata1(String TeplIN, String VlhIN, String Tlak, String TeplOUT1, String VlhOUT1, String TeplOUT2, String VlhOUT2, String TeplOUT3) { tft.setFont(Spec18); vypistext("H",90,39, ILI9341_YELLOW); tft.setFont(Arial18); String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += IP; cmd += "\",80"; Serial1.println(cmd); if (Serial1.find("Error")) { return; } cmd = "GET /update?key="; cmd += TSKEY1; if (temp12 > 0) { cmd += "&field1="; cmd += TeplIN; cmd += "&field2="; cmd += VlhIN; } if (Pressure > 950) { cmd += "&field3="; cmd += Tlak; } if (extkan[1] != 0) { cmd += "&field4="; cmd += TeplOUT1; cmd += "&field5="; cmd += VlhOUT1; } if (extkan[2] != 0) { cmd += "&field6="; cmd += TeplOUT2; cmd += "&field7="; cmd += VlhOUT2; } if (extkan[3] != 0) { cmd += "&field8="; cmd += TeplOUT3; } cmd += "\r\n"; Serial1.print("AT+CIPSEND="); Serial1.println(cmd.length()); if(Serial1.find('>')) { Serial1.print(cmd); } else { Serial1.println("AT+CIPCLOSE"); } tft.setFont(Spec18); vypistext("I",90,39, ILI9341_YELLOW); tft.setFont(Arial18); } void LEDOFF(int rychlost) //Stmivani podsviceni LCD { LCDjas = LCDjasreg; LCDON = false; LCDrezim = 0; for (LCDjas; LCDjas != 0; LCDjas--) { analogWrite(LCDLED, LCDjas); delay(rychlost); } digitalWrite (LCDLED, LOW); LCDjasreg = ch1; tft.fillScreen(ILI9341_BLACK); //smazani displaye kvuli artefaktum tft.setFont(Spec18); vypistext("C",70,39, ILI9341_YELLOW); tft.setFont(Arial18); } void LEDON(int rychlost) //Rozsviceni LCD { for (LCDjas ; LCDjas < LCDjasreg; LCDjas++) { analogWrite(LCDLED, LCDjas); delay(rychlost); } LCDON = true; } //Funkce pro zobrazeni ciselnych hodnot tak aby nezustavaly na LCD zbytky cisel - sakra, stejne nekdy zustavaj void napistext(String Co, short x, short y, short znaku) { tft.setTextArea(x, y, (znaku*10), 10); tft.printAligned(Co, gTextAlignTopRight, 10, 0); tft.setTextArea(0, 0, 320, 240); } void vypistext(String Co, int x, int y, uint16_t Barva) { tft.setTextColor(Barva, ILI9341_BLACK); tft.printAt(Co, x, y); } boolean pripojitWiFi() { Serial1.println("AT+CWMODE=1"); delay(2000); char a; String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\""; Serial1.println(cmd); Serial1.end(); Serial.println("Pokus o pripojeni k Wifi"); Serial1.begin(9600); while(true) { a = Serial1.read(); // nacteni znaku prijateho po seriove lince if(a == 'O') // je-li prijat znak 'O'k { Serial.println("Uspesne pripojeno k WiFi"); pripojeno = true; return true; break; } if(a == 'F') // je-li prijat znak 'F'ail { Serial.println("Nelze se pripojit k WiFi"); pripojeno = false; return false; break; } } } //Resetovani promenych pro prijem 433MHz kvuli pretekani micros void Resetprijimace() { fall_Time = 0; rise_Time = 0; bit_Count = 0; build_Buffer[0] = 0; build_Buffer[1] = 0; read_Buffer[0] = 0; read_Buffer[1] = 0; isrFlags = 0; } void SyncCas() { Serial1.println("AT+CIPNTP=2"); //Letni cas 2, Zimni cas 1 - Vyresit jednoduzsi prepinani Letni/zimni cas, jumper na desce?? delay(500); while(Serial1.available()) { Serial1.read(); } String result = ""; Serial1.println("AT+CIPNTP?"); if(Serial1.findUntil("Time: ", " ")) { result = Serial1.readStringUntil('\n'); if(result.length() == 26) { char timeArray[26]; result.toCharArray(timeArray, 26); //vyresit lip, sice to funguje ale je to hnus //pridana vterina kvuli zpozdeni, v pripade synchronizace v 59vterine, nastaveni neprobehne - VYRESIT!! rtc.setTime(((timeArray[0] - '0') * 10) + (timeArray[1] - '0'),((timeArray[3] - '0') *10) + (timeArray[4] - '0'),(((timeArray[6] - '0') *10) + (timeArray[7] - '0')) + 1); //+1vterina rtc.setDate(((timeArray[12] - '0') *10) + (timeArray[13] - '0'),((timeArray[9] - '0') *10) + (timeArray[10] - '0'),(((timeArray[17] - '0') *10) + (timeArray[18] - '0')+2000)); } } } double rosnyBod(double teplota, double vlhkost) { double A0= 373.15/(273.15 + teplota); double SUM = -7.90298 * (A0-1); SUM += 5.02808 * log10(A0); SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; SUM += log10(1013.246); double VP = pow(10, SUM-3) * vlhkost; double T = log(VP/0.61078); // temp var return (241.88 * T) / (17.558-T); } double teplotniIndex(double teplota, double vlhkost) { double c1 = -42.379, c2 = 2.04901523, c3 = 10.14333127, c4 = -0.22475541, c5= -6.83783e-3, c6=-5.481717e-2, c7=1.22874e-3, c8=8.5282e-4, c9=-1.99e-6 ; double Tc = teplota; double Tf; double R = vlhkost; Tf = (((9*Tc)/5)+32); double rv = ((c1+(c2*Tf))+(c3*R)+(c4*Tf*R)+(c5*(Tf*Tf))+(c6*(R*R))+(c7*((Tf*Tf)*R))+(c8*(Tf*(R*R)))+(c9*((Tf*Tf)*(R*R)))); double rvc = (5*(rv-32)/9); return rvc; } void dec2binLong(unsigned long myNum, byte NumberOfBits) //dekodovani BIN na DEC pro externi cidla { if (NumberOfBits <= 32) { myNum = myNum << (32 - NumberOfBits); for (int i=0; i