|
| 1 | +/* |
| 2 | + Wifi Signal Strength Indicator |
| 3 | + |
| 4 | + This example demonstrates the use of the bridge and process libraries |
| 5 | + to communicate between the Arduino side and the linux side of the Arduino Yun. |
| 6 | + |
| 7 | + The Linux script returns the strength of the wifi signal. |
| 8 | + |
| 9 | + The Arduino sketch uses LEDs to indicate whether the current value of |
| 10 | + the signal strength is above, below, or the same as the last value |
| 11 | + |
| 12 | + The circuit: |
| 13 | + * LEDs on pins 8, 9, and 10 |
| 14 | + * Built-in LED on pin 13 |
| 15 | + |
| 16 | + The script: |
| 17 | + The following one line script must exist in the /root directory of the |
| 18 | + linux file system, in a file named "wifiStrength.sh", and it must be executable: |
| 19 | + |
| 20 | + tail -1 /proc/net/wireless | cut -c22-23 |
| 21 | + |
| 22 | + created 06 June 2013 |
| 23 | + by Michael Shiloh |
| 24 | + modified 08 June 2013 |
| 25 | + by Tom Igoe |
| 26 | + |
| 27 | + This example code is in the public domain |
| 28 | + |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +#include <Process.h> |
| 33 | + |
| 34 | +// global variable to store the last value of the signal strength |
| 35 | +int lastValue; |
| 36 | + |
| 37 | +void setup() { |
| 38 | + // set up LED pins as outputs: |
| 39 | + pinMode(8, OUTPUT); |
| 40 | + pinMode(9, OUTPUT); |
| 41 | + pinMode(10, OUTPUT); |
| 42 | + pinMode(13,OUTPUT); |
| 43 | + |
| 44 | + // Indicate that you're ready by flashing pin 13 LED twice |
| 45 | + for (int flash = 0; flash < 3; flash++) { |
| 46 | + digitalWrite(13,HIGH); |
| 47 | + delay(200); |
| 48 | + digitalWrite(13,LOW); |
| 49 | + delay(800); |
| 50 | + } |
| 51 | + // initialize Serial and Bridge: |
| 52 | + Serial.begin(9600); |
| 53 | + Bridge.begin(); |
| 54 | + |
| 55 | + // Indicate that setup is finished |
| 56 | + digitalWrite(13,HIGH); |
| 57 | +} |
| 58 | + |
| 59 | +void loop() { |
| 60 | + int value = 0; // the signal strength as an integer |
| 61 | + String result; // the result of the process as a String |
| 62 | + Process wifiCheck; // the process itself |
| 63 | + |
| 64 | + |
| 65 | + // Run the script on the linux side. Note that any word |
| 66 | + //or text separated by a tab or space is considered |
| 67 | + //an additional parameter: |
| 68 | + wifiCheck.begin("ash"); |
| 69 | + wifiCheck.addParameter("/root/wifiStrength.sh"); |
| 70 | + wifiCheck.run(); |
| 71 | + |
| 72 | + // If the process has sent any characters: |
| 73 | + while (wifiCheck.available()>0) { |
| 74 | + result = wifiCheck.readString(); // read the result into a string |
| 75 | + value = result.toInt(); // parse the string as an int |
| 76 | + } |
| 77 | + |
| 78 | + // for debugging |
| 79 | + Serial.print("previous strength:"); |
| 80 | + Serial.print(lastValue); |
| 81 | + Serial.print("\tcurrent strength:"); |
| 82 | + Serial.println(value); |
| 83 | + |
| 84 | + // indicate the relative string by lighting the appropriate LED |
| 85 | + allOff(); // turn off all the LEDS |
| 86 | + |
| 87 | + if (value > lastValue) { // if the signal's getting stronger |
| 88 | + digitalWrite(10, HIGH); |
| 89 | + } |
| 90 | + else if (value < lastValue){ // if the signal's getting weaker |
| 91 | + digitalWrite(8, HIGH); |
| 92 | + } |
| 93 | + else { // if the signal's stayed steady |
| 94 | + digitalWrite(9, HIGH); |
| 95 | + } |
| 96 | + |
| 97 | + lastValue = value; // record this value for next time |
| 98 | + delay(10); // small delay before next time through the loop |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void allOff() { |
| 103 | + digitalWrite(8, LOW); |
| 104 | + digitalWrite(9, LOW); |
| 105 | + digitalWrite(10, LOW); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments