mb02155
Posts: 13
Joined: Sat Dec 15, 2012 10:29 pm

Turn on an LED when connected to wifi

Mon Jan 12, 2015 10:45 pm

Hello,

I am looking for a script (python?) that when will start when the pi boots up and continually check to see if the wifi has successfully connected to a wifi AP and will turn on a LED (connected to GPIO) indicating connection.

I need this because I will have a pi in a location that will not have a display and is on the edge of the wifi range. I want a quick easy way to confirm that the pi is connected.

Does anyone have any script that will help me accomplish this?

Thanks in advance!

User avatar
DougieLawson
Posts: 36312
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Turn on an LED when connected to wifi

Mon Jan 12, 2015 10:54 pm

It's not python (will bash do?)

Code: Select all

#!/bin/bash

# ping the gateway address
/bin/ping -q -c1 192.168.1.254 > /dev/null

if [ $? -eq  0 ]
then
  echo "Network active"
  # stuff to do when the network is active goes here ...   

else
  echo "Network down"
  # stuff to do when the network is down goes here ...
fi
You can also add scripts to /etc/network/if-up.d and they will be automatically run when the network connects.

Code: Select all

#!/bin/sh -e
# Called when a new interface comes up
# don't bother doing anything when lo is configured.
if [ "$IFACE" = "lo" ]; then
        exit 0
fi
# stuff to do when the network comes up active goes here ...
exit 0
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Turn on an LED when connected to wifi

Tue Jan 13, 2015 12:40 am

From python you can get wpasupplicant status, which is what I do:

Code: Select all

def getwpaclientstatus():
    import subprocess

    try:
        writedatedlogmsg(networklog, 'Attempting WPA client status read. ', 4, networkloglevel)
        result = subprocess.Popen(['/sbin/wpa_cli', 'status'], stdout=subprocess.PIPE)
    except:
        writedatedlogmsg(networklog, 'Error reading wpa client status. ', 0, networkloglevel)
    else:
        writedatedlogmsg(networklog, 'Completed WPA client status read. ', 4, networkloglevel)


    # prune interface ID
    resultdict = {}

    for result in result.stdout:
        if result.find('=') > 0:
            split = result.split('=')
            resultdict[split[0]] = split[1].strip()
    return resultdict
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

mb02155
Posts: 13
Joined: Sat Dec 15, 2012 10:29 pm

Re: Turn on an LED when connected to wifi

Tue Jan 13, 2015 12:46 am

Thank you both. I will try both of these!

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

Re: Turn on an LED when connected to wifi

Tue Jan 31, 2017 4:05 am

What pins on the gpio do I connect the led to after the script is set up

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

Re: Turn on an LED when connected to wifi

Tue Jan 31, 2017 9:52 am

Bbuschke wrote:What pins on the gpio do I connect the led to after the script is set up
In the above examples no GPIO outputs have been set up, so its down to you to set up your GPIO to work with which ever pins you wish.
here is just one example on how to do that :- https://thepihut.com/blogs/raspberry-pi ... -gpio-pins
there are plenty more out there just google it.
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

Wed Feb 01, 2017 2:41 am

So after this # stuff to do when the network is down goes here ...


I would put in gpio out low?

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

Re: Turn on an LED when connected to wifi

Wed Feb 01, 2017 12:14 pm

Hi.

So I wired the LED the same as in the web page example I posted.
I did fined when running Dougie's code that I had to change the IP to 192.168.1.1 my routers gateway or it reported down all the time ?
you may need to change the IP to match your routers gateway.

So this is how the code looks once you add in the GPIO bits

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
Now there are some things to note
1. you need to run this script as root ( to run at command line use sudo ./name of file )
2. you will get a error message on the second and all following executions " echo: write error: Device or resource busy" because the GPIO pin has already been set up on the previous execution.

now I set this to run every 5 min using cron.
now you have to run as root so use sudo crontab -e to set up cron , assuming you saved the code in a file called cnw.sh in the /home/pi directory and made it executable you just need the line

Code: Select all

*/5 * * * * /home/pi/cnw.sh >/home/pi/log.txt 2>&1
adding to the end of the cron file.
I added the redirect to the log.txt file just so you can check its working once you are happy its working ok you could just change this to

Code: Select all

*/5 * * * * /home/pi/cnw.sh >/dev/null 2>&1
so as to just discard all the output.

If you don't understand any thing or need any other help please ask
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: Turn on an LED when connected to wifi

Wed Feb 01, 2017 3:26 pm

May be, you can change this

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
and you will get no error on second (or third ...) execution.

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: Turn on an LED when connected to wifi

Wed Feb 01, 2017 4:03 pm

FM81 wrote:May be, you can change this

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
and you will get no error on second (or third ...) execution.

Greetings, FM_81
Thanks always willing to learn some thing new..
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

Mon Feb 06, 2017 6:41 pm

Ok. I think I have the coding down. Now if I find a 3v led but the gpio is sending 3.3v I need a resistor correct? What size resistor?

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

Re: Turn on an LED when connected to wifi

Mon Feb 06, 2017 6:50 pm

Bbuschke wrote:Ok. I think I have the coding down. Now if I find a 3v led but the gpio is sending 3.3v I need a resistor correct? What size resistor?
The formula for working out resistors for LEDs is fairly simple. There are even lots of calculators online for it.

You need to know the supply voltage (Vsupply = 3.3V in this case), the LED voltage (Vled = 3.0V) and the LED current in Amps (Iled = ? try 0.01 Amps which should give a reasonable brightness and not be too much for a GPIO).

The formula is: R = (Vsupply - Vled) / Iled

R= (3.3 - 3.0) / 0.01

R= 0.3 / 0.01

R = 30 Ohms

You should be able to get a 33 Ohm resistor fairly easily, that's the next value up from 30.

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

Re: Turn on an LED when connected to wifi

Mon Feb 06, 2017 7:45 pm

Bbuschke wrote:Ok. I think I have the coding down. Now if I find a 3v led but the gpio is sending 3.3v I need a resistor correct? What size resistor?
No need to find a 3v LED just use a regular LED red,green or yellow which have a forward voltage around 1.5 -2.0v and a 330ohm or 470ohm resistor and it will work just fine.
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

Tue Feb 07, 2017 3:24 am

i got up to the creating the script and im lost at the Cron thing

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

Re: Turn on an LED when connected to wifi

Tue Feb 07, 2017 4:01 am

Bbuschke wrote:i got up to the creating the script and im lost at the Cron thing
This may or may not be the topic that is causing you to get lost, but I think it is bad advice to use "sudo crontab -e".

Instead, just use ordinary "crontab -e", and use "sudo" in the command (so you just put the same string in the crontab line as you would use to run the program from the ordinary shell command line).
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

Tue Feb 07, 2017 4:06 am

Yeah I'm lost. Think I need to find a step by step tutorial. I appreciate the help but I'm very new to all this. I don't have any knowledge in coding and stuff. I don't expect you guys to make a tutorial for me. I will try to research this some more

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

Re: Turn on an LED when connected to wifi

Tue Feb 07, 2017 5:50 pm

Bbuschke wrote:Yeah I'm lost. Think I need to find a step by step tutorial. I appreciate the help but I'm very new to all this. I don't have any knowledge in coding and stuff. I don't expect you guys to make a tutorial for me. I will try to research this some more

Hi.

If you tell me were you have saved your script and what you have called it I will write you a how to. you just need to run cron as root. if you start putting sudo in your cron entries you will have problems.
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

Wed Feb 08, 2017 10:27 pm

Script named wifi_led.py

Located in
/home/pi/scripts/wifi_led.py

I really appreciate the help

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

Re: Turn on an LED when connected to wifi

Wed Feb 08, 2017 10:37 pm

if you start putting sudo in your cron entries you will have problems.
Wrong.
If this post appears in the wrong forums category, my apologies.

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

Re: Turn on an LED when connected to wifi

Thu Feb 09, 2017 12:50 pm

Bbuschke wrote:Script named wifi_led.py

Located in
/home/pi/scripts/wifi_led.py

I really appreciate the help
Hi.

Bit confused, the scripts posted here were bash scripts, but you seemed to have saved yours with a python extension.
so just to clarify is your script bash or python script.
and
Does it self loop or does it just run once on each call.
its makes a difference on how we call it in cron.
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

Thu Feb 09, 2017 5:15 pm

Saved same place and file name but bash now.
/home/pi/scripts/wifi_led.sh

I just used your script example. I don't know if it self loops
I want it to stay on at all times when Connected to a network

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

Re: Turn on an LED when connected to wifi

Thu Feb 09, 2017 8:06 pm

Bbuschke wrote:Saved same place and file name but bash now.
/home/pi/scripts/wifi_led.sh

I just used your script example. I don't know if it self loops
I want it to stay on at all times when Connected to a network
Ok no problem.

First off just in case you have not done it. we need to make the script executable, we do this with chmod.
so you need to run this command in a terminal window ( if using raspbian with desktop) or at the command line (if using raspbian lite ).

Code: Select all

chmod +x /home/pi/scripts/wifi_led.sh 
so now we need to create a cron entry.
at the same command line run this command it will open crontab as the root user.

Code: Select all

sudo crontab -e
this will open a temp cron file in nano.
so using the keyboard arrow keys scroll to the first line below the line "# m h dom mon dow command"
now enter this line

Code: Select all

*/3 * * * * /home/pi/scripts/wifi_led.sh >/dev/null 2>&1
now just press and hold the Ctrl key and press o ( thats letter O ) followed by return.
you should get a response telling you how many lines were written.
now just do Ctrl and x if all is well you will get a message saying installing new crontab.
That's it you now have you network status LED script running every 3 min.
now you could make this work every 2 min if you wanted by going through the same procedure to edit the cron entry and just change the 3 to a 2.
now by default this script prints to the screen telling you if the network is up or down as we have this script running in the back ground we don't want this that why we added the redirect on the end of the cron line.

so that's it after 3 mins your LED should be on if your network is connected, it will check your network every 3 min and update the LED.

if you have any problems or questions please come back to me.
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

Thu Feb 09, 2017 9:28 pm

i get this when making script excutable
chmod: changing permissions of ‘/home/pi/scripts/wifi_led.sh’: Operation not permitted

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

Re: Turn on an LED when connected to wifi

Thu Feb 09, 2017 9:42 pm

Bbuschke wrote:i get this when making script excutable
chmod: changing permissions of ‘/home/pi/scripts/wifi_led.sh’: Operation not permitted

how did you create this file did you use nano or sudo nano ?

ok lets see who owns the file then, from a command line
first change to the scripts directory.

Code: Select all

cd scripts
then

Code: Select all

ls -l
and post back the results
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 10:37 pm

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.
If this post appears in the wrong forums category, my apologies.

Return to “Networking and servers”