improve code, remove redundant code
This commit is contained in:
201
src/main.cpp
201
src/main.cpp
@ -13,8 +13,6 @@
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
// ================== SERIAL ==================
|
||||
#define SerialMon Serial
|
||||
|
||||
// ================== SIM800L =================
|
||||
#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX
|
||||
@ -56,15 +54,36 @@ MQUnifiedsensor MQ135(BOARD, VOLTAGE, ADC_BITS, PIN_MQ135, "MQ-135");
|
||||
MQUnifiedsensor MQ7(BOARD, VOLTAGE, ADC_BITS, PIN_MQ7, "MQ-7");
|
||||
|
||||
// ================== HELPER ==================
|
||||
void oledStatus(const String &msg)
|
||||
void oledGSMStatus(const String &msg)
|
||||
{
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("STATUS GSM:");
|
||||
display.println(msg);
|
||||
display.display();
|
||||
}
|
||||
|
||||
void oledMQStatus(const String &msg1, const String &msg2, const String &msg3, const String &msg4,String msg5="")
|
||||
{
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("MQ STATUS:");
|
||||
display.println(msg1);
|
||||
display.println(msg2);
|
||||
display.println(msg3);
|
||||
display.println(msg4);
|
||||
display.println(msg5);
|
||||
display.display();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ================= HELPER ======================
|
||||
float readAverage(MQUnifiedsensor &sensor, int samples = 5)
|
||||
{
|
||||
@ -94,113 +113,56 @@ void setup()
|
||||
}
|
||||
|
||||
// SIM800L UART
|
||||
SerialMon.println("[GSM] Init UART...");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM INIT ...");
|
||||
display.display();
|
||||
Serial.println("[GSM] Init UART...");
|
||||
oledGSMStatus("Init GSM...");
|
||||
|
||||
SerialAT.begin(9600, SERIAL_8N1, MODEM_RX, MODEM_TX);
|
||||
delay(3000);
|
||||
|
||||
SerialMon.println("[GSM] Restart modem...");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM RESTART modem...");
|
||||
display.display();
|
||||
Serial.println("[GSM] Hidupkan modem...");
|
||||
oledGSMStatus("Hidupkan modem...");
|
||||
if (!modem.restart())
|
||||
{
|
||||
SerialMon.println("[ERROR] Modem restart failed!");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM RESTART FAILED...");
|
||||
display.display();
|
||||
delay(1000);
|
||||
while (1)
|
||||
;
|
||||
Serial.println("[ERROR] Modem restart failed!");
|
||||
oledGSMStatus("MODEM GAGAL");
|
||||
oledGSMStatus("RESTART ESP32...");
|
||||
ESP.restart(); // Restart ESP32
|
||||
}
|
||||
|
||||
SerialMon.println("[GSM] Waiting for network...");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM WAIT NETWORK...");
|
||||
display.display();
|
||||
Serial.println("[GSM] Waiting for network...");
|
||||
oledGSMStatus("Menunggu jaringan...");
|
||||
delay(2000);
|
||||
if (!modem.waitForNetwork())
|
||||
{
|
||||
SerialMon.println("[ERROR] No network!");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM NO NETWORK...");
|
||||
display.display();
|
||||
delay(1000);
|
||||
while (1)
|
||||
;
|
||||
Serial.println("[ERROR] No network!");
|
||||
oledGSMStatus("GAGAL JARINGAN");
|
||||
oledGSMStatus("RESTART ESP32...");
|
||||
ESP.restart(); // Restart ESP32
|
||||
|
||||
}
|
||||
|
||||
SerialMon.println("[GSM] Network OK");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM NETWORK OK...");
|
||||
display.display();
|
||||
Serial.println("[GSM] Network OK");
|
||||
oledGSMStatus("JARINGAN OK");
|
||||
delay(2000);
|
||||
|
||||
SerialMon.println("[GSM] Connecting GPRS...");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM CONNECTING GPRS...");
|
||||
display.display();
|
||||
delay(2000);
|
||||
Serial.println("[GSM] Connecting GPRS...");
|
||||
oledGSMStatus("Menghubungkan GPRS...");
|
||||
if (!modem.gprsConnect(apn, user, pass))
|
||||
{
|
||||
SerialMon.println("[ERROR] GPRS failed!");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("");
|
||||
display.display();
|
||||
delay(1000);
|
||||
while (1)
|
||||
;
|
||||
Serial.println("[ERROR] GPRS failed!");
|
||||
oledGSMStatus("GAGAL GPRS");
|
||||
oledGSMStatus("RESTART ESP32...");
|
||||
ESP.restart(); // Restart ESP32
|
||||
}
|
||||
|
||||
SerialMon.println("[GSM] GPRS CONNECTED");
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("GSM GPRS CONNECTED...");
|
||||
display.display();
|
||||
Serial.println("[GSM] GPRS CONNECTED");
|
||||
oledGSMStatus("GPRS OK");
|
||||
delay(2000);
|
||||
|
||||
// MQ init
|
||||
SerialMon.println("[MQ] Init MQ135 & MQ7");
|
||||
Serial.println("[MQ] Init MQ135 & MQ7");
|
||||
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("Monitoring Udara");
|
||||
display.println("ESP32 + MQ");
|
||||
display.println("Kalibrasi...");
|
||||
display.display();
|
||||
|
||||
delay(2000);
|
||||
oledMQStatus("Init MQ135 & MQ7", "Mulai kalibrasi sensor...", "Pastikan udara bersih", "(±1-2 menit)");
|
||||
|
||||
// ================= KALIBRASI =================
|
||||
Serial.println("Mulai kalibrasi sensor...");
|
||||
@ -235,22 +197,13 @@ void setup()
|
||||
if (r0_135 <= 0 || r0_7 <= 0)
|
||||
{
|
||||
Serial.println("❌ Kalibrasi gagal!");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("ERROR KALIBRASI");
|
||||
display.display();
|
||||
while (true)
|
||||
delay(1000);
|
||||
oledMQStatus("KALIBRASI GAGAL", "", "", "RESTART ESP32...");
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
Serial.println("Kalibrasi selesai!");
|
||||
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Kalibrasi OK");
|
||||
display.println("Mulai Monitor");
|
||||
display.display();
|
||||
delay(2000);
|
||||
oledMQStatus("KALIBRASI OK", "...", "Mulai Monitoring", "...");
|
||||
}
|
||||
|
||||
// =====================================================
|
||||
@ -308,27 +261,7 @@ void loop()
|
||||
status = "Sangat Buruk";
|
||||
|
||||
// =============== OLED DISPLAY ==========
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
|
||||
display.print("CO2: ");
|
||||
display.print(co2, 0);
|
||||
display.println(" ppm");
|
||||
|
||||
display.print("CO : ");
|
||||
display.print(co, 1);
|
||||
display.println(" ppm");
|
||||
|
||||
display.print("BZ : ");
|
||||
display.print(benzene, 1);
|
||||
display.println(" ppm");
|
||||
|
||||
display.print("AQ : ");
|
||||
display.print(aq);
|
||||
display.print("% ");
|
||||
display.println(status);
|
||||
|
||||
display.display();
|
||||
oledMQStatus("CO2 : " + String(co2) + " ppm", "CO : " + String(co) + " ppm", "BZ : " + String(benzene) + " ppm", "AQ : " + String(aq) + " % " , status);
|
||||
|
||||
// =============== SERIAL =================
|
||||
Serial.println("===== STATUS UDARA =====");
|
||||
@ -357,15 +290,15 @@ void loop()
|
||||
url += "&bz=" + String(benzene);
|
||||
url += "&aq=" + String(aq);
|
||||
|
||||
SerialMon.print("[HTTP] GET ");
|
||||
SerialMon.println(url);
|
||||
Serial.print("[HTTP] GET ");
|
||||
Serial.println(url);
|
||||
|
||||
// Send request
|
||||
client.print(String("GET ") + url + " HTTP/1.1\r\n");
|
||||
client.print("Host: k-dummy.my.id\r\n");
|
||||
client.print("Connection: close\r\n\r\n");
|
||||
|
||||
oledStatus("MENUNGGU RESP");
|
||||
oledGSMStatus("MENUNGGU RESP");
|
||||
|
||||
// -------------------------------
|
||||
// READ RESPONSE
|
||||
@ -385,9 +318,9 @@ void loop()
|
||||
|
||||
client.stop();
|
||||
|
||||
SerialMon.println("----- HTTP RESPONSE -----");
|
||||
SerialMon.println(response);
|
||||
SerialMon.println("-------------------------");
|
||||
Serial.println("----- HTTP RESPONSE -----");
|
||||
Serial.println(response);
|
||||
Serial.println("-------------------------");
|
||||
|
||||
// -------------------------------
|
||||
// EXTRACT JSON BODY
|
||||
@ -397,25 +330,25 @@ void loop()
|
||||
{
|
||||
String json = response.substring(jsonStart);
|
||||
|
||||
SerialMon.print("[JSON] ");
|
||||
SerialMon.println(json);
|
||||
Serial.print("[JSON] ");
|
||||
Serial.println(json);
|
||||
|
||||
oledStatus("RESP OK");
|
||||
oledGSMStatus("RESP OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialMon.println("[ERROR] No JSON found!");
|
||||
oledStatus("RESP ERROR");
|
||||
Serial.println("[ERROR] No JSON found!");
|
||||
oledGSMStatus("RESP ERROR");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialMon.println("[ERROR] Server connection failed!");
|
||||
oledStatus("GAGAL KIRIM");
|
||||
Serial.println("[ERROR] Server connection failed!");
|
||||
oledGSMStatus("GAGAL KIRIM");
|
||||
}
|
||||
|
||||
client.stop();
|
||||
SerialMon.println("===== LOOP END =====");
|
||||
Serial.println("===== LOOP END =====");
|
||||
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user