Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TFT_HX8357.h> // Hardware-specific library
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #define BME_SCK 52
- #define BME_MISO 50
- #define BME_MOSI 51
- #define BME_CS 9
- Adafruit_BME280 bme(BME_CS); // hardware SPI
- #include <RF24.h>
- #define CE_PIN 7
- #define CSN_PIN 8
- const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
- RF24 radio(CE_PIN, CSN_PIN);
- TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library
- int previousRemoteTemperature = 0;
- int previousRemoteHumidity = 0;
- int previousRemotePressure = 0;
- float previousRemoteRain = 0.00;
- int previousRemoteHours = 0;
- int previousRemoteMinutes = 0;
- int previousRemoteDay = 0;
- int previousRemoteMonth = 0;
- int previousRemoteYear = 0;
- int previousIndoorTemperature = 0;
- int previousIndoorHumidity = 0;
- int previousIndoorPressure = 0;
- int remoteTemperature = 0;
- int remoteHumidity = 0;
- int remotePressure = 0;
- float remoteRain = 0.00;
- int remoteHours = 0;
- int remoteMinutes = 0;
- int remoteDay = 0;
- int remoteMonth = 0;
- int remoteYear = 0;
- int indoorTemperature = 0;
- int indoorHumidity = 0;
- int indoorPressure = 0;
- struct package
- {
- int temperature;
- int pressure;
- int humidity;
- float rain;
- int rf_hh;
- int rf_mm;
- int rf_dd;
- int rf_mo;
- int rf_yy;
- };
- typedef struct package Package;
- Package data;
- void setup()
- {
- pinMode(53, OUTPUT);
- Serial.begin(115200);
- Serial.println(F("Start"));
- Serial.println(F("BME280 test"));
- bool status;
- status = bme.begin();
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- Serial.println();
- tft.init();
- tft.setRotation(0);
- tft.fillScreen(TFT_BLACK);
- tft.setTextFont(1); // Select font 1 which is the Adafruit GLCD font
- delay(100);
- startWirelessCommunication();
- printUI();
- delay(1000);
- }
- void loop()
- {
- checkForWirelessData();
- updating();
- }
- void startWirelessCommunication()
- {
- Serial.println(F("Start Wireless Comm"));
- radio.begin();
- radio.setDataRate( RF24_250KBPS ) ;
- radio.openReadingPipe(1, thisSlaveAddress);
- radio.startListening();
- delay(100);
- }
- void checkForWirelessData()
- {
- if ( radio.available())
- {
- while (radio.available())
- {
- radio.read( &data, sizeof(data) );
- remoteTemperature = data.temperature;
- remotePressure = data.pressure;
- remoteHumidity = data.humidity;
- remoteRain = data.rain;
- remoteHours = data.rf_hh;
- remoteMinutes = data.rf_mm;
- remoteDay = data.rf_dd;
- remoteMonth = data.rf_mo;
- remoteYear = data.rf_yy;
- }
- Serial.print("\nRadio Package:");
- Serial.print("\n");
- Serial.println(data.temperature);
- Serial.println(data.pressure);
- Serial.println(data.humidity);
- Serial.println(data.rain);
- Serial.println(data.rf_hh);
- Serial.println(data.rf_mm);
- Serial.println(data.rf_dd);
- Serial.println(data.rf_mo);
- Serial.println(data.rf_yy);
- }
- }
- void updating()
- {
- const unsigned long oneMinutes = 1 * 30 * 1000UL;
- static unsigned long lastSampleTime = 0 - oneMinutes; // initialize such that a reading is due the first time through loop()
- unsigned long now = millis();
- if (now - lastSampleTime >= oneMinutes)
- {
- lastSampleTime += oneMinutes;
- readSensor();
- printIndoorTemperature();
- printIndoorPressure();
- printIndoorHumidity();
- printRemoteTemperature();
- printRemotePressure();
- printRemoteHumidity();
- printRemoteHours();
- printRemoteMinutes();
- printRemoteDay();
- printRemoteMonth();
- printRemoteYear();
- printRemoteRain();
- }
- }
- void readSensor()
- {
- previousIndoorTemperature = indoorTemperature;
- previousIndoorPressure = indoorPressure;
- previousIndoorHumidity = indoorHumidity;
- indoorTemperature = bme.readTemperature();
- indoorPressure = (bme.readPressure() / 100.0F);
- indoorHumidity = bme.readHumidity();
- }
- void printIndoorTemperature()
- {
- if(indoorTemperature != previousIndoorTemperature)
- {
- tft.fillRect(115, 145, 100, 30, TFT_BLACK);
- tft.setCursor(115,145);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print(indoorTemperature-2);
- Serial.print("Temp. Inside: ");
- Serial.println(indoorTemperature);
- previousIndoorTemperature = indoorTemperature;
- }
- }
- void printIndoorPressure()
- {
- if(indoorPressure != previousIndoorPressure)
- {
- tft.fillRect(115, 235, 100, 30, TFT_BLACK);
- tft.setCursor(115,235);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print(indoorPressure);
- Serial.print("Pressure Inside: ");
- Serial.println(indoorPressure);
- previousIndoorPressure = indoorPressure;
- }
- }
- void printIndoorHumidity()
- {
- if(indoorHumidity != previousIndoorHumidity)
- {
- tft.fillRect(115, 190, 100, 30, TFT_BLACK);
- tft.setCursor(115,190);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print(indoorHumidity);
- Serial.print("Humidity Inside: ");
- Serial.println(indoorHumidity);
- previousIndoorHumidity = indoorHumidity;
- }
- }
- void printRemoteTemperature()
- {
- if (remoteTemperature != previousRemoteTemperature)
- {
- tft.fillRect(130, 344, 100, 30, TFT_BLACK);
- tft.setCursor(130, 344);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print(remoteTemperature-2);
- Serial.print("Temp. Outside: ");
- Serial.println(remoteTemperature);
- previousRemoteTemperature = remoteTemperature;
- }
- }
- void printRemotePressure()
- {
- if (remotePressure != previousRemotePressure)
- {
- tft.fillRect(130, 409, 100, 30, TFT_BLACK);
- tft.setCursor(130, 409);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print(remotePressure);
- Serial.print("Pressure Outside: ");
- Serial.println(remotePressure);
- previousRemotePressure = remotePressure;
- }
- }
- void printRemoteHumidity()
- {
- if (remoteHumidity != previousRemoteHumidity)
- {
- tft.fillRect(130, 374, 100, 30, TFT_BLACK);
- tft.setCursor(130, 374);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print(remoteHumidity);
- Serial.print("Humidity Outside: ");
- Serial.println(remoteHumidity);
- previousRemoteHumidity = remoteHumidity;
- }
- }
- void printRemoteHours()
- {
- if (remoteHours != previousRemoteHours && remoteHours < 24)
- {
- int i = remoteHours;
- char buf[30];
- memset (buf, 0, sizeof(buf));
- snprintf(buf, sizeof(buf)-1, "%02d", i);
- tft.fillRect(224, 25, 35, 30, TFT_BLACK);
- tft.setCursor(224, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(buf);
- Serial.print("Hours: ");
- Serial.println(remoteHours);
- previousRemoteHours = remoteHours;
- }
- }
- void printRemoteMinutes()
- {
- if (remoteMinutes != previousRemoteMinutes && remoteMinutes < 60)
- {
- int i = remoteMinutes;
- char buf[30];
- memset (buf, 0, sizeof(buf));
- snprintf(buf, sizeof(buf)-1, "%02d", i);
- tft.fillRect(270, 25, 35, 30, TFT_BLACK);
- tft.setCursor(270, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(buf);
- Serial.print("Minutes: ");
- Serial.println(remoteMinutes);
- previousRemoteMinutes = remoteMinutes;
- }
- }
- void printRemoteDay()
- {
- if (remoteDay != previousRemoteDay && remoteDay < 32)
- {
- int i = remoteDay;
- char buf[30];
- memset (buf, 0, sizeof(buf));
- snprintf(buf, sizeof(buf)-1, "%02d", i);
- tft.fillRect(20, 25, 35, 30, TFT_BLACK);
- tft.setCursor(20, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(buf);
- Serial.print("Day: ");
- Serial.println(remoteDay);
- previousRemoteDay = remoteDay;
- }
- }
- void printRemoteMonth()
- {
- if (remoteMonth != previousRemoteMonth && remoteMonth < 13)
- {
- int i = remoteMonth;
- char buf[30];
- memset (buf, 0, sizeof(buf));
- snprintf(buf, sizeof(buf)-1, "%02d", i);
- tft.fillRect(77, 25, 35, 30, TFT_BLACK);
- tft.setCursor(77, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(buf);
- Serial.print("Month: ");
- Serial.println(remoteMonth);
- previousRemoteMonth = remoteMonth;
- }
- }
- void printRemoteYear()
- {
- if (remoteYear != previousRemoteYear && remoteYear < 10000)
- {
- tft.fillRect(134, 25, 70, 30, TFT_BLACK);
- tft.setCursor(134, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(remoteYear);
- Serial.print("Year: ");
- Serial.println(remoteYear);
- previousRemoteYear = remoteYear;
- }
- }
- void printRemoteRain()
- {
- if (remoteRain == 0.00 && previousRemoteRain == 0.00)
- {
- tft.fillRect(130, 439, 100, 30, TFT_BLACK);
- tft.setCursor(130, 439);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print(0.00);
- }
- if (remoteRain != previousRemoteRain)
- {
- tft.fillRect(130, 439, 100, 30, TFT_BLACK);
- tft.setCursor(130, 439);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print(remoteRain);
- Serial.print("Rain: ");
- Serial.print(remoteRain);
- Serial.println(" mm");
- previousRemoteRain = remoteRain;
- }
- }
- void printUI()
- {
- tft.drawRoundRect(5, 5, 310, 71, 5, TFT_WHITE);
- tft.drawRoundRect(6, 6, 310, 71, 5, TFT_WHITE);
- tft.drawRoundRect(5, 90, 310, 185, 5, TFT_WHITE);
- tft.drawRoundRect(6, 91, 310, 185, 5, TFT_WHITE);
- tft.drawRoundRect(5, 289, 310, 185, 5, TFT_WHITE);
- tft.drawRoundRect(6, 290, 310, 185, 5, TFT_WHITE);
- tft.fillRect(77, 90, 155, 40, TFT_CYAN);
- tft.fillRect(77, 289, 155, 40, TFT_GREEN);
- tft.setCursor(58, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print("/");
- tft.setCursor(115, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print("-");
- tft.setCursor(257, 25);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(3);
- tft.print(":");
- tft.setCursor(115, 100);
- tft.setTextColor(TFT_BLACK);
- tft.setTextSize(3);
- tft.print("INDE");
- tft.setCursor(125, 299);
- tft.setTextColor(TFT_BLACK);
- tft.setTextSize(3);
- tft.print("UDE");
- tft.setCursor(230, 190);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print("%");
- tft.setCursor(230, 374);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print("%");
- tft.setCursor(230, 145);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(2);
- tft.print("o");
- tft.setCursor(245, 145);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print("C");
- tft.setCursor(230, 344);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(1);
- tft.print("o");
- tft.setCursor(245, 344);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print("C");
- tft.setCursor(230, 409);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print("hPa");
- tft.setCursor(230, 439);
- tft.setTextColor(TFT_GREEN);
- tft.setTextSize(3);
- tft.print("mm");
- tft.setCursor(230, 235);
- tft.setTextColor(TFT_CYAN);
- tft.setTextSize(4);
- tft.print("hPa");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement