added gps for cordinate changes
This commit is contained in:
190
src/main.cpp
190
src/main.cpp
@ -1,6 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
// =====================================================
|
||||
// ESP32 + MQ-135 + MQ-7 + OLED SSD1306 (0.96" 128x64)
|
||||
// ESP32 + MQ-135 + MQ-7 + OLED SSD1306 + GPS (BN-220)
|
||||
// WiFi Version
|
||||
// =====================================================
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#include <MQUnifiedsensor.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <TinyGPSPlus.h>
|
||||
|
||||
// ================== WIFI ==================
|
||||
const char* ssid = "KARAN";
|
||||
@ -27,6 +28,14 @@ const int port = 80;
|
||||
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
|
||||
// ================= GPS CONFIG ==================
|
||||
static const int GPS_RX_PIN = 16; // GPS TX -> ESP32 GPIO 16
|
||||
static const int GPS_TX_PIN = 17; // GPS RX -> ESP32 GPIO 17
|
||||
static const uint32_t GPS_BAUD = 9600;
|
||||
|
||||
TinyGPSPlus gps;
|
||||
HardwareSerial gpsSerial(2);
|
||||
|
||||
// ================= MQ CONFIG ===================
|
||||
#define BOARD "ESP-32"
|
||||
#define VOLTAGE 3.3
|
||||
@ -79,6 +88,43 @@ void oledMQStatus(
|
||||
delay(500);
|
||||
}
|
||||
|
||||
// =============== GPS HELPERS ===================
|
||||
|
||||
// Feed any pending GPS data to the library (non-blocking)
|
||||
void updateGPS()
|
||||
{
|
||||
while (gpsSerial.available() > 0)
|
||||
{
|
||||
gps.encode(gpsSerial.read());
|
||||
}
|
||||
}
|
||||
|
||||
// Returns "lat,lng" if a valid fix exists, otherwise "-"
|
||||
String getGPSCoordinates()
|
||||
{
|
||||
updateGPS();
|
||||
|
||||
if (gps.location.isValid() && gps.location.age() < 2000)
|
||||
{
|
||||
return String(gps.location.lat(), 6) + "," +
|
||||
String(gps.location.lng(), 6);
|
||||
}
|
||||
|
||||
return "-";
|
||||
}
|
||||
|
||||
// Returns a status string for the OLED
|
||||
String getGPSStatus()
|
||||
{
|
||||
if (gps.charsProcessed() < 10)
|
||||
return "NO DATA";
|
||||
|
||||
if (gps.location.isValid() && gps.location.age() < 2000)
|
||||
return "FIX " + String(gps.satellites.value()) + " sats";
|
||||
|
||||
return "NO FIX";
|
||||
}
|
||||
|
||||
// ================= HELPER ======================
|
||||
float readAverage(MQUnifiedsensor &sensor, int samples = 5)
|
||||
{
|
||||
@ -88,6 +134,8 @@ float readAverage(MQUnifiedsensor &sensor, int samples = 5)
|
||||
{
|
||||
sensor.update();
|
||||
sum += sensor.readSensor();
|
||||
|
||||
updateGPS(); // keep GPS buffer drained during sampling
|
||||
delay(50);
|
||||
}
|
||||
|
||||
@ -144,9 +192,7 @@ bool connectWiFi()
|
||||
bool ensureWiFi()
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
Serial.println("[WiFi] Connection lost!");
|
||||
oledWiFiStatus("WIFI PUTUS");
|
||||
@ -163,6 +209,10 @@ void setup()
|
||||
Serial.begin(115200);
|
||||
delay(500);
|
||||
|
||||
// ================= GPS INIT ==================
|
||||
gpsSerial.begin(GPS_BAUD, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
|
||||
Serial.println("[GPS] Serial initialized on GPIO16/17");
|
||||
|
||||
// ================= OLED INIT =================
|
||||
|
||||
Wire.begin(21, 22);
|
||||
@ -170,9 +220,7 @@ void setup()
|
||||
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR))
|
||||
{
|
||||
Serial.println("OLED tidak ditemukan!");
|
||||
|
||||
while (true)
|
||||
delay(1000);
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
// ================= WIFI INIT =================
|
||||
@ -182,11 +230,8 @@ void setup()
|
||||
if (!connectWiFi())
|
||||
{
|
||||
Serial.println("[ERROR] WiFi gagal!");
|
||||
|
||||
oledWiFiStatus("RESTART ESP32");
|
||||
|
||||
delay(2000);
|
||||
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
@ -215,14 +260,12 @@ void setup()
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
MQ135.update();
|
||||
|
||||
r0_135 += MQ135.calibrate(RATIO_CLEAN_MQ135);
|
||||
|
||||
updateGPS(); // keep GPS data flowing
|
||||
delay(600);
|
||||
}
|
||||
|
||||
r0_135 /= 10;
|
||||
|
||||
MQ135.setR0(r0_135);
|
||||
|
||||
// ================= MQ7 =================
|
||||
@ -235,14 +278,12 @@ void setup()
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
MQ7.update();
|
||||
|
||||
r0_7 += MQ7.calibrate(RATIO_CLEAN_MQ7);
|
||||
|
||||
updateGPS();
|
||||
delay(600);
|
||||
}
|
||||
|
||||
r0_7 /= 10;
|
||||
|
||||
MQ7.setR0(r0_7);
|
||||
|
||||
// ================= CHECK CALIBRATION =================
|
||||
@ -250,32 +291,18 @@ void setup()
|
||||
if (r0_135 <= 0 || r0_7 <= 0)
|
||||
{
|
||||
Serial.println("Kalibrasi gagal!");
|
||||
|
||||
oledMQStatus(
|
||||
"KALIBRASI GAGAL",
|
||||
"",
|
||||
"",
|
||||
"RESTART ESP32...");
|
||||
|
||||
oledMQStatus("KALIBRASI GAGAL", "", "", "RESTART ESP32...");
|
||||
delay(2000);
|
||||
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
Serial.println("Kalibrasi selesai!");
|
||||
|
||||
Serial.print("MQ135 R0 = ");
|
||||
Serial.println(r0_135);
|
||||
|
||||
Serial.print("MQ7 R0 = ");
|
||||
Serial.println(r0_7);
|
||||
|
||||
oledMQStatus(
|
||||
"KALIBRASI OK",
|
||||
"...",
|
||||
"Mulai Monitoring",
|
||||
"...");
|
||||
|
||||
oledMQStatus("KALIBRASI OK", "...", "Mulai Monitoring", "...");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
@ -291,11 +318,8 @@ void loop()
|
||||
if (!ensureWiFi())
|
||||
{
|
||||
Serial.println("[WiFi] Recover failed!");
|
||||
|
||||
oledWiFiStatus("WIFI ERROR");
|
||||
|
||||
delay(5000);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -303,31 +327,20 @@ void loop()
|
||||
|
||||
MQ135.setA(110.47);
|
||||
MQ135.setB(-2.862);
|
||||
|
||||
float co2 = readAverage(MQ135);
|
||||
|
||||
if (co2 < 0)
|
||||
co2 = 0;
|
||||
|
||||
// =================================================
|
||||
if (co2 < 0) co2 = 0;
|
||||
|
||||
MQ135.setA(605.18);
|
||||
MQ135.setB(-3.937);
|
||||
|
||||
float benzene = readAverage(MQ135);
|
||||
|
||||
if (benzene < 0)
|
||||
benzene = 0;
|
||||
if (benzene < 0) benzene = 0;
|
||||
|
||||
// =============== MQ7 ===================
|
||||
|
||||
MQ7.setA(99.042);
|
||||
MQ7.setB(-1.518);
|
||||
|
||||
float co = readAverage(MQ7);
|
||||
|
||||
if (co < 0)
|
||||
co = 0;
|
||||
if (co < 0) co = 0;
|
||||
|
||||
// ========== AIR QUALITY ESTIMATION =====
|
||||
|
||||
@ -335,16 +348,12 @@ void loop()
|
||||
|
||||
if (co2 > 700)
|
||||
aq -= map(co2, 700, 2000, 0, 50);
|
||||
|
||||
if (co2 > 2000)
|
||||
aq -= map(co2, 2000, 5000, 50, 80);
|
||||
|
||||
if (co > 10)
|
||||
aq -= map(co, 10, 50, 0, 30);
|
||||
|
||||
if (co > 50)
|
||||
aq -= 30;
|
||||
|
||||
if (benzene > 5)
|
||||
aq -= map(benzene, 5, 50, 0, 40);
|
||||
|
||||
@ -353,49 +362,71 @@ void loop()
|
||||
// ================= STATUS =================
|
||||
|
||||
String status;
|
||||
if (aq >= 90) status = "Sangat Baik";
|
||||
else if (aq >= 70) status = "Baik";
|
||||
else if (aq >= 50) status = "Sedang";
|
||||
else if (aq >= 30) status = "Buruk";
|
||||
else status = "Sangat Buruk";
|
||||
|
||||
if (aq >= 90)
|
||||
status = "Sangat Baik";
|
||||
else if (aq >= 70)
|
||||
status = "Baik";
|
||||
else if (aq >= 50)
|
||||
status = "Sedang";
|
||||
else if (aq >= 30)
|
||||
status = "Buruk";
|
||||
else
|
||||
status = "Sangat Buruk";
|
||||
// =============== GPS READ ==================
|
||||
|
||||
String gps_coords = getGPSCoordinates();
|
||||
String gps_status = getGPSStatus();
|
||||
|
||||
// =============== OLED DISPLAY ==========
|
||||
// 6 lines fit on 128x64 with textSize(1)
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
|
||||
oledMQStatus(
|
||||
"CO2 : " + String(co2) + " ppm",
|
||||
"CO : " + String(co) + " ppm",
|
||||
"BZ : " + String(benzene) + " ppm",
|
||||
"AQ : " + String(aq) + " % ",
|
||||
status);
|
||||
display.println("CO2:" + String(co2, 1) + " CO:" + String(co, 1));
|
||||
display.println("BZ :" + String(benzene, 1) + " AQ:" + String(aq) + "%");
|
||||
display.println(status);
|
||||
display.println("GPS: " + gps_status);
|
||||
|
||||
String lat = "-3.9891687826423334";
|
||||
String lng = "119.65221465458342";
|
||||
|
||||
if (gps_coords != "-")
|
||||
{
|
||||
// Split lat/lng to two lines for readability
|
||||
int commaIdx = gps_coords.indexOf(',');
|
||||
display.println("Lat:" + gps_coords.substring(0, commaIdx));
|
||||
display.println("Lng:" + gps_coords.substring(commaIdx + 1));
|
||||
|
||||
lat = gps_coords.substring(0, commaIdx);
|
||||
lng = gps_coords.substring(commaIdx + 1);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
display.println("Lat: -");
|
||||
display.println("Lng: -");
|
||||
}
|
||||
|
||||
display.display();
|
||||
|
||||
// =============== SERIAL =================
|
||||
|
||||
Serial.println("===== STATUS UDARA =====");
|
||||
|
||||
Serial.print("CO2 : ");
|
||||
Serial.print(co2);
|
||||
Serial.println(" ppm");
|
||||
|
||||
Serial.print("CO : ");
|
||||
Serial.print(co);
|
||||
Serial.println(" ppm");
|
||||
|
||||
Serial.print("BZ : ");
|
||||
Serial.print(benzene);
|
||||
Serial.println(" ppm");
|
||||
|
||||
Serial.print("AQ : ");
|
||||
Serial.print(aq);
|
||||
Serial.print("% ");
|
||||
|
||||
Serial.println(status);
|
||||
|
||||
Serial.print("GPS : ");
|
||||
Serial.println(gps_coords);
|
||||
Serial.print("GPS status: ");
|
||||
Serial.println(gps_status);
|
||||
Serial.println("========================\n");
|
||||
|
||||
delay(5000);
|
||||
@ -409,12 +440,13 @@ void loop()
|
||||
HTTPClient http;
|
||||
|
||||
String url = String(server) + "/api/update?";
|
||||
|
||||
url += "co2=" + String(co2);
|
||||
url += "&co=" + String(co);
|
||||
url += "&bz=" + String(benzene);
|
||||
url += "&aq=" + String(aq);
|
||||
url += "&id=2";
|
||||
url += "&lat=" + lat;
|
||||
url += "&lng=" + lng;
|
||||
|
||||
Serial.print("[HTTP] GET ");
|
||||
Serial.println(url);
|
||||
@ -422,7 +454,6 @@ void loop()
|
||||
oledWiFiStatus("MENGIRIM DATA...");
|
||||
|
||||
http.begin(url);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
if (httpCode > 0)
|
||||
@ -431,26 +462,21 @@ void loop()
|
||||
Serial.println(httpCode);
|
||||
|
||||
String response = http.getString();
|
||||
|
||||
Serial.println("----- HTTP RESPONSE -----");
|
||||
Serial.println(response);
|
||||
Serial.println("-------------------------");
|
||||
|
||||
int jsonStart = response.indexOf('{');
|
||||
|
||||
if (jsonStart >= 0)
|
||||
{
|
||||
String json = response.substring(jsonStart);
|
||||
|
||||
Serial.print("[JSON] ");
|
||||
Serial.println(json);
|
||||
|
||||
oledWiFiStatus("RESP OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("[ERROR] No JSON found!");
|
||||
|
||||
oledWiFiStatus("RESP ERROR");
|
||||
}
|
||||
}
|
||||
@ -458,7 +484,6 @@ void loop()
|
||||
{
|
||||
Serial.print("[ERROR] HTTP failed: ");
|
||||
Serial.println(http.errorToString(httpCode));
|
||||
|
||||
oledWiFiStatus("GAGAL KIRIM");
|
||||
}
|
||||
|
||||
@ -467,7 +492,6 @@ void loop()
|
||||
else
|
||||
{
|
||||
Serial.println("[ERROR] WiFi disconnected!");
|
||||
|
||||
oledWiFiStatus("WIFI PUTUS");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user