Arduino Forum

Products => Arduino Yún => Topic started by: ProfePaco on Dec 04, 2013, 12:05 pm

Title: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 04, 2013, 12:05 pm
A friend of mine has sent me the way to know whether the yun in connected to wifi.

for example:

runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal"); // grab just the signal data
unShellCommand("/usr/bin/pretty-wifi-info.lua | grep RX");
runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Active"); // grab just the uptime
runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Interface");    // grab just the RX/TX
runShellCommand("/usr/bin/pretty-wifi-info.lua | grep IP");     // grab just the ip data

The question is if we could switch the led on/off from the arduino sketch side to show if Yun is connected.

Any way to do it? Of course this led is not attached to any pin...so, if possible, it has to be tricky.

Thanks in advance
Title: Re: is it possible to control the wifi led? Any command?
Post by: federicofissore on Dec 05, 2013, 10:59 am
Of course it's possible
Take a look at script /usr/bin/blink-start or invoke
Code: [Select]

blink-start 100
blink-start 500

You can use two leds (wlan and usb). You can find more info and docs at http://wiki.openwrt.org/doc/uci/system#leds
Title: Re: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 05, 2013, 11:14 am

Of course it's possible
Take a look at script /usr/bin/blink-start or invoke
Code: [Select]

blink-start 100
blink-start 500

You can use two leds (wlan and usb). You can find more info and docs at http://wiki.openwrt.org/doc/uci/system#leds


Thanks!

More things to study  :)
Title: Re: is it possible to control the wifi led? Any command?
Post by: federicofissore on Dec 05, 2013, 11:16 am

More things to study  :)

Yeah, "It's a long way to the top" \m/
Title: Re: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 05, 2013, 05:47 pm
Here is the code. Now Arduino Yun shows the wifi state using the blue wifi led!


Code: [Select]

/*subroutine checkwificonnected.
Checks the strngth of wifi and show information on blue wifi led of Arduino Yun.
Checks the wifi signal after period_check miliseconds

1.- if no connected then blue wifi led is off
2.- if signal strength is more than good_strength value then blinks slowly (1 second).
3.- if singal is not zero but less than good_strength blinks fast (0.2 seconds).

*/


#include <Process.h>
Process wifiCheck;  // process used to get the wifi status
unsigned long now, before;
#define period_check 5000 //time in miliseconds to check wifi signal
#define good_strength 25



void setup()
{
 before = 0;
 now = millis();
 Bridge.begin();        // initialize Bridge
 //Serial.begin(9600);    // initialize serial. Only for degugging  
}

void loop()
{
 now = millis();
if (now - before > period_check ) {checkwificonnected(); before = now;}
}





//==========================================================//

void checkwificonnected(void)
{
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal"); // grab just the signal data
 String strength = wifiCheck.readString();
 byte totallength = strength.length();
 strength = strength.substring(totallength-4,totallength-1);
 byte level = strength.toInt();
// Serial.println(level); //for debugging only
 
 if (level >= good_strength) {wifiCheck.runShellCommand("/usr/bin/blink-start 1000");};
 if (level >0 && level < good_strength) {wifiCheck.runShellCommand("/usr/bin/blink-start 200");};
 if (level == 0) {wifiCheck.runShellCommand("/usr/bin/blink-stop");} ;

}





Title: Re: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 05, 2013, 06:00 pm
this version is much better. It only runs the command if the status of quality signal has changed

Code: [Select]

/*subroutine checkwificonnected.
Checks the strngth of wifi and show information on blue wifi led of Arduino Yun.
Checks the wifi signal after period_check miliseconds

1.- if no connected then blue wifi led is off
2.- if signal strength is more than good_strength value then blinks slowly (1 second).
3.- if singal is not zero but less than good_strength blinks fast (0.2 seconds).

*/


#include <Process.h>
Process wifiCheck;  // process used to get the wifi status
unsigned long now, before;
#define period_check 5000 //time in miliseconds to check wifi signal
#define good_strength 25
byte previous_state = 0;




void setup()
{
 before = 0;
 now = millis();
 Bridge.begin();        // initialize Bridge
 //Serial.begin(9600);    // initialize serial. Only for degugging  
}

void loop()
{
 now = millis();
if (now - before > period_check ) {checkwificonnected(); before = now;}
}





//==========================================================//

void checkwificonnected(void)
{
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal"); // grab just the signal data
 String strength = wifiCheck.readString();
 byte totallength = strength.length();
 strength = strength.substring(totallength-4,totallength-1);
 byte level = strength.toInt();
// Serial.println(level); //for debugging only
 
 if (level >= good_strength) {
     if (previous_state != 1)
         {previous_state = 1;
         wifiCheck.runShellCommand("/usr/bin/blink-start 1000");}};
 if (level >0 && level < good_strength) {
     if (previous_state != 2)
         {previous_state = 2;
          wifiCheck.runShellCommand("/usr/bin/blink-start 200");};}
 if (level == 0) {
      if (previous_state != 3)
          {previous_state = 3;
           wifiCheck.runShellCommand("/usr/bin/blink-stop");} ;}

}

Title: Re: is it possible to control the wifi led? Any command?
Post by: 0miker0 on Dec 05, 2013, 06:51 pm
Nice! That comes in pretty handy.
Title: Re: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 05, 2013, 07:00 pm

Nice! That comes in pretty handy.


Thanks.

I think something is left. I think that the function milllis() reaches a maximum after several days... 52 days?... and then the value of millis() starts from zero again. If so some code has to be added.

for example:

if (before > now) {before = now;}

Am I right?
Title: Re: is it possible to control the wifi led? Any command?
Post by: noblepepper on Dec 06, 2013, 02:44 pm
The usual way people handle the millis() rollover is to use unsigned ints, the magic of ones complement math makes the difference correct.

Try this:
Code: [Select]

void setup() {
  // put your setup code here, to run once:
unsigned int a, b;
delay(1000);
a = 0xFFF8;//65528
b = 0x0008;//    8
Serial.begin(115200);
while (!Serial) ;
Serial.println(a);
Serial.println(b);
Serial.println(b-a);
}

void loop() {
  // put your main code here, to run repeatedly:

}
Title: Re: is it possible to control the wifi led? Any command?
Post by: AlexDC on Dec 09, 2013, 11:29 am
Thanks for your sketch ProfePaco !
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 09, 2013, 08:44 pm
Just as a general note, if the purpose is only to display the status via the LED, why don't you just run a Python script (possibly via cron) on the Linino side instead of going through hoops to do this on the 32u4 side?  :~

Ralf
Title: Re: is it possible to control the wifi led? Any command?
Post by: mamu on Dec 09, 2013, 09:03 pm
PCWorxLA, good point (had the same on my mind)  :smiley-roll:
Title: Re: is it possible to control the wifi led? Any command?
Post by: ProfePaco on Dec 09, 2013, 09:09 pm

Just as a general note, if the purpose is only to display the status via the LED, why don't you just run a Python script (possibly via cron) on the Linino side instead of going through hoops to do this on the 32u4 side?  :~

Ralf


Because I do not know how to do it, obviously.

Now you have a new task to do. Thanks for your offer  :smiley-roll:
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 09, 2013, 09:15 pm


Just as a general note, if the purpose is only to display the status via the LED, why don't you just run a Python script (possibly via cron) on the Linino side instead of going through hoops to do this on the 32u4 side?  :~

Ralf


Because I do not know how to do it, obviously.
Well, you have it already almost completely running on the Linino side already with all the shell commands. Would be just a matter of interpreting the output in a Python script instead of pushing it down the bridge.
There is always something new to learn...  :smiley-mr-green:
Quote
Now you have a new task to do. Thanks for your offer  :smiley-roll:
Well, "schaun 'mer mal"...  8)
But it's likely not going to happen before the weekend though...  ;)

Ralf
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 10, 2013, 04:35 am
Holy mackerel, Batman!  :smiley-eek-blue:

Nothing personal ProfePaco,  but I just took actually a closer look at the script and almost fell of my chair! =(

This is what we would call in German "Von hinten durch die Brust ins Auge".
Or as someone in another Arduino forum just stated "for someone who only knows a hammer as a tool, everything looks like a nail"...

You are actually using an Arduino Sketch on the 32u4 side to  call a Python script on the Linino side to call a Lua script and two shell scripts also on the Linino side to actually access some hardware on the Linino side...

Don't have my Yun during the (at least this) week on a wireless network to test this, but this certainly cries to be replaced by a Linino side only solution, probably by adapting the pretty-wifi-info.lua script...

Ralf
Title: Re: is it possible to control the wifi led? Any command?
Post by: bjarne on Dec 10, 2013, 07:51 am
Well, that sounded like a fun exercise as I had not heard of Lua before.  So I gave it a shot by creating a new wlanled.lua by stripping out all the stuff in pretty-wifi-info.lua not needed for checking the signal strength, and I added in the commands to blink the wlan led.  It seems to work correctly  :)

Incidentally, is the wlan LED not used for anything while the sketch is running, IOW is it free to use as we please?

wlanled.lua
Code: [Select]
#!/usr/bin/lua

local function get_wifi_info(network, iface, accumulator)
 local net = network:get_wifinet(iface)

 if net then
   local dev = net:get_device()
   if dev then
     accumulator["quality"] = net:signal_percent()
   end
 end
end

local function collect_wifi_info()
 local network = require"luci.model.network".init()
 local accumulator = {}
 get_wifi_info(network, "wlan0", accumulator)
 return accumulator
end

local info = collect_wifi_info()

if info.quality == 0 then
 print("Signal: " .. info.quality .. "%")
 os.execute("echo 'none' > /sys/class/leds/ds:green:wlan/trigger")
end
if info.quality > 0 and info.quality < 25 then
 print("Signal: " .. info.quality .. "%")
 os.execute("echo 'timer' > /sys/class/leds/ds:green:wlan/trigger")
 os.execute("echo 200 > /sys/class/leds/ds:green:wlan/delay_on")
 os.execute("echo 200 > /sys/class/leds/ds:green:wlan/delay_off")
end
if info.quality >= 25 and info.quality < 50 then
 print("Signal: " .. info.quality .. "%")
 os.execute("echo 'timer' > /sys/class/leds/ds:green:wlan/trigger")
 os.execute("echo 1000 > /sys/class/leds/ds:green:wlan/delay_on")
 os.execute("echo 1000 > /sys/class/leds/ds:green:wlan/delay_off")
end
if info.quality >= 50 then
 print("Signal: " .. info.quality .. "%")
 os.execute("echo 'timer' > /sys/class/leds/ds:green:wlan/trigger")
 os.execute("echo 2000 > /sys/class/leds/ds:green:wlan/delay_on")
 os.execute("echo 2000 > /sys/class/leds/ds:green:wlan/delay_off")
end

Title: Re: is it possible to control the wifi led? Any command?
Post by: mamu on Dec 10, 2013, 10:27 am
Awesome!
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 10, 2013, 09:43 pm

Well, that sounded like a fun exercise as I had not heard of Lua before.
Well, I knew about Lua (for me it was just a YASL-Yet Another Scripting Language) but not that it was by default available on a Yun, as everyone and their next door neighbor seems to be eager to use Python (which I don't like very much do to some design flawsfeatures)...
Quote
So I gave it a shot by creating a new wlanled.lua by stripping out all the stuff in pretty-wifi-info.lua not needed for checking the signal strength, and I added in the commands to blink the wlan led.  It seems to work correctly  :)
That was (and still is) my basic plan of action after looking at the Lua script last night. But I think this could still be improved on by moving the LED access directly in the Lua script instead of making those os.execute calls for that.
As mentioned, I don't have access to my Yun during the work week (at least not with a workable WLAN connection), but I try to get that worked out by the coming weekend...

Ralf
Title: Re: is it possible to control the wifi led? Any command?
Post by: federicofissore on Dec 13, 2013, 11:07 am
Nice to see starting some lua hacking :)

Lua was used because of its very low memory footprint and start up time. But since it lacks many features* so commonly used, we installed and used and promote python as the language of choice for the yun

* It's not the language lacking features of course, it's the libraries bundled with it that don't suffice
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 16, 2013, 04:12 am

Nice to see starting some lua hacking :)

Lua was used because of its very low memory footprint and start up time. But since it lacks many features* so commonly used, we installed and used and promote python as the language of choice for the yun

* It's not the language lacking features of course, it's the libraries bundled with it that don't suffice
Well, working on the libraries to expand them is easier than dealing with a language that has brain-dead (IMHO) structural features...
You move a piece of code around and accidentally don't pay attention to proper white-space in place and your whole program changes... yikes...

Ralf
Title: Re: is it possible to control the wifi led? Any command?
Post by: federicofissore on Dec 20, 2013, 10:31 am

You move a piece of code around and accidentally don't pay attention to proper white-space in place and your whole program changes... yikes...

Are you talking about lua or python?
Title: Re: is it possible to control the wifi led? Any command?
Post by: PCWorxLA on Dec 21, 2013, 04:47 am


You move a piece of code around and accidentally don't pay attention to proper white-space in place and your whole program changes... yikes...

Are you talking about lua or python?
Python of course...  :smiley-mr-green:

Lua doesn't depend on whitespace for program logic, all constructs are properly "end"ed. Too bad they decided to make the language case-sensitive... :~

Ralf