#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ====== WIFI & MOCK-SERVER CONFIG ======
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASS = "";
// Di Wokwi mock HTTP server berjalan di 10.0.2.2:8000
const char* TB_SERVER = "eu.thingsboard.cloud";
const char* DEVICE_TOKEN = "0FFvBgTjjjCo7D8hw5Hr";
// ====== OLED CONFIG ======
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ====== PIN DEFINITIONS ======
const int potPin = 34; // simulasi pH
const int turbPin = 35; // simulasi turbidity
const int ledGreen = 25; // indikator pH
const int ledYellow = 26;
const int ledRed = 27;
void setup() {
Serial.begin(115200);
// Setup LED
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledRed, OUTPUT);
setLED(false,false,false);
// Init OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed");
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Connecting WiFi...");
display.display();
// Connect WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected, IP: " + WiFi.localIP().toString());
display.clearDisplay();
display.setCursor(0,0);
display.println("WiFi connected!");
display.display();
delay(1000);
}
void loop() {
// --- Baca pH & turbidity ---
int rawPH = analogRead(potPin);
float pH = map(rawPH, 0, 4095, 0, 1400) / 100.0;
int rawT = analogRead(turbPin);
float turbidity = map(rawT, 0, 4095, 0, 100);
// --- Tentukan label pH & LED ---
String phLabel;
if (pH >= 6.5 && pH <= 8.0) {
phLabel = "Netral"; setLED(true, false, false);
}
else if ((pH >= 5.0 && pH < 6.5) || (pH > 8.0 && pH <= 9.0)) {
phLabel = "Agak Asam/Alkali"; setLED(false, true, false);
}
else {
phLabel = "Berbahaya"; setLED(false, false, true);
}
// --- Tentukan label turbidity ---
String turbLabel;
if (turbidity > 70) turbLabel = "Jernih";
else if (turbidity > 40) turbLabel = "Keruh Hijau";
else turbLabel = "Keruh Hitam";
// --- Tampilkan di Serial ---
Serial.printf(
"pH=%.2f[%s] (raw:%d), Turb=%.1f[%s] (raw:%d)\n",
pH, phLabel.c_str(), rawPH,
turbidity, turbLabel.c_str(), rawT
);
// --- Tampilkan di OLED ---
display.clearDisplay();
display.setCursor(0,0);
display.printf("pH: %.2f %s\n", pH, phLabel.c_str());
display.printf("Turb: %.1f NTU\n", turbidity);
display.printf("%s\n", turbLabel.c_str());
display.display();
// --- Kirim telemetry ke mock-server ---
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Bangun URL: http://10.0.2.2:8000/api/v1/TOKEN/telemetry
String url = String("http://") + TB_SERVER
+ "/api/v1/" + DEVICE_TOKEN + "/telemetry";
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Payload JSON
String payload = String("{") +
"\"pH\":" + String(pH,2) + "," +
"\"turbidity\":" + String(turbidity,1) +
"}";
int code = http.POST(payload);
Serial.printf("HTTP %d => %s\n", code, payload.c_str());
http.end();
}
delay(5000);
}
// Fungsi helper untuk LED
void setLED(bool g, bool y, bool r) {
digitalWrite(ledGreen, g ? HIGH : LOW);
digitalWrite(ledYellow, y ? HIGH : LOW);
digitalWrite(ledRed, r ? HIGH : LOW);
}