Wednesday, February 02, 2011

APRS TNC to MySQL

I normally don't program in "perl" but sometimes it's the best tool for the job... I am also sure you are more used to my soldering than programing skills....or lack of them :)
I needed to get data from a serial port and place it on a MySQL database. Using almost the same code I changed it to get the sentences from my APRS TNC.

here it goes the "perl" part:


#!/usr/bin/perl -w
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use Mysql;
# VARIABLES
my $host = "localhost";
my $database = "your_db";
my $tablename = "aprs_data";
my $user = "your_user_name";
my $pw = "keep_it_secret_the_pass";
my $status = "0";
# for the first serial port..
# my $PORT = "/dev/ttyS0";
# for the first usb/rs232 converter
my $PORT = "/dev/ttyUSB0";
# the software...
my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(9600);
$ob->write_settings;
$connect = Mysql->connect($host, $database, $user, $pw);
open(SERIAL, "+>$PORT");
while (my $line = ) {
$myquery = "INSERT INTO $tablename (id, sentence, date_created, status) VALUES (DEFAULT,'$line',DEFAULT,'$status')";
$execute = $connect->query($myquery);
}



You need to change the "VARIABLES" part... but that's should be logic for you... I hope!
The database has the following structure (for MySQL):


--
-- Table structure for table `aprs_data`
--

CREATE TABLE IF NOT EXISTS `aprs_data` (
`id` int(16) NOT NULL auto_increment,
`sentence` varchar(256) NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`status` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=288 ;



Since I am using it for other stuff I placed the extra fields of time stamp and status.
You need to parse from the database all the received sentences for a more human readable form...

This is the output from my "WB8WGA APRS TNC" (http://speakyssb.blogspot.com/2010/12/aprs-tnc.html)
in the form of (id sentence time stamp status) on boot:

289 WB8WGA MODEMLESS TNC V 1.08 2011-02-02 20:45:17 0
290 Type HELP for Info 2011-02-02 20:45:17 0
291 2011-02-02 20:45:17 0
292 cmd: � 2011-02-02 20:45:38 0

Don't forget to set it to monitor mode.
If I have a little spare time will write a parser and a Google maps display

That's it.

No comments: