#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_BMP085.h>
// ============ KONFIGURASI WIFI UNTUK WOKWI ============
const char* ssid = "Wokwi-GUEST"; // Nama WiFi maya Wokwi
const char* password = ""; // Kosong
// ============ KONFIGURASI THINGSPEAK ============
String apiKey = "ZW3NCRVP5SV077AT"; // Ganti dengan Write API Key anda
const char* server = "https://api.thingspeak.com/update";
// ============ PIN DEFINITIONS ============
const int LED_MERAH = 15; // Low pressure (below 1000 hPa)
const int LED_HIJAU = 2; // High pressure (above 1020 hPa)
const int LED_KUNING = 4; // Normal pressure (1001-1019 hPa)
const int BUZZER = 16; // Buzzer for low pressure alert
const int BMP_SDA = 27; // BMP180 SDA pin
const int BMP_SCL = 26; // BMP180 SCL pin
const int LCD_SDA = 21;
const int LCD_SCL = 22;
// ============ THRESHOLD DEFINITIONS ============
const float HIGH_PRESSURE_THRESHOLD = 1020.0; // Above 1020 hPa = High pressure
const float LOW_PRESSURE_THRESHOLD = 1000.0; // Below 1000 hPa = Low pressure
// Normal pressure: 1001 - 1019 hPa
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_BMP085 bmp;
unsigned long lastTimeSend = 0;
const unsigned long sendInterval = 15000;
void setup() {
Serial.begin(115200);
// Initialize I2C for BMP180 (custom pins)
Wire.begin(BMP_SDA, BMP_SCL);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Initialize pins
pinMode(LED_MERAH, OUTPUT);
pinMode(LED_HIJAU, OUTPUT);
pinMode(LED_KUNING, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Turn off all LEDs and buzzer initially
digitalWrite(LED_MERAH, LOW);
digitalWrite(LED_HIJAU, LOW);
digitalWrite(LED_KUNING, LOW);
digitalWrite(BUZZER, LOW);
lcd.setCursor(0, 0);
lcd.print("Barometric Press");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(1000);
// Initialize BMP180 sensor
if (!bmp.begin()) {
Serial.println("BMP180 sensor not found!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BMP180 ERROR!");
lcd.setCursor(0, 1);
lcd.print("Check wiring!");
while (1) {
delay(1000);
}
}
Serial.println("BMP180 sensor initialized successfully!");
// Connect to Wokwi WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
delay(2000);
} else {
Serial.println("\nWiFi Connection Failed!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Failed!");
delay(2000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Ready!");
lcd.setCursor(0, 1);
lcd.print("Monitoring...");
delay(2000);
lcd.clear();
}
void loop() {
// Read pressure from BMP180 (in hPa)
float pressure = bmp.readPressure() / 100.0; // Convert Pa to hPa
float temperature = bmp.readTemperature(); // Read temperature for reference
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Press: ");
lcd.print(pressure, 1); // 1 decimal place
lcd.print(" hPa ");
lcd.setCursor(0, 1);
String weatherStatus = "";
// Determine pressure condition and control LEDs/Buzzer
if (pressure >= HIGH_PRESSURE_THRESHOLD) {
// HIGH PRESSURE - Clear, calm weather
digitalWrite(LED_MERAH, LOW);
digitalWrite(LED_HIJAU, HIGH);
digitalWrite(LED_KUNING, LOW);
digitalWrite(BUZZER, LOW);
weatherStatus = "Cerah/Tenang";
lcd.print("Cerah/Tenang ");
Serial.print("STATUS: HIGH PRESSURE - Clear weather");
} else if (pressure <= LOW_PRESSURE_THRESHOLD) {
// LOW PRESSURE - Clouds, rain, storms
digitalWrite(LED_MERAH, HIGH);
digitalWrite(LED_HIJAU, LOW);
digitalWrite(LED_KUNING, LOW);
digitalWrite(BUZZER, HIGH); // Buzzer sounds for low pressure
weatherStatus = "BADAI!";
lcd.print("BADAI! Hujan ");
Serial.print("STATUS: LOW PRESSURE - Storm warning!");
} else {
// NORMAL PRESSURE - Moderate weather
digitalWrite(LED_MERAH, LOW);
digitalWrite(LED_HIJAU, LOW);
digitalWrite(LED_KUNING, HIGH);
digitalWrite(BUZZER, LOW);
weatherStatus = "Normal";
lcd.print("Normal/Cerah ");
Serial.print("STATUS: NORMAL PRESSURE - Moderate weather");
}
// Print temperature for monitoring
Serial.print(" | Pressure: ");
Serial.print(pressure, 1);
Serial.print(" hPa | Temp: ");
Serial.print(temperature, 1);
Serial.println(" C");
// Send to ThingSpeak every 15 seconds
if (millis() - lastTimeSend >= sendInterval && WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Send pressure to field1 and status to field2
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(pressure, 1) +
"&field2=" + weatherStatus;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println(" ✓ Data sent to ThingSpeak!");
lcd.setCursor(0, 0);
lcd.print("Uploaded! ");
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Press: ");
lcd.print(pressure, 1);
lcd.print(" hPa ");
} else {
Serial.println(" ✗ Failed to send data to ThingSpeak");
}
http.end();
lastTimeSend = millis();
}
delay(500);
}