This mishap may have happened to you: comfortably lounging at home, you surf on your smart phone. Suddenly, nothing anymore: you have reached the limit of your data subscription. After a short investigation, you notice that your DSL router crashed a few days before and that, meanwhile, you used your phone GSM connection while you thought you were on the Wifi. If only you could have noticed earlier... You are lucky, this week we just happen to discuss a very easy DIY project to signal network failures as soon as possible.
The basic idea is dead simple: to build a light beacon which mightily indicates any network connectivity issue.
The hardware
To do so, we suggest to use a YoctoHub-Wireless-g to connect to a PHP script located on a public server, thanks to the HTTP callback mode. If the YoctoHub-Wireless-g can connect itself on the said server, it means that the network is in working order. Otherwise, it means that the network is down. To signal the network state, a simple NEOPIXEL from Adafruit RGB led ring driven by a Yocto-Color-V2 will do.
The diagram of our network failure detector
We used a slightly modified Yoctopuce enclosure for he presentation, but any semi-transparent enclosure is enough. You can even recycle your kids' night light.
The detector
The software
The idea is to use the sequences of the Yocto-Color-V2 which allow us to create animations generated directly by the Yocto-Color. We must create a sequence that sets the lights to green, and then triggers a red blinking after a while. The PHP script is in charge of initializing the animation again at the beginning of each execution. Thus, if we take care to define a green duration slightly longer that the delay between two calls to the script, the leds stay green as long as the network is up and running.
The only difficulty consists in making the leds blink red without time limitation at the end of the green period: by default, the Yocto-Color-V2 sequences loop on themselves, so if we put the green phase and the red blinking in the same sequence, the leds will go back to green.
We can't put the blinking at the end of the green sequence...
Therefore, the PHP script uses a new function of the Yocto-Color-V2 API which allows us to make a led jump from one sequence to another. We therefore define a first sequence which contains the green phase and which ends with a jump to a second sequence dealing with the red blinking and which loops.
But we can create two sequences with a jump from one to the other
Here is a piece of PHP code illustrating the principle:
// jump to sequence 1
$leds->resetBlinkSeq(0);
$leds->addRgbMoveToBlinkSeq(0, 0x00FF00, 00);
$leds->addRgbMoveToBlinkSeq(0, 0x00FF00, 90000);
$leds->addJumpToBlinkSeq(0, 1);
// sequence 1: blinking red every second
$leds->resetBlinkSeq(1);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 60);
$leds->addRgbMoveToBlinkSeq(1, 0x000000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0x000000, 1000);
Note that there is now as well an addUnlinkToBlinkSeq() function which enables you to disconnect a led from a sequence, which implies that each led can only run the sequence once. We won't use it in this project, but you could find it useful. Here is the complete code of the script. We added the code which automatically defines the sequences if it detects that the Yocto-Color is not properly configured.
include("yocto_api.php");
include("yocto_colorledcluster.php");
include("yocto_network.php");
$errmsg='';
if (yregisterhub('callback',$errmsg)!=YAPI_SUCCESS)
die('callback calls only.');
$net = yFirstNetwork();
$leds = yFirstColorLedCluster();
if (is_null($leds)) die('No led cluster found');
// Yocto-Color Configuration
if ($leds->get_activeLedCount()!=12)
{$d=200;
// power up blue sequence
$leds->set_rgbColor(0,12,0x0000FF);
$leds->resetBlinkSeq(0);
$leds->addRgbMoveToBlinkSeq(0, 0x0000FF, 0);
$leds->addRgbMoveToBlinkSeq(0, 0x0000FF, $d);
$leds->addRgbMoveToBlinkSeq(0, 0x000000, 0);
$leds->addRgbMoveToBlinkSeq(0, 0x000000, 3*$d);
// link the leds to the blue sequences at startup
for ($i=0;$i<12;$i++)
$leds->linkLedToBlinkSeqAtPowerOn($i,1, 0,$i*$d);
// no network Red blinking
$leds->resetBlinkSeq(1);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 60);
$leds->addRgbMoveToBlinkSeq(1, 0x050000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0x050000, 150);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0xFF0000, 60);
$leds->addRgbMoveToBlinkSeq(1, 0x050000, 0);
$leds->addRgbMoveToBlinkSeq(1, 0x050000, 5000);
// save the sequences
for ($i=0;$i<2;$i++)
{ $leds->set_blinkSeqStateAtPowerOn($i,1);
$leds->saveBlinkSeq($i);
$leds->startBlinkSeq($i);
$leds->saveLedsConfigAtPowerOn();
}
$leds->set_activeLedCount(12);
}
// resets the green "everything's ok" sequence
// to make sure it restarts from the beginning,
// duration in ms is set to 1.5x the HTTP callback period.
$delay = 1500*$net->get_callbackMaxDelay();
$leds->resetBlinkSeq(2);
$leds->addRgbMoveToBlinkSeq(2, 0x001000, 200);
$leds->addRgbMoveToBlinkSeq(2, 0x001000, $delay);
$leds->addJumpToBlinkSeq(2, 1);
$leds->startBlinkSeq(2);
$leds->linkLedToBlinkSeq(0,12,2,0);
Print("Done.");
?>
As of now, there is no direct method to reset the execution of a sequence. Therefore, the scripts initializes again the green sequence by redefining it every time. Note that this is rather a bonus for us, as it enables us to automatically define its duration to one and a half time the callback period.
Here is a short video of the working system:
Conclusion
In a jiffy, we built an appliance that blinks when the wifi/network is down. Less bulky than a computer always on, it is also less greedy: a simple USB charger is enough to power it.
We could improve the concept and the PHP script to record the time of all the callbacks, which would allow us to reconstruct a history of the network failures.
One last comment: If you wish to use one of these new functions to manage sequences, don't forget to update your API and the firmware of your Yocto-Color-V2.