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>
- #include <InfluxDbCloud.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
- #define WIFI_SSID "vivo V29"
- #define WIFI_PASSWORD "Nabila041185"
- #define WIFI_SSID "vivo V29"
- #define WIFI_PASSWORD "Nabila041185"
- #define INFLUXDB_URL "http://192.168.113.185:8087"
- #define INFLUXDB_TOKEN "2a88aqokcKzzTjD42VwkqcF7Rw71k_XSd8UnKUY0f16lUt_yUp0B9d5TCCVlZv-espWVUMfmSD3xvGCNmNu9xw=="
- #define INFLUXDB_ORG "Indobot"
- #define INFLUXDB_BUCKET "pengamatan suhu & kelembaban"
- #define TZ_INFO "UTC7"
- InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
- Point sensor("wifi_status");
- 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);
- WiFi.mode(WIFI_STA);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to WiFi...");
- 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);
- // Accurate time is necessary for certificate validation and writing in batches
- timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
- // Check server connection
- if (client.validateConnection()) {
- Serial.print("Connected to InfluxDB: ");
- Serial.println(client.getServerUrl());
- } else {
- Serial.print("InfluxDB connection failed: ");
- Serial.println(client.getLastErrorMessage());
- }
- }
- 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