From 5c8342d836ac56b76df1a76cf4fa8641a51e0aee Mon Sep 17 00:00:00 2001 From: kicap Date: Fri, 30 Jan 2026 12:35:40 +0800 Subject: [PATCH] first commit --- .gitignore | 5 + .vscode/extensions.json | 10 + include/README | 37 ++++ lib/README | 46 +++++ platformio.ini | 20 ++ src/main.cpp | 421 +++++++++++++++++++++++++++++++++++++++ src/main.cpp.bck-no-edit | 182 +++++++++++++++++ src/main.cpp.gprs-ok | 228 +++++++++++++++++++++ test/README | 11 + 9 files changed, 960 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 src/main.cpp.bck-no-edit create mode 100644 src/main.cpp.gprs-ok create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..ac8bd16 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,20 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +lib_deps = + miguel5612/MQUnifiedsensor@^3.0.5 + adafruit/Adafruit SSD1306@^2.5.16 + adafruit/Adafruit GFX Library@^1.12.4 + vshymanskyy/TinyGSM@^0.12.0 +monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c1e4d72 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,421 @@ +#include +// ===================================================== +// ESP32 + MQ-135 + MQ-7 + OLED SSD1306 (0.96" 128x64) +// Bahasa Indonesia | Stabil | Client-ready +// ===================================================== + +#define TINY_GSM_MODEM_SIM800 +#define TINY_GSM_RX_BUFFER 650 + +#include +#include +#include +#include +#include + +// ================== SERIAL ================== +#define SerialMon Serial + +// ================== SIM800L ================= +#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX +#define MODEM_TX 17 // ESP32 GPIO17 → SIM800L RX (via resistor) + +HardwareSerial SerialAT(2); +TinyGsm modem(SerialAT); +TinyGsmClient client(modem); + +// APN Indonesia (change if needed) +const char apn[] = "internet"; +const char user[] = ""; +const char pass[] = ""; + +// Server +const char server[] = "k-dummy.my.id"; +const int port = 8080; + +// ================= OLED CONFIG ================= +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define OLED_RESET -1 +#define OLED_ADDR 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +// ================= MQ CONFIG =================== +#define BOARD "ESP-32" +#define VOLTAGE 3.3 +#define ADC_BITS 12 + +#define PIN_MQ135 34 +#define PIN_MQ7 35 + +#define RATIO_CLEAN_MQ135 3.6 +#define RATIO_CLEAN_MQ7 27.5 + +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) +{ + display.clearDisplay(); + display.setCursor(0, 0); + display.println("STATUS GSM:"); + display.println(msg); + display.display(); +} + +// ================= HELPER ====================== +float readAverage(MQUnifiedsensor &sensor, int samples = 5) +{ + float sum = 0; + for (int i = 0; i < samples; i++) + { + sensor.update(); + sum += sensor.readSensor(); + delay(50); + } + return sum / samples; +} + +// ===================================================== +void setup() +{ + Serial.begin(115200); + delay(500); + + // ================= OLED INIT ================= + Wire.begin(21, 22); + if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) + { + Serial.println("❌ OLED tidak ditemukan!"); + while (true) + delay(1000); + } + + // 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(); + + 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(); + 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) + ; + } + + 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(); + 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) + ; + } + + 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(); + 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); + 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) + ; + } + + 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(); + delay(2000); + + // MQ init + SerialMon.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); + + // ================= KALIBRASI ================= + Serial.println("Mulai kalibrasi sensor..."); + Serial.println("Pastikan udara bersih (±1-2 menit)"); + + // MQ135 + MQ135.setRegressionMethod(1); + MQ135.init(); + float r0_135 = 0; + for (int i = 0; i < 10; i++) + { + MQ135.update(); + r0_135 += MQ135.calibrate(RATIO_CLEAN_MQ135); + delay(600); + } + r0_135 /= 10; + MQ135.setR0(r0_135); + + // MQ7 + MQ7.setRegressionMethod(1); + MQ7.init(); + float r0_7 = 0; + for (int i = 0; i < 10; i++) + { + MQ7.update(); + r0_7 += MQ7.calibrate(RATIO_CLEAN_MQ7); + delay(600); + } + r0_7 /= 10; + MQ7.setR0(r0_7); + + 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); + } + + Serial.println("Kalibrasi selesai!"); + + display.clearDisplay(); + display.setCursor(0, 0); + display.println("Kalibrasi OK"); + display.println("Mulai Monitor"); + display.display(); + delay(2000); +} + +// ===================================================== +void loop() +{ + + // =============== MQ135 ================= + MQ135.setA(110.47); + MQ135.setB(-2.862); + float co2 = readAverage(MQ135); + if (co2 < 0) + co2 = 0; + + MQ135.setA(605.18); + MQ135.setB(-3.937); + float benzene = readAverage(MQ135); + if (benzene < 0) + benzene = 0; + + // =============== MQ7 =================== + MQ7.setA(99.042); + MQ7.setB(-1.518); + float co = readAverage(MQ7); + if (co < 0) + co = 0; + + // ========== AIR QUALITY ESTIMATION ===== + int aq = 100; + + 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); + + aq = constrain(aq, 0, 100); + + 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"; + + // =============== 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(); + + // =============== 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.println("========================\n"); + + delay(5000); + + if (client.connect(server, port)) + { + String url = "/api/update?"; + url += "co2=" + String(co2); + url += "&co=" + String(co); + url += "&bz=" + String(benzene); + url += "&aq=" + String(aq); + + SerialMon.print("[HTTP] GET "); + SerialMon.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"); + + // ------------------------------- + // READ RESPONSE + // ------------------------------- + String response = ""; + unsigned long timeout = millis(); + + while (client.connected() && millis() - timeout < 8000) + { + while (client.available()) + { + char c = client.read(); + response += c; + timeout = millis(); // reset timeout when data arrives + } + } + + client.stop(); + + SerialMon.println("----- HTTP RESPONSE -----"); + SerialMon.println(response); + SerialMon.println("-------------------------"); + + // ------------------------------- + // EXTRACT JSON BODY + // ------------------------------- + int jsonStart = response.indexOf('{'); + if (jsonStart >= 0) + { + String json = response.substring(jsonStart); + + SerialMon.print("[JSON] "); + SerialMon.println(json); + + oledStatus("RESP OK"); + } + else + { + SerialMon.println("[ERROR] No JSON found!"); + oledStatus("RESP ERROR"); + } + } + else + { + SerialMon.println("[ERROR] Server connection failed!"); + oledStatus("GAGAL KIRIM"); + } + + client.stop(); + SerialMon.println("===== LOOP END ====="); + + delay(10000); +} diff --git a/src/main.cpp.bck-no-edit b/src/main.cpp.bck-no-edit new file mode 100644 index 0000000..b5d210b --- /dev/null +++ b/src/main.cpp.bck-no-edit @@ -0,0 +1,182 @@ +#include +// ===================================================== +// ESP32 + MQ-135 + MQ-7 + OLED SSD1306 (0.96" 128x64) +// Bahasa Indonesia | Stabil | Client-ready +// ===================================================== + +#include +#include +#include +#include + +// ================= OLED CONFIG ================= +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define OLED_RESET -1 +#define OLED_ADDR 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +// ================= MQ CONFIG =================== +#define BOARD "ESP-32" +#define VOLTAGE 3.3 +#define ADC_BITS 12 + +#define PIN_MQ135 34 +#define PIN_MQ7 35 + +#define RATIO_CLEAN_MQ135 3.6 +#define RATIO_CLEAN_MQ7 27.5 + +MQUnifiedsensor MQ135(BOARD, VOLTAGE, ADC_BITS, PIN_MQ135, "MQ-135"); +MQUnifiedsensor MQ7(BOARD, VOLTAGE, ADC_BITS, PIN_MQ7, "MQ-7"); + +// ================= HELPER ====================== +float readAverage(MQUnifiedsensor &sensor, int samples = 5) { + float sum = 0; + for (int i = 0; i < samples; i++) { + sensor.update(); + sum += sensor.readSensor(); + delay(50); + } + return sum / samples; +} + +// ===================================================== +void setup() { + Serial.begin(115200); + delay(500); + + // ================= OLED INIT ================= + Wire.begin(21, 22); + if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("❌ OLED tidak ditemukan!"); + while (true) delay(1000); + } + + 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(); + + // ================= KALIBRASI ================= + Serial.println("Mulai kalibrasi sensor..."); + Serial.println("Pastikan udara bersih (±1-2 menit)"); + + // MQ135 + MQ135.setRegressionMethod(1); + MQ135.init(); + float r0_135 = 0; + for (int i = 0; i < 10; i++) { + MQ135.update(); + r0_135 += MQ135.calibrate(RATIO_CLEAN_MQ135); + delay(600); + } + r0_135 /= 10; + MQ135.setR0(r0_135); + + // MQ7 + MQ7.setRegressionMethod(1); + MQ7.init(); + float r0_7 = 0; + for (int i = 0; i < 10; i++) { + MQ7.update(); + r0_7 += MQ7.calibrate(RATIO_CLEAN_MQ7); + delay(600); + } + r0_7 /= 10; + MQ7.setR0(r0_7); + + 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); + } + + Serial.println("Kalibrasi selesai!"); + + display.clearDisplay(); + display.setCursor(0, 0); + display.println("Kalibrasi OK"); + display.println("Mulai Monitor"); + display.display(); + delay(2000); +} + +// ===================================================== +void loop() { + + // =============== MQ135 ================= + MQ135.setA(110.47); MQ135.setB(-2.862); + float co2 = readAverage(MQ135); + if (co2 < 0) co2 = 0; + + MQ135.setA(605.18); MQ135.setB(-3.937); + float benzene = readAverage(MQ135); + if (benzene < 0) benzene = 0; + + // =============== MQ7 =================== + MQ7.setA(99.042); MQ7.setB(-1.518); + float co = readAverage(MQ7); + if (co < 0) co = 0; + + // ========== AIR QUALITY ESTIMATION ===== + int aq = 100; + + 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); + + aq = constrain(aq, 0, 100); + + 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"; + + // =============== 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(); + + // =============== 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.println("========================\n"); + + delay(5000); +} diff --git a/src/main.cpp.gprs-ok b/src/main.cpp.gprs-ok new file mode 100644 index 0000000..0a17884 --- /dev/null +++ b/src/main.cpp.gprs-ok @@ -0,0 +1,228 @@ +/********************************************************* + * ESP32 + MQ135 + MQ7 + OLED SSD1306 + SIM800L + * GSM HTTP GET → http://k-dummy.my.id:8080/ + * Bahasa Indonesia | Client-ready + *********************************************************/ + +// ================== TINYGSM (MUST BE FIRST) ================== +#define TINY_GSM_MODEM_SIM800 +#define TINY_GSM_RX_BUFFER 650 + +#include +#include +#include +#include +#include +#include + +// ================== SERIAL ================== +#define SerialMon Serial + +// ================== SIM800L ================= +#define MODEM_RX 16 // ESP32 GPIO16 ← SIM800L TX +#define MODEM_TX 17 // ESP32 GPIO17 → SIM800L RX (via resistor) + +HardwareSerial SerialAT(2); +TinyGsm modem(SerialAT); +TinyGsmClient client(modem); + +// APN Indonesia (change if needed) +const char apn[] = "internet"; +const char user[] = ""; +const char pass[] = ""; + +// Server +const char server[] = "k-dummy.my.id"; +const int port = 8080; + +// ================== OLED ==================== +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define OLED_ADDR 0x3C +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); + +// ================== MQ SENSOR =============== +#define BOARD "ESP-32" +#define VOLTAGE 3.3 +#define ADC_BITS 12 +#define PIN_MQ135 34 +#define PIN_MQ7 35 + +#define RATIO_CLEAN_MQ135 3.6 +#define RATIO_CLEAN_MQ7 27.5 + +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) +{ + display.clearDisplay(); + display.setCursor(0, 0); + display.println("STATUS GSM:"); + display.println(msg); + display.display(); +} + +float readAverage(MQUnifiedsensor &s) +{ + float sum = 0; + for (int i = 0; i < 5; i++) + { + s.update(); + sum += s.readSensor(); + delay(50); + } + return sum / 5; +} + +void logSerial(const String &msg) +{ + SerialMon.println(msg); +} + +// ============================================================= +void setup() +{ + SerialMon.begin(115200); + delay(1000); + + SerialMon.println("================================="); + SerialMon.println(" ESP32 + MQ + SIM800L START "); + SerialMon.println("================================="); + + // OLED + Wire.begin(21, 22); + display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + + oledStatus("Init GSM..."); + logSerial("[OLED] Init GSM..."); + + // SIM800L UART + SerialMon.println("[GSM] Init UART..."); + SerialAT.begin(9600, SERIAL_8N1, MODEM_RX, MODEM_TX); + delay(3000); + + SerialMon.println("[GSM] Restart modem..."); + if (!modem.restart()) + { + SerialMon.println("[ERROR] Modem restart failed!"); + oledStatus("MODEM FAIL"); + while (1); + } + + SerialMon.println("[GSM] Waiting for network..."); + oledStatus("Cari jaringan..."); + if (!modem.waitForNetwork()) + { + SerialMon.println("[ERROR] No network!"); + oledStatus("NO SIGNAL"); + while (1); + } + + SerialMon.println("[GSM] Network OK"); + + SerialMon.println("[GSM] Connecting GPRS..."); + oledStatus("GPRS connect..."); + if (!modem.gprsConnect(apn, user, pass)) + { + SerialMon.println("[ERROR] GPRS failed!"); + oledStatus("GPRS FAIL"); + while (1); + } + + SerialMon.println("[GSM] GPRS CONNECTED"); + oledStatus("GSM READY"); + delay(1500); + + // MQ init + SerialMon.println("[MQ] Init MQ135 & MQ7"); + + MQ135.setRegressionMethod(1); + MQ135.init(); + MQ135.setR0(10); + + MQ7.setRegressionMethod(1); + MQ7.init(); + MQ7.setR0(10); + + SerialMon.println("[SYSTEM] Setup complete!"); +} + + +// ============================================================= +void loop() +{ + SerialMon.println("\n===== LOOP START ====="); + + // MQ135 → CO2 + MQ135.setA(110.47); + MQ135.setB(-2.862); + float co2 = readAverage(MQ135); + if (co2 < 0) co2 = 0; + + // MQ135 → Benzene + MQ135.setA(605.18); + MQ135.setB(-3.937); + float bz = readAverage(MQ135); + if (bz < 0) bz = 0; + + // MQ7 → CO + MQ7.setA(99.042); + MQ7.setB(-1.518); + float co = readAverage(MQ7); + if (co < 0) co = 0; + + int aq = constrain(100 - (co2 / 50) - (co * 2) - (bz * 3), 0, 100); + + // ===== SERIAL SENSOR LOG ===== + SerialMon.println("[SENSOR]"); + SerialMon.print("CO2 : "); SerialMon.print(co2); SerialMon.println(" ppm"); + SerialMon.print("CO : "); SerialMon.print(co); SerialMon.println(" ppm"); + SerialMon.print("BZ : "); SerialMon.print(bz); SerialMon.println(" ppm"); + SerialMon.print("AQ : "); SerialMon.print(aq); SerialMon.println(" %"); + + // OLED + display.clearDisplay(); + display.setCursor(0, 0); + display.printf("CO2: %.0f ppm\n", co2); + display.printf("CO : %.1f ppm\n", co); + display.printf("BZ : %.1f ppm\n", bz); + display.printf("AQ : %d%%\n", aq); + display.display(); + + // ===== SEND TO SERVER ===== + SerialMon.println("[HTTP] Sending data..."); + oledStatus("Kirim data..."); + + if (client.connect(server, port)) + { + String url = "/api/update?"; + url += "CO2=" + String(co2); + url += "&CO=" + String(co); + url += "&BZ=" + String(bz); + url += "&AQ=" + String(aq); + + SerialMon.print("[HTTP] GET "); + SerialMon.println(url); + + 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"); + + SerialMon.println("[HTTP] Data sent!"); + oledStatus("TERKIRIM"); + } + else + { + SerialMon.println("[ERROR] Server connection failed!"); + oledStatus("GAGAL KIRIM"); + } + + client.stop(); + SerialMon.println("===== LOOP END ====="); + + delay(15000); +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html