-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_retriever.cpp
More file actions
81 lines (63 loc) · 1.99 KB
/
data_retriever.cpp
File metadata and controls
81 lines (63 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <FirebaseESP32.h>
#include <WiFi.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Firebase credentials
#define FIREBASE_HOST "chesstrace-42e8d-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "O0nQyi0FrejhdMu8XcEhQxED5pBUgU9eygFDG4mL"
FirebaseData firebaseData;
// Multiplexer control pins
#define S0 16
#define S1 17
#define S2 18
#define S3 19
// Multiplexer signal pins for each MUX
const int sigPins[4] = {32, 33, 34, 35};
void setup() {
Serial.begin(115200);
// Setup MUX control pins
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Setup signal pins as input
for (int i = 0; i < 4; i++) {
pinMode(sigPins[i], INPUT);
}
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
// Connect to Firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void selectMuxChannel(int channel) {
digitalWrite(S0, bitRead(channel, 0));
digitalWrite(S1, bitRead(channel, 1));
digitalWrite(S2, bitRead(channel, 2));
digitalWrite(S3, bitRead(channel, 3));
}
void loop() {
for (int square = 0; square < 64; square++) {
int muxIndex = square / 16;
int channel = square % 16;
selectMuxChannel(channel);
delayMicroseconds(5); // Allow signal to settle
bool isPiecePresent = digitalRead(sigPins[muxIndex]) == LOW;
String path = "/chessboard/" + String(square);
String pieceState = isPiecePresent ? "P" : " ";
if (Firebase.setString(firebaseData, path + "/piece", pieceState)) {
Serial.printf("Updated square %d: %s\n", square, pieceState.c_str());
} else {
Serial.printf("Failed to update square %d: %s\n", square, firebaseData.errorReason().c_str());
}
delay(20); // small delay to avoid spamming
}
delay(1000); // Update every second
}