send rfid tag uid

This commit is contained in:
Christophe Robillard 2025-08-07 23:31:30 +02:00
parent 1c5937c34e
commit c135446c89

135
the-cup.ino Normal file
View file

@ -0,0 +1,135 @@
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
MFRC522DriverPinSimple ss_1_pin(4); // Configurable, take an unused pin, only HIGH/LOW required, must be different to SS 2.
MFRC522DriverSPI driver_1{ss_1_pin};
MFRC522 reader{driver_1}; // Create MFRC522 instance.
void setup() {
// WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
// wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("MoodBox"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
reader.PCD_Init(); // Init each MFRC522 card.
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient client;
HTTPClient http;
if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) {
// Show some details of the PICC (that is: the tag/card).
MFRC522Debug::PrintUID(Serial, reader.uid);
// Halt PICC.
reader.PICC_HaltA();
// Stop encryption on PCD.
reader.PCD_StopCrypto1();
Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin(client, "http://192.168.1.32:3000/rfid_tags"); // HTTP
http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] POST...\n");
char str[32] = "";
array_to_string(reader.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
Serial.println(str); //Print the output uid string
// start connection and send HTTP header and body
int httpCode = http.POST("{\"identifier\":\"" + String(str) + "\"}");
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
// if (httpCode == HTTP_CODE_OK) {
if (httpCode == HTTP_CODE_CREATED) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
displaySuccess();
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
displayError();
}
http.end();
}
}
void array_to_string(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
void displayError()
{
for (unsigned int i = 0; i < 6; i++)
{
digitalWrite(LED_BUILTIN, LOW);
delay(600);
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
}
}
void displaySuccess()
{
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
}