To monitor some important values from Duet Trikarus project is going to monitor different values like MCU temperature or general printer status. This is done by triggering M408 S4 GCode. In Repetier Server a callback function is registered to monitor this command and append the output to the callback log / websocket stream. A bash script takes this information and parses it. The parsed data will be pushed into InfluxDB. The data is graphically evaluated by Grafana instance.


Create Repetier Server callback

The bash script

vim /opt/duet_status.sh
#!/bin/bash
# this script will read the current IP Adress by using a pre-defined custom event in Repetier Server. Please see documentation for more details on how to do that

PID_FILE="/opt/duet_status.pid"

case "$1" in
    start)
    #get the scripts own PID number
    echo $$>"$PID_FILE"
        while true; do
                STATUS="-999" #reset status because we are in a while loop
                #authenticate
                curl --silent "yourduet/rr_connect?password=somePassword" > /dev/null

                if [ $? == 7 ]; then
                                STATUS="-2" #network connection could not be established
                fi

                #get status response from Duet (note that if previous authentication is some seconds too old it will fail because you need to re-authenticate) - regular output is "{"err":0,"sessionTimeout":8000,"boardType":"duetethernet102"}"
                LOG_LINE=$(curl --silent "yourduet/rr_status?type=2")
                #echo $LOG_LINE

                #status mapping (InfluxDB cannot store characters):
                # -1=O=Offline
                # 0=I=idle
                # 1=P=printing from SD card
                # 2=S=stopped (i.e. needs a reset)
                # 3=C=running config file (i.e starting up)
                # 4=A=paused
                # 5=D=pausing
                # 6=R=resuming from a pause
                # 7=B=busy (e.g. running a macro)
                # 8=F=performing firmware update

                # Warning: the following lines will fail if the confiuration of firmware changes (number of hotends for example)
                if [[ ! $STATUS == "-2" ]]; then
                        STATUS=$(jq -r '.|{status}|.status' <<< ${LOG_LINE})
                fi

                MCU_TEMP=$(jq -r '.|{temps}|.[]|{extra}|.[]|.[]|.temp' <<< ${LOG_LINE})
                HOTEND_ACTIVE=$(jq -r '.|{temps}|.[]|{tools}|.[]|{active}|.[]|.[]|.[]' <<< ${LOG_LINE})
                HOTEND_TEMP=$(jq -r '.|{temps}|.[]|{current}|.[]|.[0]' <<< ${LOG_LINE})
                SPEEDFACTOR=$(jq -r '.|{params}|.[]|{speedFactor}|.speedFactor' <<< ${LOG_LINE})
                EXTRFACTOR=$(jq -r '.|{params}|.[]|{extrFactors}|.[]|.[0]' <<< ${LOG_LINE})
                BABYSTEP=$(jq -r '.|{params}|.[]|{babystep}|.[]' <<< ${LOG_LINE})
                VIN=$(jq -r '.|.vin|{cur}|.[]' <<< ${LOG_LINE})
                Z_COORD=$(jq -r '.|{coords}|.[]|{xyz}|.[]|.[2]' <<< ${LOG_LINE})

                #now replace Status with integer values for InfluxDB
                STATUS=$(echo ${STATUS/O/-1})
                STATUS=$(echo ${STATUS/I/0})
                STATUS=$(echo ${STATUS/P/1})
                STATUS=$(echo ${STATUS/S/2})
                STATUS=$(echo ${STATUS/C/3})
                STATUS=$(echo ${STATUS/A/4})
                STATUS=$(echo ${STATUS/D/5})
                STATUS=$(echo ${STATUS/R/6})
                STATUS=$(echo ${STATUS/B/7})
                STATUS=$(echo ${STATUS/F/8})
                STATUS=$(echo ${STATUS/T/9})
                STATUS=$(echo ${STATUS/S/10})
                STATUS=$(echo ${STATUS/H/11})

                # note that space white characters destroy the --data-binary. So watch them exactly!
                # if errors occure values might need to be converted from int to float! (missing yet)
                if [[ $STATUS == "-2" ]]; then
                                #echo "Duet is not connected by LAN"
                                #MCU_TEMP=99999
                                #HOTEND_ACTIVE=99999
                                #HOTEND_TEMP=99999
                                #SPEEDFACTOR=99999
                                #EXTRFACTOR=99999
                                #BABYSTEP=99999
                                #VIN=99999
                                #Z_COORD=99999

                # note that space white characters destroy the --data-binary. So watch them exactly!
                                curl --silent -k -XPOST "http://localhost:8086/write?db=trikarus" --data-binary "duet_ethernet,host=hangdevice.fablabchemnitz.de status=${STATUS}" --user dbuser:dbpassword > /dev/null
                else
                                #echo "Writing regular Duet values to InfluxDB"
                                echo STATUS=$STATUS
                                echo MCU_TEMP=$MCU_TEMP
                                echo HOTEND_ACTIVE=$HOTEND_ACTIVE
                                echo HOTEND_TEMP$HOTEND_TEMP
                                echo SPEEDFACTOR=$SPEEDFACTOR
                                echo EXTRFACTOR=$EXTRFACTOR
                                echo BABYSTEP=$BABYSTEP
                                echo VIN=$VIN
                                echo Z_COORD=$Z_COORD

                                curl --silent -k -XPOST "http://localhost:8086/write?db=trikarus" --data-binary "duet_ethernet,host=hangdevice.fablabchemnitz.de status=${STATUS},mcu_temp=${MCU_TEMP},hotend_temp=${HOTEND_TEMP},speedfactor=${SPEEDFACTOR},extrfactor=${EXTRFACTOR},babystep=${BABYSTEP},vin=${VIN},z_coord=${Z_COORD}" --user dbuser:dbpassword> /dev/null
                fi
                sleep 1 #wait to generate not too much data
        done
;;

    stop)
    pkill -P `cat "$PID_FILE"`
    rm "$PID_FILE"
;;

    restart)
    $0 stop
    $0 start
;;

    status)
    if [ -e "$PID_FILE" ]; then
            echo Service is still running, pid=`cat "$PID_FILE"`
    else
            echo Service is NOT running
    exit 1
    fi
;;

    *)
    echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0
 
chmod +x /opt/duet_status.sh

Install as service

vim /opt/duet_status.service
[Unit]
After=network.target
Description=Duet Ethernet Controller Status Service

[Service]
Type=simple
ExecStart=/opt/duet_status.sh start
ExecStop=/opt/duet_status.sh stop
KillMode=process
Restart=on-failure
RestartSec=10
RemainAfterExit=no
User=root
Group=root

[Install]
WantedBy= multi-user.target
systemctl enable /opt/duet_status.service
service duet_status restart && journalctl -f -u duet_status.service

Dropping old values

influx

use trikarus
drop series from duet_ethernet
show series
show measurements

Create Grafana dashboard

(info) Some old example screenshot. Recent version of the dashboard is much more versatile

  • No labels
Write a comment…