diff options
Diffstat (limited to 'libraries/Bridge/examples')
10 files changed, 755 insertions, 0 deletions
diff --git a/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino b/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino new file mode 100644 index 0000000..7833d54 --- /dev/null +++ b/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino @@ -0,0 +1,90 @@ + +/* + Arduino Yun Boot watcher + + Allows you to use the Yun's 32U4 processor as a + serial terminal for the linino processor + + Upload this to an Arduino Yun via serial (not WiFi) + then open the serial monitor at 115200 to see the boot process + of the linino processor. You can also use the serial monitor + as a basic command line interface for the linino processor using + this sketch. + + The circuit: + * Arduino Yun + + created March 2013 + by Massimo Banzi + modified 26 May 2013 + by Tom Igoe + + This example code is in the public domain. + */ + +long baud = 115200; + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; +int ledState = HIGH; // whether the LED is high or low + +String bootString = ""; +int bootLineCount = 0; +boolean booting = true; + +void setup() { + Serial.begin(baud); // open serial connection to Linino + Serial1.begin(baud); // open serial connection via USB-Serial + + // initialize the digital pin as an output. + pinMode(led, OUTPUT); + digitalWrite(led, ledState); // turn the LED on (HIGH is the voltage level) + while(booting) { + listenForBoot(); + } + delay(500); +} + + +void loop() { + // After booting, become a serial terminal: + if (Serial.available()) { // got anything from USB-Serial? + char c = (char)Serial.read(); // read from USB-serial + Serial1.write(c); // write to Linino + ledState=!ledState; // invert LED state + digitalWrite(led, ledState); // toggle the LED + } + if (Serial1.available()) { // got anything from Linino? + char c = (char)Serial1.read(); // read from Linino + Serial.write(c); // write to USB-serial + } + +} + +void listenForBoot() { + char c; + if (Serial1.available()) { // got anything from Linino? + c = (char)Serial1.read(); // read from Linino + + if (c == '\n') { // clear the bootString every newline + bootLineCount++; // increment the boot line counter + Serial.println(bootLineCount); // print the count + bootString = ""; // clear the boot string + } + else { // anything other than newline, add to string + bootString += c; + } + } + + // look for the final boot string message: + if (bootString.endsWith("entered forwarding state")) { + Serial1.println(); + } + + // look for the command prompt: + if (bootString.endsWith(":/#")) { + Serial.println("Ready for action."); + booting = false; + } +} diff --git a/libraries/Bridge/examples/Bridge/Bridge.ino b/libraries/Bridge/examples/Bridge/Bridge.ino new file mode 100644 index 0000000..dc453c4 --- /dev/null +++ b/libraries/Bridge/examples/Bridge/Bridge.ino @@ -0,0 +1,104 @@ + +#include <Bridge.h> + +void setup() { + pinMode(13,OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); + digitalWrite(13, HIGH); +} + +void loop() { + while (Bridge.messageAvailable()) { + uint8_t buff[64]; + int l = Bridge.readMessage(buff, 64); + process(buff, l); + } + delay(100); // Poll every 0.100s +} + +void process(uint8_t buff[], int l) { + // "DWppv" -> digitalWrite(pp, v) + // "DRpp" -> digitalRead(pp) -> "Dpp0" / "Dpp1" + // "AWppvvv" -> analogWrite(pp, vvv) + // "ARpp" -> analogRead(pp) -> "App0000" - "App1023" + // "PIpp" -> pinMode(pp, INPUT) + // "POpp" -> pinMode(pp, OUTPUT) + + // Sanity check + if (l<4 || l>7) + return; + if (buff[2]<'0' || buff[2]>'9') + return; + if (buff[3]<'0' || buff[3]>'9') + return; + char cmd0 = buff[0]; + char cmd1 = buff[1]; + int pin = (buff[2]-'0')*10 + (buff[3]-'0'); + if (pin<0 || pin>13) + return; + + // Command selection + if (l==5 && cmd0=='D' && cmd1=='W') { + char c = buff[4]; + if (c=='0' || c=='1') { + digitalWrite(pin, c-'0'); + reportDigitalRead(pin, true, true); + } + } else if (l==4 && cmd0=='D' && cmd1=='R') { + reportDigitalRead(pin, true, true); + } else if (l==7 && cmd0=='A' && cmd1=='W') { + analogWrite(pin, buff[4]); + reportAnalogRead(pin); + } else if (l==4 && cmd0=='A' && cmd1=='R') { + reportAnalogRead(pin); + } else if (l==4 && cmd0=='P' && cmd1=='I') { + pinMode(pin, INPUT); + reportPinMode(pin, INPUT); + } else if (l==4 && cmd0=='P' && cmd1=='O') { + pinMode(pin, OUTPUT); + reportPinMode(pin, OUTPUT); + } +} + +void reportPinMode(int pin, uint8_t dir) { + uint8_t buff[] = { 'P', 'I', '0', '0' }; + buff[1] = dir == INPUT ? 'I' : 'O'; + buff[2] += pin/10; + buff[3] += pin%10; + Bridge.writeMessage(buff, 4); +} + +void reportDigitalRead(int pin, boolean raw, boolean dataset) { + // "Dpp0" - "Dpp1" + // 0 1 2 3 + uint8_t buff[] = { 'D', '0', '0', '0' }; + buff[1] += pin/10; + buff[2] += pin%10; + if (digitalRead(pin) == HIGH) + buff[3] = '1'; + if (raw) + Bridge.writeMessage(buff, 4); + if (dataset) { + char *val = "0"; + val[0] = buff[3]; + buff[3] = 0; + Bridge.put((const char *)buff, val); + } +} + +void reportAnalogRead(int pin) { + // "App0000" - "App1023" + // 0 1 2 3 4 5 6 + uint8_t buff[] = { 'A', '0', '0', '0', '0', '0', '0' }; + buff[1] += pin/10; + buff[2] += pin%10; + + int v = analogRead(pin); + buff[6] += v%10; v /= 10; + buff[5] += v%10; v /= 10; + buff[4] += v%10; v /= 10; + buff[3] += v; + Bridge.writeMessage(buff, 7); +} + diff --git a/libraries/Bridge/examples/ConsoleAsciiTable/ConsoleAsciiTable.ino b/libraries/Bridge/examples/ConsoleAsciiTable/ConsoleAsciiTable.ino new file mode 100644 index 0000000..4cdf4c1 --- /dev/null +++ b/libraries/Bridge/examples/ConsoleAsciiTable/ConsoleAsciiTable.ino @@ -0,0 +1,94 @@ +/* + ASCII table + + Prints out byte values in all possible formats: + * as raw binary values + * as ASCII-encoded decimal, hex, octal, and binary values + + For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII + + The circuit: No external hardware needed. + + created 2006 + by Nicholas Zambetti + modified 9 Apr 2012 + by Tom Igoe + modified 22 May 2013 + by Cristian Maglie + + This example code is in the public domain. + + <http://www.zambetti.com> + + */ + +#include <Console.h> + +void setup() { + //Initialize Console and wait for port to open: + Bridge.begin(); + Console.begin(); + + // Uncomment the following line to enable buffering: + // - better transmission speed and efficiency + // - needs to call Console.flush() to ensure that all + // transmitted data is sent + + //Console.buffer(64); + + while (!Console) { + ; // wait for Console port to connect. + } + + // prints title with ending line break + Console.println("ASCII Table ~ Character Map"); +} + +// first visible ASCIIcharacter '!' is number 33: +int thisByte = 33; +// you can also write ASCII characters in single quotes. +// for example. '!' is the same as 33, so you could also use this: +//int thisByte = '!'; + +void loop() { + // prints value unaltered, i.e. the raw binary version of the + // byte. The Console monitor interprets all bytes as + // ASCII, so 33, the first number, will show up as '!' + Console.write(thisByte); + + Console.print(", dec: "); + // prints value as string as an ASCII-encoded decimal (base 10). + // Decimal is the default format for Console.print() and Console.println(), + // so no modifier is needed: + Console.print(thisByte); + // But you can declare the modifier for decimal if you want to. + //this also works if you uncomment it: + + // Console.print(thisByte, DEC); + + Console.print(", hex: "); + // prints value as string in hexadecimal (base 16): + Console.print(thisByte, HEX); + + Console.print(", oct: "); + // prints value as string in octal (base 8); + Console.print(thisByte, OCT); + + Console.print(", bin: "); + // prints value as string in binary (base 2) + // also prints ending line break: + Console.println(thisByte, BIN); + + // if printed last visible character '~' or 126, stop: + if(thisByte == 126) { // you could also use if (thisByte == '~') { + // ensure the latest bit of data is sent + Console.flush(); + + // This loop loops forever and does nothing + while(true) { + continue; + } + } + // go on to the next character + thisByte++; +} diff --git a/libraries/Bridge/examples/FileTest/FileTest.ino b/libraries/Bridge/examples/FileTest/FileTest.ino new file mode 100644 index 0000000..3fd7ce9 --- /dev/null +++ b/libraries/Bridge/examples/FileTest/FileTest.ino @@ -0,0 +1,50 @@ + +#include <FileIO.h> + +void setup() { + Bridge.begin(); + SD.begin(); + + boolean r; + r=SD.exists("/arduino/test"); + if (r) error("1"); + r=SD.exists("/arduino"); + if (!r) error("2"); + + r=SD.mkdir("/arduino/test"); + if (!r) error("3"); + r=SD.exists("/arduino/test"); + if (!r) error("4"); + + File f = SD.open("/arduino/test/bla", FILE_WRITE); + if (!f) error("5"); + f.println("CIAO!"); + f.close(); + + delay(10000); + + r=SD.rmdir("/arduino/test"); + if (r) error("6"); + r=SD.remove("/arduino/test"); + if (r) error("7"); + + r=SD.remove("/arduino/test/bla"); + if (!r) error("8"); + r=SD.rmdir("/arduino/test"); + if (!r) error("9"); + r=SD.exists("/arduino/test"); + if (r) error("10"); +} + + +void error(const char *s) { + Bridge.print("# ERROR "); + Bridge.println(s); + while (true); +} + +void loop() { +} + + + diff --git a/libraries/Bridge/examples/HttpClient/HttpClient.ino b/libraries/Bridge/examples/HttpClient/HttpClient.ino new file mode 100644 index 0000000..bf5e8ff --- /dev/null +++ b/libraries/Bridge/examples/HttpClient/HttpClient.ino @@ -0,0 +1,23 @@ + +#include <HttpClient.h> + +void setup() { + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); +} + +void loop() { + HttpClient client; + client.get("http://my.server.address/file.php"); + + char c = client.read(); + if (c=='1') + digitalWrite(13, HIGH); + if (c=='0') + digitalWrite(13, LOW); + + delay(5000); +} + + diff --git a/libraries/Bridge/examples/Process/Process.ino b/libraries/Bridge/examples/Process/Process.ino new file mode 100644 index 0000000..919cea7 --- /dev/null +++ b/libraries/Bridge/examples/Process/Process.ino @@ -0,0 +1,70 @@ +/* + Running process using Process class. + + This sketch demonstrate how to run linux processes + using an Arduino Yún. + + created 5 Jun 2013 + by Cristian Maglie + + */ + +#include <Process.h> + +void setup() { + // Setup Bridge (needed every time we communicate with the Arduino Yún) + Bridge.begin(); + + // Setup Console + Console.begin(); + // Buffering improves Console performance, but we must remember to + // finish sending using the Console.flush() command. + Console.buffer(64); + + // Wait until a Network Monitor is connected. + while (!Console); + + // run various example processes + runCurl(); + runCpuInfo(); +} + +void loop() { + // Do nothing here. +} + +void runCurl() { + // Launch "curl" command and get Arduino asciilogo from the network + + Process p; // Create a process and call it "p" + p.begin("curl"); // Process should launch the "curl" command + p.addParameter("http://arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl" + p.run(); // Run the process and wait for its termination + + // Print arduino logo over the console. + // A process output can be read with the stream methods + while (p.available()>0) { + char c = p.read(); + Console.print(c); + } + // Ensure the latest bit of data is sent. + Console.flush(); +} + +void runCpuInfo() { + // Launch "cat /proc/cpuinfo" command (shows info on Atheros CPU) + Process p; + p.begin("cat"); + p.addParameter("/proc/cpuinfo"); + p.run(); + + // Print command output on the Console. + // A process output can be read with the stream methods + while (p.available()>0) { + char c = p.read(); + Console.print(c); + } + // Ensure the latest bit of data is sent. + Console.flush(); +} + diff --git a/libraries/Bridge/examples/TimeCheck/TimeCheck.ino b/libraries/Bridge/examples/TimeCheck/TimeCheck.ino new file mode 100644 index 0000000..54fd131 --- /dev/null +++ b/libraries/Bridge/examples/TimeCheck/TimeCheck.ino @@ -0,0 +1,79 @@ + +/* + Time Check + + Gets the time from the linino processor via Bridge + then parses out hours, minutes and seconds for the Arduino + using an Arduino Yun. + + created 27 May 2013 + By Tom Igoe + */ + + +#include <Process.h> + +Process date; // process used to get the date +int hours, minutes, seconds; // for the results +int lastSecond = -1; // need an impossible value for comparison + +void setup() { + Serial.begin(9600); // initialize serial + Bridge.begin(); // initialize Bridge + delay(2000); // wait 2 seconds + + while(!Serial); // wait for Serial Monitor to open + Serial.println("Time Check"); // Title of sketch + + // run an initial date process. Should return: + // hh:mm:ss : + if (!date.running()) { + date.begin("date"); + date.addParameter("+%T"); + date.run(); + } +} + +void loop() { + + if(lastSecond != seconds) { // if a second has passed + // print the time: + if (hours <= 9) Serial.print("0"); // adjust for 0-9 + Serial.print(hours); + Serial.print(":"); + if (minutes <= 9) Serial.print("0"); // adjust for 0-9 + Serial.print(minutes); + Serial.print(":"); + if (seconds <= 9) Serial.print("0"); // adjust for 0-9 + Serial.println(seconds); + + // restart the date process: + if (!date.running()) { + date.begin("date"); + date.addParameter("+%T"); + date.run(); + } + } + + //if there's a result from the date process, parse it: + while (date.available()>0) { + // get the result of the date process (should be hh:mm:ss): + String timeString = date.readString(); + + // find the colons: + int firstColon = timeString.indexOf(":"); + int secondColon= timeString.lastIndexOf(":"); + + // get the substrings for hour, minute second: + String hourString = timeString.substring(0, firstColon); + String minString = timeString.substring(firstColon+1, secondColon); + String secString = timeString.substring(secondColon+1); + + // convert to ints,saving the previous second: + hours = hourString.toInt(); + minutes = minString.toInt(); + lastSecond = seconds; // save to do a time comparison + seconds = secString.toInt(); + } + +} diff --git a/libraries/Bridge/examples/WiFiCheck/WiFiCheck.ino b/libraries/Bridge/examples/WiFiCheck/WiFiCheck.ino new file mode 100644 index 0000000..1cb9f03 --- /dev/null +++ b/libraries/Bridge/examples/WiFiCheck/WiFiCheck.ino @@ -0,0 +1,53 @@ +/* + Arduino Yun Wireless Config Check + + Checks the wireless state of Arduino Yun by calling + the linux command iwconfig. + + Upload this to an Arduino Yun via serial (not WiFi) + then open the serial monitor to see the status of + your Yun's WiFi connection. If it's connected to + a wireless network, the ESSID (name) of that network + and the signal strength will appear. + + The circuit: + * Arduino Yun + + created 22 May 2013 + by Tom Igoe + + This example code is in the public domain. + */ + +#include <Process.h> + +void setup() { + Serial.begin(9600); // initialize serial communication + while(!Serial); // do nothing until the serial monitor is opened + + pinMode(13,OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); // make contact with the linux processor + digitalWrite(13, HIGH); + + delay(2000); // wait 2 seconds + + Process wifiCheck; // initialize a new process + + + wifiCheck.begin("iwconfig"); // command you want to run + wifiCheck.addParameter("wlan0"); // parameter of the command + wifiCheck.run(); // run the command + + // while there's any characters coming back from the + // process, print them to the serial monitor: + while (wifiCheck.available() > 0) { + char thisChar = wifiCheck.read(); + Serial.print(thisChar); + } +} + +void loop() { + // nothing to do here. +} + diff --git a/libraries/Bridge/examples/XivelyClient/XivelyClient.ino b/libraries/Bridge/examples/XivelyClient/XivelyClient.ino new file mode 100644 index 0000000..48b1403 --- /dev/null +++ b/libraries/Bridge/examples/XivelyClient/XivelyClient.ino @@ -0,0 +1,110 @@ +/* + Xively sensor client with Strings + + This sketch connects an analog sensor to Xively, + using an Arduino Yún. + + created 15 March 2010 + updated 27 May 2013 + by Tom Igoe + + */ + +// include all Libraries needed: +#include <Process.h> +#include "passwords.h" // contains my passwords, see below + +/* + NOTE: passwords.h is not included with this repo because it contains my passwords. + You need to create it for your own version of this application. To do so, make + a new tab in Arduino, call it passwords.h, and include the following variables and constants: + + #define APIKEY "foo" // replace your pachube api key here + #define FEEDID 0000 // replace your feed ID + #define USERAGENT "my-project" // user agent is the project name + */ + + +// set up net client info: +const unsigned long postingInterval = 60000; //delay between updates to xively.com +unsigned long lastRequest = 0; // when you last made a request +String dataString = ""; + +void setup() { + // start serial port: + Bridge.begin(); + Console.begin(); + + while(!Console); // wait for Network Console to open + Console.println("Xively client"); + + // Do a first update immediately + updateData(); + sendData(); + lastRequest = millis(); +} + +void loop() { + // get a timestamp so you can calculate reading and sending intervals: + long now = millis(); + + // if the sending interval has passed since your + // last connection, then connect again and send data: + if (now - lastRequest >= postingInterval) { + updateData(); + sendData(); + lastRequest = now; + } +} + +void updateData() { + // convert the readings to a String to send it: + dataString = "Temperature,"; + dataString += random(10) + 20; + // add pressure: + dataString += "\nPressure,"; + dataString += random(5) + 100; +} + +// this method makes a HTTP connection to the server: +void sendData() { + // form the string for the API header parameter: + String apiString = "X-ApiKey: "; + apiString += APIKEY; + + // form the string for the URL parameter: + String url = "https://api.xively.com/v2/feeds/"; + url += FEEDID; + url += ".csv"; + + // Send the HTTP PUT request + + // Is better to declare the Process here, so when the + // sendData function finishes the resources are immediately + // released. Declaring it global works too, BTW. + Process xively; + Console.print("\n\nSending data... "); + xively.begin("curl"); + xively.addParameter("-k"); + xively.addParameter("--request"); + xively.addParameter("PUT"); + xively.addParameter("--data"); + xively.addParameter(dataString); + xively.addParameter("--header"); + xively.addParameter(apiString); + xively.addParameter(url); + xively.run(); + Console.println("done!"); + + // If there's incoming data from the net connection, + // send it out the Console: + while (xively.available()>0) { + char c = xively.read(); + Console.write(c); + } + +} + + + + diff --git a/libraries/Bridge/examples/YunSerialTerminal/YunSerialTerminal.ino b/libraries/Bridge/examples/YunSerialTerminal/YunSerialTerminal.ino new file mode 100644 index 0000000..aa152e1 --- /dev/null +++ b/libraries/Bridge/examples/YunSerialTerminal/YunSerialTerminal.ino @@ -0,0 +1,82 @@ +/* + Arduino Yun USB-to-Serial + + Allows you to use the Yun's 32U4 processor as a + serial terminal for the linino processor. + + Upload this to an Arduino Yun via serial (not WiFi) + then open the serial monitor at 115200 to see the boot process + of the linino processor. You can also use the serial monitor + as a basic command line interface for the linino processor using + this sketch. + + From the serial monitor the following commands can be issued: + + '~' followed by '0' -> Set the UART speed to 57600 baud + '~' followed by '1' -> Set the UART speed to 115200 baud + + The circuit: + * Arduino Yun + + created March 2013 + by Massimo Banzi + modified by Cristian Maglie + + This example code is in the public domain. + */ + +long lininoBaud = 57600; + +// Pin 13 has an LED connected on most Arduino boards. +int led = 13; +int ledState = HIGH; // whether the LED is high or low + + +void setup() { + Serial.begin(115200); // open serial connection via USB-Serial + Serial1.begin(lininoBaud); // open serial connection to Linino + + // initialize the digital pin as an output. + pinMode(led, OUTPUT); + digitalWrite(led, ledState); // turn the LED on (HIGH is the voltage level) +} + + +boolean commandMode = false; + +void loop() { + // copy from virtual serial line to uart and vice versa + if (Serial.available()) { // got anything from USB-Serial? + char c = (char)Serial.read(); // read from USB-serial + if (commandMode == false) { // if we aren't in command mode... + if (c == '~') { // Tilde '~' key pressed? + commandMode = true; // enter in command mode + } else { + Serial1.write(c); // otherwise write char to Linino + } + } else { // if we are in command mode... + if (c == '0') { // '0' key pressed? + Serial1.begin(57600); // set speed to 57600 + Serial.println("Speed set to 57600"); + } else if (c == '1') { // '1' key pressed? + Serial1.begin(115200); // set speed to 115200 + Serial.println("Speed set to 115200"); + } else { // any other key pressed? + Serial1.write('~'); // write '~' to Linino + Serial1.write(c); // write char to Linino + } + commandMode = false; // in all cases exit from command mode + } + ledState=!ledState; // invert LED state + digitalWrite(led, ledState); // toggle the LED + } + if (Serial1.available()) { // got anything from Linino? + char c = (char)Serial1.read(); // read from Linino + Serial.write(c); // write to USB-serial + } +} + + + + + |