pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Thu Feb 09, 2017 10:49 pm

Martin Frezman wrote:This is precisely why you should drop the sudo in all your "normal"/"terminal" commands. You don't need it (really, seriously, and you can ignore anyone who says you do) and, as you've discovered, it can screw you up (you end up with files owned by root and not writable by pi).

Instead, as I stated above, you should just put the 'sudo' in the crontab file (i.e., as part of the command to run). Then your crontab entry looks just like the command you would use to run the program "manually" (i.e., from the shell/terminal) and everything is as it should be.

And any comments by any other posters to the effect that you can't put "sudo" inside a crontab line - can, as you now know, be ignored.
ok please explain what's the difference between running a cron as a user and putting cron in the line and just running corn as root anyway.
if you tried this on any other linux, other than raspbian that is, you would find putting sudo in a cron line would not work as you need to enter your password to elevate a use to root using sudo. so you have to run cron as root.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Turn on an LED when connected to wifi

Thu Feb 09, 2017 11:04 pm

@pcmanbob:

I'll be sending you a PM sometime later, answering your questions.

No need to bother the open forum any further with these details.
If this post appears in the wrong forums category, my apologies.

Bbuschke
Posts: 16
Joined: Tue Jan 31, 2017 4:02 am

Re: Turn on an LED when connected to wifi

Fri Feb 10, 2017 1:12 am

owner and group is root

Bbuschke
Posts: 16
Joined: Tue Jan 31, 2017 4:02 am

Re: Turn on an LED when connected to wifi

Fri Feb 10, 2017 1:16 am

used sudo nano

sudolea
Posts: 7
Joined: Thu Feb 09, 2017 9:55 pm

Re: Turn on an LED when connected to wifi

Fri Feb 10, 2017 6:40 am

If you would be willing to "sacrifice" one of the raspberry's own LEDs, you wouldn't need to do all that hassle with GPIO pins and self-made electronic circuits. Instead, you could use libusb...

pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Fri Feb 10, 2017 10:21 am

Bbuschke wrote:owner and group is root
OK not problem, we can correct this, at least you are learning, probably more than if things had gone right.

I am going to change the owner of the "scripts" directory and the file "wifi_led.sh" just to be sure they are both owned by Pi user.
so to do this we use the command "chown" we can do it with one line doing both the directory and the files within it at the same time.
but we have to use sudo in this case because we are changing the ownership from root to pi.

so at the command line as before enter this.

Code: Select all

sudo chown -R pi:pi /home/pi/scripts
now if you do

Code: Select all

ls -l /home/pi/scripts
you should see the owner and group have changed to pi.

now we can make the file executable with the command

Code: Select all

chmod +x /home/pi/scripts/wifi_led.sh
this time it should work as pi now owns the file.
now we can test the file like this ( yes you need to use sudo in this command)

Code: Select all

sudo /home/pi/scripts/wifi_led.sh
if all is well you should get the response "0 network up" and your LED should light up.

if you have got this far with out problems you can go and create your cron file as I explained before.

let me know how you get on.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

sudolea
Posts: 7
Joined: Thu Feb 09, 2017 9:55 pm

Re: Turn on an LED when connected to wifi

Fri Feb 10, 2017 12:22 pm

I once had a similar issue. I added a script to /etc/init.d/rc.local (call it e.g. "/home/myusername/bin/myscript.sh"), with a trailing ampersand ("&") so as to have it execute in the background.

The script "/home/myusername/bin/myscript.sh" itself was (is) ever-running in an endless loop. Beware, in such a case, to build in some sleep statement in that script. Because otherwise, it eats up a lot of processor time and power.

Such an approach isn't necessarily better than a crontab entry, but just another approach to the same problem. It just might be a little less cumbersome to setup...

Bbuschke
Posts: 16
Joined: Tue Jan 31, 2017 4:02 am

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 7:56 pm

now when i enter

Code: Select all

sudo /home/pi/scripts/wifi_led.sh
to test i get this

Code: Select all

sh: echo: I/O error
0 network up
got the led jumped to pin 18 and ground because thats what you had in your example

pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 8:51 pm

Bbuschke wrote:now when i enter

Code: Select all

sudo /home/pi/scripts/wifi_led.sh
to test i get this

Code: Select all

sh: echo: I/O error
0 network up
got the led jumped to pin 18 and ground because thats what you had in your example
Bbuschke.
please post your script as I cant get mine to do this.

does the led light up ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bbuschke
Posts: 16
Joined: Tue Jan 31, 2017 4:02 am

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 9:24 pm

LED does not light up

Code: Select all

#!/bin/bash
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction

/bin/ping -q -c1 192.168.1.1 > /dev/null
if [ $? -eq 0 ]
then
   echo "$? network up"
   # Sets pin 18 to high

   echo "1" > /sys/class/gpio/gpio18/value

   
 else
   echo "$? network down"
   # Sets pin 18 to low

   echo "0" > /sys/class/gpio/gpio18/value
   
fi

sudolea
Posts: 7
Joined: Thu Feb 09, 2017 9:55 pm

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 10:26 pm

Bbuschke, you really should read what FM81 posted on the first of Feb...

He indicated to modify that echo "18" - line !!!

That line is causing the I/O error ...

pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 10:28 pm

HI Bbuschke

Well you made me think on this one.
but finally I think its down to spacing within your file. I copied your posted script in to nano and it looked like this
Image
and when I ran it it produced the error.

so then I edited it so it looked like this
Image
removing all the spaces on the left and the 2 lines with the green blocks and then it ran without the error.
there is one more thing you need to change while you are editing your script.
you need to change this line

Code: Select all

echo "18" > /sys/class/gpio/export
to this

Code: Select all

[ -L /sys/class/gpio/gpio18 ] || echo "18" > /sys/class/gpio/export
to prevent a message a write error: device or resource busy

then I think you should be good
so running the script again and several more times just to make sure there are no other errors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Turn on an LED when connected to wifi

Sat Feb 11, 2017 11:38 pm

The underlying gist of the previous post (i.e., the one previous to this one) is that echoing 18 to the "export" file causes the system to create a symlink called "gpio18" in /sys/class/gpio. And that this is a one-time operation. Once you've done it once (presumably, since system boot), you both don't need to and, indeed, can't do it again.

Which begs the question: How do you "undo" the effect of echoing 18 out to that file (short of rebooting) ? How do you make the symlink go away (I tried "sudo rm"ing it - no go)
If this post appears in the wrong forums category, my apologies.

Bbuschke
Posts: 16
Joined: Tue Jan 31, 2017 4:02 am

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 3:02 am

that worked. the light is on!!!!

I turned off the wifi connection and after 3 min it went off.
also had jumper to pin 18 not gpio 18
thank you very much for the help.

I know you said earlier what to change to make it go off after 2 min but is there away to make it go off right away or seconds after disconnect? just curious for learning purposes

sudolea
Posts: 7
Joined: Thu Feb 09, 2017 9:55 pm

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 8:19 am

Bbuschke, you may want to have a look at the post of by pcmanbob dated Thu Feb 09, 2017 8:06 pm

In there, you should change the /3 into /2

Actually, if it were me having this problem, I would even change it into a /1 because this script isn't exactly a very expensive script in terms of processing power. Even executing it once every minute wouldn't exactly "overload" your PI. And then, you have a quicker visual feedback on your wifi connection status.

User avatar
rpdom
Posts: 15422
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 8:37 am

Martin Frezman wrote:The underlying gist of the previous post (i.e., the one previous to this one) is that echoing 18 to the "export" file causes the system to create a symlink called "gpio18" in /sys/class/gpio. And that this is a one-time operation. Once you've done it once (presumably, since system boot), you both don't need to and, indeed, can't do it again.

Which begs the question: How do you "undo" the effect of echoing 18 out to that file (short of rebooting) ? How do you make the symlink go away (I tried "sudo rm"ing it - no go)
Of course rm won't get rid of it. It's not even a real symlink as the /sys filesystem is entirely virtual and within the kernel workspace.

The obvious answer to this question is to echo 18 to the "unexport" file.

However, a neater way to make the script run cleanly without having to export/unexport every time, is to check for the existence of the virtual symlink before trying to create it.
Bbuschke wrote:I know you said earlier what to change to make it go off after 2 min but is there away to make it go off right away or seconds after disconnect? just curious for learning purposes
I'd run the script once at startup, and have a loop inside to keep checking the network, maybe with a sleep 5 inside to stop it polling too often.

pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 10:09 am

Bbuschke wrote:that worked. the light is on!!!!

I turned off the wifi connection and after 3 min it went off.
also had jumper to pin 18 not gpio 18
thank you very much for the help.

I know you said earlier what to change to make it go off after 2 min but is there away to make it go off right away or seconds after disconnect? just curious for learning purposes

That's great news, yes you can change how often its checked as I said you could change the /3 to /2 or you could just replace the /3 with * and then it would run every minute.
if you wanted to run it more often that that then you would need to make part of the script loop its self and only call it once at boot.
this way you could make it check the connection as often as you like down to about once every 10 sec. any faster than that might not allow the script to execute before you call it again.
I hope you enjoyed sorting this out and that you learned from your errors.
Glad to have been able to help.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 7135
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 10:32 am

To answer some of the other points raised.
As I said I actually copied Bbuschke's script in to nano, on the forum it looked ok but once in nano you could see there were lots of extra spaces and a few hidden characters, this was what was causing the I/O error, which is a different error to device busy caused by the repeated calling of echo "18" > /sys/class/gpio/export.
After experimentation I found you only actually needed to remove the spaces before #!/bin/bash in the first line to make it work correctly but it looked better if you removed all the other spacing and hidden characters as well.

regarding the error caused but calling echo "18" > /sys/class/gpio/export more than once, FM81 had already suggested to fix for this in his post on Feb 01, 2017 3:26 pm, it works and you no longer get the device busy error , I just never updated my posted script, and Bbuschke was only working from my original script and had not applied FM81's fix.

thanks to everyone for there input.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
rpdom
Posts: 15422
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Turn on an LED when connected to wifi

Sun Feb 12, 2017 11:35 am

pcmanbob wrote:As I said I actually copied Bbuschke's script in to nano, on the forum it looked ok but once in nano you could see there were lots of extra spaces and a few hidden characters
The forum adds those spaces when you use the "Select all" and copy from the code box. It is annoying.

unusuallyalice
Posts: 5
Joined: Sat Dec 09, 2017 4:45 pm

Re: Turn on an LED when connected to wifi

Mon Dec 11, 2017 12:43 am

Hey, also new here.

I would like a script that makes an LED blink when WiFi is connected to it. The LED should be either on or off normally but blinks only when connected to wifi.

I have no idea fully what I am doing. But any help would be appreciated.

Return to “Networking and servers”