WiFi and NFC Working Together

by NFCRenCS in Circuits > Arduino

48 Views, 0 Favorites, 0 Comments

WiFi and NFC Working Together

ptx105r-card-on-antenna.jpg

The Arduino Uno R4 WiFi board and the PTX105R NFC shield is the perfect combination, to make these two wireless technologies work together. This project creates a WiFi Access Point, reads a card and displays the NDEF message on a webpage. The WiFi part is based on the WiFIS3 library's AP_SimpleWebServer example.

Supplies

  1. Arduino IDE
  2. Arduino Uno R4 WiFi
  3. Renesas PTX105R NFC shield
  4. NFC cards of Type 2, 3, 4 or 5 containing NDEF messages

Configure the Shield

shield-pincaps.jpg

The shield can be configured to use the SPI, I2C or UART interface, by arranging the pin-caps. The out-of-the-box configuration is SPI.

Install the PTX105R NFC Library

libmanager.jpg

Install the library from the Arduino Library Manager for SPI, I2C or UART. Make sure only one of the interfaces is installed at a time, and the shield's interface configuration and the installed library match.

Upload the Project

initial-console-output.jpg
  1. Open the Arduino IDE and copy the source code from below.
  2. Plug in the Arduino Uno R4 WiFi board.
  3. Open and configure the Serial Monitor to use 115200 baud rate.
  4. Upload the project to the board

Wait until the initial output appears on the serial monitor, confirming that the PTX105R was successfully initialized and the access point was created. If the PTX105R fails to initialize, press the reset button on the Arduino board.

/*
---------------------------------------------------------------
SPDX-License-Identifier: BSD-3-Clause
Copyright (c) 2024, Renesas Electronics Corporation and/or its affiliates
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of Renesas nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Renesas "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL RENESAS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------
This project creates a WiFi Access Point, reads and displays NDEF message on a webpage.
The WiFi part is based on the WiFIS3 library's AP_SimpleWebServer example.
How to set up and run the example:
1. Connect to WiFi network "ptxAP". Password is 123456789
2. Refresh page to read NDEF message, it will be displayed below.
3. Open http://192.48.56.2 in a browser.
4. Refresh page to read NDEF message, it will be displayed on the webpage.
*/
#include "WiFiS3.h"
#include <IotReader.h>
using namespace PtxIotReader;
bool ptxStatus;
// Configure polling for type A, B, F and V types
// with 500 ms idle time between cycles
// Low Power Card Detection enabled with regular polling at every 10th cycle
// and stand-by mode enabled
const PollingConfig pollConfig = {.pollTypeA = 1U,
.pollTypeB = 1U,
.pollTypeF212 = 1U,
.pollTypeV = 1U,
.idleTime = 500U,
.discoverMode = 10U,
.enableStandBy = 1U};
char ssid[] = "ptxAP"; // network SSID (name)
char pass[] = "123456789"; // network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // network key index number (needed only for WEP)
int wifiStatus = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to open
ptxStatus = IotReader::getReader().begin();
if (!ptxStatus) {
Serial.println("ERROR: failed to initialize IoT reader");
return;
}
Serial.println("IoT reader initialized");
// check the WiFi module
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the WiFi firmware");
}
// by default the local IP address will be 192.168.4.1
// you can override it with the following:
WiFi.config(IPAddress(192,48,56,2));
// print the network name (SSID);
Serial.print("Creating access point: ");
Serial.println(ssid);
// Create open network. Change this line if you want to create an WEP network:
wifiStatus = WiFi.beginAP(ssid, pass);
if (wifiStatus != WL_AP_LISTENING) {
Serial.println("Creating access point failed");
// don't continue
while (true);
}
// wait 10 seconds for connection:
delay(10000);
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (wifiStatus != WiFi.status()) {
// it has changed update the variable
wifiStatus = WiFi.status();
if (wifiStatus == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
// read the HTTP request header line by line
while (client.connected()) {
if (client.available()) {
String httpHeader = client.readStringUntil('\n'); // read the header line of HTTP request
if (httpHeader.equals("\r")) // the end of HTTP request
break;
Serial.print("<< ");
Serial.println(httpHeader); // print HTTP request to Serial Monitor
}
}
// send the HTTP response
// send the HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println(); // the separator between HTTP header and body
// send the HTTP response body
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<center>");
client.print("<p style=\"font-size:5vw;color:blue;\">Renesas Electronics<br></p>");
client.println("</center>");
client.println("</head>");
client.print("<ol style=\"font-size:3vw;margin-left:100px;margin-right:100px;\">");
client.print("<li>Place card on the PTX105R shield's antenna</li>");
client.print("<li>Refresh page to read NDEF message, it will be displayed below</li>");
client.print("</ol>");
client.println("<p>");
std::vector<uint8_t> messageBuffer;
std::string ndefMessage;
while(!IotReader::getReader().detectCard(pollConfig));
Serial.println("Card found");
if (IotReader::getReader().ndefRead(messageBuffer)) {
// If an NDEF message was read, display it
if (messageBuffer.size() > 0) {
const NdefRecord message =
IotReader::getReader().getNdefMessage(messageBuffer);
ndefMessage = std::string(message.payload.begin() + 1U, message.payload.end());
Serial.print("NDEF payload: ");
for (const auto &i : message.payload) {
Serial.print(static_cast<char>(i));
}
Serial.println();
}
} else {
ndefMessage = "No NDEF record found!";
Serial.println("No NDEF record found");
}
Serial.println();
IotReader::getReader().pollingStop();
client.println("<center>");
client.print("<p style=\"font-size:5vw;color:red;\">");
client.print(ndefMessage.c_str());
client.print("<br></p>");
client.println("</center>");
client.println("</html>");
client.flush();
// give the web browser time to receive the data
delay(10);
// close the connection:
client.stop();
}
}
void printWiFiStatus() {
// print the SSID of the network
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this project in action, open a browser to http://");
Serial.println(ip);
}

Connect to WiFi AP and Read Cards

wifi-connection.jpg
browser.jpg
  1. Open the WiFi settings on your client device and connect to the network ptxAP. The password is 123456789.
  2. Place an NFC card on the PTX105R shield's antenna.
  3. Open http://192.48.56.2 in a browser. If the card contains an NDEF message, it will be displayed in the browser.

In order to read different cards via the browser, make sure there's a card on the antenna and refresh the webpage each time.