1

I have a led connected to a gpio pin using wiringpi, and I am trying to have it affected by the temperature reported by

/opt/vc/bin/vcgencmd measure_temp

I have this command in an executable file so that it can be referenced elsewhere more simply, and its output is like this:

temp=50.0'C

What I am doing is utilizing this output to affect an led I have connected to pin 17 (or pin 0 in wiringpi). The led itself works, as does the majority of this script:

Removed to create space for complete code.

However, this seems to only trigger the first part of the if, and never runs any other part of the script.

Any help will be appreciated.

EDIT: Code now works, thanks all for the help:

    #!/bin/bash
a=1
b=0
temp=$(temp | cut -c 6-7)
while [[ $a > $b ]]
do
if [[ $(temp | cut -c 6-7) > 51 ]]; then
        echo "temp over 50"
        temp
        gpio write 0 1
        sleep 0.5
        gpio write 0 0
        sleep 0.5
        #echo "actual temp is"
        #temp
elif [[ $(temp | cut -c 6-7) < 52 ]]; then
        echo "temp under 50"
        temp
        gpio write 0 1
        sleep 0.1
        gpio write 0 0
        sleep 0.9
        #echo "actual temp is"
        #temp
else
        echo "temp is 50"
        temp
        gpio write 0 1
        sleep 0.2
        gpio write 0 0
        sleep 0.8
fi
done
  • 1
    I am not sure you can use < and > in if statements like that, try -gt and -lt – rob Aug 6 '14 at 14:03
  • it most certainly did not like that :) it threw "integer expression expected" – Kernel Fergus Aug 6 '14 at 14:16
  • Look up the differences between [ and [[. – joan Aug 6 '14 at 14:21
  • i seem to have narrowed it down to the way the value of temp is being retrieved – Kernel Fergus Aug 6 '14 at 15:06
1

Replace your [ with [[ and ] with ]].

Replace temp="temp | cut -c 6-9" with temp=$(temp | cut -c 6-9).

  • It seems to work on my Pi with whatever shell bash defaults to. – joan Aug 6 '14 at 19:33
  • Never mind. My mistake. Sorry. – Bex Aug 6 '14 at 20:10
  • 1
    OK. Made me check though :) – joan Aug 6 '14 at 20:23
1

Edit Oh! Before anything else, just to complete the answer - there is a problem in your assignment. temp="temp | cut -c 6-9" will give you a $temp with the actual value temp | cut -c 6-9. What you want is the result of the command. You get that by temp=$(temp | cut -c 6-9), as pointed out by @joan in another answer.

Original answer Well, bash doesn't handle floating point arithmetic on its own all that well. There are two ways to handle this - the easiest way is to change your cut -c 6-9 to cut -c 6-7, thereby stripping the decimals, and get integer number. After that the -gt (as mentioned in the comments above) will work.

If you desperately need the decimals, you can keep the cut as it is (but have a look at the edit above), but you need to use an external program to do the calculation for you. If you change your if-clause to

if [ $(echo "$temp > $target" | bc) -gt 0 ] 

it will use the external program bc to do the calculations.

By the way, you have a problem with your code. Right now you try to check if the temperature is strictly greater than $target, and if the temperature is strictly less than $target - but if it is the same, you will print an error. That is almost certainly not what you want. You probably want to do one of the checks (for example the greater-than) and handle the other case in an else-clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.