Blog 05

From BitWizard Wiki
Jump to navigation Jump to search

!BETA!

Now the temperature sensor: For making the temperature sensor we have to make 2 script.

Script 1(The name I gave it was: ui):

#!/bin/sh 
 ui="bw_tool -a 94 -I -D /dev/i2c-1" 
 $ui -W 70:c7:b # internal tempsens with internal 1.1V as reference 
 $ui -W 71:c6:b # external tempsens with internal 1.1V as reference 
 $ui -W 81:1000:s 
 $ui -W 82:6:b 
 $ui -W 80:2:b 

After this we make ui executable:

chmod +x ui 

And look if it worked:

ls -l ui 

This result should have -rwxr-xr-x like me: -rwxr-xr-x 1 root root 216 Sep 7 15:46 ui

Ater you see it works activate it:

./ui 

( There will be nothing printed out when you activate it )

Now it's time to make the second script. ( I named it showtemp)

#!/bin/sh
# 
#
# Set the number of samples to 64, and shift by 0, or set the 
# number of samples to 4096 and shift by 6. The latter is recommended!
#
adcmaxval=65520
#adcmaxval=1023
#
# MCP9700 degrees per volt. 
vperdeg=0.010
#refvoltage=4.9000
refvoltage=1.1000
# mcp offset voltage is 500mv at 0 deg. At 0.010 volt per deg that comes to 
# 50 degrees C. 
offset=50
#
#
# 60 adc channel 0, direct value (0-1023)
# 68 adc channel 0, averaged value (0-65520 with nsamp=4096, shift=6)
# 61 adc channel 1, direct value (0-1023)
# 69 adc channel 1, averaged value (0-65520 with nsamp=4096, shift=6)
register_to_read=69

#
# settings are done, now the preparations. 
convfactor=`(echo scale = 10 ; echo obase=16;echo $refvoltage / $vperdeg / $adcmaxval ) | bc`
offsethex=`(echo obase=16; echo scale=3;echo $offset) | bc `


# now do the real deal. 
rawtemp=`sudo bw_tool -I -D /dev/i2c-1 -a 94 -R $register_to_read:s | tr a-z A-Z`
(echo ibase=16; echo scale=3;echo $rawtemp \* $convfactor - $offsethex)  | bc

After that we make is executable, like we did with the ui script.

chmod +x showtemp 
ls -l showtemp 

-rwxr-xr-x 1 pi pi 999 Sep 7 14:26 showtemp


We trying it out I got failures, because I didn't download bc.:

apt-get install bc 

To check if your scripts work:

sh -x ./ui 
+ ui=bw_tool -a 94 -I -D /dev/i2c-1 
+ bw_tool -a 94 -I -D /dev/i2c-1 -W 70:c7:b 
+ bw_tool -a 94 -I -D /dev/i2c-1 -W 71:c6:b 
+ bw_tool -a 94 -I -D /dev/i2c-1 -W 81:1000:s 
+ bw_tool -a 94 -I -D /dev/i2c-1 -W 82:6:b 
+ bw_tool -a 94 -I -D /dev/i2c-1 -W 80:2:b 
sh -x ./showtemp 
+ adcmaxval=65520 
+ vperdeg=0.010 
+ refvoltage=1.1000 
+ offset=50 
+ register_to_read=69 
+ + bc 
echo scale = 10 
+ echo obase=16 
+ echo 1.1000 / 0.010 / 65520 
+ convfactor=.006E06E01 
+ echo obase=16 
+ echo+  scale=3 
bc+ echo 50  
+ offsethex=32 
+ sudo bw_tool+  -I -Dtr a-z A-Z 
 /dev/i2c-1 -a 94 -R 69:s 
+ rawtemp=ACC3 
+ echo ibase=16 
+ echo scale=3 
+ echo ACC3 * .006E06E01 - 32 
+ bc 
24.251648852 

If a line is wrong it should give a warning.

So now everything is fine it is time to print it out:

bw_tool -I -D /dev/i2c-1 -a 94 -t "temp: "`./showtemp`
Temperature.png


(The remove the latest temperature and see the newest one:

bw_tool -I -D /dev/i2c-1 -a 94 -w 10:00 
bw_tool -I -D /dev/i2c-1 -a 94 -t "temp: "`./showtemp

)

Code that I used: [Temperature Sensor example]


Now we have this. It may be better that we also get it to refresh around a several second instead we have to type the code:

bw_tool -I -D /dev/i2c-1 -a 94 -w 10:00 
bw_tool -I -D /dev/i2c-1 -a 94 -t "temp: "`./showtemp

over and over again. So I am going to try to mix the previous code with temperature code so it will check the code every amount of time. ( I will take 5 sec )

I made the script(Temp5):

#!/bin/bash 
DISPL="bw_tool -I -D /dev/i2c-1 -a 94"

$DISPL -W 10:0:b

 while true;do 
        bw_tool -I -D /dev/i2c-1 -a 94 -w 10:00 
        bw_tool -I -D /dev/i2c-1 -a 94 -t "temp: "`./showtemp` 
        sleep 5 
done 


Do like the other times: chmod +x Temp5

And now when you do:

./Temp5

He will print the temperature every 5 seconds.

On Blog 06 I combined made it possible to show the time and temperature on the display.