// ── Blynk config (HARUS sebelum #include Blynk) ─────────────────
#define BLYNK_TEMPLATE_ID "TMPL6d0eIAZGb"
#define BLYNK_TEMPLATE_NAME "pertemuan9"
#define BLYNK_AUTH_TOKEN "U7DJ2hI_1pUgUZV5aUDQC32v-OI9RrB6"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <TinyGPS++.h>
// ── WiFi ─────────────────────────────────────────────────────────
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ── GPS (tidak diubah dari kode aslimu) ──────────────────────────
#define GPS_BAUDRATE 9600
TinyGPSPlus gps;
// ── Potensio ─────────────────────────────────────────────────────
#define POT1_PIN 34
#define POT2_PIN 35
// ── Counter durasi kategori (dalam detik) ─────────────────────────
unsigned long pot1_dingin = 0, pot1_hangat = 0, pot1_panas = 0;
unsigned long pot2_dingin = 0, pot2_hangat = 0, pot2_panas = 0;
// ── Blynk timer ──────────────────────────────────────────────────
BlynkTimer timer;
// ════════════════════════════════════════════════════════════════
// Timer 1 — tiap 1 detik: baca pot, count kategori, kirim realtime
// ════════════════════════════════════════════════════════════════
void sendRealtime() {
int pot1 = analogRead(POT1_PIN);
int pot2 = analogRead(POT2_PIN);
// Count kategori pot1
if (pot1 <= 1500) pot1_dingin++;
else if (pot1 <= 2500) pot1_hangat++;
else pot1_panas++;
// Count kategori pot2
if (pot2 <= 1500) pot2_dingin++;
else if (pot2 <= 2500) pot2_hangat++;
else pot2_panas++;
// Kirim nilai realtime pot ke Blynk (V6 & V7)
Blynk.virtualWrite(V6, pot1);
Blynk.virtualWrite(V7, pot2);
// Kirim GPS ke Blynk (V0-V3)
Blynk.virtualWrite(V0, gps.location.isValid() ? String(gps.location.lat(), 6) : "INVALID");
Blynk.virtualWrite(V1, gps.location.isValid() ? String(gps.location.lng(), 6) : "INVALID");
Blynk.virtualWrite(V2, gps.altitude.isValid() ? String(gps.altitude.meters(), 2) : "INVALID");
Blynk.virtualWrite(V3, gps.speed.isValid() ? String(gps.speed.kmph(), 2) : "0.00");
}
// ════════════════════════════════════════════════════════════════
// Timer 2 — tiap 10 menit: hitung persentase, kirim bar chart
// ════════════════════════════════════════════════════════════════
void sendBarChart() {
unsigned long t1 = pot1_dingin + pot1_hangat + pot1_panas;
unsigned long t2 = pot2_dingin + pot2_hangat + pot2_panas;
if (t1 > 0) {
Blynk.virtualWrite(V4, (int)((float)pot1_panas / t1 * 100)); // % panas pot1
Blynk.virtualWrite(V8, (int)((float)pot1_hangat / t1 * 100)); // % hangat pot1
Blynk.virtualWrite(V9, (int)((float)pot1_dingin / t1 * 100)); // % dingin pot1
}
if (t2 > 0) {
Blynk.virtualWrite(V5, (int)((float)pot2_panas / t2 * 100)); // % panas pot2
Blynk.virtualWrite(V10, (int)((float)pot2_hangat / t2 * 100)); // % hangat pot2
Blynk.virtualWrite(V11, (int)((float)pot2_dingin / t2 * 100)); // % dingin pot2
}
// Reset counter untuk window berikutnya
pot1_dingin = pot1_hangat = pot1_panas = 0;
pot2_dingin = pot2_hangat = pot2_panas = 0;
}
// ════════════════════════════════════════════════════════════════
// SETUP
// ════════════════════════════════════════════════════════════════
void setup() {
Serial.begin(9600);
Serial2.begin(GPS_BAUDRATE); // serial2 for GPS UART
Serial.println(F("ESP32 - GPS module_Simulation"));
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
timer.setInterval(1000L, sendRealtime); // tiap 1 detik
timer.setInterval(10UL * 60UL * 1000L, sendBarChart); // tiap 10 menit
}
// ════════════════════════════════════════════════════════════════
// LOOP — kode GPS aslimu tidak diubah sama sekali
// ════════════════════════════════════════════════════════════════
void loop() {
if (Serial2.available() > 0) {
if (gps.encode(Serial2.read())) {
if (gps.location.isValid()) {
Serial.print(F("Latitude: "));
Serial.println(gps.location.lat(), 6);
Serial.print(F("Longitude: "));
Serial.println(gps.location.lng(), 6);
Serial.print(F("Altitude: "));
if (gps.altitude.isValid())
Serial.println(gps.altitude.meters());
else
Serial.println(F("INVALID"));
} else {
Serial.println(F("Location: INVALID"));
}
Serial.print(F("Speed: "));
if (gps.speed.isValid()) {
Serial.print(gps.speed.kmph());
Serial.println(F(" km/h"));
} else {
Serial.println(F("INVALID"));
}
Serial.print(F("GPS date&time: "));
if (gps.date.isValid() && gps.time.isValid()) {
Serial.print(gps.date.year());
Serial.print(F("-"));
Serial.print(gps.date.month());
Serial.print(F("-"));
Serial.print(gps.date.day());
Serial.print(F(" "));
Serial.print(gps.time.hour());
Serial.print(F(":"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
Serial.println(gps.time.second());
} else {
Serial.println(F("INVALID"));
}
Serial.println();
}
}
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
// Blynk & timer (ditambahkan di paling bawah loop)
Blynk.run();
timer.run();
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4