Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <FirebaseESP32.h> // Firebase library
- #include <addons/TokenHelper.h> // Token helper
- #include <addons/RTDBHelper.h> // RTDB helper
- FirebaseAuth auth;
- FirebaseConfig config;
- // Assign the project host and API key (Replace with actual credentials)
- const char* FIREBASE_HOST = "https://facerecognition-tank-default-rtdb.firebaseio.com/";
- const char* API_KEY = "AIzaSyAjG_MOsClpLU7N7o-D1PkI08_tFO84w-c";
- const char* WIFI_SSID = "Kinneret College";
- const char* WIFI_PASSWORD = "";
- #define LED_PIN 2
- #define RXD2 16 // FPGA TX → ESP32 RX
- #define TXD2 17 // FPGA RX → ESP32 TX
- // Define Firebase Data object
- FirebaseData fbdo;
- FirebaseData stream;
- int FromAlt;
- bool ledState = false;
- // Store last commands to prevent duplicate messages
- String lastMovement = "";
- String lastLaser = "";
- String lastSpeed = "";
- String lastServo = "";
- String lastStepper = "";
- /* ------------- Function Declarations --------------------- */
- void blink1();
- void readFPGA();
- void sendCommandToFPGA(String command);
- void streamCallback(FirebaseStream &data); // ✅ FIXED: Corrected function signature
- void streamTimeoutCallback(bool timeout);
- /*-----------------------------------------------------------*/
- void setup() {
- pinMode(LED_PIN, OUTPUT);
- Serial.begin(115200);
- Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); // UART for FPGA communication
- delay(2000); // Wait two seconds for initializing
- // Connecting to WiFi
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(300);
- }
- Serial.println("\nConnected with IP: ");
- Serial.println(WiFi.localIP());
- // Assign Firebase credentials
- config.host = FIREBASE_HOST;
- config.api_key = API_KEY;
- config.token_status_callback = tokenStatusCallback; // Ensure this function is defined elsewhere
- Firebase.reconnectWiFi(true);
- if (Firebase.signUp(&config, &auth, "", "")) {
- Serial.println("Firebase SignUp successful.");
- } else {
- Serial.println("Firebase SignUp failed: " + fbdo.errorReason());
- }
- Firebase.begin(&config, &auth);
- // ✅ FIXED: Use reference instead of pointer
- Firebase.beginStream(stream, "tank/commands");
- Firebase.setStreamCallback(stream, streamCallback, streamTimeoutCallback);
- }
- void loop() {
- readFPGA(); // Read data from FPGA and update Firebase
- blink1();
- delay(20); // Keep loop fast
- }
- /*------------------------ Read FPGA & Update Firebase ---------------------------*/
- void readFPGA() {
- if (Serial2.available()) {
- FromAlt = Serial2.read();
- Serial.println("Received from FPGA: " + String(FromAlt));
- // Update Firebase with FPGA sensor data
- if (!Firebase.setInt(fbdo, "tank/sensors/distance", FromAlt)) {
- Serial.println("Failed to write to Firebase: " + fbdo.errorReason());
- }
- }
- }
- /*------------------------ Firebase Stream Callback (Corrected) ---------------------------*/
- void streamCallback(FirebaseStream &data) { // ✅ FIXED: Use FirebaseStream instead of StreamData
- if (!data.streamAvailable()) return; // ✅ FIXED: Use streamAvailable()
- String path = data.dataPath();
- String command = data.stringData();
- // Process Movement Command
- if (path == "/movement" && command != lastMovement && command != "0") {
- Serial.print("Movement Command: ");
- Serial.println(command);
- sendCommandToFPGA(command);
- lastMovement = command;
- }
- // Process Laser Command
- if (path == "/laser" && command != lastLaser && command != "0") {
- Serial.print("Laser Command: ");
- Serial.println(command);
- sendCommandToFPGA(command);
- lastLaser = command;
- }
- // Process Speed Command
- if (path == "/speed" && command != lastSpeed && command != "0") {
- Serial.print("Speed Command: ");
- Serial.println(command);
- sendCommandToFPGA(command);
- lastSpeed = command;
- }
- // Process Servo Command
- if (path == "/servo" && command != lastServo && command != "0") {
- Serial.print("Servo Command: ");
- Serial.println(command);
- sendCommandToFPGA(command);
- lastServo = command;
- }
- // Process Stepper Command
- if (path == "/stepper" && command != lastStepper && command != "0") {
- Serial.print("Stepper Command: ");
- Serial.println(command);
- sendCommandToFPGA(command);
- lastStepper = command;
- }
- }
- /*------------------------ Firebase Timeout Callback ---------------------------*/
- void streamTimeoutCallback(bool timeout) {
- if (timeout) {
- Serial.println("Firebase Stream Timeout, reconnecting...");
- Firebase.beginStream(stream, "tank/commands");
- }
- }
- /*------------------------ Send Command to FPGA ---------------------------*/
- void sendCommandToFPGA(String command) {
- Serial2.write(command.charAt(0)); // Send single-character command to FPGA
- }
- /*------------------------ Blink LED Function ---------------------------*/
- void blink1() {
- ledState = !ledState;
- digitalWrite(LED_PIN, ledState);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement