28 November 2011

Packetlogger documentation: n2kd - producing a json server for a webpage

As you may have guessed from the -json option to the analyzer, I use it myself to produce data that is consumed by a webpage.
The webpage reads the data using AJAX and XmlHttpRequest. But for that to work nicely it needs a server that produces JSON, and that JSON should be as short as possible with each PGN only produced once. In other words, a webpage that is updated every 2 seconds does not want to know about the 19 rudder angle changes that were sent on the CAN bus in the mean time, only about the last one (or even better, the average -- but that is something my code doesn't do yet). Furthermore, if it is interested in AIS messages it wants to know about all AIS messages, not just the ones produced in the last two seconds.
So that's where a new program comes in, called n2kd (for NMEA 2000 deamon). It is available for Linux and OS X. This isn't very configurable yet, but I also don't know if anyone is interested. It is what I am using on-board at the moment and can be improved considerably. Let me know if you think this is useful if only it had such-and-such feature and I will consider it!
Here's the actual command that I use on board:
actisense-serial -r $ACTISENSE_PRIMARY | 
  analyzer -clocksrc 35 -json |
As you can see my GPS has CAN bus ID 35, so I give that to the analyzer to correct the computer's clock based on the GPS data.
The N2KD program is then contacted on port 597, and it will wait for 2 seconds and then spit out a full JSON structure containing all knowledge it has over all PGNs.
Note that this program will intentionally crash when it has no further input. I'm using a watchdog to restart the above command string when something goes wrong. It should probably just quit instead, but that's for next time.

No comments:

Post a Comment