Advertisement
mudhita_triari

ESP32 - Influxdb

May 9th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <Wire.h>
  3. #include <ModbusMaster.h>
  4. #include <SoftwareSerial.h>
  5. #include <InfluxDbClient.h>
  6. #include <InfluxDbCloud.h>
  7.  
  8. #define MAX485_RE_NEG 5
  9. #define MAX485_DE 4
  10. #define SSERIAL_RX_PIN 16
  11. #define SSERIAL_TX_PIN 17
  12.  
  13. #define NUM_SENSORS 2
  14. #define TEMP_SENSOR 0
  15. #define HUM_SENSOR 1
  16.  
  17. #define WIFI_SSID "vivo V29"
  18. #define WIFI_PASSWORD "Nabila041185"
  19.  
  20. #define WIFI_SSID "vivo V29"
  21. #define WIFI_PASSWORD "Nabila041185"
  22. #define INFLUXDB_URL "http://192.168.113.185:8087"
  23. #define INFLUXDB_TOKEN "2a88aqokcKzzTjD42VwkqcF7Rw71k_XSd8UnKUY0f16lUt_yUp0B9d5TCCVlZv-espWVUMfmSD3xvGCNmNu9xw=="
  24. #define INFLUXDB_ORG "Indobot"
  25. #define INFLUXDB_BUCKET "pengamatan suhu & kelembaban"
  26.  
  27. #define TZ_INFO "UTC7"
  28.  
  29. InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
  30.  
  31. Point sensor("wifi_status");
  32.  
  33. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  34. ModbusMaster node;
  35.  
  36. float sensorData[NUM_SENSORS];
  37. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  38.  
  39. void preTransmission() {
  40. digitalWrite(MAX485_RE_NEG, 1);
  41. digitalWrite(MAX485_DE, 1);
  42. }
  43.  
  44. void postTransmission() {
  45. digitalWrite(MAX485_RE_NEG, 0);
  46. digitalWrite(MAX485_DE, 0);
  47. }
  48.  
  49. void setup() {
  50. pinMode(MAX485_RE_NEG, OUTPUT);
  51. pinMode(MAX485_DE, OUTPUT);
  52. digitalWrite(MAX485_RE_NEG, 0);
  53. digitalWrite(MAX485_DE, 0);
  54.  
  55. Serial.begin(115200);
  56.  
  57. WiFi.mode(WIFI_STA);
  58. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  59.  
  60. Serial.print("Connecting to WiFi...");
  61. while (WiFi.status() != WL_CONNECTED) {
  62. delay(500);
  63. Serial.print(".");
  64. }
  65. Serial.println("Connected to WiFi");
  66.  
  67. RS485Serial.begin(9600);
  68. node.begin(1, RS485Serial);
  69. node.preTransmission(preTransmission);
  70. node.postTransmission(postTransmission);
  71.  
  72. // Accurate time is necessary for certificate validation and writing in batches
  73. timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  74.  
  75. // Check server connection
  76. if (client.validateConnection()) {
  77. Serial.print("Connected to InfluxDB: ");
  78. Serial.println(client.getServerUrl());
  79. } else {
  80. Serial.print("InfluxDB connection failed: ");
  81. Serial.println(client.getLastErrorMessage());
  82. }
  83. }
  84.  
  85. void loop() {
  86. for (int i = 0; i < NUM_SENSORS; i++) {
  87. uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  88.  
  89. if (result == node.ku8MBSuccess) {
  90. sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  91. } else {
  92. delay(1000);
  93. return;
  94. }
  95. }
  96.  
  97. Serial.print("Temperature: ");
  98. Serial.print(sensorData[TEMP_SENSOR]);
  99. Serial.println(" °C");
  100. Serial.print("Humidity: ");
  101. Serial.print(sensorData[HUM_SENSOR]);
  102. Serial.println(" %");
  103.  
  104. // Send data to InfluxDB
  105. sensor.clearFields();
  106. sensor.addField("temperature", sensorData[TEMP_SENSOR]);
  107. sensor.addField("humidity", sensorData[HUM_SENSOR]);
  108.  
  109. if (client.validateConnection()) {
  110. Serial.println("Sending data to InfluxDB...");
  111. if (!client.writePoint(sensor)) {
  112. Serial.print("InfluxDB write failed: ");
  113. Serial.println(client.getLastErrorMessage());
  114. } else {
  115. Serial.println("Data sent to InfluxDB successfully");
  116. }
  117. } else {
  118. Serial.print("InfluxDB connection failed: ");
  119. Serial.println(client.getLastErrorMessage());
  120. }
  121.  
  122. delay(5000); // Example of data sending interval every 5 seconds
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement