MQTT CAN Gateway

Setup

I mentioned in an earlier post that my CAN Input modules now supported analog input mode on the first 8 inputs. I showed how I could use these analog inputs with NTC temperature sensors to take readings on a wired network rather than wireless.

The next step was to write a gateway that would take these analog readings off the CAN bus and send them to MQTT in order for the existing perl script to pick them up and mash them into RRD databases – as it already does for the radio sensor data.

Other earlier posts provided some details and results while writing the SMS MQTT gateway. Its no surprise I mostly just copied the code for that interface and used it as a starting point.

The only major changes were

  • Removing the SMS specific code and replacing it with the CAN device equivalent.
  • Adding publish ability to my MqttDataProvider class
  • Hard coded decoding of CAN messages into MQTT topics/content publish calls inside this interface. The topic used is in this case is “/sensors/can/<bus id>/<id>/temperature”

In theory (perhaps I’ll do this later) this gateway should just publish all incoming messages into a single topic (e.g. “/messages/can/incoming”) that other “decoder” applications can subscribe to, decode any messages they know how to and then publish new messages to new topics (e.g. “/sensors/can/1/1/temperature”). 

Test

Anyway, with those changes done its time to test. Here we see log showing CAN interface messages coming in.

CAN MQTT log

Following each CAN messages we see an MQTT publish success – in this case there have been 21,100+ published messages since start up.

Results

So CAN to MQTT is publishing, but how can we see the data – easy enough. The old perl script just needed a new subscribe string to take into account the data coming from can and radio, so here it is. Notice the “+/+/+” replacing the old “radio/+/+”

I still have to maunally create the RRD databases and add the extra graph generation script entries but its working.

The infrastructure needed to get CAN messages into MQTT is working, ready for something perhaps a bit more useful.

 

 

MQTT and RRDTool

I mentioned in one of my earlier posts that Xively was nice as a quick test setup, but in the end I would like to have my data locally.

To this end I have installed Mosquitto as an MQTT server on a spare Linux machine in the office.  I changed the nRF receiver Arduino to send the sensor data to topics in Mosquitto. I used the PubSubClient for the MQTT connection.

The format of the topics are

“/sensors/radio/<device-id>/<sensor-number>/<type>”

device-id is the address of the remote sensor device

sensor-number is the sensor on the device 1, 2, 3 … etc

type is the type of the sensor temperature, light, battery, etc

This is working as expected, but the data is more or less thrown away unless we have something to subscribe to it!

So on the subscription side i wanted something up and running quick, so I found this snippet of perl code that subscribes to a topic extracts data from the message content and updates an RRD database. For the moment it just does the temperature data.

I then have a script that runs every so often to create some graphs from the rrd data.

The results can be seen here.