Dallas DS18B20 temp sensor(s) into Domoticz

Moderator: leecollings

Post Reply
User avatar
ILoveIOT
Posts: 59
Joined: Sunday 25 March 2018 17:47
Target OS: Linux
Domoticz version: 2020.2
Location: NL
Contact:

Dallas DS18B20 temp sensor(s) into Domoticz

Post by ILoveIOT »

Code: Select all

#!/bin/bash
INFO="Orange/Raspberry PI Dallas DS18B20 temp sensor(s), get value(s), and send to Domoticz every 5 minutes"
#
# Created by ILoveIoT 2019
# 
# For console use     : /root/scripts/gpio/dallas.temp.reading.sh
# For background use  : /root/scripts/gpio/dallas.temp.reading.sh &
#
# Set in /boot/armbianEnv.txt 
# dtoverlay=w1-gpio 
# overlays=w1-gpio
#
# GPIO1 is used in this example for reading the Dallas DS18B20
#
# Use : gpio readall
# | Physical 12 | V1 | Mode IN | Name GPIO.1 | wPi 1 | BCM 110 |
#
# Uncomment/remove lines if you not using 4 Dallas device's
#


# Set the Domoticz server & port
SERVERIP="192.168.8.189"
SERVERPORT="8080"


# Set the Domoticz IDX for you're dummy temp device, check device's inside Domoticz for you're IDX
# Dallas Sensor at the breadboard high
DALLAS1IDX="88"
# Dallas Sensor kitchen low
DALLAS2IDX="35"
# Dallas Boiler sensor
DALLAS3IDX="87"
# Dallas shower sensor
DALLAS4IDX="36"
# Dallas shower sensor
DALLAS5IDX="283"


# Use : ls /sys/bus/w1/devices/
#
# For getting the id for the device's, if you see none, something is wrong, (check /boot/armbianEnv.txt, dtoverlay=w1-gpio, overlays=w1-gpio)
# Dallas Sensor at the breadboard high
DALLAS1="28-7187391e64ff"
# Dallas Sensor kitchen low
DALLAS2="28-000004b4abc3"
# Dallas Boiler sensor
DALLAS3="28-0315902d93ff"
# Dallas shower sensor
DALLAS4="28-000004b413dc"
# Dallas accu sensor
DALLAS5="28-03159062bcff"

# Time for repeated sensor reading, in seconds
SLEEPTIME="120"
#SLEEPTIME="60"


# Use : dmesg | grep w1
#
# You should get a output like below, if get get abnormal readings to low/high check the cables of resistor (4.7-10k)
# 
#[    6.220359] w1_master_driver w1_bus_master1: Attaching one wire slave 28.dac81d1e64ff crc da
#[  753.961177] w1_master_driver w1_bus_master1: Attaching one wire slave 28.6483391e64ff crc 45
#[ 2908.310134] w1_master_driver w1_bus_master1: Attaching one wire slave 28.7187391e64ff crc dc

####################################################################################################
#################################### BEGIN SCRIPT ##################################################
####################################################################################################


# Going into the loop, so you will have to run it once
while true; do


	# Get the sensor temp value
	SENSORTEMP1=`cat /sys/bus/w1/devices/$DALLAS1/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}'`
	SENSORTEMP2=`cat /sys/bus/w1/devices/$DALLAS2/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}'`
	SENSORTEMP3=`cat /sys/bus/w1/devices/$DALLAS3/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}'`
	SENSORTEMP4=`cat /sys/bus/w1/devices/$DALLAS4/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}'`
	SENSORTEMP5=`cat /sys/bus/w1/devices/$DALLAS5/w1_slave | sed -n 's/^.*\(t=[^ ]*\).*/\1/p' | sed 's/t=//' | awk '{x=$1}END{print(x/1000)}'`
	
	
	
	# Just for debugging, you can comment these lines out if it works
	#LS=`ls /sys/bus/w1/devices/ | grep 28`
	#DMESG=`dmesg | grep w1`
	#clear
	#echo "$INFO"
	#echo ""
	#echo "Output off command dmesg | grep w1"
	#echo "$DMESG"
	#echo ""
	#echo "Output off command /sys/bus/w1/devices/ | grep 28"
	#echo "$LS"
	#echo ""
	#echo "Sensor ID = $DALLAS1 IDX = $DALLAS1IDX Temp = $SENSORTEMP1 C" 
	#echo "Sensor ID = $DALLAS2 IDX = $DALLAS2IDX Temp = $SENSORTEMP2 C"
	#echo "Sensor ID = $DALLAS3 IDX = $DALLAS3IDX Temp = $SENSORTEMP3 C"
	#echo "Sensor ID = $DALLAS4 IDX = $DALLAS4IDX Temp = $SENSORTEMP4 C"
	#echo "Sensor ID = $DALLAS5 IDX = $DALLAS5IDX Temp = $SENSORTEMP5 C"
	#echo ""
	#echo ""
	#current_date_time="`date +%H:%M:%S`";
	#echo "$0 @ $current_date_time"
	

	# Send the value to the Domoticz server, dont send if its 0 or more then 99 or less then -40, maybe you get false readings
	if (( $(awk 'BEGIN {print ("'$SENSORTEMP1'" > "0")}') && $(awk 'BEGIN {print ("'$SENSORTEMP1'" < "99")}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS1IDX&nvalue=0&svalue=$SENSORTEMP1" >> /dev/null 2>&1
	else 
		if (( $(awk 'BEGIN {print ("'$SENSORTEMP1'" < 0)}') && $(awk 'BEGIN {print ("'$SENSORTEMP1'" < -40)}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS1IDX&nvalue=0&svalue=$SENSORTEMP1" >> /dev/null 2>&1
		fi
	fi
	
	if (( $(awk 'BEGIN {print ("'$SENSORTEMP2'" > "0")}') && $(awk 'BEGIN {print ("'$SENSORTEMP2'" < "99")}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS2IDX&nvalue=0&svalue=$SENSORTEMP2" >> /dev/null 2>&1
	else 
		if (( $(awk 'BEGIN {print ("'$SENSORTEMP2'" < 0)}') && $(awk 'BEGIN {print ("'$SENSORTEMP2'" < -40)}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS2IDX&nvalue=0&svalue=$SENSORTEMP2" >> /dev/null 2>&1
		fi
	fi

	if (( $(awk 'BEGIN {print ("'$SENSORTEMP3'" > "0")}') && $(awk 'BEGIN {print ("'$SENSORTEMP3'" < "99")}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS3IDX&nvalue=0&svalue=$SENSORTEMP3" >> /dev/null 2>&1
	else 
		if (( $(awk 'BEGIN {print ("'$SENSORTEMP3'" < 0)}') && $(awk 'BEGIN {print ("'$SENSORTEMP3'" < -40)}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS3IDX&nvalue=0&svalue=$SENSORTEMP3" >> /dev/null 2>&1
		fi
	fi

	if (( $(awk 'BEGIN {print ("'$SENSORTEMP4'" > "0")}') && $(awk 'BEGIN {print ("'$SENSORTEMP4'" < "99")}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS4IDX&nvalue=0&svalue=$SENSORTEMP4" >> /dev/null 2>&1
	else 
		if (( $(awk 'BEGIN {print ("'$SENSORTEMP4'" < 0)}') && $(awk 'BEGIN {print ("'$SENSORTEMP4'" < -40)}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS4IDX&nvalue=0&svalue=$SENSORTEMP4" >> /dev/null 2>&1
		fi
	fi
	
	if (( $(awk 'BEGIN {print ("'$SENSORTEMP5'" > "0")}') && $(awk 'BEGIN {print ("'$SENSORTEMP5'" < "99")}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS5IDX&nvalue=0&svalue=$SENSORTEMP5" >> /dev/null 2>&1
	else 
		if (( $(awk 'BEGIN {print ("'$SENSORTEMP5'" < 0)}') && $(awk 'BEGIN {print ("'$SENSORTEMP5'" < -40)}') )); then
		curl -i -s "http://$SERVERIP:$SERVERPORT/json.htm?type=command&param=udevice&idx=$DALLAS5IDX&nvalue=0&svalue=$SENSORTEMP5" >> /dev/null 2>&1
		fi
	fi

	
	# sleep for 5 minutes, en then restart the process
	# sleep 300
	sleep $SLEEPTIME

	
	
	
done


##_EOF_##
1x OPI PC 5.8.16 Debian 10, MiniDNLA,SMD230,EpEver,MPD,800GB,Wiegand,7 temps,DHT,32/64 I/O,2022.1
2x OPI PC 4.19.25 Debian 9, MPD,7 temps,16/32 I/O,BMP,4.10717
1x IntelNUC 5.10 Debian 10, 2TB,DalyBMS,2023.1
3x ESPEasy NodeMCU V3, 16/16 I/O
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest