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.

No comments:

Post a Comment