Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <WiFi.h>
- #include <WiFiMulti.h>
- #include <WiFiManager.h>
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- #include <otadrive_esp.h>
- WiFiManager wm;
- // Server details
- const char* serverName = "https://bootcampindobot.com/index_mudhita1.php";
- const char* apiKeyValue = "tPmAT5Ab3j7F9";
- // UART communication settings
- const int SERIAL_BAUD_RATE = 115200;
- const int SERIAL_RX_PIN = 16;
- const int SERIAL_TX_PIN = 17;
- // WiFiMulti instance
- WiFiMulti wifiMulti;
- // HardwareSerial instance for UART2
- HardwareSerial SerialESP32_7(2);
- String FW_VERSION = "1.0.4";
- unsigned long previousSyncMillis = 0;
- const long syncInterval = 60000; // Sync every 60 seconds
- // LED pin
- const int LED_PIN = 2; // Ganti dengan pin yang sesuai untuk LED
- // Function prototypes
- void saveWifiCallback();
- void configModeCallback(WiFiManager *myWiFiManager);
- void onUpdateProgress(int progress, int total);
- void sync_task();
- void sendDataOverUART(const char* chipId, const char* tempOffset, const char* humOffset, const char* ppmOffset,
- const char* luxOffset, const char* longitude, const char* latitude, const char* elevation);
- void sendHttpPost(const char* id, float hum, float temp, float ppm, float lux, float longitude, float latitude, float elevation);
- void printAligned(const char* label, const char* value);
- void logData(const char* label, float value);
- void logData(const char* label, const char* value);
- void blinkLED(int times, int duration);
- void setup() {
- // Inisialisasi pin LED
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW);
- Serial.begin(SERIAL_BAUD_RATE);
- Serial.println("Starting setup...");
- // Inisialisasi UART pada pin RX dan TX yang ditentukan
- SerialESP32_7.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_RX_PIN, SERIAL_TX_PIN);
- // Enable debug output untuk WiFiManager
- wm.setDebugOutput(true);
- // Tambahkan parameter custom untuk WiFiManager
- WiFiManagerParameter custom_id("id", "Device ID", "", 32);
- WiFiManagerParameter custom_hum("hum", "Humidity", "", 5);
- WiFiManagerParameter custom_temp("temp", "Temperature", "", 5);
- WiFiManagerParameter custom_ppm("ppm", "PPM", "", 5);
- WiFiManagerParameter custom_lux("lux", "Lux", "", 5);
- WiFiManagerParameter custom_longitude("longitude", "Longitude", "", 12);
- WiFiManagerParameter custom_latitude("latitude", "Latitude", "", 12);
- WiFiManagerParameter custom_elevation("elevation", "Elevation", "", 5);
- // Tambahkan parameter ke WiFiManager
- wm.addParameter(&custom_id);
- wm.addParameter(&custom_hum);
- wm.addParameter(&custom_temp);
- wm.addParameter(&custom_ppm);
- wm.addParameter(&custom_lux);
- wm.addParameter(&custom_longitude);
- wm.addParameter(&custom_latitude);
- wm.addParameter(&custom_elevation);
- // Set callback untuk WiFiManager
- wm.setAPCallback(configModeCallback);
- wm.setSaveConfigCallback(saveWifiCallback);
- // Mulai konfigurasi WiFiManager portal
- if (!wm.autoConnect("ENVIRO OBSERVER DIAGNOSIS", "password")) {
- Serial.println("Failed to connect to WiFi and timeout");
- delay(3000);
- ESP.restart();
- }
- Serial.println("Connected to WiFi");
- // Kirim data melalui UART setelah terhubung
- sendDataOverUART(custom_id.getValue(), custom_hum.getValue(), custom_temp.getValue(), custom_ppm.getValue(),
- custom_lux.getValue(), custom_longitude.getValue(), custom_latitude.getValue(),
- custom_elevation.getValue());
- // Periksa nilai parameter yang disimpan
- Serial.println("\n-----------------KALIBRASI NODEMESH-----------------\n");
- printAligned("Device ID:", custom_id.getValue());
- printAligned("Humidity:", custom_hum.getValue());
- printAligned("Temperature:", custom_temp.getValue());
- printAligned("PPM:", custom_ppm.getValue());
- printAligned("Lux:", custom_lux.getValue());
- Serial.println("\n------------------LOKASI NODEMESH------------------\n");
- printAligned("Longitude:", custom_longitude.getValue());
- printAligned("Latitude:", custom_latitude.getValue());
- printAligned("Elevation:", custom_elevation.getValue());
- Serial.println("Setup complete.");
- }
- void loop() {
- if (SerialESP32_7.available()) {
- blinkLED(3, 100); // LED berkedip 3 kali saat data diterima
- String serialMsg = SerialESP32_7.readStringUntil('\n');
- serialMsg.trim();
- Serial.println("Diterima dari Pusat Data: " + serialMsg);
- if (serialMsg.length() > 0) {
- DynamicJsonDocument doc(256);
- DeserializationError error = deserializeJson(doc, serialMsg);
- if (error) {
- Serial.print("deserializeJson() gagal: ");
- Serial.println(error.c_str());
- } else {
- const char* id = doc["id"];
- if (!id) {
- Serial.println("JSON tidak valid: 'id' tidak ditemukan");
- } else {
- if (doc.containsKey("hum") && doc.containsKey("temp") && doc.containsKey("ppm") && doc.containsKey("lux") &&
- doc.containsKey("longitude") && doc.containsKey("latitude") && doc.containsKey("elevation")) {
- float hum = doc["hum"];
- float temp = doc["temp"];
- float ppm = doc["ppm"];
- float lux = doc["lux"];
- float longitude = doc["longitude"].as<float>();
- float latitude = doc["latitude"].as<float>();
- float elevation = doc["elevation"];
- // Log data received from UART
- Serial.println("\nData diterima dari UART:");
- logData("ID:", id);
- logData("Kelembaban:", hum);
- logData("Suhu:", temp);
- logData("PPM:", ppm);
- logData("Lux:", lux);
- logData("Longitude:", longitude);
- logData("Latitude:", latitude);
- logData("Elevasi:", elevation);
- // Send data to the server
- sendHttpPost(id, hum, temp, ppm, lux, longitude, latitude, elevation);
- Serial.println("\nMengirim data berikut ke server:");
- logData("ID:", id);
- logData("UNIT:", id);
- logData("Kelembaban:", hum);
- logData("Suhu:", temp);
- logData("PPM:", ppm);
- logData("Lux:", lux);
- logData("Longitude:", longitude);
- logData("Latitude:", latitude);
- logData("Elevasi:", elevation);
- } else {
- Serial.println("JSON tidak valid: beberapa kunci data tidak ditemukan");
- }
- }
- }
- }
- }
- sync_task();
- }
- void sync_task() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousSyncMillis >= syncInterval) {
- previousSyncMillis = currentMillis;
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("WiFi tidak terhubung! Mencoba menghubungkan kembali...");
- WiFi.reconnect();
- return;
- }
- Serial.println("Firmware version: " + FW_VERSION);
- OTADRIVE.updateFirmware();
- }
- }
- void sendDataOverUART(const char* chipId, const char* tempOffset, const char* humOffset, const char* ppmOffset,
- const char* luxOffset, const char* longitude, const char* latitude, const char* elevation) {
- StaticJsonDocument<200> jsonDocument;
- jsonDocument["chipId"] = chipId;
- jsonDocument["tempOffset"] = tempOffset;
- jsonDocument["humOffset"] = humOffset;
- jsonDocument["ppmOffset"] = ppmOffset;
- jsonDocument["luxOffset"] = luxOffset;
- jsonDocument["longitude"] = longitude;
- jsonDocument["latitude"] = latitude;
- jsonDocument["elevation"] = elevation;
- String jsonString;
- serializeJson(jsonDocument, jsonString);
- SerialESP32_7.println(jsonString);
- }
- void sendHttpPost(const char* id, float hum, float temp, float ppm, float lux, float longitude, float latitude, float elevation) {
- if (WiFi.status() == WL_CONNECTED) {
- blinkLED(2, 100); // LED berkedip 2 kali saat data dikirim
- HTTPClient http;
- http.begin(serverName);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- String httpRequestData = "api_key=" + String(apiKeyValue) + "&id=" + String(id) + "&hum=" + String(hum) +
- "&temp=" + String(temp) + "&ppm=" + String(ppm) + "&lux=" + String(lux) +
- "&longitude=" + String(longitude) + "&latitude=" + String(latitude) + "&elevation=" + String(elevation);
- int httpResponseCode = http.POST(httpRequestData);
- if (httpResponseCode > 0) {
- String response = http.getString();
- Serial.println("HTTP Response code: " + String(httpResponseCode));
- Serial.println("Response: " + response);
- } else {
- Serial.println("Error on sending POST: " + String(httpResponseCode));
- }
- http.end();
- } else {
- Serial.println("WiFi Disconnected");
- }
- }
- void saveWifiCallback() {
- Serial.println("Parameter WiFi tersimpan.");
- }
- void configModeCallback(WiFiManager *myWiFiManager) {
- Serial.println("Entered configuration mode");
- Serial.println(WiFi.softAPIP());
- Serial.println(myWiFiManager->getConfigPortalSSID());
- }
- void onUpdateProgress(int progress, int total) {
- int percentage = (progress * 100) / total;
- Serial.printf("Progress: %d%%\n", percentage);
- }
- void printAligned(const char* label, const char* value) {
- Serial.print(label);
- int len = strlen(label) + strlen(value);
- for (int i = len; i < 40; i++) {
- Serial.print(" ");
- }
- Serial.println(value);
- }
- void logData(const char* label, float value) {
- Serial.print(label);
- Serial.println(value);
- }
- void logData(const char* label, const char* value) {
- Serial.print(label);
- Serial.println(value);
- }
- void blinkLED(int times, int duration) {
- for (int i = 0; i < times; i++) {
- digitalWrite(LED_PIN, HIGH);
- delay(duration);
- digitalWrite(LED_PIN, LOW);
- delay(duration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement