Advertisement
mudhita_triari

Tugas_Firebase_membuat_SHT22_XY_MD02

Apr 6th, 2024 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | Source Code | 0 0
  1. #include <ModbusMaster.h>
  2. #include <SoftwareSerial.h>
  3. #include <WiFi.h>
  4. #include <Firebase_ESP_Client.h>
  5. #include <HTTPClient.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. #define LED_1 13
  17. #define LED_2 12
  18. #define LED_3 14
  19.  
  20. const char* ssid = "vivo V29";
  21. const char* password = "Nabila041185";
  22. const char* firebaseApiKey = "AIzaSyDloJq58w1ms17_opl5SW5L7ccD1DgIDv4";
  23.  
  24. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  25. ModbusMaster node;
  26. FirebaseData fbdo;
  27. FirebaseAuth auth;
  28. FirebaseConfig config;
  29. WiFiClient client;
  30.  
  31. float sensorData[NUM_SENSORS];
  32. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  33.  
  34. void preTransmission() {
  35.   digitalWrite(MAX485_RE_NEG, 1);
  36.   digitalWrite(MAX485_DE, 1);
  37. }
  38.  
  39. void postTransmission() {
  40.   digitalWrite(MAX485_RE_NEG, 0);
  41.   digitalWrite(MAX485_DE, 0);
  42. }
  43.  
  44. void setup() {
  45.   pinMode(MAX485_RE_NEG, OUTPUT);
  46.   pinMode(MAX485_DE, OUTPUT);
  47.   digitalWrite(MAX485_RE_NEG, 0);
  48.   digitalWrite(MAX485_DE, 0);
  49.  
  50.   pinMode(LED_1, OUTPUT);
  51.   pinMode(LED_2, OUTPUT);
  52.   pinMode(LED_3, OUTPUT);
  53.  
  54.   Serial.begin(115200);
  55.  
  56.   Setup_Sensor();
  57.   Setup_Wifi();
  58.   Setup_Firebase();
  59. }
  60.  
  61. void loop() {
  62.   Pembacaan_Sensor();
  63.   Kirim_data_Firebase();
  64.  
  65.   delay(2000);
  66. }
  67.  
  68. void Setup_Wifi() {
  69.   WiFi.begin(ssid, password);
  70.   while (WiFi.status() != WL_CONNECTED) {
  71.     delay(500);
  72.     Serial.print(".");
  73.   }
  74.  
  75.   Serial.println("");
  76.   Serial.println("WiFi connected");
  77.   Serial.println("IP address: ");
  78.   Serial.println(WiFi.localIP());
  79. }
  80.  
  81. void Setup_Firebase() {
  82.   config.host = "https://indobot-44870-default-rtdb.asia-southeast1.firebasedatabase.app/";
  83.   config.signer.tokens.legacy_token = firebaseApiKey;
  84.   Firebase.begin(&config, &auth);
  85. }
  86.  
  87. void Setup_Sensor() {
  88.   RS485Serial.begin(9600);
  89.   node.begin(1, RS485Serial);
  90.   node.preTransmission(preTransmission);
  91.   node.postTransmission(postTransmission);
  92. }
  93.  
  94. void Pembacaan_Sensor() {
  95.   for (int i = 0; i < NUM_SENSORS; i++) {
  96.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  97.  
  98.     if (result == node.ku8MBSuccess) {
  99.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  100.     } else {
  101.       delay(1000);
  102.       return;
  103.     }
  104.   }
  105. }
  106.  
  107. void Kirim_data_Firebase() {
  108.   String jsonString = "{\"temperature\": " + String(sensorData[TEMP_SENSOR]) + ", \"humidity\": " + String(sensorData[HUM_SENSOR]) + "}";
  109.   String url = "/indobot-44870-default-rtdb-export.json?auth=" + String(firebaseApiKey);
  110.   String firebaseUrl = String(config.host.c_str()) + url;
  111.  
  112.   HTTPClient http;
  113.   http.begin(firebaseUrl);
  114.   http.addHeader("Content-Type", "application/json");
  115.   int httpResponseCode = http.POST(jsonString);
  116.   if (httpResponseCode > 0) {
  117.     Serial.print("Data berhasil dikirim ke Firebase. Status code: ");
  118.     Serial.println(httpResponseCode);
  119.   } else {
  120.     Serial.print("Gagal mengirim data ke Firebase. Error code: ");
  121.     Serial.println(httpResponseCode);
  122.   }
  123.   http.end();
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement