BASIC4MCU | 질문게시판 | 아두이노 NFC 태그 질문입니다.
페이지 정보
작성자 라스카리스 작성일2018-12-01 18:24 조회4,398회 댓글1건본문
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#define DEBUG true
#define PN532_IRQ (9) // (2) <- changed to D9 for IRQ pin, refer to schematic
#define PN532_RESET (8) // (3) <- changed to D8 for nReset pin, refer to schematic
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using
programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
#define Serial SerialUSB
#endif
int a = 0;
void setup()
{
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
nfc.begin();
pinMode(11,OUTPUT);
digitalWrite(11,LOW);
pinMode(12,OUTPUT);
digitalWrite(12,LOW);
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata)
{
Serial.print("\r\nDidn't find PN53x board");
while (1); // halt
}
sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
// Got ok data, print it out!
Serial.print("\r\nFound chip PN5");
Serial.print((versiondata>>24) & 0xFF, HEX);
Serial.print("\r\nFirmware ver. ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.');
Serial.print((versiondata>>8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.print("\r\nWaiting for an ISO14443A card");
}
void loop()
{
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success)
{
Serial.print("\r\nFound a card!");
Serial.print("\r\nUID Length: ");
Serial.print(uidLength, DEC);
Serial.print(" bytes");
Serial.print("\r\nUID Value: ");
if (a == 0){
digitalWrite(11,HIGH);
a = 1;
}
else {
digitalWrite(11,LOW);
a = 0;
}
Serial.println("LED2 on");
for (uint8_t i = 0; i < uidLength; i++)
{
Serial.print(" 0x");
Serial.print(uid[i], HEX);
}
// Wait 1 second before continuing
delay(1000);
}
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
if(digitalRead(12) & a==1){ digitalWrite(12,LOW ); a=0; Serial.println("LED1 off"); }
else if (a==0) { digitalWrite(12,HIGH); a=1; Serial.println("LED1 on "); }
delay(500); // wait for the serial buffer to fill up (read all the serial data)
}
}
}
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
우노에서 esp8266 과 pin 532를 써서 WiFi와 NFC로 LED 2개를 각자 하나씩 on/off 하려는데요
NFC는 on/off가 잘되는데 WiFi는 신호를 보내면 수신했다가 NFC 태그 신호가 오면 on/off 가 됩니다.
다시말해 WiFi는 12번LED, NFC는 11번LED를 on/off하게 하고 싶은데
12번 LED는 WiFi 신호 + NFC 태그 여야 on/off됩니다.
뭐가 문제인지 찾아보니 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
부분이 문제인것 같습니다. 이 부분을 주석 처리 하면 WiFi로 12번 LED는 잘 되는데
이러면 문제는 nfc.readPassiveTargetID가 NFC 태그를 확인해서 토글시키는 역할이
라 NFC 11번 LED는 on/off가 불가능하지요. 어떡하면 좋을까요??
댓글 1
조회수 4,398master님의 댓글
master 작성일
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
배열에 ID를 기입해야 하는 것 아닌가요?