@ DIeHarke Sure,
Here’s what I did.
I have a Raspberry Pi3 with raspbian installed close to my sugar valley computer.
I already used this Pi to share an RFXCOM controller with my domoticz server using ser2net.
(basically to share a USB module over ethernet)
In order to read the sugar valley’s modbus data, I bought this USB adapter from aliexpress (ch341).
FYI, I only want to read MODBUS data, not write it.
Use the “extern” RS485 connector on the sugar valley computer (right side).
On this connector you need PIN 3 and 4.
PIN 3 = white wire
PIN 4 = red wire
PIN3-white goes to ch341 PIN A
PIN4-red goes to ch341 PIN B
Connect the ch341 to the Pi, and install mbpoll
wget -O- http://www.piduino.org/piduino-key.asc | sudo apt-key add -
echo 'deb http://raspbian.piduino.org stretch piduino' | sudo tee /etc/apt/sources.list.d/piduino.list
sudo apt update
sudo apt install mbpoll
Find the USB ID using “dmesg | grep tty”
Use mbpoll to read a few registers, in order to test connectivity:
mbpoll -m rtu -b 19200 -t 4 -r 256 /dev/ttyUSB1 -P none -1 -c 31 -q -0
In this example, I read register 256 + the next 31 registers.
-0 is important here, because sugar valley starts with register 0, not register 1 like most devices do.
Then I created this script to import only the values I need, to pre-created domoticz sensors.
(I know my scripting skills are limited, but they do what I need so far )
mbpoll -m rtu -b 19200 -t 4 -r 100 /dev/ttyUSB1 -P none -1 -c 31 -q -0 > /steegy/vistapool-modbusrtu-domot3/poll-part1
mbpoll -m rtu -b 19200 -t 4 -r 256 /dev/ttyUSB1 -P none -1 -c 31 -q -0 >> /steegy/vistapool-modbusrtu-domot3/poll-part1
mbpoll -m rtu -b 19200 -t 4 -r 1000 /dev/ttyUSB1 -P none -1 -c 31 -q -0 >> /steegy/vistapool-modbusrtu-domot3/poll-part1
mbpoll -m rtu -b 19200 -t 4 -r 1031 /dev/ttyUSB1 -P none -1 -c 31 -q -0 >> /steegy/vistapool-modbusrtu-domot3/poll-part1
mbpoll -m rtu -b 19200 -t 4 -r 1280 /dev/ttyUSB1 -P none -1 -c 31 -q -0 >> /steegy/vistapool-modbusrtu-domot3/poll-part1
##remove spaces and tabs from export file
cat /steegy/vistapool-modbusrtu-domot3/poll-part1 | tr -d "[:blank:]" > /steegy/vistapool-modbusrtu-domot3/poll-part2
############################################
# update Pool Hydrolysis [257] - IDX2537
###########################################
hydrosv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[257\]' | sed 's/^.*://')
if [ -z "$hydrosv" ]
then
echo "empty var not doing any update"
else
hydrosv="$(( hydrosv / 10 ))"
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2537&nvalue=0&svalue=$hydrosv"
fi
############################################
# update Pool Current pH [258] - IDX2524
###########################################
phsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[258\]' | sed 's/^.*://')
if [ -z "$phsv" ]
then
echo "empty var not doing any update"
else
phsv=$(echo "scale=2; $phsv /100" | bc)
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2524&nvalue=0&svalue=$phsv"
fi
############################################
# update current Redox [259] - IDX2522
############################################
redoxcurrentsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[259\]' | sed 's/^.*://')
if [ -z "$redoxcurrentsv" ]
then
echo "empty var not doing any update"
else
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2522&nvalue=0&svalue=$redoxcurrentsv"
fi
############################################
# update temperature [262] - IDX2514
############################################
tempsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[262\]' | sed 's/^.*://')
if [ -z "$tempsv" ]
then
echo "empty var not doing any update"
else
tempsv=$(echo "scale=1; $tempsv /10" | /usr/bin/bc)
### temp offset = 1.7 degrees
temp2sv=$(echo "scale=2; $tempsv +1.7" | bc)
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2514&nvalue=0&svalue=$temp2sv"
fi
############################################
# update Pool Target pH [1284] - IDX2525
############################################
targetphsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[1284\]' | sed 's/^.*://')
if [ -z "$targetphsv" ]
then
echo "empty var not doing any update"
else
targetphsv=$(echo "scale=2; $targetphsv /100" | bc)
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2525&nvalue=0&svalue=$targetphsv"
fi
############################################
# update target Redox [1288] - IDX2523
############################################
redoxtargetsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[1288\]' | sed 's/^.*://')
if [ -z "$redoxtargetsv" ]
then
echo "empty var not doing any update"
else
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2523&nvalue=0&svalue=$redoxtargetsv"
fi
############################################
# update Pool Filtration Status [1057] - IDX2518
############################################
filtrationstatsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[1057\]' | sed 's/^.*://')
if [ -z "$filtrationstatsv" ]
then
echo "empty var not doing any update"
else
if [ $filtrationstatsv = "0" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2518&nvalue=0&svalue=Off"
elif [ $filtrationstatsv = "1" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2518&nvalue=0&svalue=On"
fi
fi
############################################
# update Pool Filtration Mode [1041] - IDX2519
############################################
filtrationmodesv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[1041\]' | sed 's/^.*://')
if [ -z "$filtrationmodesv" ]
then
echo "empty var not doing any update"
else
if [ $filtrationmodesv = "0" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2519&nvalue=0&svalue=Manual"
elif [ $filtrationmodesv = "1" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2519&nvalue=0&svalue=Auto"
else
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2519&nvalue=0&svalue=UNKNOWN"
fi
fi
############################################
# update Pool Pump Flow Status [269] - IDX2711
## get bit number 3 (0x0008)
## 1 = flow running
## 0 = flow stopped
###########################################
flowsv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[269\]' | sed 's/^.*://')
echo `date '+%d/%m/%Y_%H:%M:%S'` $flowsv >> /steegy/vistapool-modbusrtu-domot3/flow-log
if [ -z "$flowsv" ]
then
echo "empty var not doing any update"
else
##echo "$flowsv"
##flowsvbinary=$(echo "obase=2;$flowsv" | bc)
##echo "$flowsvbinary"
##flowsvhex=$(echo "obase=16;$flowsv" | bc)
##echo "$flowsvhex"
flowsv=$((("$flowsv" & 0x0008) != 0))
##echo "$flowsv"
if [ $flowsv = "1" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2711&nvalue=1"
echo "flow running" >> /steegy/vistapool-modbusrtu-domot3/flow-log
elif [ $flowsv = "0" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=2711&nvalue=0"
echo "flow stopped" >> /steegy/vistapool-modbusrtu-domot3/flow-log
fi
fi
############################################
# update Pool Cover [Also 269] - IDX4005
# get bit number 4 (0x0010)
# 0 = cover open
# 1 = cover closed
############################################
coversv=$(cat /steegy/vistapool-modbusrtu-domot3/poll-part2 | grep '\[269\]' | sed 's/^.*://')
if [ -z "$coversv" ]
then
echo "empty var not doing any update"
else
coversv=$((("$coversv" & 0x0010) != 0))
##echo "$coversv"
if [ $coversv = "0" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=4005&nvalue=0&svalue=Open"
echo "cover opened" >> /steegy/vistapool-modbusrtu-domot3/flow-log
elif [ $coversv = "1" ];then
curl "http://domoticz.steegy.com/json.htm?type=command¶m=udevice&idx=4005&nvalue=0&svalue=Closed"
echo "cover closed" >> /steegy/vistapool-modbusrtu-domot3/flow-log
fi
fi
In case you want to add additional registers, you can get the register numbers from the sugar valley modbus PDF manual, just remeber to translate HEX to decimal first.
But in my case, these are the most important registers I need, in order to get my winter-anti-freeze protection automation up and running, which is automated through domoticz.
My first attempt was to share the ch341 USB adapter using ser2net, with my domoticz server, but this was unsuccessful, so I decided to have the Pi run the mbpoll script (or optinally, you can call the mbpoll export though SSH)
FYI, to explain the “-z $flowsv” routine:
I have about a 1/100 chance that a modbus read fails, in case it does, and my variables are empty, I want to prevent the script to update my domoticz sensors, and wait untill the next successful run to update the sensor.