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 <InfluxDbClient.h>
- #include <InfluxDbCloud.h>
- #include <ArduinoJson.h>
- #include <otadrive_esp.h>
- WiFiManager wm;
- // WiFi and InfluxDB credentials
- char ssid[32];
- char password[32];
- const char* INFLUXDB_URL = "https://us-east-1-1.aws.cloud2.influxdata.com";
- const char* INFLUXDB_TOKEN = "bY5Y8WsUTkW4aPPhvbrx6XWXi-s_-Da8JA3sN14ti2_QdzyCKBkoht93Ms_aoiYlZNuwGa2OmGwSuAPiwVJoYQ==";
- const char* INFLUXDB_ORG = "023982a3a96b797a";
- const char* INFLUXDB_BUCKET = "INDOBOT_FSID";
- const char* TZ_INFO = "UTC7";
- // 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);
- // InfluxDB client instance
- InfluxDBClient influxClient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
- // Data point for InfluxDB
- Point sensor("sensor_data");
- String FW_VERSION = "1.0.4";
- unsigned long previousSyncMillis = 0;
- const long syncInterval = 60000; // Sync every 60 seconds
- // 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 printAligned(const char* label, const char* value);
- void logData(const char* label, float value);
- void logData(const char* label, const char* value);
- void setup() {
- Serial.begin(SERIAL_BAUD_RATE);
- Serial.println("Starting setup...");
- // Initialize UART on specified RX and TX pins
- SerialESP32_7.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_RX_PIN, SERIAL_TX_PIN);
- // Enable debug output for WiFiManager
- wm.setDebugOutput(true);
- // Add custom parameters for 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);
- // Add parameters to 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 WiFiManager callbacks
- wm.setAPCallback(configModeCallback);
- wm.setSaveConfigCallback(saveWifiCallback);
- // Start WiFiManager configuration 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");
- // Send data over UART after connection
- 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());
- // Check saved parameter values
- 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()) {
- 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 = String(doc["longitude"].as<float>(), 6).toFloat();
- float latitude = String(doc["latitude"].as<float>(), 6).toFloat();
- 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);
- sensor.clearFields();
- sensor.clearTags();
- sensor.addTag("id", id);
- sensor.addField("hum", hum);
- sensor.addField("temp", temp);
- sensor.addField("ppm", ppm);
- sensor.addField("lux", lux);
- sensor.addField("longitude", longitude);
- sensor.addField("latitude", latitude);
- sensor.addField("elevation", elevation);
- Serial.println("\nMengirim data berikut ke InfluxDB:");
- logData("ID:", id);
- logData("Kelembaban:", hum);
- logData("Suhu:", temp);
- logData("PPM:", ppm);
- logData("Lux:", lux);
- logData("Longitude:", longitude);
- logData("Latitude:", latitude);
- logData("Elevasi:", elevation);
- if (!influxClient.writePoint(sensor)) {
- Serial.print("InfluxDB.write gagal: ");
- Serial.println(influxClient.getLastErrorMessage());
- }
- } 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 printAligned(const char* label, const char* value) {
- char buffer[100];
- snprintf(buffer, sizeof(buffer), "%-15s %s", label, value);
- Serial.println(buffer);
- }
- void logData(const char* label, float value) {
- char buffer[100];
- snprintf(buffer, sizeof(buffer), "%-15s %.2f", label, value);
- Serial.println(buffer);
- }
- void logData(const char* label, const char* value) {
- char buffer[100];
- snprintf(buffer, sizeof(buffer), "%-15s %s", label, value);
- Serial.println(buffer);
- }
- void saveWifiCallback() {
- // Code to be executed when WiFi configuration is saved
- }
- void configModeCallback(WiFiManager *myWiFiManager) {
- Serial.println("Entered config mode");
- Serial.println(WiFi.softAPIP());
- Serial.println(myWiFiManager->getConfigPortalSSID());
- }
- void onUpdateProgress(int progress, int total) {
- // Code to handle OTA update progress
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement