Advertisement
solielios

קבלת נתונים מן הבקר לפיירבייס

Dec 25th, 2024 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. // חיבור ספריות Wi-Fi בהתאם לסוג הבקר (ESP32 או ESP8266)
  4. #if defined(ESP32)
  5.   #include <WiFi.h>
  6. #elif defined(ESP8266)
  7.   #include <ESP8266WiFi.h>
  8. #endif
  9.  
  10. #include <Firebase_ESP_Client.h>
  11.  
  12. // ספריות לטיפול בתהליכי יצירת אסימונים ועזרי Firebase
  13. #include "addons/TokenHelper.h" // מספק מידע על יצירת אסימונים
  14. #include "addons/RTDBHelper.h"  // מספק כלים לעבודה עם נתוני RTDB
  15.  
  16. // פרטי החיבור לרשת האלחוטית
  17. #define WIFI_SSID "Galaxy S22+3663"        // שם הרשת (SSID) פה לשנות!!!!!!!!!!!!!!!!
  18. #define WIFI_PASSWORD "fnyh1479"          // סיסמת הרשת פה לשנות!!!!!!!!!!!!!!!!
  19.  
  20. // מפתח API של פרויקט Firebase
  21. #define API_KEY "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c"  // מפתח API של Firebase פה לשנות!!!!!!!!!!!!!!!!
  22.  
  23. // כתובת URL של מסד הנתונים בזמן אמת (RTDB) של Firebase
  24. #define DATABASE_URL "https://facerecognition-tank-default-rtdb.firebaseio.com"  // כתובת RTDB פה לשנות!!!!!!!!!!!!!!!!
  25.  
  26. // יצירת אובייקט נתונים ל-Firebase
  27. FirebaseData fbdo;
  28.  
  29. // אובייקטים עבור אימות והגדרות Firebase
  30. FirebaseAuth auth;
  31. FirebaseConfig config;
  32.  
  33. // משתנים לשליטה בזמני העלאת הנתונים וספירה
  34. unsigned long sendDataPrevMillis = 0; // משתנה לשמירת זמן ההעלאה האחרון
  35. int count = 0;  // מונה נתונים
  36. bool signupOK = false;  // סטטוס הרשמה ל-Firebase
  37.  
  38. void setup() {
  39.   Serial.begin(115200); // אתחול חיבור סריאלי לניפוי שגיאות
  40.  
  41.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // התחברות ל-Wi-Fi
  42.   Serial.print("Connecting to Wi-Fi");
  43.   while (WiFi.status() != WL_CONNECTED) { // המתנה עד לחיבור
  44.     Serial.print(".");
  45.     delay(300);
  46.   }
  47.   Serial.println();
  48.   Serial.print("Connected with IP: ");
  49.   Serial.println(WiFi.localIP()); // הצגת כתובת IP
  50.   Serial.println();
  51.  
  52.   config.api_key = API_KEY; // הגדרת מפתח ה-API
  53.   config.database_url = DATABASE_URL; // הגדרת כתובת RTDB
  54.  
  55.   if (Firebase.signUp(&config, &auth, "", "")) { // הרשמה אנונימית ל-Firebase
  56.     Serial.println("ok");
  57.     signupOK = true; // אם ההרשמה הצליחה
  58.   } else {
  59.     Serial.printf("%s\n", config.signer.signupError.message.c_str()); // הצגת שגיאת הרשמה
  60.   }
  61.  
  62.   config.token_status_callback = tokenStatusCallback; // פונקציה לטיפול במצב האסימון
  63.   Firebase.begin(&config, &auth); // אתחול Firebase
  64.   Firebase.reconnectWiFi(true); // הפעלה מחדש של חיבור Wi-Fi במידת הצורך
  65. }
  66.  
  67. void loop() {
  68.   if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)) { // בדיקת מוכנות Firebase והמתנה של 15 שניות
  69.     sendDataPrevMillis = millis(); // עדכון הזמן האחרון
  70.  
  71.     if (Firebase.RTDB.setInt(&fbdo, "test/int", count)) { // כתיבת ערך שלם למסד הנתונים בנתיב "test/int"
  72.       Serial.println("PASSED");
  73.       Serial.println("PATH: " + fbdo.dataPath());
  74.       Serial.println("TYPE: " + fbdo.dataType());
  75.     } else {
  76.       Serial.println("FAILED");
  77.       Serial.println("REASON: " + fbdo.errorReason());
  78.     }
  79.     count++; // הגדלת המונה
  80.  
  81.     if (Firebase.RTDB.setFloat(&fbdo, "test/float", 0.01 + random(0, 100))) { // כתיבת ערך נקודה צפה בנתיב "test/float"
  82.       Serial.println("PASSED");
  83.       Serial.println("PATH: " + fbdo.dataPath());
  84.       Serial.println("TYPE: " + fbdo.dataType());
  85.     } else {
  86.       Serial.println("FAILED");
  87.       Serial.println("REASON: " + fbdo.errorReason());
  88.     }
  89.   }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement