Starting nfc-eventd from systemd on the beaglebone

Attaching an nfc reader to the beaglebone. In my case a cheap one called COOQROBOT which I think is pn532 variant.

I installed both libnfc and nfc-eventd from source. The ususal ./configure; make ; sudo make install. ( You might need to follow this with a ldconfig -v to get the library picked up )

Now you can start a service that monitors your nfc reader for new tags. Just drop the following into /etc/systemd/system/nfceventd.service and run sudo systemd daemon-reload.

[Unit]
Description=nfc-eventd

[Service]
ExecStart=/usr/local/bin/nfc-eventd

[Install]
WantedBy=multi-user.target

Now you can start the service with systemctl start nfceventd.service. And look at errors and output with systemctl status nfceventd.service.

By default the config for nfc-eventd just saves the events to /tmp/nfc-eventd. If you edit /usr/local/etc/nfc-eventd.conf you can change what this now does. So it could for instance send those events to your node-red server via mqtt.

action = "mosquitto_pub -h mosquitto.example.com -t /nfc/tag/ -m $TAG_UID "

Bash Linting. Checking your scripts are nice and lint free

Continiuing on from my previous post on testing shell scripts with bats I have been looking for a linting solution for shell scripts. And I think I have found a nice one in shellcheck. Which is aviailble as both a website and a packge on debian.

It's really simple to run just give it your shell script as an argument and you get a nice set of comments on the style of your code. Here is the result of testing their test script.

#!/usr/bin/env bash
## Example of a broken script. Hit the Down Arrow button to ShellCheck it!
for f in $(ls *.m3u)
do
  grep -qi hq.*mp3 $f \
    && echo 'Playlist $f contains a HQ file in mp3 format'
done

Then run shellcheck foo.sh

In foo.sh line 3:
for f in $(ls *.m3u)
         ^-- SC2045: Iterating over ls output is fragile. Use globs.
              ^-- SC2035: Use ./*.m3u so names with dashes won't become options.


In foo.sh line 5:
  grep -qi hq.*mp3 $f \
           ^-- SC2062: Quote the grep pattern so the shell won't interpret it.
                   ^-- SC2086: Double quote to prevent globbing and word splitting.


In foo.sh line 6:
    && echo 'Playlist $f contains a HQ file in mp3 format'
            ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

I have a simple makefile now that's just there mostly as a reminder of the testing that is aviailble for that script.

bats: *.bats
    bats *.bats

lint: your_script your_nextscript
    shellcheck your_script
    shellcheck your_nextscript

Next up is intergration with vim. I use the really nice syntastic vim plugin. Syntastic does automatic style checking on files as you save them and shows a set of errors. Installing it is simple. Install shellcheck, I use apt-get install shellcheck on Debian but you can use your favorite way on your OS. Then install the vim syntastic, again I use Vundle so adding Bundle scrooloose/syntastic to my .vimrc and then running BundleInstall in vim did the trick for me. Now when I write a shell script to disk vim will comment on my style.

Setting Soundcard names in Gnome

I have a couple of sound cards attached to my laptop at work and it gets confusing to know which one is which when they have names like "Analogue line out". So I set about changing them and struggled a bit in the process so I thought I would note it here for reference.

Gnome takes the name of the soundcards from pulseaudio. There does not seem to be a way to rename them directly in gnome but you can do it from the command line. The command pacmd list-sinks will give you a list of the devices that you have. Looking through that list at the 'name:' field are the identifiers that you can use. ( You can also use the ID but I am not sure how stable they are ) The man page for pulse-cli-syntax is the rather confusing location for the commands that you can give the pacmd command.

Now we know which card we want we can change the description property that Gnome uses with the following command. Note the extra quotes are required if your description has spaces in it.

update-sink-proplist alsa_output.usb-Burr-Brown_from_TI_USB_Audio_DAC-00-DAC.analog-stereo 'device.description="External Blue headphones"'

Then you can run the pacmd list-sinks command to check that works. At the gnome level nothing will have changed. So now you need to add this to your config file ~/.config/pulse/default.pa which is just a list of pacmd commands to run on startup. You can now drop the extra single quotes as it does not need to be escaped for the shell prompt.

Update: You need to add the line below to your default.pa so that it knows about the default config. Pulseaudio only loads one config file by default so this just makes sure you get the default settings first.

.include /etc/pulse/default.pa

Now it just remains to get pulseaudio to re-read it's config in the usual unix way by giving it a HUP.

pkill -HUP pulseaudio

Listening to SSL with socat

I wanted to dump the headers for a http request over ssl today. Pulled out socat and this command and it seemed to work quite nicely.

socat OPENSSL-LISTEN:4433,cert=server.crt,key=server.key,verify=0 -

You will need to create server.crt and server.key and if you don't want the other end to complain then they should be a valid keypair.

Socat is a really nice tool that exposes the power of Unix sockets and allows you to connect anything together. In this case a tcp socket with ssl support and the standard out. But it could be a UDP port and a serial cable for all all socat cares. A really nice tool.

Testing Bash with Bats

Spectacled Fruit Bat by Shek Graham

Spectacled Fruit Bat by Shek Graham

I quite like unit tests. They give you that quick feedback that things are going to be okay. I can write a test watch it fail. Write some code and watch it fail also. Then learn very quickly what I have done wrong.

At some point last year I read the Clean Code book by Rober C Martin and it opened up to me a lot of the ideas about testing and code that I had not really got to grips with at the time. Martin takes you through the reasoning and process for producing code that, in a years time, you might want to actually look at and feel comfortable editing.

What has this go to do with bash and bats? Well we have been adding more tests to our puppet code and it came time to write a bit of bash for moving some files around. I thought there must be a simple way to check some of this and sure enought that is where bats comes in.

bats is a testing framework for bash scripts. I know it sounds crazy but it really does work. If you start applying some of the lessons from clean code and spliting your work into functions with local varaiables it soon becomes easier to test. You can, as ever, take things too far but this does at least allow you to test a lot of your logic before you roll it out. With the added benifit of makeing the code a little more readable.

I found this blog post by Spike cleared up some of the questions I had about testing functions as well. http://blog.spike.cx/post/60548255435/testing-bash-scripts-with-bats

Some other useful things are the google shell style guide and shUnit2 which I have yet to look at. The Defensive Shell Guide by Kfir Lavi and Better Bash Scripting in 15 Minutes by Robert Muth are also good.

Using the ACS ACR122 NFC card reader under linux

If you are seeing some errors in the logs like the ones below:

pcscd[4489]: 58837251 ccid_usb.c:645:OpenUSBByName() Can't claim interface 001/110: Device or resource busy
pcscd[4489]: 00040743 ifdhandler.c:118:IFDHCreateChannelByName() failed
pcscd[4489]: 00000030 readerfactory.c:1043:RFInitializeReader() Open Port 0x200000 Failed (usb:072f/2200:libudev:0:/dev/bus/usb/001/110)
pcscd[4489]: 00000008 readerfactory.c:335:RFAddReader() ACS ACR122U init failed.

You need to prevent the kenel nfc drivers loading so add the following lines to /etc/modprobe.d/rfid-blacklist.conf

blacklist pn533
blacklist nfc

as suggested here: http://maciaszek.net/26522/rfid-reader-mit-debian/

Then remove those modules with modprobe -r pn533 nfc

Then install some packages

apt-get install libpcsclite1 pcsc-tools pcscd

Then run:

pcsc_scan

You should get some output similar to the stuff below.

PC/SC device scanner
V 1.4.23 (c) 2001-2011, Ludovic Rousseau <ludovic.rousseau@free.fr>
Compiled with PC/SC lite version: 1.8.11
Using reader plug'n play mechanism
Scanning present readers...
0: ACS ACR122U 00 00

Thu Jan 22 23:00:58 2015
Reader 0: ACS ACR122U 00 00
  Card state: Card removed,

Thu Jan 22 23:01:06 2015
Reader 0: ACS ACR122U 00 00
  Card state: Card inserted,
  ATR: 3B 8F 81 01 80 4F 0C A0 00 00 04 06 03 00 01 00 00 00 00 6A

ATR: 3B 8F 81 01 80 4F 0C A0 00 00 04 06 03 00 01 00 00 00 00 6A
+ TS = 3B --> Direct Convention
+ T0 = 8F, Y(1): 1000, K: 15 (historical bytes)
  TD(1) = 80 --> Y(i+1) = 1000, Protocol T = 0
-----
  TD(2) = 01 --> Y(i+1) = 0000, Protocol T = 1
-----
+ Historical bytes: 80 4F 0C A0 00 00 04 06 03 00 01 00 00 00 00
  Category indicator byte: 80 (compact TLV data object)
    Tag: 4, len: F (initial access data)
      Initial access data: 0C A0 00 00 04 06 03 00 01 00 00 00 00
+ TCK = 6A (correct checksum)

Possibly identified card (using /usr/share/pcsc/smartcard_list.txt):
3B 8F 81 01 80 4F 0C A0 00 00 03 06 04 00 01 00 00 00 00 6A
3B 8F 81 01 80 4F 0C A0 00 00 03 06 .. 00 01 00 00 00 00 ..
    Mifare Standard 1K (as per PCSC std part3)
3B 8F 81 01 80 4F 0C A0 00 00 03 06 04 00 01 00 00 00 00 6A
3B 8F 81 01 80 4F 0C A0 00 00 03 06 04 .. .. 00 00 00 00 ..
    RFID - ISO 14443 Type A Part 3 (as per PCSC std part3)
3B 8F 81 01 80 4F 0C A0 00 00 03 06 04 00 01 00 00 00 00 6A
    Philips MIFARE Standard (1 Kbytes EEPROM)
    http://www.nxp.com/#/pip/pip=[pfp=41863]|pp=[t=pfp,i=41863]
    RFID - ISO 14443 Type A - Transport for London Oyster
    ACOS5/1k Mirfare
    RFID - ISO 14443 Type A - NXP Mifare card with 1k EEPROM
    vivotech ViVOcard Contactless Test Card
    Bangkok BTS Sky SmartPass

More next time.

Scrolling text on a rainboduino

Spent a little time last night at Hackspace writing some basic code to scroll some text across a rainbowduino.

I use arduino-mk ( pacakge available on debian ) so here is the basic makefile to build it.

BOARD_TAG    = atmega328
ARDUINO_LIBS = Rainbowduino
ARDUINO_DIR   = /usr/share/arduino
ARDMK_DIR     = /usr/share/arduino
AVR_TOOLS_DIR = /usr

include /usr/share/arduino/Arduino.mk
/*
 Rainbowduino v3.0

 Scrolling text

*/

#include <Rainbowduino.h>

String message = "John is ace!";

void setup()
{
  Rb.init();
}

unsigned char x,y,z;

void scroll_text(String msg)
{
    int len = msg.length();
    const int kern = 6;
    byte msg_b[len+1];
    msg.getBytes(msg_b, len+1);

    for (int i = 0; i <= len * kern ; i++) {
        for (int b = 0 ; b < len ; b++) {
            if ((b * kern) > ( i - kern) && (b * kern) < (i + kern) ) {
                Rb.drawChar(msg_b[b],- i + (b * kern),1,random(0xFFFFFF));
            }
        }
        delay(200);
        Rb.blankDisplay();
    }
}

void loop()
{

    scroll_text(message);

}

Internet Notes from about week 02

A new year and a new blog post....

Tech news

All things python: http://pycoders.com/2014/ - This is a list of the best of the pycoders weekly from last year. Some really great posts in there.

Testing our puppet code has become a big thing for us now. More and more of our code has spec tests using rspec-puppet and puppetlabs-rspec-helper. We are also starting to experiment with beaker that server-spec that allow us to boot up a Virtual Machine(VM) and run tests against an applied manifests. Which then leads me on to a interesting project called test-kitchen which was created to do a similar thing for Chef recipies. http://pocket.co/s_5Sx is an interesting post about how to use test-kitchen and docker to do the same for puppet.

Clearing out

Keeping up with my clearing up and with a bit of a new year burst of enthusiasm I have binned my old CD player that I brought at University and no longer works feels such a shame to take it to the tip. Also selling DVD player, AV Amp, Huge speakers on Gumtree. Now I just have a nice stereo amp and small speakers hooked up to a squeezebox for music and radio. Then an Kodi ( was XBMC ) box for watching Youtube and movies etc. All neat and tidy. Next up is sorting the Minidisc collection and getting rid of that too. Everything must go.

I got given a vice for Christmas, no not that sort, a woodworking vice. So now I just need to build a bench to attach it too. So look out for some more woodworking stuff.

Do no evil

I am going to try going back to this blog for some online stuff. I am starting to worry about my dependence on Google. I have been looking at owncloud and I kickstarted ( is that a word now? ) the http://sandstorm.io/ project and the Mozilla Matchstick last year as well.

Stuff from the internet

Bikes and that

http://www.thediscerningcyclist.co.uk/2014/09/quoc-pham-the-derby-review/ - Some really nice cycling shoes. I don't have clipless pedals but if I did these would be the shoes for me. Oh and their boots in the winter.

Coding

https://medium.com/@lucperkins/web-development-in-c-crazy-or-crazy-like-a-fox-ff723209f8f5 - It may seem mad but why not write your next web application in C? Some good tips if you do and a couple of interesting libraries even if you don't.

https://vimeo.com/71278954 - The future of programming circa 1974, given in a talk in 2014. TL;DW we are still programming like it's 1974.

Sysadmin

http://blog.petersobot.com/pipes-and-filters - A really simple introduction to what is going on in a unix pipe. Good to remind yourself of the basics sometimes.

http://youtu.be/mgcJPREl3CU?list=PLDGkOdUX1Ujo3wHw9-z5Vo12YLqXRjzg2 - Some good videos showing you how to driver Grafana which is turning out to be a really smart front end to graphite.

Making

http://www.rayjardine.com/papers/why-sew/index.htm - I brougt a broken sewing machine of ebay based on this post. ( Why do I insist on shaving the yak first every time?)

https://woodworkingmasterclasses.com/ - I have been watching hours or Paul Sellers videos. He has such a great presenting style. Just watching him work is a pleasure. It's all hand tools as well so it a much more relaxed pace. I am seriously concierding treating myself to one of his courses.