Tuesday, May 17, 2016

Circuit Chaser: Raspberry Pi + NFC = timer for children's run thru a playground.

Circuit Chaser uses a Raspberry Pi along with an NFC reader to clock outgoing and incoming times for cards (or wristbands in this case) being placed near it.


Hardware wise, it consists of 3 major components:
Software wise, the main components are:
  • Python application using NFC.py to track card touches
  • Python application using CherryPy to supply latest info to web browser display
  • Web page running in Epiphany browser using some heavy duty JavaScripting to update screen continuously
  • Python application monitoring usage and enabling/disabling screen when required
 And then, of course, there is the case, which is actually a case within a case. But more about that later.

This project got started when my daughter bought Zoom Zoom's, an indoor children's playground. Always looking to get an edge up on the competition, we brainstormed several ideas around until the idea of Circuit Chaser came to the fore. This device lets kids clock out, run through a previously determined course in the playground and then clock back in. Runs can be done simultaneously, i.e. more than 1 kid can be out on course at the same time (up to 11).

I covered the installation of the NFC reader on the Pi here: http://khekker.blogspot.ca/2014/01/raspberry-pi-clean-install-of-nfcpy.html.

As a card is brought near the reader, it triggers the reader into action. It reports the unique id of the card, and my Python application then records this in a SQLite3 database table. If a card currently is 'out', the application records the new time as 'in' (or finish, if you like). Otherwise, it records this as 'out', along with the time of the event. The application next does a retrieval of the last 11 records and creates a text file, which will be used by the CherryPy based application described below.

The application also retrieves the pseudonym of the card, making it more palatable for human eyes.

The application based on CherryPy continuously monitors this text file for changes. If found, it will send this file to any browser currently connected to it. If none are found, it goes to sleep for .25 seconds and repeats.

Along with the file sent to the browser is a variable containing the number of seconds expired since midnight, since this is important to keep the times shown on the display accurate. Each system does not necessarily have the same time as the next.

An insight as to how the browser gets to display this information I provided here: http://khekker.blogspot.ca/2013/12/raspberry-pi-cherrypy-and-asynchronous.html

Once received by the browser, the JavaScript takes over and runs continuously, every second updating times for every card/wristband line shown on the display. It uses the variable containing seconds since midnight sent from the CherryPy application and compares it to its own, and either adds or subtracts the difference.

Lastly, the application monitoring usage runs continuously and shuts down the display if no activity has been detected in the last half hour and turns it back on when a card is entered near the reader. The shutting off and starting up is based on this hack by Alex Eames for the HDMPi.

The normal screen blanking on the Pi has been disabled, as explained here:http://khekker.blogspot.ca/2015/12/screen-blanking-raspberry-pi-b.html

I spent an inordinate amount of time on the case. I knew I wanted a transparent case. First, I found a lexan case on the web, which I spent alot of time hacking so the Pi and all its connectors would fit. It looked really good. Then I realized, hey, this has to be used with kids, they can break 'pretty near' (Canadian for 'almost') anything. So I built yet another lexan case around it. Next time, I'll simply go to the specialty place where they sell lexan sheets and for not too much money they can build me a case. A couple of aluminum strips attached to the back of the case allow for attaching to a wall.

The device has been running for over three months now, and has been received well. Better still, it hasn't been broken, hardware nor software wise. It is connected wirelessly to ZoomZooms LAN, and its output could, if port forwarding was employed, be visible on the greater Interweb.

 All files are up on Github: https://github.com/khekker/CircuitChaser, with the exception of NFC.py and CherryPy, as explained above.

The NFC wristbands I obtained from Overair.ca