Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <Wire.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #include <InfluxDbClient.h>
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- // WiFi Configuration
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- #define INFLUXDB_URL "http://127.0.0.1:8087"
- // InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
- #define INFLUXDB_TOKEN "hh4g2GQn1cgSstvgUtTuYFT95Gn49Hf7qBou3OGWsJDW5lhwY-seBycBMqljiJ_aepMpNbMoiZqBiNyibynb3Q=="
- // InfluxDB 2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
- #define INFLUXDB_ORG "Indobot"
- // InfluxDB 2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
- #define INFLUXDB_BUCKET "pengamatan suhu & kelembaban"
- // InfluxDB client instance
- InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
- // Data point
- Point sensor("sensor_data");
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- float sensorData[NUM_SENSORS];
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- Serial.begin(115200);
- // Start WiFi connection
- Serial.println("Connecting to WiFi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("Connected to WiFi");
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- void loop() {
- for (int i = 0; i < NUM_SENSORS; i++) {
- uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
- if (result == node.ku8MBSuccess) {
- sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
- } else {
- delay(1000);
- return;
- }
- }
- Serial.print("Temperature: ");
- Serial.print(sensorData[TEMP_SENSOR]);
- Serial.println(" °C");
- Serial.print("Humidity: ");
- Serial.print(sensorData[HUM_SENSOR]);
- Serial.println(" %");
- // Send data to InfluxDB
- sensor.clearFields();
- sensor.addField("temperature", sensorData[TEMP_SENSOR]);
- sensor.addField("humidity", sensorData[HUM_SENSOR]);
- if (client.validateConnection()) {
- Serial.println("Sending data to InfluxDB...");
- if (!client.writePoint(sensor)) {
- Serial.print("InfluxDB write failed: ");
- Serial.println(client.getLastErrorMessage());
- } else {
- Serial.println("Data sent to InfluxDB successfully");
- }
- } else {
- Serial.print("InfluxDB connection failed: ");
- Serial.println(client.getLastErrorMessage());
- }
- delay(5000); // Example of data sending interval every 5 seconds
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement