#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <TFT_HX8357.h>
// tft
TFT_HX8357 tft = TFT_HX8357();
// sensors
float previousRemoteHumidity = 0.1;
float previousRemoteTemperature = 0.1;
float previousRemotePressure = 0.1;
float remoteHumidity = 0.0;
float remoteTemperature = 0.0;
float remotePressure = 0.0;
// trans package
struct package
{
float temperature;
float pressure;
float humidity;
};
// radio
RF24 radio(6, 7);
byte addresses[][6] = {"0"};
typedef struct package Package;
Package data;
void setup(void) {
// serial
Serial.begin(9600);
// radio
startWirelessCommunication();
// tft
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextFont(1);
delay(100);
printUI();
}
void loop(void) {
checkForWirelessData();
printRemoteTemperature();
printRemoteHumidity();
printRemotePressure();
}
void startWirelessCommunication()
{
radio.begin();
radio.setChannel(115);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate( RF24_250KBPS ) ;
radio.openReadingPipe(1, addresses[0]);
radio.startListening();
delay(100);
}
void checkForWirelessData()
{
if ( radio.available())
{
while (radio.available())
{
radio.read( &data, sizeof(data) );
remoteTemperature = data.temperature;
remoteHumidity = data.humidity;
remotePressure = data.pressure;
}
Serial.print("\nPackage:");
Serial.print("\n");
Serial.println(data.temperature);
Serial.println(data.humidity);
Serial.println(data.pressure);
}
}
void printUI()
{
tft.drawRoundRect(5, 5, 470, 71, 5, TFT_WHITE);
tft.drawRoundRect(6, 6, 470, 71, 5, TFT_WHITE);
tft.drawRoundRect(5, 90, 220, 225, 5, TFT_WHITE);
tft.drawRoundRect(6, 91, 220, 225, 5, TFT_WHITE);
tft.drawRoundRect(250, 90, 220, 225, 5, TFT_WHITE);
tft.drawRoundRect(251, 91, 220, 225, 5, TFT_WHITE);
tft.fillRect(26, 90, 180, 40, TFT_GREEN);
tft.fillRect(270, 90, 180, 40, TFT_CYAN);
tft.setCursor(45, 100);
tft.setTextColor(TFT_BLACK);
tft.setTextSize(3);
tft.print("EXTERIOR");
tft.setCursor(288, 100);
tft.setTextColor(TFT_BLACK);
tft.setTextSize(3);
tft.print("INTERIOR");
tft.setCursor(160, 155);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print("%");
tft.setCursor(412, 165);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(6);
tft.print("%");
tft.setCursor(175, 210);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print("C");
tft.setCursor(160, 210);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(2);
tft.print("o");
tft.setCursor(412, 230);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(6);
tft.print("C");
tft.setCursor(395, 230);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(2);
tft.print("o");
tft.setCursor(160, 265);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print("mb");
}
void printRemoteHumidity()
{
String humidity;
if (remoteHumidity != previousRemoteHumidity)
{
if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
{
humidity = "---";
} else
{
humidity = String(remoteHumidity, 1);
}
tft.fillRect(35, 152, 120, 40, TFT_BLACK);
tft.setCursor(35, 152);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print(humidity);
previousRemoteHumidity = remoteHumidity;
}
}
void printRemoteTemperature()
{
String temperature;
if (remoteTemperature != previousRemoteTemperature)
{
if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
{
temperature = "---";
} else if (remoteTemperature >= 100)
{
temperature = String(remoteTemperature, 0);
} else
{
temperature = String(remoteTemperature, 1);
}
tft.fillRect(35, 209, 120, 40, TFT_BLACK);
tft.setCursor(35, 209);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print(temperature);
previousRemoteTemperature = remoteTemperature;
}
}
void printRemotePressure()
{
String pressure;
if (remotePressure != previousRemotePressure)
{
if (remoteHumidity == 0.0 && remoteTemperature == 0.0 && remotePressure == 0.0) //We just booted up
{
pressure = "---";
} else if (remotePressure >= 100)
{
pressure = String(remotePressure, 0);
} else
{
pressure = String(remotePressure, 1);
}
tft.fillRect(35, 266, 120, 40, TFT_BLACK);
tft.setCursor(35, 266);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(4);
tft.print(pressure);
previousRemotePressure = remotePressure;
}
}
Comments