Advertisement
mudhita_triari

ESP32 Influxdb

May 8th, 2024
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 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.  
  7. #define MAX485_RE_NEG  5
  8. #define MAX485_DE      4
  9. #define SSERIAL_RX_PIN 16
  10. #define SSERIAL_TX_PIN 17
  11.  
  12. #define NUM_SENSORS 2
  13. #define TEMP_SENSOR 0
  14. #define HUM_SENSOR 1
  15.  
  16. // WiFi Configuration
  17. const char* ssid = "vivo V29";
  18. const char* password = "Nabila041185";
  19.  
  20. #define INFLUXDB_URL "http://127.0.0.1:8087"
  21. // InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
  22. #define INFLUXDB_TOKEN "hh4g2GQn1cgSstvgUtTuYFT95Gn49Hf7qBou3OGWsJDW5lhwY-seBycBMqljiJ_aepMpNbMoiZqBiNyibynb3Q=="
  23. // InfluxDB 2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
  24. #define INFLUXDB_ORG "Indobot"
  25. // InfluxDB 2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
  26. #define INFLUXDB_BUCKET "pengamatan suhu & kelembaban"
  27.  
  28. // InfluxDB client instance
  29. InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
  30.  
  31.  
  32. // Data point
  33. Point sensor("sensor_data");
  34.  
  35. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  36. ModbusMaster node;
  37.  
  38. float sensorData[NUM_SENSORS];
  39. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  40.  
  41. void preTransmission() {
  42.   digitalWrite(MAX485_RE_NEG, 1);
  43.   digitalWrite(MAX485_DE, 1);
  44. }
  45.  
  46. void postTransmission() {
  47.   digitalWrite(MAX485_RE_NEG, 0);
  48.   digitalWrite(MAX485_DE, 0);
  49. }
  50.  
  51. void setup() {
  52.   pinMode(MAX485_RE_NEG, OUTPUT);
  53.   pinMode(MAX485_DE, OUTPUT);
  54.   digitalWrite(MAX485_RE_NEG, 0);
  55.   digitalWrite(MAX485_DE, 0);
  56.  
  57.   Serial.begin(115200);
  58.  
  59.   // Start WiFi connection
  60.   Serial.println("Connecting to WiFi...");
  61.   WiFi.begin(ssid, password);
  62.   while (WiFi.status() != WL_CONNECTED) {
  63.     delay(500);
  64.     Serial.print(".");
  65.   }
  66.   Serial.println("Connected to WiFi");
  67.  
  68.   RS485Serial.begin(9600);
  69.   node.begin(1, RS485Serial);
  70.   node.preTransmission(preTransmission);
  71.   node.postTransmission(postTransmission);
  72. }
  73.  
  74. void loop() {
  75.   for (int i = 0; i < NUM_SENSORS; i++) {
  76.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  77.  
  78.     if (result == node.ku8MBSuccess) {
  79.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  80.     } else {
  81.       delay(1000);
  82.       return;
  83.     }
  84.   }
  85.  
  86.   Serial.print("Temperature: ");
  87.   Serial.print(sensorData[TEMP_SENSOR]);
  88.   Serial.println(" °C");
  89.   Serial.print("Humidity: ");
  90.   Serial.print(sensorData[HUM_SENSOR]);
  91.   Serial.println(" %");
  92.  
  93.   // Send data to InfluxDB
  94.   sensor.clearFields();
  95.   sensor.addField("temperature", sensorData[TEMP_SENSOR]);
  96.   sensor.addField("humidity", sensorData[HUM_SENSOR]);
  97.  
  98.   if (client.validateConnection()) {
  99.     Serial.println("Sending data to InfluxDB...");
  100.     if (!client.writePoint(sensor)) {
  101.       Serial.print("InfluxDB write failed: ");
  102.       Serial.println(client.getLastErrorMessage());
  103.     } else {
  104.       Serial.println("Data sent to InfluxDB successfully");
  105.     }
  106.   } else {
  107.     Serial.print("InfluxDB connection failed: ");
  108.     Serial.println(client.getLastErrorMessage());
  109.   }
  110.  
  111.   delay(5000); // Example of data sending interval every 5 seconds
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement