added check gprs connection at the top of loop and added new 'ensureGPRS' function to ensure GPRS is connected

This commit is contained in:
kicap
2026-01-30 14:46:48 +08:00
parent 5dde5ef9a4
commit f40aa81a35

View File

@ -13,7 +13,6 @@
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ================== SIM800L =================
#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX
#define MODEM_TX 17 // ESP32 GPIO17 → SIM800L RX (via resistor)
@ -65,7 +64,7 @@ void oledGSMStatus(const String &msg)
display.display();
}
void oledMQStatus(const String &msg1, const String &msg2, const String &msg3, const String &msg4,String msg5="")
void oledMQStatus(const String &msg1, const String &msg2, const String &msg3, const String &msg4, String msg5 = "")
{
display.clearDisplay();
display.setTextSize(1);
@ -80,10 +79,6 @@ void oledMQStatus(const String &msg1, const String &msg2, const String &msg3, co
display.display();
}
// ================= HELPER ======================
float readAverage(MQUnifiedsensor &sensor, int samples = 5)
{
@ -138,7 +133,6 @@ void setup()
oledGSMStatus("GAGAL JARINGAN");
oledGSMStatus("RESTART ESP32...");
ESP.restart(); // Restart ESP32
}
Serial.println("[GSM] Network OK");
@ -206,9 +200,52 @@ void setup()
oledMQStatus("KALIBRASI OK", "...", "Mulai Monitoring", "...");
}
bool ensureGPRS()
{
if (!modem.isNetworkConnected())
{
Serial.println("[GSM] Network lost, waiting...");
oledGSMStatus("NET PUTUS");
if (!modem.waitForNetwork(60000L))
{
Serial.println("[GSM] Network still down!");
return false;
}
}
if (!modem.isGprsConnected())
{
Serial.println("[GSM] GPRS lost, reconnecting...");
oledGSMStatus("GPRS ULANG");
modem.gprsDisconnect();
delay(1000);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println("[GSM] GPRS reconnect failed!");
return false;
}
Serial.println("[GSM] GPRS reconnected");
}
return true;
}
// =====================================================
void loop()
{
if (!ensureGPRS())
{
Serial.println("[GSM] Recover failed, restarting modem...");
oledGSMStatus("RESTART MODEM");
modem.restart();
delay(5000);
return; // skip this loop
}
// =============== MQ135 =================
MQ135.setA(110.47);
@ -261,7 +298,7 @@ void loop()
status = "Sangat Buruk";
// =============== OLED DISPLAY ==========
oledMQStatus("CO2 : " + String(co2) + " ppm", "CO : " + String(co) + " ppm", "BZ : " + String(benzene) + " ppm", "AQ : " + String(aq) + " % " , status);
oledMQStatus("CO2 : " + String(co2) + " ppm", "CO : " + String(co) + " ppm", "BZ : " + String(benzene) + " ppm", "AQ : " + String(aq) + " % ", status);
// =============== SERIAL =================
Serial.println("===== STATUS UDARA =====");
@ -282,6 +319,9 @@ void loop()
delay(5000);
client.stop();
delay(1000);
if (client.connect(server, port))
{
String url = "/api/update?";