#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// ===== DS18B20 =====
#define ONE_WIRE_BUS 1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// ===== TFT =====
#define TFT_DC 35
#define TFT_CS 3
#define TFT_MOSI 37
#define TFT_CLK 36
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK);
// ===== Colors =====
#define BG 0x0841 // Dark gray (premium)
#define CARD 0x10A2 // Card background
#define ACCENT ILI9341_CYAN
#define TEXT ILI9341_WHITE
#define GOOD ILI9341_GREEN
#define WARN ILI9341_ORANGE
#define BAD ILI9341_RED
void drawUI() {
tft.fillScreen(BG);
// ===== Header Bar =====
tft.fillRect(0, 0, 320, 40, ACCENT);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 12);
tft.print("TMIC Smart Monitor");
// ===== Company =====
tft.setTextColor(TEXT);
tft.setTextSize(1);
tft.setCursor(10, 50);
tft.print("Trade Master International Co., Ltd.");
tft.setCursor(10, 62);
tft.print("Tel: 081 924 6556");
tft.setCursor(10, 74);
tft.print("[email protected]");
// ===== Engineer =====
tft.setCursor(10, 90);
tft.print("A.Ryan Thatsuwan");
// ===== Card (Temperature Box) =====
tft.fillRoundRect(10, 110, 300, 110, 8, CARD);
tft.setTextSize(2);
tft.setTextColor(ACCENT);
tft.setCursor(20, 120);
tft.print("TEMPERATURE");
// Divider
tft.drawLine(20, 140, 290, 140, ILI9341_DARKGREY);
}
void updateTemp(float tempC) {
// ===== Clear value area =====
tft.fillRect(20, 150, 280, 50, CARD);
// ===== Color Logic =====
uint16_t color = GOOD;
const char* status = "NORMAL";
if (tempC > 60) {
color = BAD;
status = "HIGH";
} else if (tempC > 30) {
color = WARN;
status = "WARNING";
}
// ===== Big Value =====
tft.setTextColor(color);
tft.setTextSize(5);
tft.setCursor(30, 155);
tft.print(tempC, 1);
tft.print(" C");
// ===== Status Badge =====
tft.fillRoundRect(20, 210, 120, 25, 5, color);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(25, 215);
tft.print(status);
// ===== Footer =====
tft.setTextColor(TEXT);
tft.setTextSize(1);
tft.setCursor(180, 215);
tft.print("IoT Ready");
}
void setup() {
Serial.begin(115200);
sensors.begin();
tft.begin();
// orientation (ของอาจารย์ fix แล้ว)
tft.setRotation(1);
uint8_t madctl = 0xC8;
tft.sendCommand(ILI9341_MADCTL, &madctl, 1);
drawUI();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
updateTemp(tempC);
delay(1000);
}Loading
m5stack-core-s3
m5stack-core-s3