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.
Here is the code. Now Arduino Yun shows the wifi state using the blue wifi led!
/*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");} ;
}
this version is much better. It only runs the command if the status of quality signal has changed
/*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");} ;}
}
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.
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:
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:
}
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? :~
PCWorxLA:
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
PCWorxLA:
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...
Now you have a new task to do. Thanks for your offer
Well, "schaun 'mer mal"... 8)
But it's likely not going to happen before the weekend though...
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...
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
#!/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
bjarne:
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)...
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...
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
[quote author=Federico Fissore link=topic=202718.msg1505402#msg1505402 date=1386929268]
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
[/quote]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...