Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable input from php form at other webserver #652

Closed
rochmatsalim opened this issue Feb 4, 2025 · 1 comment
Closed

Variable input from php form at other webserver #652

rochmatsalim opened this issue Feb 4, 2025 · 1 comment
Labels
invalid 🚫 not relevant for the library

Comments

@rochmatsalim
Copy link

Hi Everyone...
I want to get some variable from other webserver, that will I use to input ESP32 RFID Write Code.

Can anyone send me an example code?

@rochmatsalim
Copy link
Author

I get from Deeseek 👍
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>

// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Web server URL
const char* serverUrl = "http://yourserver.com/getVariable"; // Replace with your server URL

// RFID pins
#define SS_PIN 5 // ESP32 pin GPIO5
#define RST_PIN 27 // ESP32 pin GPIO27

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup() {
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");

// Initialize SPI and MFRC522
SPI.begin();
mfrc522.PCD_Init();
Serial.println("RFID reader initialized");
}

void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Fetch variable from web server
String variable = fetchVariableFromServer();
if (variable != "") {
Serial.println("Fetched variable: " + variable);

  // Write variable to RFID tag
  writeToRFID(variable);
}

} else {
Serial.println("Wi-Fi disconnected. Reconnecting...");
WiFi.begin(ssid, password);
}

delay(5000); // Wait before next iteration
}

String fetchVariableFromServer() {
HTTPClient http;
String payload = "";

// Send GET request to server
http.begin(serverUrl);
int httpCode = http.GET();

if (httpCode == HTTP_CODE_OK) {
payload = http.getString(); // Get the response payload
Serial.println("Server response: " + payload);
} else {
Serial.println("Error fetching variable from server. HTTP code: " + String(httpCode));
}

http.end();
return payload;
}

void writeToRFID(String data) {
// Prepare RFID tag for writing
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
Serial.println("No RFID tag present.");
return;
}

// Write data to the RFID tag
byte buffer[18]; // MIFARE Classic blocks are 16 bytes, but we need 2 extra bytes for the trailer
data.getBytes(buffer, 18);

MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; // Default key

// Authenticate and write to block 4 (you can change this)
if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)) == MFRC522::STATUS_OK) {
if (mfrc522.MIFARE_Write(4, buffer, 16) == MFRC522::STATUS_OK) {
Serial.println("Data written to RFID tag successfully.");
} else {
Serial.println("Error writing to RFID tag.");
}
} else {
Serial.println("Authentication failed.");
}

mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}

@Rotzbua Rotzbua added the invalid 🚫 not relevant for the library label Feb 14, 2025
@Rotzbua Rotzbua closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid 🚫 not relevant for the library
Projects
None yet
Development

No branches or pull requests

2 participants