Skip to content

Instantly share code, notes, and snippets.

@TheRayTracer
Created November 8, 2015 11:20
Show Gist options
  • Save TheRayTracer/2d6fc8730284c26ecf15 to your computer and use it in GitHub Desktop.
Save TheRayTracer/2d6fc8730284c26ecf15 to your computer and use it in GitHub Desktop.
This entry contributes a simple Bash script to query and control a LM75 Digital Temperature Sensor via I2C. The second Bash script is a utility to interact with a real-time clock (RTC). The third entry is a Python script for a mini web server to retrieve the temperature in either Celsius (default) or Fahrenheit. These were a few very fun weekend…
#!/bin/bash
readonly lm75=0x48 # Slave address with A0, A1, and A2 set to 0v (ground).
while [ $# -gt 0 ]
do
if [ "$1" == "-debug" ]
then
debug=true
fi
if [ "$1" == "-on" ]
then
i2cset -y -m 0x01 1 $lm75 0x01 0x00 b
fi
if [ "$1" == "-off" ]
then
i2cset -y -m 0x01 1 $lm75 0x01 0x01 b
fi
if [ "$1" == "-f" ]
then
fahrenheit=true
fi
if [ "$1" == "-reset" ]
then
i2cset -y 1 $lm75 0x01 0x00 b
i2cset -y 1 $lm75 0x02 0x004B w
i2cset -y 1 $lm75 0x03 0x0050 w
fi
if [ "$1" == "-set" ] # Bash shell does not support floating point types - please avoid floating point input.
then
shift
tos=$1
shift
thy=$1
if [ $tos -gt $thy ]
then
tos=$(($tos & 0xffff))
tos=$(echo "$tos / 0.125" | bc)
t=$(($tos << 5))
t=$(($t & 0xffff))
t=$(($(($t << 8)) | $(($t >> 8))))
t=$(($t & 0xffff))
i2cset -y 1 $lm75 0x03 $t w
thy=$(($thy & 0xffff))
thy=$(echo "$thy / 0.125" | bc)
t=$(($thy << 5))
t=$(($t & 0xffff))
t=$(($(($t << 8)) | $(($t >> 8))))
t=$(($t & 0xffff))
i2cset -y 1 $lm75 0x02 $t w
else
echo "Tos register should be greater than Thyst register. Aborted."
exit
fi
fi
shift
done
tmp_raw=$(i2cget -y 1 $lm75 0x00 w)
if [ "$debug" = true ]
then
# Setup for debugging - convert into binary.
D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
cnf_raw=$(i2cget -y 1 $lm75 0x01 b)
thy_raw=$(i2cget -y 1 $lm75 0x02 w)
tos_raw=$(i2cget -y 1 $lm75 0x03 w)
dbg_tmp_lo_byte=${tmp_raw:2:2}
dbg_tmp_hi_byte=${tmp_raw:4:2}
dbg_thy_lo_byte=${thy_raw:2:2}
dbg_thy_hi_byte=${thy_raw:4:2}
dbg_cnf_lo_byte=${cnf_raw:2:2}
dbg_tos_lo_byte=${tos_raw:2:2}
dbg_tos_hi_byte=${tos_raw:4:2}
echo "Raw Temp register: 0x$dbg_tmp_hi_byte 0x$dbg_tmp_lo_byte [${D2B[0x$dbg_tmp_hi_byte]} ${D2B[0x$dbg_tmp_lo_byte]}]"
echo "Raw Conf register: ---- 0x$dbg_cnf_lo_byte [-------- ${D2B[0x$dbg_cnf_lo_byte]}]"
echo "Raw Thyst register: 0x$dbg_thy_hi_byte 0x$dbg_thy_lo_byte [${D2B[0x$dbg_thy_hi_byte]} ${D2B[0x$dbg_thy_lo_byte]}]"
echo "Raw Tos register: 0x$dbg_tos_hi_byte 0x$dbg_tos_lo_byte [${D2B[0x$dbg_tos_hi_byte]} ${D2B[0x$dbg_tos_lo_byte]}]"
fi
adj_neg=0
adj_tmp=$(($(($tmp_raw >> 8)) | $(($tmp_raw << 8))))
adj_tmp=$(($adj_tmp & 0xffff))
# Check if the MSB (of the word) is set to indicate a negitive value
if [[ $(($adj_tmp & 0x8000)) -eq 0x8000 ]]
then
adj_neg=256
fi
adj_tmp=$(($adj_tmp >> 5))
if [ "$fahrenheit" = true ]
then
echo "scale=3; (($adj_tmp * 0.125) - $adj_neg) * (9 / 5) + 32" | bc
else
echo "scale=3; ($adj_tmp * 0.125) - $adj_neg" | bc
fi
#!/bin/bash
readonly rtc=0x51 # Slave address.
readonly alm_flg=0x10
while [ $# -gt 0 ]
do
if [ "$1" == "-debug" ]
then
debug=true
fi
if [ "$1" == "-unload" ]
then
modprobe -r rtc_pcf2127 # Must have admin access.
fi
if [ "$1" == "-load" ]
then
modprobe rtc_pcf2127 # Must have admin access.
fi
if [ "$1" == "-12" ] # Set 12 hour clock.
then
i2cset -y -m 0x04 1 $rtc 0x00 0x04 b
fi
if [ "$1" == "-24" ] # Set 24 hour clock.
then
i2cset -y -m 0x04 1 $rtc 0x00 0x00 b
fi
if [ "$1" == "-clear" ] # Clear the alarm flag.
then
i2cset -y -m 0x10 1 $rtc 0x01 0x00 b
fi
if [ "$1" == "-reset" ] # Disable and zero the alarm.
then
i2cset -y -m 0xFF 1 $rtc 0x0A 0x80 b
i2cset -y -m 0xFF 1 $rtc 0x0B 0x80 b
i2cset -y -m 0xFF 1 $rtc 0x0C 0x80 b
i2cset -y -m 0xFF 1 $rtc 0x0D 0x80 b
i2cset -y -m 0xFF 1 $rtc 0x0E 0x80 b
fi
if [ "$1" == "-set" ] # Set the alarm - hours, minutes, and seconds.
then
shift
hrs=$1
shift
min=$1
shift
sec=$1
i2cset -y -m 0xFF 1 $rtc 0x0A 0x$sec b
i2cset -y -m 0xFF 1 $rtc 0x0B 0x$min b
i2cset -y -m 0xFF 1 $rtc 0x0C 0x$hrs b
fi
shift
done
rtc1_raw=$(i2cget -y 1 $rtc 0x01 b)
if [ "$debug" = true ]
then
# Setup for debugging - convert into binary.
D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
rtc0_raw=$(i2cget -y 1 $rtc 0x00 b)
# rtc1_raw=$(i2cget -y 1 $rtc 0x01 b)
rtc2_raw=$(i2cget -y 1 $rtc 0x02 b)
alm_sec_raw=$(i2cget -y 1 $rtc 0x0A b)
alm_min_raw=$(i2cget -y 1 $rtc 0x0B b)
alm_hrs_raw=$(i2cget -y 1 $rtc 0x0C b)
alm_day_raw=$(i2cget -y 1 $rtc 0x0D b)
alm_wkd_raw=$(i2cget -y 1 $rtc 0x0E b)
echo "RTC control 1 register: $rtc0_raw [${D2B[$rtc0_raw]}]"
echo "RTC control 2 register: $rtc1_raw [${D2B[$rtc1_raw]}]"
echo "RTC control 3 register: $rtc2_raw [${D2B[$rtc2_raw]}]"
echo "ALM seconds register: $alm_sec_raw [${D2B[$alm_sec_raw]}]"
echo "ALM minutes register: $alm_min_raw [${D2B[$alm_min_raw]}]"
echo "ALM hours register: $alm_hrs_raw [${D2B[$alm_hrs_raw]}]"
echo "ALM days register: $alm_day_raw [${D2B[$alm_day_raw]}]"
echo "ALM week day register: $alm_wkd_raw [${D2B[$alm_wkd_raw]}]"
fi
sec_raw=$(i2cget -y 1 $rtc 0x03 b)
min_raw=$(i2cget -y 1 $rtc 0x04 b)
hrs_raw=$(i2cget -y 1 $rtc 0x05 b)
day_raw=$(i2cget -y 1 $rtc 0x06 b)
mth_raw=$(i2cget -y 1 $rtc 0x08 b)
yar_raw=$(i2cget -y 1 $rtc 0x09 b)
sec1_raw=$((${sec_raw:2:1} & 0x7))
sec2_raw=${sec_raw:3:1}
echo "Date (UTC): 20${yar_raw:2:2}-${mth_raw:2:2}-${day_raw:2:2} ${hrs_raw:2:2}:${min_raw:2:2}:${sec1_raw}${sec2_raw}H"
if [[ $(($rtc1_raw & $alm_flg)) -eq $alm_flg ]]
then
echo "Alarm"
fi
#!/usr/bin/env python
import socket
import os
host = ""
port = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(1)
while True:
try:
csock, caddr = sock.accept()
print "Connection from: " + `caddr`
request = csock.recv(1024) # Get the request, 1kB max.
request = request.splitlines()[0]
print request
if request.startswith("GET"):
if request.find("style=f") == -1:
if request.find("debug=true") == -1:
p = os.popen("./lm75a.sh")
else:
p = os.popen("./lm75a.sh -debug")
else:
p = os.popen("./lm75a.sh -f")
t = p.read()
csock.sendall("HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Cache-Control: no-cache, no-store, must-revalidate" + "\r\n" + "Expires$
else:
csock.sendall("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n")
csock.close()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment