Today about Near Field Communication Article, and called NFC.
I use ACR122U USB NFC Tag Reader & Writer, and this article about how to use ACR122U USB NFC Reader for Get UID of NFC Tags.
In ACR122U Software usually there are: Get Data and Get ATR.
in this article about NFC Get Data:
Get data (UID) from NFC Tags and transfer / send to Siemens PLC via Modbus communication with Arduino Help.
ACR122U NFC Reader same with NFC for Android, NFC for iPhone, NFC for Smart phone and etc.
Video Demonstration: ACR122U USB NFC Near Field Communication, Arduino, and Siemens PLC
I use ACR122U USB NFC Tag Reader & Writer, and this article about how to use ACR122U USB NFC Reader for Get UID of NFC Tags.
In ACR122U Software usually there are: Get Data and Get ATR.
in this article about NFC Get Data:
Get data (UID) from NFC Tags and transfer / send to Siemens PLC via Modbus communication with Arduino Help.
ACR122U NFC Reader same with NFC for Android, NFC for iPhone, NFC for Smart phone and etc.
Hardware Needed for this NFC Application
- ACR122U USB NFC Tag Reader & Writer
- NFC / Near Field Communication Tags, I use:
- 2 pieces NFC Chip NTAG213 / Universal NFC tags sticker
- 2 pieces NFC / Near Field Communication Cards
- Arduino USB Host Shield
- Arduino UNO
- TTL to Male RS232 Module
- Power Supply DC 5 Volt for Arduino and ACR122U USB NFC
- Siemens PLC with Modbus Slave Support, or other device with Modbus Slave Support
- RS232 PLC Cable
- 4 pieces Lamp / Light for testing
NFC / Near Field Communication Hardware
Hardware Connections for this NFC Application
1. ACR122U USB NFC Reader, Arduino and Siemens PLC
2. Siemens PLC to Light
- Light 1 connect to Siemens PLC Output Q0.0
- Light 2 connect to Siemens PLC Output Q0.1
- Light 3 connect to Siemens PLC Output Q0.2
- Light 4 connect to Siemens PLC Output Q0.3
Arduino Software and Library for this NFC Application
- I use Arduino Software version 1.6.6
- NFC / Near Field Communication Arduino Library: click here
ACR122U Software / ACR122U Tool Download
1. ACR122U SDK Download
I use Windows 7, for download : click hereProject File for this NFC Application
- Arduino project file : click here
- Siemens PLC S7-200 project file : click here
Siemens PLC and NFC / Near Field Communication Programming
Modbus Addressing in PLC Ladder Programming
Arduino Code for this NFC Application
#include <usbhub.h> #include <SimpleModbusMaster.h> #define MODBUSSlaveAddress 1 #define MODBUSbaud 9600 #define MODBUSformat SERIAL_8E1 #define MODBUStimeout 100 #define MODBUSpolling 1 #define MODBUSretry_count 0 #define MODBUSTxEnablePin 2 enum { MODBUSPACKET1, MODBUSTOTAL_NO_OF_PACKETS }; Packet MODBUSpackets[MODBUSTOTAL_NO_OF_PACKETS]; packetPointer MODBUSpacket1 = &MODBUSpackets[MODBUSPACKET1]; #define NFC_Hardware "ACR122U" #define NFC_Tags_Max 7 unsigned int MODBUSwriteRegs[NFC_Tags_Max+1]; USB Usb; USB_DEVICE_DESCRIPTOR buf; uint8_t addr; uint8_t rcode; uint8_t NFC_USB_State; bool NFC_USB_RUN = false; uint8_t NFC_Receive[64]; String NFC_Receive_String = ""; unsigned long NFC_Get_UID_Timeout; uint16_t rcvd; uint16_t NFC_Receive_Size; uint16_t NFC_Tags_Size; uint8_t NFC_Tags[NFC_Tags_Max]; unsigned int NFC_Send_Success; uint8_t NFC_Order; uint8_t NFC_EPP_OUT = 0x22; //NFC Endpoint Address Out Transfer : 0x02 uint8_t NFC_EPP_IN1 = 0x01; //NFC Endpoint Address In Transfer 1: 0x81 uint8_t NFC_EPP_IN2 = 0x02; //NFC Endpoint Address In Transfer 2: 0x82 uint8_t NFC_Easy_Connect_Code[15] = {0x6B,0x05,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xFF,0x00,0x48,0x00,0x00}; uint8_t NFC_Reader_Code[10] = {0x62,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00}; uint8_t NFC_Get_UID_Code[15] = {0x6F,0x05,0x00,0x00,0x00,0x00,0x03,0x04,0x00,0x00,0xFF,0xCA,0x00,0x00,0x00}; void setup() { if (Usb.Init() == -1){ //Near Field Communication (NFC) USB Reader did not start while(1); } modbus_construct(MODBUSpacket1, MODBUSSlaveAddress, PRESET_MULTIPLE_REGISTERS, 0, NFC_Tags_Max+1, MODBUSwriteRegs); modbus_configure(&Serial, MODBUSbaud, MODBUSformat, MODBUStimeout, MODBUSpolling, MODBUSretry_count, MODBUSTxEnablePin, MODBUSpackets, MODBUSTOTAL_NO_OF_PACKETS); NFC_USB_State=0; NFC_Order=1; NFC_Receive_String.reserve(64); } void loop() { Usb.Task(); if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) { NFC_USB_RUN = true; switch(NFC_USB_State) { case 0: // NFC USB Configuration rcode = ACR122U_NFC_Configuration(); if(rcode){ asm volatile (" jmp 0"); }else{ NFC_USB_State=1; } break; case 1: // ACR122U Open NFC or NFC Connect NFC_Order++; rcode = ACR122U_Open_NFC(); if(rcode){ NFC_USB_State=1; }else{ NFC_USB_State=2; } break; case 2: // ACR122U USB NFC Setup NFC_Order++; rcode = ACR122U_USB_NFC_Reader(); if(rcode){ NFC_USB_State=2; }else{ NFC_USB_State=3; } break; case 3: // ACR122U USB NFC Tag Reader NFC_Receive_Size=0; NFC_Receive[0]=0; NFC_Receive[1]=0; rcode = ACR122U_USB_NFC_Read(NFC_EPP_IN1,&NFC_Receive_Size, NFC_Receive, 1); if(rcode){ NFC_USB_State=3; }else{ if(NFC_Receive_Size==2){ if(NFC_Receive[0]==0x50 && NFC_Receive[1]==0x03 ){ NFC_Get_UID_Timeout = millis() + 100; NFC_USB_State=4; } } } break; case 4: // ACR122U NFC Get Data UID NFC_Order++; NFC_Tags_Size=0; rcode = ACR122U_USB_NFC_Tag_Reader(&NFC_Tags_Size, NFC_Tags); if(rcode){ if(millis() > NFC_Get_UID_Timeout)NFC_USB_State=3; }else{ if(NFC_Tags_Size>0){ memset(MODBUSwriteRegs,0,sizeof(MODBUSwriteRegs)); MODBUSwriteRegs[0]=NFC_Tags_Size; for(uint16_t i=0; i < NFC_Tags_Size; i++ ) { MODBUSwriteRegs[i+1]= NFC_Tags[i]; } NFC_Send_Success = MODBUSpacket1->successful_requests; NFC_Get_UID_Timeout = millis() + 1000; NFC_USB_State=5; }else{ if(millis() > NFC_Get_UID_Timeout)NFC_USB_State=3; } } break; case 5: // NFC UID Send to Siemens PLC modbus_update(); if(NFC_Send_Success != MODBUSpacket1->successful_requests)NFC_USB_State=3; if(millis() > NFC_Get_UID_Timeout)NFC_USB_State=3; break; } }else{ if(NFC_USB_RUN){ // ACR122U NFC USB Not RUN asm volatile (" jmp 0"); } } } void NFC_USB_GetAddresses(UsbDevice *pdev) { UsbDeviceAddress adr; adr.devAddress = pdev->address.devAddress; addr = adr.devAddress; } uint8_t ACR122U_NFC_Configuration() { uint8_t Rcode; Usb.ForEachUsbDevice(&NFC_USB_GetAddresses); Rcode = Usb.getDevDescr(addr, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*) & buf); if (Rcode) { return (Rcode); }else{ Rcode = Usb.setConf(addr, 0, buf.bNumConfigurations); return (Rcode); } return (USB_STATE_ERROR); } uint8_t ACR122U_Open_NFC() { uint8_t Rcode; NFC_Easy_Connect_Code[6]=NFC_Order; Rcode = ACR122U_USB_NFC_Write(NFC_EPP_OUT,sizeof(NFC_Easy_Connect_Code),NFC_Easy_Connect_Code); if(Rcode) return Rcode; rcvd=0; NFC_Receive[0]=0; NFC_Receive[1]=0; Rcode = ACR122U_USB_NFC_Read(NFC_EPP_IN2,&rcvd, NFC_Receive, 10); if(Rcode) return Rcode; if(rcvd<12) return (1); if(NFC_Receive[0]!=0x83 || NFC_Receive[1]!=0x0A) return (1); NFC_Receive_String=""; for(uint16_t i=7; i < rcvd; i++ ) NFC_Receive_String += String(char(NFC_Receive[i])); if (NFC_Receive_String.indexOf(NFC_Hardware)!=-1) return (0); return (1); } uint8_t ACR122U_USB_NFC_Reader() { uint8_t Rcode; NFC_Reader_Code[6]=NFC_Order; Rcode = ACR122U_USB_NFC_Write(NFC_EPP_OUT,sizeof(NFC_Reader_Code),NFC_Reader_Code); if(Rcode) return Rcode; rcvd=0; NFC_Receive[0]=0; Rcode = ACR122U_USB_NFC_Read(NFC_EPP_IN2,&rcvd, NFC_Receive, 10); if(Rcode) return Rcode; if(rcvd<12) return (1); if(NFC_Receive[0]==0x80) return (0); return (1); } uint8_t ACR122U_USB_NFC_Tag_Reader(uint16_t *NFC_Key_Size, uint8_t* NFC_Key) { *NFC_Key_Size=0; uint8_t Rcode; NFC_Get_UID_Code[6]=NFC_Order; Rcode = ACR122U_USB_NFC_Write(NFC_EPP_OUT,sizeof(NFC_Get_UID_Code),NFC_Get_UID_Code); if(Rcode) return Rcode; rcvd=0; NFC_Receive[0]=0; NFC_Receive[1]=0; Rcode = ACR122U_USB_NFC_Read(NFC_EPP_IN2,&rcvd, NFC_Receive, 50); if(Rcode) return Rcode; if(rcvd<12) return (1); uint16_t Buffer_NFC_Key_Size = 0; if(NFC_Receive[0]==0x80 && NFC_Receive[1]>0x02){ for(uint16_t i=10; i < (rcvd-2); i++ ) { *NFC_Key++ = NFC_Receive[i]; Buffer_NFC_Key_Size++; } *NFC_Key_Size = Buffer_NFC_Key_Size; return (0); } return (1); } uint8_t ACR122U_USB_NFC_Write(uint8_t vHXFR,uint16_t nbytes, uint8_t* data) { Usb.bytesWr(rSNDFIFO, nbytes, data); Usb.regWr(rSNDBC, nbytes); Usb.regWr(rHXFR, vHXFR); while(!(Usb.regRd(rHIRQ) & bmHXFRDNIRQ)); Usb.regWr(rHIRQ, bmHXFRDNIRQ); return (0); } uint8_t ACR122U_USB_NFC_Read(uint8_t vHXFR,uint16_t *pktsize, uint8_t* data, unsigned long timeout) { unsigned long timeout_start = millis() + timeout; while((long)(millis() - timeout_start) < 0L) { Usb.regWr(rHXFR, vHXFR); if((Usb.regRd(rHIRQ) & bmRCVDAVIRQ)==bmRCVDAVIRQ){ uint16_t buff_pktsize = Usb.regRd(rRCVBC); *pktsize = buff_pktsize; data = Usb.bytesRd(rRCVFIFO, buff_pktsize, data); Usb.regWr(rHIRQ, bmRCVDAVIRQ); return (0); } } return (1); }
Video Demonstration: ACR122U USB NFC Near Field Communication, Arduino, and Siemens PLC