esp32 code

This commit is contained in:
2026-05-14 00:22:46 +00:00
parent 87b8e8e890
commit dc83364b1b

221
esp32/app.ino Normal file
View File

@@ -0,0 +1,221 @@
#include <SPI.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Ethernet.h>
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
// =========================
// USER CONFIG
// =========================
const String POST_URL = "https://cije.harryesses.com";
const char* LOCATION_ID = "office_1";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// WiFi fallback
// const char* WIFI_SSID = "Hillel_Student";
// const char* WIFI_PASS = "Hillel1025";
const char* WIFI_SSID = "Esses House Wifi";
const char* WIFI_PASS = "9175768551";
// =========================
// RC522 (Software SPI)
// =========================
#define SS_PIN 21
#define RST_PIN 38 // safer than 15
#define SCK_PIN 18
#define MISO_PIN 16
#define MOSI_PIN 17
SPIClass softSPI(HSPI);
MFRC522DriverPinSimple ss_pin(SS_PIN);
MFRC522DriverSPI driver{ss_pin, softSPI};
MFRC522 mfrc522(driver);
// =========================
// Ethernet pins (W5500)
// =========================
#define ETH_MOSI 11
#define ETH_MISO 12
#define ETH_SCK 13
#define ETH_CS 14
bool ethernetOK = false;
bool wifiOK = false;
String action = "";
// =========================
// NETWORK STATUS
// =========================
void checkEthernet() {
// ETH.begin(ETH_CS, ETH_MOSI, ETH_MISO, ETH_SCK);
// delay(2000);
if (!Ethernet.begin(mac) == 0) {
ethernetOK = true;
Serial.print("Ethernet IP: ");
Serial.println(Ethernet.localIP());
} else {
ethernetOK = false;
Serial.println("Ethernet failed");
}
}
void connectWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting WiFi");
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
wifiOK = true;
Serial.println();
Serial.print("WiFi IP: ");
Serial.println(WiFi.localIP());
} else {
wifiOK = false;
Serial.println("\nWiFi failed");
}
}
bool sendHTTP(String uid, String action) {
if (!ethernetOK && !wifiOK) return false;
if (action == "") {
action = "other";
}
HTTPClient http;
http.begin(POST_URL+"/scan");
http.addHeader("Content-Type", "application/json");
String payload =
"{"
"\"uid\":\"" + uid + "\","
"\"location_id\":\"" + String(LOCATION_ID) + "\","
"\"action\":\"" + action + "\""
"}";
int code = http.POST(payload);
// Serial.print("HTTP Code: ");
// Serial.println(code);
http.end();
return code > 0;
}
bool checkBathroom() {
if (!ethernetOK && !wifiOK) return false;
HTTPClient http;
http.begin(POST_URL+"/lights/bathroom/" + String(LOCATION_ID));
http.addHeader("Content-Type", "application/json");
int code = http.GET();
// Serial.print("HTTP Code: ");
// Serial.println(code);
http.end();
return code == 202;
}
// =========================
// SETUP
// =========================
void setup() {
Serial.begin(115200);
delay(3000);
Serial.println("\nBooting RFID Scanner...");
// RC522 SPI
softSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
pinMode(RST_PIN, OUTPUT);
digitalWrite(RST_PIN, HIGH);
mfrc522.PCD_Init();
Serial.println("RC522 ready");
// NETWORK
checkEthernet();
if (!ethernetOK) {
connectWiFi();
}
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(48, OUTPUT);
}
// =========================
// LOOP
// =========================
void loop() {
// buttonState = digitalRead(1);/
if (checkBathroom()){
digitalWrite(48, LOW);
}
if (digitalRead(1) == LOW) { // Button is pressed
action = "bathroom";
delay(200); // Simple debounce
}
if (digitalRead(2) == LOW) { // Button is pressed
action = "other";
delay(200); // Simple debounce
}
if (digitalRead(3) == LOW) { // Button is pressed
action = "office";
delay(200); // Simple debounce
}
if (!mfrc522.PICC_IsNewCardPresent()) {
delay(50);
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
// UID build
String uidString = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] < 0x10) {
uidString += "0";
}
uidString += String(mfrc522.uid.uidByte[i], HEX);
}
uidString.toUpperCase();
Serial.print("Card UID: ");
Serial.println(uidString);
// send to server
bool ok = sendHTTP(uidString, action);
action = "";
if (ok) {
Serial.println("Sent OK");
} else {
Serial.println("Send FAILED");
}
mfrc522.PICC_HaltA();
delay(2000);
}