Bash script to parse JSON for Phidgets voltage value

  hardware, linux, phidgets, raspberry-pi

Also see: https://wiki.lupsha.com/raspberry-pi-3-setting-up-a-phidgets-voltage-reader-2/

#!/bin/bash
#
# hits the sensorreading app and grabs sensor raw value, and computes it, per phidgets 1117 raw to voltage
#
# Alan Lupsha 10/20/2019
#


# hit the API and get the reading
#
# ex:  {"userCommand":"/sensor/0","errors":[],"workLog":[],"port":{"index":0,"value":584}}
#
READING=`curl --silent -H "Accept: application/json" http://10.1.10.12:50003/sensor/0`
echo "API reading: $READING"

#
# extract the value, ex: 584
#
SENSORREADING=`echo $READING | sed "s/.*value//" | sed 's/[^0-9]*//g'`
echo "Sensor: $SENSORREADING"

#
# don't forget to install the bc calculator: sudo apt-get install bc
#
# compute the value, it's VOLTAGE = raw * 0.06 - 30
VOLTAGE=$(echo $SENSORREADING*0.06-30 | bc )
echo "Voltage: $VOLTAGE"

DESIREDVALUE=4.5
echo "Desired at least: $DESIREDVALUE"


if (( $(echo "$VOLTAGE > $DESIREDVALUE" |bc -l) )); then
  echo "Voltage $VOLTAGE > $DESIREDVALUE so it is good"
else
  echo "Low voltage alarm!"
fi

Testing low voltage alarm by requiring DESIREDVALUE=5.5 VDC

Testing ok voltage by requiring DESIREDVALUE=4.5 VDC