timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Bash - what next ?

Fri Jan 04, 2013 5:27 am

Here is a extract of what I'm trying to understand.

Quote

# Set up GPIO 4 and set to output
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

# Set up GPIO 7 and set to input
echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction

# Write output
echo "1" > /sys/class/gpio/gpio4/value

# Read from input
cat /sys/class/gpio/gpio7/value

end quote

Its to talk to the GPIO

Now what I want to do is to input a Hi or Lo one one pin port of the GPIO and output it to another (and apply some delay(other processes) to the data.

now this line has got me stuck.

cat /sys/class/gpio/gpio7/value

where where does the read data wind up ? If I write a line

let x='read data'

clearly 'read data' isn't going to work and the data is going to be a '1' or a '0'

Is it as simple as

let x="in"

?

*** as a beginner with this level of BASH, I very uncertain of where I should look for a solution to what is probably a very simple solution.

Any thoughts very welcome !

:D :D :D

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: Bash - what next ?

Fri Jan 04, 2013 7:08 am

timmoore46 wrote:cat /sys/class/gpio/gpio7/value
where where does the read data wind up ?
The cat writes to the standard output stream, which unless you have redirected it will normally be the terminal the script was run from.

There is a construct called backquotes or command substitution that captures the output from one command so you can use it in another command or assign it to a variable:

Code: Select all

# old style:
x="`cat /sys/class/gpio/gpio7/value`"
echo "$x" > /sys/class/gpio/gpio4/value

# new style:
x="$(cat /sys/class/gpio/gpio7/value)"
echo "$x" > /sys/class/gpio/gpio4/value
(The extra double quotes are not necessary in these examples, but it is good practice always to put double quotes around expansions and substitutions. Otherwise, text that includes spaces will be split into multiple words, which can lead to undesirable changes in behaviour.)

In this simple example you could also just redirect the cat:

Code: Select all

cat /sys/class/gpio/gpio7/value > /sys/class/gpio/gpio4/value

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ? -FIXED

Fri Jan 04, 2013 7:22 am

Many many thanks, that is exactly what I needed !

A very happy,

:D :D :D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 05, 2013 2:42 pm

the input worked fine

# Read from input
cat /sys/class/gpio/gpio7/value

and so did

cat /sys/class/gpio/gpio7/value > /sys/class/gpio/gpio4/value

but assigning the input value to 'x' did not work just a variety of error messages,
quote
# old style:
x="`cat /sys/class/gpio/gpio7/value`"
echo "$x" > /sys/class/gpio/gpio4/value

# new style:
x="$(cat /sys/class/gpio/gpio7/value)"
echo "$x" > /sys/class/gpio/gpio4/value

end quote

______

would this work ?

cat /sys/class/gpio/gpio7/value > x

or does it need "$x" or something ?

A very puzzled

:cry: :cry: :cry:

Tim

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Bash - what next ?

Sat Jan 05, 2013 3:28 pm

timmoore46 wrote:the input worked fine

# Read from input
cat /sys/class/gpio/gpio7/value

and so did

cat /sys/class/gpio/gpio7/value > /sys/class/gpio/gpio4/value

but assigning the input value to 'x' did not work just a variety of error messages,
quote
# old style:
x="`cat /sys/class/gpio/gpio7/value`"
echo "$x" > /sys/class/gpio/gpio4/value

# new style:
x="$(cat /sys/class/gpio/gpio7/value)"
echo "$x" > /sys/class/gpio/gpio4/value

end quote

______

would this work ?

cat /sys/class/gpio/gpio7/value > x

or does it need "$x" or something ?

A very puzzled

:cry: :cry: :cry:

Tim
No, cat /sys/class/gpio/gpio7/value > x would write the value to a file with the name 'x' in your current directory.

The GPIO stuff only works if the GPIO patches are loaded with modprobe first. You're doing battle with the complex areas of bash before you're comfortable with the basics.

try

Code: Select all

x="`cat /sys/class/thermal/thermal_zone0/temp`"
which should return without error
then

Code: Select all

echo $x
It should return a number which is the temperature of the CPU in millidegrees C. This is incorporated in recent kernals by default and isn't dependant on patches loading first. Getting something working first is often the precursor to success.

Basically you just assign values to variables just using a name (x) and you access the value stored in the variable by using $x.

The bit with cat loads the value returned by the cat program (which just lists a file or something masquerading as a file) into the variable x.

Code: Select all

cputemp=$(cat /sys/class/thermal/thermal_zone0/temp)
echo $cputemp

Does exactly the same. The GPIO stuff is exactly the same but needs the GPIO patches loading beforehand.

Code: Select all

cat /sys/class/thermal/thermal_zone0/temp
Just returns the temperature without assigning it to a variable.

If you're not logged in as root, writing anything to a device or loading patches etc will need sudo sticking in front of it. But you can read the value from a device with just ordinary user priviledges.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: Bash - what next ?

Sat Jan 05, 2013 3:33 pm

timmoore46 wrote:but assigning the input value to 'x' did not work just a variety of error messages,
Note that there must be no spaces around the =.

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 05, 2013 3:36 pm

Very very interesting ! Greatly appreciated !

I'll try all that out tomorrow,,,,,,,

:D :D :D :D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 05, 2013 5:49 pm

looked in 'class' directory but no thermal.

How do I install the 'thermal' thing ?

:cry:

Tim

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: Bash - what next ?

Sat Jan 05, 2013 6:25 pm

Maybe that is part of firmware updates in rpi-update https://github.com/Hexxeh/rpi-update

I don't even have any Raspbian SD cards without rpi-update, because without that, ethernet and USB drop out for me after a random period of time.

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 05, 2013 8:27 pm

for anyone following this thread, to use comparisons etc to make decisions on input data, this works to get the data:-

cat /sys/class/gpio/gpio4/ value > x

cat x

also for the input :-

cat /sys/class/gpio/gpio7/ value > y

cat y

now the rest is a lot easier ! I hope !

:D :D :D :D

Tim

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Bash - what next ?

Sat Jan 05, 2013 9:31 pm

[quote="timmoore46"] for anyone following this thread, to use comparisons etc to make decisions on input data, this works to get the data:-

cat /sys/class/gpio/gpio4/ value > x

cat x

also for the input :-

cat /sys/class/gpio/gpio7/ value > y

cat y


now the rest is a lot easier ! I hope !

:D :D :D :D

Tim [/quote]

It will work provided you don't change directories in the script. You're physically writing the data to the current folder as files x and y and then listing the files.

x and y will survive a reboot. Probably not the best way of doing it, unless you want a permanent record.

The thermal data is in recent raspdian images. It doesn't need rpi-update. Its used in the LXDE desktop for the CPU temperature if you enable it. It sounds like you have an old kernel.

[code]uname -a [/code]

Will give you the kernel version.

Mine which is a normally updated raspdian (apt-get update & apt-get upgrade) gives

Linux raspberrypi3 3.2.27+ #250 PREEMPT Thu Oct 18 19:03:02 BST 2012 armv6l GNU/Linux
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ? = Fixed

Mon Jan 07, 2013 7:22 am

I'm sure there is a more elegant of writing this code for inputting off one port of the GPIO and then sending it to another port. but hey ! it works !

Many thanks to the many who have helped me achieve this !

root@raspberrypi:~# cat io_003
#!/bin/sh
#setup Input
echo "7" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio7/direction
cat /sys/class/gpio/gpio7/value > y
cat y

# setup output
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

cat y > /sys/class/gpio/gpio4/value

root@raspberrypi:~#
end quote

I am now working on an "if then else" extension to the code so that a '0' input fires up another port and a '1' fires up the original port.

The application I'm working on it to harvest spare solar panel energy and switch on a 2.7kW immersion tank heater = free hot water ! I'm hoping to use a 3.3V schmitt trigger, and a ldr sensor which offers 180 ohms when the sun is strong enough . (that is the easy bit !)

:D :D :D :D :D :D :D

Tim

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Bash - what next ?

Mon Jan 07, 2013 5:49 pm

Hi Tim

I have done the "control the low power immersion from the invertor" and I put my scripts at http://www.raspberrypi.org/phpBB3/viewt ... =37&t=8345

Basically I use bluetooth to interrogate the invertor and if its over 1.5kw then switch on the 1kw immersion and get free hot water :) I use a USB serial relay to switch the mains - as this is what I had on another server before it used its smoke chip up!! :o

