diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ + diff --git a/badgeuse_nfc/.gitignore b/badgeuse_nfc/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/badgeuse_nfc/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/badgeuse_nfc/.vscode/extensions.json b/badgeuse_nfc/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/badgeuse_nfc/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/badgeuse_nfc/include/README b/badgeuse_nfc/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/badgeuse_nfc/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/badgeuse_nfc/lib/README b/badgeuse_nfc/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/badgeuse_nfc/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/badgeuse_nfc/platformio.ini b/badgeuse_nfc/platformio.ini new file mode 100644 index 0000000..6b1408b --- /dev/null +++ b/badgeuse_nfc/platformio.ini @@ -0,0 +1,18 @@ +[env:esp32-c6-devkitm-1] +; On force l'utilisation d'une plateforme qui intègre Arduino 3.x +platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip +board = esp32-c6-devkitm-1 +framework = arduino +monitor_speed = 115200 + +; Ces deux lignes sont cruciales pour les ESP32-C6 ! +; Elles forcent l'affichage du Serial Monitor sur le port USB natif de la puce. +build_flags = + -D ARDUINO_USB_MODE=1 + -D ARDUINO_USB_CDC_ON_BOOT=1 + +lib_deps = + adafruit/Adafruit PN532 @ ^1.3.0 + +monitor_dtr = 0 +monitor_rts = 0 \ No newline at end of file diff --git a/badgeuse_nfc/src/main.cpp b/badgeuse_nfc/src/main.cpp new file mode 100644 index 0000000..f1746e1 --- /dev/null +++ b/badgeuse_nfc/src/main.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include + +// ========================================== +// 1. CONFIGURATION RÉSEAU & API +// ========================================== +const char *ssid = "HUAWEI-45D7"; +const char *password = "72935939"; + +// L'URL de ton script PHP sur IONOS (utilise https si possible) +const char *serverName = "https://dev-reservation.campusfab.net/ControlLab/config/pointage.php"; +String api_key = "votre_cle_api_secrete"; // Pour sécuriser ton API + +// ========================================== +// 2. CONFIGURATION LECTEUR NFC (PN532 en I2C) +// ========================================== +#define SDA_PIN 21 +#define SCL_PIN 22 + +// On passe des broches par défaut (2 et 3) pour IRQ et RESET (même si on ne les câble pas). +// Cela force la librairie à utiliser le mode "I2C Matériel" au lieu d'essayer de bidouiller les broches. +Adafruit_PN532 nfc(2, 3); + +// ========================================== +// FONCTIONS UTILITAIRES +// ========================================== + +// Fonction pour envoyer la donnée au serveur +void envoyerPointage(String uid_str) +{ + if (WiFi.status() == WL_CONNECTED) + { + HTTPClient http; + + // Début de la connexion + http.begin(serverName); + + // On précise qu'on envoie les données comme un formulaire web + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + // Préparation des données (ex: api_key=cle_secrete_123&uid=A1B2C3D4) + String httpRequestData = "api_key=" + api_key + "&uid=" + uid_str; + + Serial.print("Envoi en cours pour l'UID: "); + Serial.println(uid_str); + + // Envoi de la requête POST + int httpResponseCode = http.POST(httpRequestData); + + if (httpResponseCode > 0) + { + Serial.print("Succès ! Code HTTP : "); + Serial.println(httpResponseCode); + String payload = http.getString(); + Serial.println("Réponse du serveur : " + payload); + } + else + { + Serial.print("Erreur lors de l'envoi HTTP. Code : "); + Serial.println(httpResponseCode); + } + + http.end(); // Libère les ressources + } + else + { + Serial.println("Erreur : ESP32 déconnecté du Wi-Fi"); + } +} + +// ========================================== +// INITIALISATION +// ========================================== +void setup() +{ + Serial.begin(115200); + delay(2000); // Délai plus long pour ESP32-C6 + + Serial.println("\n\n=== ESP32-C6 DÉMARRÉ ==="); + Serial.println("Si vous voyez ce message, le Serial fonctionne !"); + + // --- Connexion Wi-Fi --- + Serial.print("Connexion au Wi-Fi"); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println("\nConnecté au réseau ! IP: " + WiFi.localIP().toString()); + + // --- Initialisation NFC --- + Serial.println("Initialisation du bus I2C..."); + + // ÉTAPE CRUCIALE POUR L'ESP32-C6 : On route le signal I2C sur les broches 21 et 22 + Wire.begin(SDA_PIN, SCL_PIN); + nfc.begin(); + uint32_t versiondata = nfc.getFirmwareVersion(); + if (!versiondata) + { + Serial.print("Lecteur PN532 introuvable. Vérifiez le câblage !"); + while (1) + ; // On bloque ici si pas de lecteur + } + + Serial.println("Lecteur NFC trouvé et prêt !"); + // Configure la carte pour lire les tags RFID classiques (Mifare) + nfc.SAMConfig(); +} + +// ========================================== +// BOUCLE PRINCIPALE +// ========================================== +void loop() +{ + uint8_t success; + uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0}; // Buffer pour stocker l'UID lu + uint8_t uidLength; // Longueur de l'UID (4 ou 7 octets) + + // Attend passivement qu'une carte soit approchée + success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); + + if (success) + { + Serial.println("\n--- CARTE DÉTECTÉE ---"); + + // Conversion du tableau de bytes (uid) en String Hexadécimal lisible + String uidString = ""; + for (uint8_t i = 0; i < uidLength; i++) + { + if (uid[i] < 0x10) + uidString += "0"; // Ajoute un zéro pour garder 2 caractères (ex: 0A) + uidString += String(uid[i], HEX); + } + + uidString.toUpperCase(); // Met tout en majuscules (ex: a1b2 -> A1B2) + + // Envoi au serveur + envoyerPointage(uidString); + + // Délai pour éviter de scanner la même carte 50 fois par seconde si on la laisse posée + delay(3000); + } +} \ No newline at end of file diff --git a/badgeuse_nfc/test/README b/badgeuse_nfc/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/badgeuse_nfc/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html