added check gprs connection at the top of loop and added new 'ensureGPRS' function to ensure GPRS is connected
This commit is contained in:
56
src/main.cpp
56
src/main.cpp
@ -13,7 +13,6 @@
|
|||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
|
|
||||||
|
|
||||||
// ================== SIM800L =================
|
// ================== SIM800L =================
|
||||||
#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX
|
#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX
|
||||||
#define MODEM_TX 17 // ESP32 GPIO17 → SIM800L RX (via resistor)
|
#define MODEM_TX 17 // ESP32 GPIO17 → SIM800L RX (via resistor)
|
||||||
@ -65,7 +64,7 @@ void oledGSMStatus(const String &msg)
|
|||||||
display.display();
|
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.clearDisplay();
|
||||||
display.setTextSize(1);
|
display.setTextSize(1);
|
||||||
@ -80,10 +79,6 @@ void oledMQStatus(const String &msg1, const String &msg2, const String &msg3, co
|
|||||||
display.display();
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ================= HELPER ======================
|
// ================= HELPER ======================
|
||||||
float readAverage(MQUnifiedsensor &sensor, int samples = 5)
|
float readAverage(MQUnifiedsensor &sensor, int samples = 5)
|
||||||
{
|
{
|
||||||
@ -138,7 +133,6 @@ void setup()
|
|||||||
oledGSMStatus("GAGAL JARINGAN");
|
oledGSMStatus("GAGAL JARINGAN");
|
||||||
oledGSMStatus("RESTART ESP32...");
|
oledGSMStatus("RESTART ESP32...");
|
||||||
ESP.restart(); // Restart ESP32
|
ESP.restart(); // Restart ESP32
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("[GSM] Network OK");
|
Serial.println("[GSM] Network OK");
|
||||||
@ -206,9 +200,52 @@ void setup()
|
|||||||
oledMQStatus("KALIBRASI OK", "...", "Mulai Monitoring", "...");
|
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()
|
void loop()
|
||||||
{
|
{
|
||||||
|
if (!ensureGPRS())
|
||||||
|
{
|
||||||
|
Serial.println("[GSM] Recover failed, restarting modem...");
|
||||||
|
oledGSMStatus("RESTART MODEM");
|
||||||
|
|
||||||
|
modem.restart();
|
||||||
|
delay(5000);
|
||||||
|
return; // skip this loop
|
||||||
|
}
|
||||||
|
|
||||||
// =============== MQ135 =================
|
// =============== MQ135 =================
|
||||||
MQ135.setA(110.47);
|
MQ135.setA(110.47);
|
||||||
@ -261,7 +298,7 @@ void loop()
|
|||||||
status = "Sangat Buruk";
|
status = "Sangat Buruk";
|
||||||
|
|
||||||
// =============== OLED DISPLAY ==========
|
// =============== 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 =================
|
||||||
Serial.println("===== STATUS UDARA =====");
|
Serial.println("===== STATUS UDARA =====");
|
||||||
@ -282,6 +319,9 @@ void loop()
|
|||||||
|
|
||||||
delay(5000);
|
delay(5000);
|
||||||
|
|
||||||
|
client.stop();
|
||||||
|
delay(1000);
|
||||||
|
|
||||||
if (client.connect(server, port))
|
if (client.connect(server, port))
|
||||||
{
|
{
|
||||||
String url = "/api/update?";
|
String url = "/api/update?";
|
||||||
|
|||||||
Reference in New Issue
Block a user