Advertisement
basyair7

esp32_prog

May 19th, 2022
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #if defined(ESP32)
  3.   #include <WiFi.h>
  4. #elif defined(ESP8266)
  5.   #include <ESP8266WiFi.h>
  6. #endif
  7. #include <Firebase_ESP_Client.h>
  8. #include <DHT.h>
  9.  
  10. //Provide the token generation process info.
  11. #include "addons/TokenHelper.h"
  12. //Provide the RTDB payload printing info and other helper functions.
  13. #include "addons/RTDBHelper.h"
  14.  
  15. // Pin Sensor
  16. #define sensorsuhu 17
  17. #define DHTTYPE DHT11 // Modul DHTTYPE
  18. #define sensorapi 4
  19. #define sensorasap 2
  20.  
  21. // Insert your network credentials
  22. #define WIFI_SSID "realme C15"
  23. #define WIFI_PASSWORD "katasandi"
  24.  
  25. // Insert Firebase project API Key
  26. #define API_KEY "AIzaSyB8ksD7MFTYcEn1vb_YOy9qDczN-AMpQ7c"
  27.  
  28. // Insert RTDB URLefine the RTDB URL */
  29. #define DATABASE_URL "https://whatsapp-database-7681d-default-rtdb.firebaseio.com/"
  30.  
  31. //Define Firebase Data object
  32. FirebaseData firebasedb;
  33.  
  34. // Define DHT Sensor
  35. DHT dht(sensorsuhu, DHTTYPE);
  36.  
  37. FirebaseAuth auth;
  38. FirebaseConfig config;
  39.  
  40. bool signupOK = false;
  41.  
  42. int suhu(){
  43.   int celcius = dht.readTemperature();
  44.  
  45.   if(isnan(celcius)){
  46.     Serial.print(F("Sensor DHT Error"));
  47.     delay(2000);
  48.   }
  49.   return celcius;
  50. }
  51.  
  52. int dataapi(){
  53.   int api = analogRead(sensorapi);
  54.   return api;
  55. }
  56.  
  57. int datagas(){
  58.   int gas = analogRead(sensorasap);
  59.   return gas;
  60. }
  61.  
  62. void setup() {
  63.   // put your setup code here, to run once:
  64.   Serial.begin(115200);
  65.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  66.   Serial.print("Connecting to Wi-Fi");
  67.   while (WiFi.status() != WL_CONNECTED){
  68.     Serial.print(".");
  69.     delay(300);
  70.   }
  71.   Serial.println();
  72.   Serial.print("Connected with IP: ");
  73.   Serial.println(WiFi.localIP());
  74.   Serial.println();
  75.  
  76.   /* Assign the api key (required) */
  77.   config.api_key = API_KEY;
  78.  
  79.   /* Assign the RTDB URL (required) */
  80.   config.database_url = DATABASE_URL;
  81.  
  82.   /* Sign up */
  83.   if (Firebase.signUp(&config, &auth, "", "")){
  84.     Serial.println("ok");
  85.     signupOK = true;
  86.   }
  87.   else{
  88.     Serial.printf("%s\n", config.signer.signupError.message.c_str());
  89.   }
  90.  
  91.   /* Assign the callback function for the long running token generation task */
  92.   config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  93.  
  94.   Firebase.begin(&config, &auth);
  95.   Firebase.reconnectWiFi(true);
  96. }
  97.  
  98. void loop() {
  99.   // put your main code here, to run repeatedly:
  100.   if (Firebase.ready()){
  101.     int state = dataapi();
  102.     if(state == LOW)
  103.     {
  104.       Serial.println(F("Tidak ada api"));
  105.       Firebase.RTDB.setString(&firebasedb, "/info-data/keterangan", "aman");
  106.       Firebase.RTDB.setString(&firebasedb, "/info-data/pesan/", "Tidak ada api");
  107.     }
  108.     else
  109.     {
  110.       Serial.println(F("Api terdeteksi disekitar"));
  111.       Firebase.RTDB.setString(&firebasedb, "/info-data/keterangan", "tidak aman");
  112.       Firebase.RTDB.setString(&firebasedb, "/info-data/pesan", "Api terdeteksi disekitar");
  113.     }
  114.  
  115.     int gassensor = datagas();
  116.     if(gassensor >= 0)
  117.     {
  118.       Serial.println(F("Keadaan di sekitar aman"));
  119.       Firebase.RTDB.setString(&firebasedb, "/info-data/keterangan", "aman");
  120.       Firebase.RTDB.setString(&firebasedb, "/info-data/pesan", "Keadaan di sekitar aman");
  121.     }
  122.     else if((gassensor >= 25) && (gassensor <= 50))
  123.     {
  124.       Serial.println(F("Perhatikan!! Asap Menyebar"));
  125.       Firebase.RTDB.setString(&firebasedb, "/info-data/keterangan", "sedang");
  126.       Firebase.RTDB.setString(&firebasedb, "/info-data/pesan", "Perhatian!! Kebakaran");
  127.     }
  128.  
  129.     Firebase.RTDB.setInt(&firebasedb, "/info-data/suhu", suhu());
  130.     Firebase.RTDB.setInt(&firebasedb, "/info-data/gas/", gassensor);
  131.     Serial.print(F("Suhu: "));
  132.     Serial.print(suhu());
  133.     Serial.println(F("°C "));
  134.  
  135.     Serial.print(F("Asap: "));
  136.     Serial.print(gassensor);
  137.     Serial.print(gassensor);
  138.     Serial.println(F(" ppm"));
  139.   }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement