Monday 13 February 2012

NFC endeavours

NFC or near field communication has been around for years, I originally knew it as RFID. The first practical application I saw was, and still is the door control system at the London Hackspace, where you get your RFID card number put onto the list and to get into the space you simply swipe your card over the reader and the door opens. GLaDOS announcing your entrance is not only fun, but also practical, as everyone in the space knows who has just entered without having to look.

Well I fancy having a go at that, and bought myself a Touchatag reader with some tags, the same kit that's used in LHS, so that I can just copy what they've done, giving me a nice easy start point. At least that was the plan.

Bearing in mind that the Touchatag has been around for a while; searching for Linux Touchatag brings up a stack of hits and there are numerous sites covering how to get them to work together it really should be easy.

I've always found it best to go with the most up-to-date article I can find, and so I used this one, and pretty much followed it to the letter, although I couldn't patch the info file, but managed to get hold of one.

So when I came to run the first command I got an error:

error while loading shared libraries: libnfc.so.2:cannot open shared object file: No such file or directory
Damn, now a quick check back through to make sure I'd done everything correctly, and an email to Ben. I had to add the location of libnfc.so.2 to /etc/environment and that should be it.

Alas no.

The only thing that's different between my setup and Ben's is that I'm using Ubuntu 11.10 and he's using 10.04 and now... well I have nowhere to go with this. Annoying really, I know it should work.

Sunday 5 February 2012

See MQTT actually do something

Yeah yeah... "connectivity protocol"... "useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium"... "used in sensors communicating to a broker via satellite link"... "in a range of home automation and small device scenarios" but I want to actually see this working before I spend the next few weeks/months learning about it.

We're surrounded by things that can be measured, and that measurement data can be used, whether it's the temperature of your fridge, the amount of water you've used today, the amount of electricity you've used in the past hour, the temperature of your fish tank, how many people have walked past your house, how many times your home phone has rung whilst you were out etc etc. All of this data can be used for something. MQTT is the protocol that will allow you to do something with that data.

Now without a source of data, it can be hard to grasp what can be done. Luckily there's a broker running at test.mosquitto.org that will provide you with a large stream of topics, and data to watch flowing. That site also tells you how to subscribe to the broker using the mosquitto_sub client using both Linux and Windows.

There are a vast amount of topics available, and there's a list available at test-mosquitto.heroku.com which is an mqtt to web bridge for test.mosquitto.org. Each of the topics listed there will give you the last message listed under that topic.

Now using that vast list of topics it's possible to experiment with them and try to get only the specific information you want. For example,
mosquitto_sub -h test.mosquitto.org -t \energy/# -v
Will provide only the energy topics, and
mosquitto_sub -h test.mosquitto.org -t energy/generation/realtime/wind/# -v
should provide all of the realtime wind data. Of course, what you do with this data is another matter entirely. Put it in a database, make some funky web page out of it, mash the figures into another person's for comparison, the list is limited by your imagination.

Once you've had a ponder on that, consider the fact that you can also send mqtt messages that would be able to turn the heating up/down at home, open your garage door automatically when you get close enough, turn your porch light on, again the list is endless.

Saturday 4 February 2012

Which is the easiest?

If you're going to get into MQTT you're going to have to get into code. Most people like me who can just about spell Perl and Python will end up asking the question "Which is easiest to learn?". I believe that they're both equally hard/easy, and so maybe the question should be "Which will I get the most support with". There are books, there are on line forums and all of that sort of thing but you essentially need a mentor, and that person will help you decide which language to focus on.

The mqtt community is pretty helpful and there's a stack of blogs and code out there that you can get your hands on, like this bit of Perl:
#!/usr/bin/perl -w

# Reads data from a Current Cost device via serial port.
# Publishes unprocessed data to mqtt

use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );

my $pubclient = "mosquitto_pub -t sensors/cc128/raw -q 2 -s";
my $PORT = "/dev/ttyUSB0";
local $| = 1;
my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(57600);
$ob->write_settings;

open(SERIAL, "+<$PORT");
while (my $line = ) {
chomp $line;
open(MQTT, "|$pubclient");
print(MQTT "$line");
close(MQTT);
}
Those few lines take the data from my Current Cost and publishes it with mosquitto. But, if it doesn't work, you have to know Perl to be able to work out why it doesn't work. As I mentioned in the previous post, for anyone without coding knowledge this can be a major stumbling block.

So it's important to have a goal, be it making graphs for your website, pushing weather data to your phone or having your house tell you what's going on. Have a go at some sort of plan as to how things might work before seeing if someone's done it before. Then hopefully someone will help explaining what lines like use Device::SerialPort qw( :PARAM :STAT 0.07 ); actually mean.

Adventures with mqtt

MQTT is superb, "designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium"

This is similar to the tactical packet communications I helped bring into the UK a few years ago. I haven't used APRS for well over 10 years, and it was just hardware and software, set it up and off you go. I had my weather station, house and car all on the 2m ham band.

MQTT on the other hand needs coding skill to get anything out of it. So how does a non-coder get anything done with this fantastic capability?

One of the simplest things you can do is grab a Current Cost energy monitor and use that as a data source for "doing something with". There's a lot of information out there to cover this very project, with scripts in python, perl and java available to take data that the monitor is publishing and make energy usage graphs or pump it to an mqtt message broker such as mosquitto.

But... everything out there is written by people that know what they're doing. The Perl and Python works for those that wrote it, if it doesn't work when you try it, well your adventure can end pretty quickly. Of course there are irc channels and email lists to talk about your problems but it can be a long and frustrating struggle.

I have my Current Cost publishing data (again) using mosquitto and from there I should be able to take the data and do, well anything I can imagine. Of course there's some web based graphs to be made, but I'd also like to have some way of pushing selected data to my phone. Then there's the rest of the house to measure and publish, oh and I have my weather station to get up and running again before I see if that can be used with mqtt.

All of this I plan to document, for myself as much as anyone, from the codeless and clueless perspective so that those that can't, might one day be able to too.