Advertisement
safwan092

Untitled

Nov 5th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <FirebaseESP32.h>
  3. #include <addons/TokenHelper.h>
  4. #include <addons/RTDBHelper.h>
  5.  
  6. #define button_pin 34
  7. #define buzzer_pin 19
  8.  
  9. #define WIFI_SSID "network"
  10. #define WIFI_PASSWORD "123456789"
  11. #define API_KEY "AIzaSyAjIS98IBo4xU9DAqFbo6ZJLPqBwzrJvzk"
  12. #define USER_EMAIL "smartpostcar@gmail.com"
  13. #define USER_PASSWORD "123456789"
  14. #define DATABASE_URL "postcar-57fe1-default-rtdb.firebaseio.com"
  15. #define DATABASE_SECRET "7yJzXNiqlkQZUFb3Ul2KJACDHnztTMrDoFLgPOZM"
  16.  
  17.  
  18. FirebaseData fbdo;
  19. FirebaseAuth auth;
  20. FirebaseConfig config;
  21. unsigned long dataMillis = 0;
  22.  
  23. int button_pressed_flag = 0;
  24. int button_state = 0;
  25. int home1_value_from_DB = 0;
  26. int home2_value_from_DB = 0;
  27. int home3_value_from_DB = 0;
  28.  
  29. void setup()
  30. {
  31. Serial.begin(115200);
  32. setup_inputs_and_outputs();
  33. connect_to_WiFi_and_Firebase_database();
  34. }
  35.  
  36. void loop()
  37. {
  38. read_button_and_send_data_to_database();
  39. read_from_database();
  40. }
  41.  
  42.  
  43. void read_button_and_send_data_to_database() {
  44. button_state = digitalRead(button_pin);
  45. if (button_state == 1 && button_pressed_flag == 0) {
  46. digitalWrite(buzzer_pin, 1);
  47. button_pressed_flag = 1;
  48. write_to_database();
  49. button_pressed_flag = 0;
  50. digitalWrite(buzzer_pin, 0);
  51. }
  52. }
  53.  
  54. void setup_inputs_and_outputs() {
  55. pinMode(button_pin, INPUT);
  56. pinMode(buzzer_pin, OUTPUT);
  57. digitalWrite(buzzer_pin, LOW);
  58. }
  59.  
  60. void connect_to_WiFi_and_Firebase_database() {
  61. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  62. Serial.print("Connecting to Wi-Fi");
  63. while (WiFi.status() != WL_CONNECTED)
  64. {
  65. Serial.print(".");
  66. delay(300);
  67. }
  68. Serial.println();
  69. Serial.print("Connected with IP: ");
  70. Serial.println(WiFi.localIP());
  71. Serial.println();
  72. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  73. config.api_key = API_KEY;
  74. auth.user.email = USER_EMAIL;
  75. auth.user.password = USER_PASSWORD;
  76. config.database_url = DATABASE_URL;
  77. Firebase.reconnectWiFi(true);
  78. fbdo.setResponseSize(4096);
  79. String base_path = "/UsersData/";
  80. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  81. config.max_token_generation_retry = 5;
  82. Firebase.begin(&config, &auth);
  83. String var = "$userId";
  84. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  85. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  86. }
  87.  
  88. void read_from_database() {
  89.  
  90. if (millis() - dataMillis > 5000 && Firebase.ready()) {
  91. dataMillis = millis();
  92. //////////////////////////////////////////////////////////
  93. //home_1
  94. if (Firebase.RTDB.getInt(&fbdo, "/home1")) {
  95. if (fbdo.dataType() == "int") {
  96. home1_value_from_DB = fbdo.intData();
  97. Serial.print("home1_value_from_DB = ");
  98. Serial.println(home1_value_from_DB);
  99. }
  100. }
  101. else {
  102. Serial.println(fbdo.errorReason());
  103. }
  104. //////////////////////////////////////////////////////////
  105. //////////////////////////////////////////////////////////
  106. //home_2
  107. if (Firebase.RTDB.getInt(&fbdo, "/home2")) {
  108. if (fbdo.dataType() == "int") {
  109. home2_value_from_DB = fbdo.intData();
  110. Serial.print("home2_value_from_DB = ");
  111. Serial.println(home2_value_from_DB);
  112. }
  113. }
  114. else {
  115. Serial.println(fbdo.errorReason());
  116. }
  117. //////////////////////////////////////////////////////////
  118. //////////////////////////////////////////////////////////
  119. //home_3
  120. if (Firebase.RTDB.getInt(&fbdo, "/home3")) {
  121. if (fbdo.dataType() == "int") {
  122. home3_value_from_DB = fbdo.intData();
  123. Serial.print("home3_value_from_DB = ");
  124. Serial.println(home3_value_from_DB);
  125. }
  126. }
  127. else {
  128. Serial.println(fbdo.errorReason());
  129. }
  130. //////////////////////////////////////////////////////////
  131. }
  132.  
  133.  
  134. }
  135.  
  136. void write_to_database() {
  137. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home1", 0) ? "ok" : fbdo.errorReason().c_str());
  138. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home2", 0) ? "ok" : fbdo.errorReason().c_str());
  139. Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, "/home3", 0) ? "ok" : fbdo.errorReason().c_str());
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement