#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <Keypad.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String writeAPIKey = "JQ53BYN69KKIOLJS";
String serverTS = "https://api.thingspeak.com/update?api_key=";
const int buzzerPin = 19;
#define NOTE_C 261
#define NOTE_D 294
#define NOTE_E 329
#define NOTE_F 349
#define NOTE_G 392
#define NOTE_A 440
#define NOTE_B 494
#define NOTE_C2 523
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {18, 5, 17, 16};
byte colPins[COLS] = {4, 0, 2, 15};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ===== Countdown Variabel =====
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 15000; // 15 detik
bool countdownAktif = false;
unsigned long countdownStart = 0;
void updateThingSpeak(int field, int value) {
unsigned long now = millis();
if (now - lastUpdate >= updateInterval) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = serverTS + writeAPIKey + "&field" + String(field) + "=" + String(value);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("ThingSpeak updated: field" + String(field) + " = " + String(value));
} else {
Serial.println("Error updating ThingSpeak");
}
http.end();
lastUpdate = now;
// Mulai countdown
countdownAktif = true;
countdownStart = millis();
}
} else {
Serial.println("Update diblokir, tunggu cooldown selesai.");
}
}
void playNote(int freq, int duration, int field, int valueTS) {
tone(buzzerPin, freq, duration);
updateThingSpeak(field, valueTS);
Serial.print("Play note: ");
Serial.println(valueTS);
delay(duration + 100);
noTone(buzzerPin);
}
void playNoteByName(String note) {
if (note == "C") playNote(NOTE_C, 400, 1, 1);
else if (note == "D") playNote(NOTE_D, 400, 1, 2);
else if (note == "E") playNote(NOTE_E, 400, 1, 3);
else if (note == "F") playNote(NOTE_F, 400, 1, 4);
else if (note == "G") playNote(NOTE_G, 400, 1, 5);
else if (note == "A") playNote(NOTE_A, 400, 1, 6);
else if (note == "B") playNote(NOTE_B, 400, 1, 7);
else if (note == "C2") playNote(NOTE_C2, 400, 1, 8);
}
void setup() {
Serial.begin(115200);
pinMode(buzzerPin, OUTPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP ESP32: ");
Serial.println(WiFi.localIP());
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Tombol ditekan: ");
Serial.println(key);
if (key == '1') playNoteByName("C");
else if (key == '2') playNoteByName("D");
else if (key == '3') playNoteByName("E");
else if (key == '4') playNoteByName("F");
else if (key == '5') playNoteByName("G");
else if (key == '6') playNoteByName("A");
else if (key == '7') playNoteByName("B");
else if (key == '8') playNoteByName("C2");
}
// ===== Countdown Display =====
if (countdownAktif) {
unsigned long elapsed = millis() - countdownStart;
int detikSisa = updateInterval / 1000 - elapsed / 1000;
if (detikSisa >= 0) {
static int lastShown = -1;
if (detikSisa != lastShown) { // hanya print sekali per detik
Serial.println("Cooldown: " + String(detikSisa) + " detik");
lastShown = detikSisa;
}
} else {
countdownAktif = false; // selesai cooldown
Serial.println("Siap update lagi!");
}
}
}