ESP 8266 RTC
Saya menggunakan modul RTC DS3231, Sensor DHT22, ESP 8266, dan display oled.
FULL CODE
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, –1);
#define DHTPIN 14 // Digital pin connected to the DHT sensor
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
RTC_DS1307 rtc;
DHT dht(DHTPIN, DHTTYPE);
char days[7][12] = {“Minggu”, “Senin”, “Selasa”, “Rabu”, “Kamis”, “Jumat”, “Sabtu”};
void setup() {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println(“Could not find RTC! Check circuit.”);
while (1);
}
// rtc.adjust(DateTime(__DATE__, __TIME__));
dht.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F(“SSD1306 allocation failed”));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
DateTime now = rtc.now();
// delay(5000);
//read temperature and humidity
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println(“Failed to read from DHT sensor!”);
}
// clear display
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0, 40);
display.print(“SUHU: “);
display.setTextSize(2);
display.setCursor(0, 50);
display.print(t);
display.print(” “);
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print(“C”);
// display humidity
display.setTextSize(1);
display.setCursor(13, 0);
display.print(days[now.dayOfTheWeek()]);
display.setTextSize(1);
display.setCursor(51, 0);
display.print(“,”);
display.setTextSize(1);
display.setCursor(61, 0);
display.println(now.day(), DEC);
display.setTextSize(1);
display.setCursor(76, 0);
display.println(now.month(), DEC);
display.setTextSize(1);
display.setCursor(93, 0);
display.println(now.year(), DEC);
display.setTextSize(1);
display.setCursor(0, 10);
display.print(“JAM : “);
display.setTextSize(2);
display.setCursor(0, 20);
display.print(now.hour(), DEC);
display.setTextSize(2);
display.print(“:”);
display.setTextSize(2);
display.print(now.minute(), DEC);
display.setTextSize(2);
display.print(“:”);
display.setTextSize(2);
display.print(now.second(), DEC);
// display.print(” %”);
display.display();
}