/* Pin layout used: * ----------------------------------------------------------------------------------------- * MFRC522 ESP32C6 * Reader/PCD Feather * Signal Pin Pin * ----------------------------------------------------------------------------------------- * SPI SS 1 SDA(SS) SDA/4 * SPI SCK SCK SCK * SPI MOSI MOSI MOSI * SPI MISO MISO MISO * */ #include // https://github.com/tzapu/WiFiManager #include #include // https://github.com/OSSLibraries/Arduino_MFRC522v2 #include #include #include #define LED_PIN LED_BUILTIN #define SS_PIN SDA #define SLEEP_DURATION 1 #define RGB_BRIGHTNESS 4 // Change neopixel brightness (max 255) MFRC522DriverPinSimple ss_1_pin(SS_PIN); // 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 read_card(char reader_uid[]); void send_uid(char reader_uid[]); void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); reader.PCD_Init(); // Init each MFRC522 card. } void loop() { if (reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()) { char reader_uid[32] = ""; read_card(reader_uid); send_uid(reader_uid); } } void read_card(char reader_uid[]) { reader.PICC_HaltA(); reader.PCD_StopCrypto1(); byte_to_string(reader.uid.uidByte, 4, reader_uid); Serial.println(reader_uid); displaySuccess(); } void send_uid(char reader_uid[]) { WiFiManager wm; bool res; res = wm.autoConnect("KLUK"); // password protected ap if(!res) { Serial.println("Failed to connect"); displayError(); // ESP.restart(); } else { //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } HTTPClient https; Serial.print("[HTTPS] begin...\n"); // configure traged server and url https.begin("https://mood.robiweb.net/rfid_tags"); // HTTP https.addHeader("Content-Type", "application/json"); // start connection and send HTTP header and body int httpCode = https.POST("{\"identifier\":\"" + String(reader_uid) + "\"}"); Serial.print("httpCode: "); Serial.println(httpCode); if (httpCode > 0) { if (httpCode == HTTP_CODE_CREATED) { } else { Serial.println("Remote error while sending uid"); displayError(); } } else { Serial.print("Remote error while sending uid"); displayError(); } https.end(); Serial.print("[HTTPS] ended...\n"); ESP.deepSleep(SLEEP_DURATION * 1000000); } void byte_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() { rgbLedWrite(RGB_BUILTIN, RGB_BRIGHTNESS, 0, 0); // Green delay(1000); } void displaySuccess() { rgbLedWrite(RGB_BUILTIN, 0, RGB_BRIGHTNESS, 0); // Green delay(1000); } void blink(unsigned int times, unsigned int duration) { for (unsigned int i = 0; i < times; i++) { digitalWrite(LED_PIN, HIGH); delay(duration); digitalWrite(LED_PIN, LOW); delay(duration); } }