Raspian/Debian Thermometer

Hi,

Today’s post is about using the TEMPerV2:

https://www.modmypi.com/raspberry-pi/sensors-1061/temperaturepressurehumidity-1066/temper2-usb-dual-temperature-sensor

My version reports as:

lsusb
Bus 001 Device 004: ID 0b95:1790 ASIX Electronics Corp. AX88179 Gigabit Ethernet
Bus 001 Device 005: ID 413d:2107
Bus 001 Device 006: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I am currently using this with Raspbian in conjunction with a Zabbix server. Installing is as follows:

sudo apt-get install libhidapi-dev libhidapi-hidraw0 cmake git
git clone https://github.com/edorfaus/TEMPered.git

TEMPered/libtempered/temper_type.c needs modifying to work with this device, details be found here.

cd TEMPered 
mkdir build
cd build
cmake ..
make
sudo make install

On Raspbian/Debian the libs need to copied to another location, before TEMPered works as intended:

sudo cp /usr/local/lib/libtempered* /usr/lib/

TEMPered should now be working as intended, for example:

pi@pitemp:~ $ sudo tempered
/dev/hidraw1 0: temperature 26.56 °C

Zabbix

If you are using Zabbix:

https://www.zabbix.com/

like me then you can install zappix-agent

apt-get install zabbix-agent

And add the following to the bottom of: /etc/zabbix/zabbix_agentd.conf

UserParameter=cluster1.roomTemperature,tempered | grep -oP '(?<=/dev/hidraw1 0: temperature) [\d.]+'

Then restart Zabbix-agent:

systemctl restart zabbix-agent

Then add to your Zabbix  Server as an host item and Zabbix agent type.

SNMP (Not tested, but should work)

If your are using Nagios or PRTG, or if prefer SNMP can also use if it with Zabbix; advanced knowledge of SNMP is presumed:

And add the following to the bottom of:  /etc/snmp/snmpd.conf

pass .1.3.6.1.2.1.25.1.8 /bin/sh /usr/local/bin/snmp-cpu-temp

/usr/local/bin/snmp-cpu-temp

#!/bin/sh
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.8
echo gauge
tempered | grep -oP '(?<=/dev/hidraw1 0: temperature) [\d.]+'
fi
exit 0

Key: gauge

SNMP OID: .1.3.6.1.2.1.25.1.8

Edit 10/11/19 – /dev/hidraw1 needs to accessible by non root users.  This should be done with a udev rule, but I couldn’t get this this to work; if any has an answer to this please comment below. Anyway so I fudged it by adding /bin/chmod 666 /dev/hidraw1 to rc.local. On distos running systemd, a systemd unit will probably need to be created and enabled; I won’t advise how to that here as a google search will give results pretty quickly.

Cron

If you don’ want to use a network monitor server you could just set a cronjob to check the temperature and send an email alert if temperature is to high, for example:

sudo crontab -e
*/15 * * * * /bin/bash /home/pi/check-temp.sh

This will check the temperature every 15 minutes and send an alert if required.

/home/pi/check-temp.sh

#!/bin/bash
a=$(/usr/local/bin/tempered | grep -oP '(?<=/dev/hidraw1 0: temperature) [\d.]+')
b=40
export a
if [ $a \> $b ];
then
python /home/pi/alert.py
else
echo "Temperature" $a"c, normal";
fi;
sleep 10
unset a b

Will call /home/pi/alert.py and send alert if temperature is over 40 °c (to change this update b= ).

/home/pi/alert.py

# import necessary packages

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import os

# create message object instance
msg = MIMEMultipart()

message = os.environ.get('a')

# setup the parameters of the message
password = "password"
msg['From'] = "email@example.com"
msg['To'] = "email@example.com"
msg['Subject'] = "Temperature Alert! Value in body is in Centigrade" 

# add in the message body
msg.attach(MIMEText(message, 'plain'))

#create server
server = smtplib.SMTP('mailserver.example.com: 25')

server.starttls()

# Login Credentials for sending the mail
server.login(msg['From'], password)


# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

print "successfully sent email to %s:" % (msg['To'])

Thanks Tom.

P.S Please feel free to comment.

Sources:
https://github.com/edorfaus/TEMPered/issues/51#issuecomment-360364866

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.