Firebase Authentication Failure When Using Bluetooth Initialization. #195
-
I'm working on a project that uses two packages: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
#include <WiFi.h>
#include <Preferences.h>
#include <FirebaseClient.h>
#include <WiFiClientSecure.h>
#include "BluetoothSerial.h"
String device_name = "ESP32-BT-Slave";
// Pin Definitions
#define RELAY_PIN 22
// Firebase Realtime Database Variables
Preferences preferences;
DefaultNetwork network;
FirebaseApp app;
WiFiClientSecure ssl_client1, ssl_client2;
RealtimeDatabase Database;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client1, getNetwork(network)), aClient2(ssl_client2, getNetwork(network));
String wifiSSID, wifiPassword, webKey, userEmail, userPassword, databaseURL;
bool isRelayOn = false;
bool taskComplete = false;
bool initialStreamEventIgnored = false;
char lastHash[32] = { 0 };
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
SerialBT.begin(device_name); //comment out this
preferences.begin("user_prefs", false);
loadPreferences();
}
// Load Configuration from Preferences
void loadPreferences() {
if (preferences.isKey("wifiSSID")) {
wifiSSID = preferences.getString("wifiSSID");
wifiPassword = preferences.getString("wifiPassword");
webKey = preferences.getString("webKey");
userEmail = preferences.getString("userEmail");
userPassword = preferences.getString("userPassword");
databaseURL = preferences.getString("databaseURL");
Serial.println("loaded preferences data.");
} else {
// Set default values if preferences are not set
// wifiSSID = "YourWiFiSSID";
// wifiPassword = "YourWiFiPassword";
// webKey = "YourFirebaseWebKey";
// userEmail = "YourEmail@example.com";
// userPassword = "YourPassword";
// databaseURL = "YourFirebaseDatabaseURL";
// Save them for future use
// preferences.putString("wifiSSID", wifiSSID);
// preferences.putString("wifiPassword", wifiPassword);
// preferences.putString("webKey", webKey);
// preferences.putString("userEmail", userEmail);
// preferences.putString("userPassword", userPassword);
// preferences.putString("databaseURL", databaseURL);
Serial.println("Default preferences saved.");
}
connectWiFi();
}
void connectWiFi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
connectFirebase();
}
void connectFirebase() {
UserAuth user_auth(webKey, userEmail, userPassword);
Firebase.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
ssl_client1.setInsecure();
ssl_client2.setInsecure();
initializeApp(aClient2, app, getAuth(user_auth), asyncCB, "authTask");
app.getApp<RealtimeDatabase>(Database);
Database.url(databaseURL);
Database.setSSEFilters("get,put,patch,keep-alive,cancel,auth_revoked");
}
void loop() {
app.loop();
app.ttl();
Database.loop();
ensureRelayOff();
if (app.ready() && !taskComplete) {
taskComplete = true;
String userUID = app.getUid();
if (!userUID.isEmpty()) {
Serial.printf("User authenticated with UID: %s\n", userUID.c_str());
startListeningToAlert(userUID);
} else {
Serial.println("Failed to retrieve UID. Ensure authentication is successful.");
}
}
// if (Serial.available()) {
// SerialBT.write(Serial.read());
// }
// if (SerialBT.available()) {
// Serial.write(SerialBT.read());
// }
}
void startListeningToAlert(String uid) {
String alertPath = "internalsosalert/" + uid + "/alert";
Serial.printf("Listening for alerts at path: %s\n", alertPath.c_str());
Database.get(aClient, alertPath, asyncCB, true, "streamTask");
}
void asyncCB(AsyncResult &aResult) {
printResult(aResult);
}
void printResult(AsyncResult &aResult) {
if (aResult.isEvent()) {
Serial.printf("Event: %s, Msg: %s, Code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
} else if (aResult.isError()) {
Serial.printf("Error: %s, Msg: %s, Code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
} else if (aResult.available()) {
RealtimeDatabaseResult &RTDB = aResult.to<RealtimeDatabaseResult>();
if (RTDB.isStream()) {
String currentData = String(RTDB.to<const char *>());
char currentHash[32];
calculateSimpleHash(currentData.c_str(), currentHash);
if (strcmp(lastHash, currentHash) != 0) {
strncpy(lastHash, currentHash, sizeof(lastHash));
if (!initialStreamEventIgnored) {
triggerRelay(2000);
initialStreamEventIgnored = true;
} else {
triggerRelay(10000);
}
}
}
}
}
void triggerRelay(int duration) {
Serial.printf("Relay triggered for %d ms\n", duration);
digitalWrite(RELAY_PIN, LOW);
delay(duration);
digitalWrite(RELAY_PIN, HIGH);
}
void ensureRelayOff() {
if (isRelayOn) {
digitalWrite(RELAY_PIN, HIGH);
isRelayOn = false;
Serial.println("Relay was forcefully turned off.");
}
}
void calculateSimpleHash(const char *data, char *hash) {
uint8_t xorSum = 0;
while (*data) {
xorSum ^= *data++;
}
snprintf(hash, 32, "%02X", xorSum);
}
|
Beta Was this translation helpful? Give feedback.
-
USING ESP32 board BEFORE comment out
AFTER comment out
|
Beta Was this translation helpful? Give feedback.
-
You should debug the FYI. |
Beta Was this translation helpful? Give feedback.
You should debug the
WiFiClientSecure
which defined on your side why it fails.FYI.
You cannot use WiFi while using Bluetooth and vice versa because they share the same radio antenna.