Advertisement
mudhita_triari

Untitled

Jun 24th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <WiFi.h>
  3. #include <WiFiMulti.h>
  4. #include <WiFiManager.h>
  5. #include <InfluxDbClient.h>
  6. #include <InfluxDbCloud.h>
  7. #include <ArduinoJson.h>
  8. #include <otadrive_esp.h>
  9.  
  10. WiFiManager wm;
  11.  
  12. // WiFi and InfluxDB credentials
  13. char ssid[32];
  14. char password[32];
  15.  
  16. const char* INFLUXDB_URL = "https://us-east-1-1.aws.cloud2.influxdata.com";
  17. const char* INFLUXDB_TOKEN = "bY5Y8WsUTkW4aPPhvbrx6XWXi-s_-Da8JA3sN14ti2_QdzyCKBkoht93Ms_aoiYlZNuwGa2OmGwSuAPiwVJoYQ==";
  18. const char* INFLUXDB_ORG = "023982a3a96b797a";
  19. const char* INFLUXDB_BUCKET = "INDOBOT_FSID";
  20. const char* TZ_INFO = "UTC7";
  21.  
  22. // UART communication settings
  23. const int SERIAL_BAUD_RATE = 115200;
  24. const int SERIAL_RX_PIN = 16;
  25. const int SERIAL_TX_PIN = 17;
  26.  
  27. // WiFiMulti instance
  28. WiFiMulti wifiMulti;
  29.  
  30. // HardwareSerial instance for UART2
  31. HardwareSerial SerialESP32_7(2);
  32.  
  33. // InfluxDB client instance
  34. InfluxDBClient influxClient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
  35.  
  36. // Data point for InfluxDB
  37. Point sensor("sensor_data");
  38.  
  39. String FW_VERSION = "1.0.4";
  40.  
  41. unsigned long previousSyncMillis = 0;
  42. const long syncInterval = 60000; // Sync every 60 seconds
  43.  
  44. // Function prototypes
  45. void saveWifiCallback();
  46. void configModeCallback(WiFiManager *myWiFiManager);
  47. void onUpdateProgress(int progress, int total);
  48. void sync_task();
  49. void sendDataOverUART(const char* chipId, const char* tempOffset, const char* humOffset, const char* ppmOffset,
  50. const char* luxOffset, const char* longitude, const char* latitude, const char* elevation);
  51. void printAligned(const char* label, const char* value);
  52. void logData(const char* label, float value);
  53. void logData(const char* label, const char* value);
  54.  
  55. void setup() {
  56. Serial.begin(SERIAL_BAUD_RATE);
  57. Serial.println("Starting setup...");
  58.  
  59. // Initialize UART on specified RX and TX pins
  60. SerialESP32_7.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_RX_PIN, SERIAL_TX_PIN);
  61.  
  62. // Enable debug output for WiFiManager
  63. wm.setDebugOutput(true);
  64.  
  65. // Add custom parameters for WiFiManager
  66. WiFiManagerParameter custom_id("id", "Device ID", "", 32);
  67. WiFiManagerParameter custom_hum("hum", "Humidity", "", 5);
  68. WiFiManagerParameter custom_temp("temp", "Temperature", "", 5);
  69. WiFiManagerParameter custom_ppm("ppm", "PPM", "", 5);
  70. WiFiManagerParameter custom_lux("lux", "Lux", "", 5);
  71. WiFiManagerParameter custom_longitude("longitude", "Longitude", "", 12);
  72. WiFiManagerParameter custom_latitude("latitude", "Latitude", "", 12);
  73. WiFiManagerParameter custom_elevation("elevation", "Elevation", "", 5);
  74.  
  75. // Add parameters to WiFiManager
  76. wm.addParameter(&custom_id);
  77. wm.addParameter(&custom_hum);
  78. wm.addParameter(&custom_temp);
  79. wm.addParameter(&custom_ppm);
  80. wm.addParameter(&custom_lux);
  81. wm.addParameter(&custom_longitude);
  82. wm.addParameter(&custom_latitude);
  83. wm.addParameter(&custom_elevation);
  84.  
  85. // Set WiFiManager callbacks
  86. wm.setAPCallback(configModeCallback);
  87. wm.setSaveConfigCallback(saveWifiCallback);
  88.  
  89. // Start WiFiManager configuration portal
  90. if (!wm.autoConnect("ENVIRO OBSERVER DIAGNOSIS", "password")) {
  91. Serial.println("Failed to connect to WiFi and timeout");
  92. delay(3000);
  93. ESP.restart();
  94. }
  95.  
  96. Serial.println("Connected to WiFi");
  97.  
  98. // Send data over UART after connection
  99. sendDataOverUART(custom_id.getValue(), custom_hum.getValue(), custom_temp.getValue(), custom_ppm.getValue(),
  100. custom_lux.getValue(), custom_longitude.getValue(), custom_latitude.getValue(),
  101. custom_elevation.getValue());
  102.  
  103. // Check saved parameter values
  104. Serial.println("\n-----------------KALIBRASI NODEMESH-----------------\n");
  105. printAligned("Device ID:", custom_id.getValue());
  106. printAligned("Humidity:", custom_hum.getValue());
  107. printAligned("Temperature:", custom_temp.getValue());
  108. printAligned("PPM:", custom_ppm.getValue());
  109. printAligned("Lux:", custom_lux.getValue());
  110.  
  111. Serial.println("\n------------------LOKASI NODEMESH------------------\n");
  112. printAligned("Longitude:", custom_longitude.getValue());
  113. printAligned("Latitude:", custom_latitude.getValue());
  114. printAligned("Elevation:", custom_elevation.getValue());
  115.  
  116. Serial.println("Setup complete.");
  117. }
  118.  
  119. void loop() {
  120. if (SerialESP32_7.available()) {
  121. String serialMsg = SerialESP32_7.readStringUntil('\n');
  122. serialMsg.trim();
  123.  
  124. Serial.println("Diterima dari Pusat Data: " + serialMsg);
  125.  
  126. if (serialMsg.length() > 0) {
  127. DynamicJsonDocument doc(256);
  128. DeserializationError error = deserializeJson(doc, serialMsg);
  129.  
  130. if (error) {
  131. Serial.print("deserializeJson() gagal: ");
  132. Serial.println(error.c_str());
  133. } else {
  134. const char* id = doc["id"];
  135. if (!id) {
  136. Serial.println("JSON tidak valid: 'id' tidak ditemukan");
  137. } else {
  138. if (doc.containsKey("hum") && doc.containsKey("temp") && doc.containsKey("ppm") && doc.containsKey("lux") &&
  139. doc.containsKey("longitude") && doc.containsKey("latitude") && doc.containsKey("elevation")) {
  140. float hum = doc["hum"];
  141. float temp = doc["temp"];
  142. float ppm = doc["ppm"];
  143. float lux = doc["lux"];
  144. float longitude = String(doc["longitude"].as<float>(), 6).toFloat();
  145. float latitude = String(doc["latitude"].as<float>(), 6).toFloat();
  146. float elevation = doc["elevation"];
  147.  
  148. // Log data received from UART
  149. Serial.println("\nData diterima dari UART:");
  150. logData("ID:", id);
  151. logData("Kelembaban:", hum);
  152. logData("Suhu:", temp);
  153. logData("PPM:", ppm);
  154. logData("Lux:", lux);
  155. logData("Longitude:", longitude);
  156. logData("Latitude:", latitude);
  157. logData("Elevasi:", elevation);
  158.  
  159. sensor.clearFields();
  160. sensor.clearTags();
  161. sensor.addTag("id", id);
  162. sensor.addField("hum", hum);
  163. sensor.addField("temp", temp);
  164. sensor.addField("ppm", ppm);
  165. sensor.addField("lux", lux);
  166. sensor.addField("longitude", longitude);
  167. sensor.addField("latitude", latitude);
  168. sensor.addField("elevation", elevation);
  169.  
  170. Serial.println("\nMengirim data berikut ke InfluxDB:");
  171. logData("ID:", id);
  172. logData("Kelembaban:", hum);
  173. logData("Suhu:", temp);
  174. logData("PPM:", ppm);
  175. logData("Lux:", lux);
  176. logData("Longitude:", longitude);
  177. logData("Latitude:", latitude);
  178. logData("Elevasi:", elevation);
  179.  
  180. if (!influxClient.writePoint(sensor)) {
  181. Serial.print("InfluxDB.write gagal: ");
  182. Serial.println(influxClient.getLastErrorMessage());
  183. }
  184. } else {
  185. Serial.println("JSON tidak valid: beberapa kunci data tidak ditemukan");
  186. }
  187. }
  188. }
  189. }
  190. }
  191. sync_task();
  192. }
  193.  
  194. void sync_task() {
  195. unsigned long currentMillis = millis();
  196. if (currentMillis - previousSyncMillis >= syncInterval) {
  197. previousSyncMillis = currentMillis;
  198.  
  199. if (WiFi.status() != WL_CONNECTED) {
  200. Serial.println("WiFi tidak terhubung! Mencoba menghubungkan kembali...");
  201. WiFi.reconnect();
  202. return;
  203. }
  204.  
  205. Serial.println("Firmware version: " + FW_VERSION);
  206. OTADRIVE.updateFirmware();
  207. }
  208. }
  209.  
  210. void sendDataOverUART(const char* chipId, const char* tempOffset, const char* humOffset, const char* ppmOffset,
  211. const char* luxOffset, const char* longitude, const char* latitude, const char* elevation) {
  212. StaticJsonDocument<200> jsonDocument;
  213.  
  214. jsonDocument["chipId"] = chipId;
  215. jsonDocument["tempOffset"] = tempOffset;
  216. jsonDocument["humOffset"] = humOffset;
  217. jsonDocument["ppmOffset"] = ppmOffset;
  218. jsonDocument["luxOffset"] = luxOffset;
  219. jsonDocument["longitude"] = longitude;
  220. jsonDocument["latitude"] = latitude;
  221. jsonDocument["elevation"] = elevation;
  222.  
  223. String jsonString;
  224. serializeJson(jsonDocument, jsonString);
  225.  
  226. SerialESP32_7.println(jsonString);
  227. }
  228.  
  229. void printAligned(const char* label, const char* value) {
  230. char buffer[100];
  231. snprintf(buffer, sizeof(buffer), "%-15s %s", label, value);
  232. Serial.println(buffer);
  233. }
  234.  
  235. void logData(const char* label, float value) {
  236. char buffer[100];
  237. snprintf(buffer, sizeof(buffer), "%-15s %.2f", label, value);
  238. Serial.println(buffer);
  239. }
  240.  
  241. void logData(const char* label, const char* value) {
  242. char buffer[100];
  243. snprintf(buffer, sizeof(buffer), "%-15s %s", label, value);
  244. Serial.println(buffer);
  245. }
  246.  
  247. void saveWifiCallback() {
  248. // Code to be executed when WiFi configuration is saved
  249. }
  250.  
  251. void configModeCallback(WiFiManager *myWiFiManager) {
  252. Serial.println("Entered config mode");
  253. Serial.println(WiFi.softAPIP());
  254. Serial.println(myWiFiManager->getConfigPortalSSID());
  255. }
  256.  
  257. void onUpdateProgress(int progress, int total) {
  258. // Code to handle OTA update progress
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement