#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TM1637Display.h>
#include <DHT.h>
// ===== WiFi Config =====
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ===== Pin Config =====
#if defined(ESP32)
#define CLK 18
#define DIO 19
#define DHTPIN 4
#else
#define CLK D5
#define DIO D6
#define DHTPIN D2
#endif
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
TM1637Display display(CLK, DIO);
// ===== NTP Config =====
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7 * 3600); // +7 ชั่วโมง (ไทย)
// ===== Variables =====
bool showColon = true;
unsigned long lastSensorRead = 0;
unsigned long lastModeSwitch = 0;
float temperature = NAN, humidity = NAN;
int displayMode = 0; // 0=เวลา, 1=อุณหภูมิ, 2=ความชื้น
// ===== Segment codes for digits 0–9 =====
const uint8_t digitCode[10] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
Serial.begin(115200);
display.setBrightness(7);
display.showNumberDecEx(0, 0b01000000, true);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
timeClient.begin();
dht.begin();
}
void loop() {
timeClient.update();
// อ่าน DHT22 ทุก 5 วินาที
if (millis() - lastSensorRead > 5000) {
lastSensorRead = millis();
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (!isnan(humidity) && !isnan(temperature))
Serial.printf("Temp: %.1f°C | Hum: %.1f%%\n", temperature, humidity);
}
// สลับโหมดทุก 5 วินาที
if (millis() - lastModeSwitch > 5000) {
lastModeSwitch = millis();
displayMode = (displayMode + 1) % 3;
}
if (displayMode == 0) {
// ===== เวลา =====
int hours = timeClient.getHours();
int minutes = timeClient.getMinutes();
int displayTime = hours * 100 + minutes;
showColon = !showColon;
display.showNumberDecEx(displayTime, showColon ? 0b01000000 : 0, true);
}
else if (displayMode == 1) {
// ===== อุณหภูมิ 1 ตำแหน่งทศนิยม =====
if (!isnan(temperature)) {
int temp10 = (int)round(temperature * 10); // 25.3 → 253
uint8_t segs[4];
segs[0] = digitCode[(temp10 / 100) % 10]; // หลักสิบ
segs[1] = digitCode[(temp10 / 10) % 10] | 0x80; // หลักหน่วย + จุดทศนิยม
segs[2] = digitCode[temp10 % 10]; // หลักทศนิยม
segs[3] = 0b00111001; // C
display.setSegments(segs);
} else {
display.showNumberDecEx(8888, 0, true);
}
}
else if (displayMode == 2) {
// ===== ความชื้น 1 ตำแหน่งทศนิยม =====
if (!isnan(humidity)) {
int hum10 = (int)round(humidity * 10); // 55.2 → 552
uint8_t segs[4];
segs[0] = digitCode[(hum10 / 100) % 10]; // หลักสิบ
segs[1] = digitCode[(hum10 / 10) % 10] | 0x80; // หลักหน่วย + จุดทศนิยม
segs[2] = digitCode[hum10 % 10]; // หลักทศนิยม
segs[3] = 0b01110110; // H
display.setSegments(segs);
} else {
display.showNumberDecEx(8888, 0, true);
}
}
delay(1000);
}