Have a look and then reply to this topic. I will pop in at some time and check this thread.
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Tue Jan 08, 2013 9:26 am

Very very interesting ! I have a long hard look at your links !

Back to my software that now works/

What it does is to reflect a switch from one port to another in a continuous loop, control C gets it to jump out of the loop:-

quote:-

root@raspberrypi:~# cat loop_004

#! /bin/sh
# chmod a+x loop_004
# control c stops the program

while true

do


#setup Input

echo "7" > /sys/class/gpio/export

echo "in" > /sys/class/gpio/gpio7/direction

cat /sys/class/gpio/gpio7/value > y

cat y


# setup output

echo "4" > /sys/class/gpio/export

echo "out" > /sys/class/gpio/gpio4/direction

cat y > /sys/class/gpio/gpio4/value

continue
done



root@raspberrypi:~#
end quote

Now I have got a if 'then else working' for use later:-

quote:-root@raspberrypi:~# cat ifthenelse_003
#!/bin/sh
#experiment with if then else

read count
if [ $count -eq 100 ]
then

a=20
echo $a
else
echo "count is not 100"
fi

root@raspberrypi:~#

end quote

So next part of the project is to get the relay driven from the GPIO.

Hope most members are brighter than me when it come to Bash ! *LOL*

:D :D :D :D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Wed Jan 09, 2013 11:47 am

Source for serious 30Amp relays available from Maplin.

Unusually for this part their way of displaying the parts for purchase is dire ! So check your basket before paying very very carefully.

12 volt coil is N23AW
24 volt coil not sure
Mains coil is N24AW

socket to mount the relay in (recommended) is N19AW.

I mention this as its very very difficult to find on their www site without a part number. (unusually)

:D :D

Tim

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Bash - what next ?

Wed Jan 09, 2013 12:17 pm

If you're planning on switching mains, I'd use a solid state relay, they are an order of magnitude easier to interface to the Pi's GPIO pins than a mechanical relay with a 12V coil. (you just connect them direct, but if you're running them at any kind of AC load they need a heatsink).

Pros and cons, They don't have contacts to spark if you're switching a heavy load direct, but they do have a small leakage current which can give a shock when off if the load is otherwise isolated. They are silent. The low voltage bit is optically isolated from the dangerous stuff, if something goes wrong you can't get silly voltages in the input.

http://www.ebay.co.uk/itm/Solid-State-R ... 4aba8cef60
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Thu Jan 10, 2013 6:26 am

Many thanks Pluggy, I was totally unaware of such a device. I have used relays often and trust them. but fully aware the back emf of a large traditional relay can slay a lot of kit ! *LOL*, so your solution is elegant !

I've ordered one !

I'm switching 2.7kW immersion heater element with it.

I'm opto-isolating the GPIO pins I'm using, I don't want to slay the Raspberry with any silly wiring errors, once I go beyond the 3.3V supply from the Raspberry !

Many thanks again.

:D :D :D :D :D

Tim

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Bash - what next ?

Thu Jan 10, 2013 10:42 am

If you have a little mechanical skill you can adapt an old PC heatsink. If you get one with a degree of overkill, they don't get that hot anyway. I have one switching a 2kW water heater, but theres an Arduino on the controlling end.

Hows the BASH coming ?
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Thu Jan 10, 2013 1:43 pm

BASH has yielded all the major problems nicely, thanks to the patience and skill of the other forum members.

The RPi seems rock solid and doing what BASH tells it to ! (eventually)

So its now 95% hardware and waiting on the postman.

:D :D :D :D :D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 19, 2013 7:36 am

The Output board design is now complete and I have a working version.

I would like to share the circuit diagram which is in jpg format. That will follow on the next post.

If there is any interest I could turn it into a pcb and offer that to anyone who wants one.

The key design features are that it has a very small load on the RPi (10k) and it uses an opto isolator plus relay to drive anything in the outside world. (outside world = big 30A mains relay from Maplin which has a 12V coil)

I'm sure someone with more skill could reduce the component count also cost, but as it works I'm happy !

:D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Thu Jan 24, 2013 10:36 am

Any thoughts on improvements re welcome. Always happy to learn.

This is from the RPi GPIO to a big Relay to switch a 2.7Kw Immersion heater.

:D

Tim

PS Ah ! What the best / easiest way to add a photo ?

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sat Jan 26, 2013 7:14 am

http://i109.photobucket.com/albums/n62/ ... ev003a.jpg

The second R 1 is really Relay 1

Hope I got the jpg thing right !

:o

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Sun Jan 27, 2013 11:06 am

Following connecting it all up to the RPi is now works with a Bash Script I've called control_003

for those of you who are interested , here are the circuit diagrams.

The Supplier of most of the parts was Rapid Electronics.


http://i109.photobucket.com/albums/n62/ ... ev001b.jpg


http://i109.photobucket.com/albums/n62/ ... ev005b.jpg

Please note the earlier diagram of the output crt has been changed, so don't use !


:D :D :D :D :D :D

Tim

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Bash - what next ?

Wed Feb 27, 2013 1:30 pm

Update on the input board circuit diagram.

I'm going to use two of them to control the point of switching on and also point of switching off.

Software will take another month... maybe...

Image

:D :D :D

Tim

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Bash - what next ?

Wed Feb 27, 2013 2:11 pm

Good solid back of envelope circuit diagram, can't be all bad if its got a BC107 in it....... ;)
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

Return to “Beginners”