Advertisement
solielios

rx tx

Feb 3rd, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <FirebaseESP32.h> // Firebase library
  3. #include <addons/TokenHelper.h> // Token helper
  4. #include <addons/RTDBHelper.h> // RTDB helper
  5.  
  6. FirebaseAuth auth;
  7. FirebaseConfig config;
  8.  
  9. // Assign the project host and API key (Replace with actual credentials)
  10. const char* FIREBASE_HOST = "https://facerecognition-tank-default-rtdb.firebaseio.com/";
  11. const char* API_KEY = "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c";
  12.  
  13. const char* WIFI_SSID = "Kinneret College";
  14. const char* WIFI_PASSWORD = "";
  15.  
  16. #define LED_PIN 2
  17. #define RXD2 16 // FPGA TX → ESP32 RX
  18. #define TXD2 17 // FPGA RX → ESP32 TX
  19.  
  20. // Define Firebase Data object
  21. FirebaseData fbdo;
  22. FirebaseData stream;
  23. int FromAlt;
  24. bool ledState = false;
  25.  
  26. // Store last commands to prevent duplicate messages
  27. String lastMovement = "";
  28. String lastLaser = "";
  29. String lastSpeed = "";
  30. String lastServo = "";
  31. String lastStepper = "";
  32.  
  33. /* ------------- Function Declarations --------------------- */
  34. void blink1();
  35. void readFPGA();
  36. void sendCommandToFPGA(String command);
  37. void streamCallback(FirebaseStream &data); // ✅ FIXED: Corrected function signature
  38. void streamTimeoutCallback(bool timeout);
  39.  
  40. /*-----------------------------------------------------------*/
  41.  
  42. void setup() {
  43. pinMode(LED_PIN, OUTPUT);
  44. Serial.begin(115200);
  45. Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); // UART for FPGA communication
  46.  
  47. delay(2000); // Wait two seconds for initializing
  48.  
  49. // Connecting to WiFi
  50. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  51. Serial.print("Connecting to Wi-Fi");
  52. while (WiFi.status() != WL_CONNECTED) {
  53. Serial.print(".");
  54. delay(300);
  55. }
  56. Serial.println("\nConnected with IP: ");
  57. Serial.println(WiFi.localIP());
  58.  
  59. // Assign Firebase credentials
  60. config.host = FIREBASE_HOST;
  61. config.api_key = API_KEY;
  62. config.token_status_callback = tokenStatusCallback; // Ensure this function is defined elsewhere
  63.  
  64. Firebase.reconnectWiFi(true);
  65.  
  66. if (Firebase.signUp(&config, &auth, "", "")) {
  67. Serial.println("Firebase SignUp successful.");
  68. } else {
  69. Serial.println("Firebase SignUp failed: " + fbdo.errorReason());
  70. }
  71.  
  72. Firebase.begin(&config, &auth);
  73.  
  74. // ✅ FIXED: Use reference instead of pointer
  75. Firebase.beginStream(stream, "tank/commands");
  76. Firebase.setStreamCallback(stream, streamCallback, streamTimeoutCallback);
  77. }
  78.  
  79. void loop() {
  80. readFPGA(); // Read data from FPGA and update Firebase
  81. blink1();
  82. delay(20); // Keep loop fast
  83. }
  84.  
  85. /*------------------------ Read FPGA & Update Firebase ---------------------------*/
  86. void readFPGA() {
  87. if (Serial2.available()) {
  88. FromAlt = Serial2.read();
  89. Serial.println("Received from FPGA: " + String(FromAlt));
  90.  
  91. // Update Firebase with FPGA sensor data
  92. if (!Firebase.setInt(fbdo, "tank/sensors/distance", FromAlt)) {
  93. Serial.println("Failed to write to Firebase: " + fbdo.errorReason());
  94. }
  95. }
  96. }
  97.  
  98. /*------------------------ Firebase Stream Callback (Corrected) ---------------------------*/
  99. void streamCallback(FirebaseStream &data) { // ✅ FIXED: Use FirebaseStream instead of StreamData
  100. if (!data.streamAvailable()) return; // ✅ FIXED: Use streamAvailable()
  101.  
  102. String path = data.dataPath();
  103. String command = data.stringData();
  104.  
  105. // Process Movement Command
  106. if (path == "/movement" && command != lastMovement && command != "0") {
  107. Serial.print("Movement Command: ");
  108. Serial.println(command);
  109. sendCommandToFPGA(command);
  110. lastMovement = command;
  111. }
  112.  
  113. // Process Laser Command
  114. if (path == "/laser" && command != lastLaser && command != "0") {
  115. Serial.print("Laser Command: ");
  116. Serial.println(command);
  117. sendCommandToFPGA(command);
  118. lastLaser = command;
  119. }
  120.  
  121. // Process Speed Command
  122. if (path == "/speed" && command != lastSpeed && command != "0") {
  123. Serial.print("Speed Command: ");
  124. Serial.println(command);
  125. sendCommandToFPGA(command);
  126. lastSpeed = command;
  127. }
  128.  
  129. // Process Servo Command
  130. if (path == "/servo" && command != lastServo && command != "0") {
  131. Serial.print("Servo Command: ");
  132. Serial.println(command);
  133. sendCommandToFPGA(command);
  134. lastServo = command;
  135. }
  136.  
  137. // Process Stepper Command
  138. if (path == "/stepper" && command != lastStepper && command != "0") {
  139. Serial.print("Stepper Command: ");
  140. Serial.println(command);
  141. sendCommandToFPGA(command);
  142. lastStepper = command;
  143. }
  144. }
  145.  
  146. /*------------------------ Firebase Timeout Callback ---------------------------*/
  147. void streamTimeoutCallback(bool timeout) {
  148. if (timeout) {
  149. Serial.println("Firebase Stream Timeout, reconnecting...");
  150. Firebase.beginStream(stream, "tank/commands");
  151. }
  152. }
  153.  
  154. /*------------------------ Send Command to FPGA ---------------------------*/
  155. void sendCommandToFPGA(String command) {
  156. Serial2.write(command.charAt(0)); // Send single-character command to FPGA
  157. }
  158.  
  159. /*------------------------ Blink LED Function ---------------------------*/
  160. void blink1() {
  161. ledState = !ledState;
  162. digitalWrite(LED_PIN, ledState);
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement