This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot:examples:nfc [2024/06/21 18:41] – karllall | en:iot:examples:nfc [2024/06/21 18:41] (current) – karllall | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== NFC example ====== | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | SCK -> 4 \\ | ||
| + | MISO -> 5 \\ | ||
| + | MOSI -> 6 \\ | ||
| + | SS -> 13 \\ | ||
| + | VCC -> 8 \\ | ||
| + | GND -> 15 \\ | ||
| + | |||
| + | Needed libraries: | ||
| + | < | ||
| + | |||
| + | The code below will upload card ID to mqtt server, when a card is present. | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | /* | ||
| + | * IoT NFC example | ||
| + | * | ||
| + | * This example will publish to " | ||
| + | * | ||
| + | * Created 19 Nov 2019 | ||
| + | * Modified by Lars Briedis 17 June 2024 | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Define pins for communication with the NFC reader | ||
| + | #define PN532_SCK | ||
| + | #define PN532_MOSI D7 | ||
| + | #define PN532_SS | ||
| + | #define PN532_MISO D6 | ||
| + | |||
| + | //Setup Adafruit PN532 library | ||
| + | Adafruit_PN532 nfc(PN532_SCK, | ||
| + | |||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | |||
| + | nfc.begin(); | ||
| + | uint32_t versiondata = nfc.getFirmwareVersion(); | ||
| + | if (! versiondata) { | ||
| + | Serial.println(" | ||
| + | while (1); | ||
| + | } | ||
| + | nfc.SAMConfig(); | ||
| + | |||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | boolean success; | ||
| + | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; | ||
| + | uint8_t uidLength; | ||
| + | char buf[10]; | ||
| + | char str[70] = ""; | ||
| + | |||
| + | // | ||
| + | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, | ||
| + | |||
| + | if (success) | ||
| + | { | ||
| + | // | ||
| + | for(int i = 0; i < uidLength; | ||
| + | { | ||
| + | String(uid[i]).toCharArray(buf, | ||
| + | strcat(str, buf); | ||
| + | } | ||
| + | // | ||
| + | | ||
| + | Serial.println(str); | ||
| + | delay(1000); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||