Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #include <WiFi.h>
- #include <Firebase_ESP_Client.h>
- #include <HTTPClient.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 LED_1 13
- #define LED_2 12
- #define LED_3 14
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- const char* firebaseApiKey = "AIzaSyDloJq58w1ms17_opl5SW5L7ccD1DgIDv4";
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- WiFiClient client;
- 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);
- pinMode(LED_1, OUTPUT);
- pinMode(LED_2, OUTPUT);
- pinMode(LED_3, OUTPUT);
- Serial.begin(115200);
- Setup_Sensor();
- Setup_Wifi();
- Setup_Firebase();
- }
- void loop() {
- Pembacaan_Sensor();
- Kirim_data_Firebase();
- delay(2000);
- }
- void Setup_Wifi() {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void Setup_Firebase() {
- config.host = "https://indobot-44870-default-rtdb.asia-southeast1.firebasedatabase.app/";
- config.signer.tokens.legacy_token = firebaseApiKey;
- Firebase.begin(&config, &auth);
- }
- void Setup_Sensor() {
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- void Pembacaan_Sensor() {
- 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;
- }
- }
- }
- void Kirim_data_Firebase() {
- String jsonString = "{\"temperature\": " + String(sensorData[TEMP_SENSOR]) + ", \"humidity\": " + String(sensorData[HUM_SENSOR]) + "}";
- String url = "/indobot-44870-default-rtdb-export.json?auth=" + String(firebaseApiKey);
- String firebaseUrl = String(config.host.c_str()) + url;
- HTTPClient http;
- http.begin(firebaseUrl);
- http.addHeader("Content-Type", "application/json");
- int httpResponseCode = http.POST(jsonString);
- if (httpResponseCode > 0) {
- Serial.print("Data berhasil dikirim ke Firebase. Status code: ");
- Serial.println(httpResponseCode);
- } else {
- Serial.print("Gagal mengirim data ke Firebase. Error code: ");
- Serial.println(httpResponseCode);
- }
- http.end();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement