#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define TFT_CS 16
#define TFT_DC 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define Num_Samples 96
static byte WaveFormTable[Num_Samples] =
{
0x80, 0x83, 0x87, 0x8A, 0x8E, 0x91, 0x95, 0x98, 0x9B, 0x9E, 0xA2, 0xA5, 0xA7, 0xAA, 0xAD, 0xAF,
0xB2, 0xB4, 0xB6, 0xB8, 0xB9, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xBF, 0xBF, 0xC0, 0xBF, 0xBF, 0xBF,
0xBE, 0xBD, 0xBC, 0xBB, 0xB9, 0xB8, 0xB6, 0xB4, 0xB2, 0xAF, 0xAD, 0xAA, 0xA7, 0xA5, 0xA2, 0x9E,
0x9B, 0x98, 0x95, 0x91, 0x8E, 0x8A, 0x87, 0x83, 0x80, 0x7C, 0x78, 0x75, 0x71, 0x6E, 0x6A, 0x67,
0x64, 0x61, 0x5D, 0x5A, 0x58, 0x55, 0x52, 0x50, 0x4D, 0x4B, 0x49, 0x47, 0x46, 0x44, 0x43, 0x42,
0x41, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x41, 0x42, 0x43, 0x44, 0x46, 0x47, 0x49, 0x4B,
0x4D, 0x50, 0x52, 0x55, 0x58, 0x5A, 0x5D, 0x61, 0x64, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C
};
#define LTBLUE 0xB6DF
#define LTTEAL 0xBF5F
#define LTGREEN 0xBFF7
#define LTCYAN 0xC7FF
#define LTRED 0xFD34
#define LTMAGENTA 0xFD5F
#define LTYELLOW 0xFFF8
#define LTORANGE 0xFE73
#define LTPINK 0xFDDF
#define LTPURPLE 0xCCFF
#define LTGREY 0xE71C
#define BLUE 0x001F
#define TEAL 0x0438
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define ORANGE 0xFC00
#define PINK 0xF81F
#define PURPLE 0x8010
#define GREY 0xC618
#define WHITE 0xFFFF
#define BLACK 0x0000
#define DKBLUE 0x000D
#define DKTEAL 0x020C
#define DKGREEN 0x03E0
#define DKCYAN 0x03EF
#define DKRED 0x6000
#define DKMAGENTA 0x8008
#define DKYELLOW 0x8400
#define DKORANGE 0x8200
#define DKPINK 0x9009
#define DKPURPLE 0x4010
#define DKGREY 0x4A49
double tempC, humid;
// this is the only external variable used by the graph
// it's a flag to draw the coordinate system only on the first pass
boolean display1 = true;
boolean display2 = true;
double ox , oy ;
double y;
int x, xmin, xmax;
void setup() {
x = 0; xmin = 0; xmax=60;
Serial.begin(115200);
dht.begin();
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
drawTempGraph();
drawWaveForm();
}
void loop() {
delay(500);
tempC = dht.readTemperature();
humid = dht.readHumidity();
Serial.print("Temperature: "); Serial.print(tempC); Serial.print("C ");
Serial.print("Humidity: "); Serial.print(humid); Serial.println("%");
// Update temperature graph
if (isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
} else {
y = tempC;
tft.fillCircle(ox, oy, 2, ILI9341_BLACK);
x = x + 1;
if (x > xmax)
{
xmin = xmax;
xmax = x + 60;
display1 = true;
drawTempGraph();
}
ox = map(x - 1, xmin, xmax, 30, 300);
oy = map(y, 15, 35, 200, 100);
tft.fillCircle(ox, oy, 2, ILI9341_YELLOW);
display1 = false;
}
// Update waveform
updateWaveForm();
}
void drawTempGraph()
{
if (display1 == true) {
tft.fillRect(0, 0, 320, 240, ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
tft.drawLine(30, 50, 30, 220, ILI9341_WHITE);
tft.drawLine(30, 220, 300, 220, ILI9341_WHITE);
tft.setCursor(0, 0);
tft.print("Temperatura [C] ");
tft.setCursor(0, 230);
tft.print("Tempo [s] ");
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
for (int i = 15; i < 35; i = i + 5) {
tft.setCursor(0, 210 - (i - 15) * 5);
tft.print(i);
}
}
}
void drawWaveForm() {
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(10, 250);
tft.print("Waveform");
}
void updateWaveForm() {
static int waveX = 0;
static int prevWaveY = 120;
// Clear previous waveform point
tft.drawPixel(waveX, prevWaveY, ILI9341_BLACK);
// Calculate new Y position based on waveform table
int waveY = map(WaveFormTable[waveX % Num_Samples], 0, 255, 200, 100);
// Draw new waveform point
tft.drawPixel(waveX, waveY, ILI9341_CYAN);
// Save current position for clearing in next loop
prevWaveY = waveY;
// Move to the next X position
waveX++;
if (waveX >= tft.width()) {
waveX = 0; // Reset X position
tft.fillRect(0, 100, tft.width(), 100, ILI9341_BLACK); // Clear waveform area
}
}