This script will get temperature from Watchport/T and Watchport/H sensors and write it to /var/log/watchport1.log file.
#!/bin/bash # # This script reads the temparature value from Watchport/T Temperature sensor # which is connected to the USB port. # Working on Centos 5 Linux, run by crontab # Change '\rT\r' (Celcius) to '\rTF\r' to get Fahrenheit t_name=T1 # Sensor name t_timesec=5 t_parity=none t_speed=115200 t_file1=/var/log/watchport1.tmp logfile=/var/log/watchport1.log t_USBport=/dev/ttyUSB0 if [ ! -f ${logfile} ]; then echo "Sensor;Date;Time;Temperature">$logfile fi t_time=`date +%H:%M` t_date=`date +%d:%m:%Y` # Remark file is Binary ! (echo -e '\rT\r' ; sleep $t_timesec; ) |cu --parity=$t_parity -l $t_USBport -s $t_speed dir>$t_file1 2>/dev/null t_temperature=`cat -A $t_file1 | grep ^+|sed -e 's/\^.*//'` echo "$t_name;$t_date;$t_time;$t_temperature">>$logfile exit 0
Thanks about this script to Timo Unkuri.
If you don’t get temperature to your log file you might have same problem which I had. I tried to talk directly to sensor with cu:
cu --parity=none -l /dev/ttyUSB0
and just got this kind of error:
cu: open (/dev/ttyUSB0): Permission denied cu: /dev/ttyUSB0: Line in use
Only way that I found to fix this was change ownership of /dev/ttyUSB0 to uucp:
chown uucp /dev/ttyUSB0
after that script started to work perfectly.
If you want to run this script example every 5 minute just write to /etc/crontab:
*/5 * * * * /where/is/your/script.sh
this will give u the humidity too for those with the watchport/H
i’ve also slightly improved the pattern matching in the script
#!/bin/bash
# # This script reads the temparature value from Watchport/T Temperature sensor
# which is connected to the USB port.
# Working on Centos 5 Linux, run by crontab
# Change ‘\rT\r’ (Celcius) to ‘\rTF\r’ to get Fahrenheit
t_name=T1 # Sensor name
t_timesec=5
t_parity=none
t_speed=115200
t_file1=/var/log/watchport1.tmp
logfile=/var/log/watchport1.log
t_USBport=/dev/ttyUSB0
if [ ! -f ${logfile} ]; then
echo “Sensor;Date;Time;Temperature;Humidity”>$logfile
fi
t_time=`date +%H:%M`
t_date=`date +%d:%m:%Y`
# Remark file is Binary !
(echo -e ‘\rTCH\r’ ; sleep $t_timesec; ) |cu –parity=$t_parity -l $t_USBport -s $t_speed dir>$t_file1 2>/dev/null
t_temperature=`cat -A $t_file1 | grep -e ^+ -e ^- | sed -e ‘s/\^.*//’`
h_humidity=`cat -A $t_file1 | grep -e ^+ -e ^- | sed -e ‘s/.*[C|F]?*\^M//’|sed -e ‘s/\^M.*//’`
echo “$t_name;$t_date;$t_time;$t_temperature;$h_humidity”>>$logfile
exit 0
Hello mates, how is everything, and what you desire to say regarding this
piece of writing, in my view its really amazing designed for me.
Hey,
This seems not working at all. I tried it out on Raspbian and I got this:
root@teardrop:/var/log# cat watchport1.log
Sensor;Date;Time;Temperature
T1;18:01:2014;00:48;
root@teardrop:/var/log# cat watchport1.tmp
Connected.
Connected.
Disconnected.
root@teardrop:/var/log#
No real temperature values returned.
root@teardrop:/var/log# ls -la /dev/ttyUSB*
lrwxrwxrwx 1 root root 7 Jan 1 1970 /dev/ttyUSB.Ebedlo -> ttyUSB1
lrwxrwxrwx 1 root root 7 Jan 1 1970 /dev/ttyUSB.Haloszoba -> ttyUSB0
lrwxrwxrwx 1 root root 7 Jan 1 1970 /dev/ttyUSB.Nappali -> ttyUSB2
crw-rw—- 1 uucp dialout 188, 0 Jan 18 00:48 /dev/ttyUSB0
crw-rw—- 1 uucp dialout 188, 1 Jan 18 00:46 /dev/ttyUSB1
crw-rw—- 1 uucp dialout 188, 2 Jan 18 00:22 /dev/ttyUSB2
Could you please help me with this issue?
I have had some problems with Raspberry Pi and watchport too :-/ same script seems to work on desktop/laptop PC but when I try it on raspberry pi it does something weird.
Thanks for this script. I couldn’t get it to work properly, but it was the main basis for my own script. I have a Digi Watchport /H that does both temp and humidity. My problem was the output from the Watchport terminated with a CTRL+G (ASCII “bell”) and CTRL+M (DOS/Win newline) characters. When I used Linux “cat” command, the ^G was killing the temp and humid readings on a line-by-line basis. I ultimately piped the output to “tr -d ‘[:cntrl:]'” and that got rid of the control characters.
I added some further refinements so I could use MRTG to graph T/H in my apartment. It’s working really well thanks to your help!
http://ibm.vardaman.org/sens.html
Great blog!! You should start many more. I love all the info provided. I will stay tuned :)
I didn’t really need to log to a file or schedule it. I basically just wanted to create a nagios checks. I used the script above, and modified it to create nagios scripts for checking temperature & humidity, and it really didn’t take much.
Basically, I made the following modifications
-check permissions on /dev/ttyUSB0, since this seemed to be an issue on my system
If [ -w “/dev/ttyUSB0”]…then….fi
-check to see if the script was already running, and wait until the previous check completed.
SERVICE=`ps ax | grep -v grep | grep $t_USBport`
while [ “$SERVICE” ]
do
sleep
done
-check to see if output temperature or humidity was within a range.
if test $t_temperature -gt $HWARN -o $t_temperature -lt $LWARN
then
if test $t_temperature -gt $HCRIT -o $t_temperature -lt $LCRIT
Works great but in the code above all the instances of ‘>’ should be replaced by ‘>’. Somehow your text got mangled when you posted it.
I also had to compile the io_ti.ko module to get usbserial to recognize my Watchport/H.
And ofcourse a
while true
do
.
sleep 60
done
around your code and starting it in de background:
./get-data.sh &
will log the temperature every minute.
Thanks!