Giter Club home page Giter Club logo

pn532-spi-js's Introduction

pn532-spi-js

This is a 1:1 ES2016 reimplementation of Adafruit's PN532 Python module, allowing interaction with the PN532 NFC board over software SPI.

It's tested only on a Raspberry Pi 3b using the wiring diagram here.

Installation

npm install pn532-spi

Usage

The simplest example is setting up an instance of the PN532 class, printing the board's firmware version, and polling for a UID.

Note: This needs to either run as root or with the appropriate capabilities set on the node binary.

// index.js

import PN532 from 'pn532-spi'

let bytesToHex = function(arr) {
  return arr.reduce(function(a, b) {
    let result = b.toString(16)
    if (result.length == 1) result = '0' + result
    return a + result
  }, '0x')
}

let pn532 = new PN532({
  clock:  22, // SCLK (GPIO 25)
  mosi:   16, // MOSI (GPIO 23)
  miso:   18, // MISO (GPIO 24)
  client: 12, // SSEL (GPIO 18)
  // Enable to get debug logging for frames
  // debug: true
})

// Will throw an error if PN532 is unresponsive or misconfigured
pn532.begin()

let version = pn532.getFirmwareVersion()
console.log('PN532 Firmware version: ', version[1] + '.' + version[2])

// Configure PN532 for Mifare cards
pn532.samConfiguration()

// Poll until we get a response and print the UID
for(;;) {
  console.log('Waiting for scan...')
  let uid = pn532.readPassiveTarget()
  if (uid == null) continue

  console.log('Found UID: ', bytesToHex(uid))
}

Sample output:

$ babel-node ./index.js
PN532 Firmware version: 1.6
Waiting for scan...
Waiting for scan...
Waiting for scan...
Waiting for scan...
Found UID: 0x7C7B7B7B7B7B7B

More thorough examples can be found in the Python library.

Known Issues

  • Hardware SPI currently not supported (though probably trivial to add)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.