can2mqtt_tuc_2

package module
v0.0.0-...-d07d911 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 14 Imported by: 0

README

can2mqtt_tuc

Major Update -> all payload now as JSON !!

Modified for Drillbotics can2mqtt is a small piece of software written in Go. Its purpose is to be a bridge between a CAN-Bus and a MQTT-Broker. Those are completely different worlds but they have similiaritys in the way they are built. I think i don't have to speak about the differences so i will just pick up the similiarities: In the CAN-world you have so called CAN-Frames. Each CAN-Frame can contain up to eight bytes of payload and CAN-Frame has an ID. In the MQTT-world you have topics and messages. Each message has a specific topic. As you can see it should be possible to map CAN-IDs to MQTT-Topics and their respective payload to messages. That's what this little programm does.

Installation

First install can utils

sudo nano /boot/config.txt Add this line to the file:

dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000

https://crycode.de/can-bus-am-raspberry-pi

https://www.e-tinkers.com/2019/06/better-way-to-install-golang-go-on-raspberry-pi/

You need to have go installed. After that you should be able to get can2mqtt to run with the following commands:

$ mkdir ~/go
$ export GOPATH=~/go
$ go install github.com/schollie1000/can2mqtt_tuc/can2mqtt_tuc@latest

After that you should have a runnable binary under $GOPATH/bin/ called can2mqtt.

Usage

The commandline parameters are the following:

can2mqtt -f <can2mqtt.csv> -c <can-interface> -m <mqtt-connectstring> [-v]

Where can2mqtt.csv is the file for the configuration of can and mqtt pairs, can-interface is a socketcan interface and mqtt-connectstring is string that is accepted by the eclipse paho mqtt client. An additional -v flag can be passed to get verbose debug output. Here an example that runs on our Raspberry Pi @c3RE:

can2mqtt -f /etc/can2mqtt.csv -c can0 -m tcp://127.0.0.1:1883

Screenshot

Here you can see can2mqtt in action: can2mqtt screenshot

can2mqtt.csv

The file can2mqtt.csv has three columns. In the first column you need to specify the CAN-ID as a decimal number. In the second column you have to specify the convert-mode. You can find a list of available convert-modes below. In the last column you have to specify the MQTT-Topic. Each CAN-ID and each MQTT-Topic is allowed to appear only once in the whole file.

Here again the example from the Pi@c3RE:

112,none,huette/all/a03/door/sensors/opened
113,2uint322ascii,huette/all/000/ccu/sensors/time
115,uint322ascii,huette/serverraum/000/filebitch/sensors/ftp_diskusage_percent
116,uint322ascii,huette/all/000/router/sensors/rx_bytes_s
117,uint322ascii,huette/all/000/router/sensors/tx_bytes_s
118,uint322ascii,huette/clubraum/000/ds18b20/sensors/temperatur
119,uint322ascii,huette/all/000/airmonitor/sensors/temp
120,uint322ascii,huette/all/000/airmonitor/sensors/hum
121,uint322ascii,huette/all/000/airmonitor/sensors/airq
122,uint322ascii,huette/all/000/airmonitor/sensors/pm2_5
123,uint322ascii,huette/all/000/airmonitor/sensors/pm10

Explanation for the 1st Line: For example our Doorstatus is published on the CAN-Bus every second with the CAN-ID 112 (decimal). can2mqtt will take everything thats published there and will push it through to mqtt-topic huette/all/a03/door/sensors/opened.

convert-modes

Here they are:

none

does not convert anything. It just takes a bunch of bytes and hands it over to the other side. If you want to send strings, this will be your choice.

uint82ascii / uint162ascii / uint322ascii / uint642ascii

On the can2mqtt way it takes 1, 2, 4 or 8 byte and interprets it as an uint of that size and parses it to a human readable string for the mqtt side. The other way round this convert motde takes an int in a string representation and sends out an array of bytes representing that number (little-endian)

2uint322ascii

This one is a bit special but all it does is that it takes 8 bytes from the CAN-Bus and parses two uint32s out of it and sends them in a string representation to MQTT. The two numbers are seperated with a simple space(" "). MQTT2CAN-wise it takes two string representations of numbers and converts them to 8 bytes representing them as 2 uint32.

bytecolor2colorcode

Converts an bytearray of 3 bytes to hexadecimal colorcode

pixelbin2ascii

This mode was designed to adress colorized pixels. MQTT-wise you can insert a string like "<0-255> #RRGGBB" wich will be converted to 4 byte on the CAN-BUS the first byte will be the number of the LED 0-255 and bytes 1, 2, 3 are the color of red, green and blue.

Documentation

Overview

Package can2mqtt contains some tools for briding a CAN-Interface and a mqtt-network

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetC2mf

func SetC2mf(f string)

SetC2mf expects a string which is a path to a can2mqtt.csv file Default is: can2mqtt.csv

func SetCi

func SetCi(c string)

SetCi sets the CAN-Interface to use for the CAN side of the bridge. Default is: can0.

func SetCs

func SetCs(s string)

SetCs sets the MQTT connectstring which contains: protocol, hostname and port. Default is: tcp://localhost:1883

func SetDbg

func SetDbg(v bool)

SetDbg decides wether there is really verbose output or just standard information output. Default is false.

func Start

func Start()

Start is the function that should be called after debug-level connectstring, can interface and can2mqtt file have been set. Start takes care of everything that happens after that. It starts the CAN-Bus connection and the MQTT-Connection. It parses the can2mqtt.csv file and from there everything takes its course...

Types

This section is empty.

Directories

Path Synopsis
minimal main package, only commandline argument parsing and a printHelp() function
minimal main package, only commandline argument parsing and a printHelp() function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL