added arduino, modified build
This commit is contained in:
183
arduino/libraries/Bridge/examples/Bridge/Bridge.ino
Normal file
183
arduino/libraries/Bridge/examples/Bridge/Bridge.ino
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
Arduino Yún Bridge example
|
||||
|
||||
This example for the YunShield/Yún shows how
|
||||
to use the Bridge library to access the digital and
|
||||
analog pins on the board through REST calls.
|
||||
It demonstrates how you can create your own API when
|
||||
using REST style calls through the browser.
|
||||
|
||||
Possible commands created in this shetch:
|
||||
|
||||
"/arduino/digital/13" -> digitalRead(13)
|
||||
"/arduino/digital/13/1" -> digitalWrite(13, HIGH)
|
||||
"/arduino/analog/2/123" -> analogWrite(2, 123)
|
||||
"/arduino/analog/2" -> analogRead(2)
|
||||
"/arduino/mode/13/input" -> pinMode(13, INPUT)
|
||||
"/arduino/mode/13/output" -> pinMode(13, OUTPUT)
|
||||
|
||||
This example code is part of the public domain
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Bridge
|
||||
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <BridgeServer.h>
|
||||
#include <BridgeClient.h>
|
||||
|
||||
// Listen to the default port 5555, the Yún webserver
|
||||
// will forward there all the HTTP requests you send
|
||||
BridgeServer server;
|
||||
|
||||
void setup() {
|
||||
// Bridge startup
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin();
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
// Listen for incoming connection only from localhost
|
||||
// (no one from the external network could connect)
|
||||
server.listenOnLocalhost();
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Get clients coming from server
|
||||
BridgeClient client = server.accept();
|
||||
|
||||
// There is a new client?
|
||||
if (client) {
|
||||
// Process request
|
||||
process(client);
|
||||
|
||||
// Close connection and free resources.
|
||||
client.stop();
|
||||
}
|
||||
|
||||
delay(50); // Poll every 50ms
|
||||
}
|
||||
|
||||
void process(BridgeClient client) {
|
||||
// read the command
|
||||
String command = client.readStringUntil('/');
|
||||
|
||||
// is "digital" command?
|
||||
if (command == "digital") {
|
||||
digitalCommand(client);
|
||||
}
|
||||
|
||||
// is "analog" command?
|
||||
if (command == "analog") {
|
||||
analogCommand(client);
|
||||
}
|
||||
|
||||
// is "mode" command?
|
||||
if (command == "mode") {
|
||||
modeCommand(client);
|
||||
}
|
||||
}
|
||||
|
||||
void digitalCommand(BridgeClient client) {
|
||||
int pin, value;
|
||||
|
||||
// Read pin number
|
||||
pin = client.parseInt();
|
||||
|
||||
// If the next character is a '/' it means we have an URL
|
||||
// with a value like: "/digital/13/1"
|
||||
if (client.read() == '/') {
|
||||
value = client.parseInt();
|
||||
digitalWrite(pin, value);
|
||||
} else {
|
||||
value = digitalRead(pin);
|
||||
}
|
||||
|
||||
// Send feedback to client
|
||||
client.print(F("Pin D"));
|
||||
client.print(pin);
|
||||
client.print(F(" set to "));
|
||||
client.println(value);
|
||||
|
||||
// Update datastore key with the current pin value
|
||||
String key = "D";
|
||||
key += pin;
|
||||
Bridge.put(key, String(value));
|
||||
}
|
||||
|
||||
void analogCommand(BridgeClient client) {
|
||||
int pin, value;
|
||||
|
||||
// Read pin number
|
||||
pin = client.parseInt();
|
||||
|
||||
// If the next character is a '/' it means we have an URL
|
||||
// with a value like: "/analog/5/120"
|
||||
if (client.read() == '/') {
|
||||
// Read value and execute command
|
||||
value = client.parseInt();
|
||||
analogWrite(pin, value);
|
||||
|
||||
// Send feedback to client
|
||||
client.print(F("Pin D"));
|
||||
client.print(pin);
|
||||
client.print(F(" set to analog "));
|
||||
client.println(value);
|
||||
|
||||
// Update datastore key with the current pin value
|
||||
String key = "D";
|
||||
key += pin;
|
||||
Bridge.put(key, String(value));
|
||||
} else {
|
||||
// Read analog pin
|
||||
value = analogRead(pin);
|
||||
|
||||
// Send feedback to client
|
||||
client.print(F("Pin A"));
|
||||
client.print(pin);
|
||||
client.print(F(" reads analog "));
|
||||
client.println(value);
|
||||
|
||||
// Update datastore key with the current pin value
|
||||
String key = "A";
|
||||
key += pin;
|
||||
Bridge.put(key, String(value));
|
||||
}
|
||||
}
|
||||
|
||||
void modeCommand(BridgeClient client) {
|
||||
int pin;
|
||||
|
||||
// Read pin number
|
||||
pin = client.parseInt();
|
||||
|
||||
// If the next character is not a '/' we have a malformed URL
|
||||
if (client.read() != '/') {
|
||||
client.println(F("error"));
|
||||
return;
|
||||
}
|
||||
|
||||
String mode = client.readStringUntil('\r');
|
||||
|
||||
if (mode == "input") {
|
||||
pinMode(pin, INPUT);
|
||||
// Send feedback to client
|
||||
client.print(F("Pin D"));
|
||||
client.print(pin);
|
||||
client.print(F(" configured as INPUT!"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == "output") {
|
||||
pinMode(pin, OUTPUT);
|
||||
// Send feedback to client
|
||||
client.print(F("Pin D"));
|
||||
client.print(pin);
|
||||
client.print(F(" configured as OUTPUT!"));
|
||||
return;
|
||||
}
|
||||
|
||||
client.print(F("error: invalid mode "));
|
||||
client.print(mode);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
Console ASCII table for YunShield/Yún
|
||||
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:
|
||||
- YunShield/Yún
|
||||
|
||||
created 2006
|
||||
by Nicholas Zambetti
|
||||
http://www.zambetti.com
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
modified 22 May 2013
|
||||
by Cristian Maglie
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ConsoleAsciiTable
|
||||
|
||||
*/
|
||||
|
||||
#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++;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Console Pixel
|
||||
|
||||
An example of using YunShield/Yún board to receive data from the
|
||||
Console on the Yún. In this case, the board turns on an LED when
|
||||
it receives the character 'H', and turns off the LED when it
|
||||
receives the character 'L'.
|
||||
|
||||
To see the Console, pick your Yún's name and IP address in the Port menu
|
||||
then open the Port Monitor. You can also see it by opening a terminal window
|
||||
and typing
|
||||
ssh root@ yourYunsName.local 'telnet localhost 6571'
|
||||
then pressing enter. When prompted for the password, enter it.
|
||||
|
||||
|
||||
The circuit:
|
||||
* LED connected from digital pin 13 to ground
|
||||
|
||||
created 2006
|
||||
by David A. Mellis
|
||||
modified 25 Jun 2013
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ConsolePixel
|
||||
|
||||
*/
|
||||
|
||||
#include <Console.h>
|
||||
|
||||
const int ledPin = 13; // the pin that the LED is attached to
|
||||
char incomingByte; // a variable to read incoming Console data into
|
||||
|
||||
void setup() {
|
||||
Bridge.begin(); // Initialize Bridge
|
||||
Console.begin(); // Initialize Console
|
||||
|
||||
// Wait for the Console port to connect
|
||||
while (!Console);
|
||||
|
||||
Console.println("type H or L to turn pin 13 on or off");
|
||||
|
||||
// initialize the LED pin as an output:
|
||||
pinMode(ledPin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// see if there's incoming Console data:
|
||||
if (Console.available() > 0) {
|
||||
// read the oldest byte in the Console buffer:
|
||||
incomingByte = Console.read();
|
||||
Console.println(incomingByte);
|
||||
// if it's a capital H (ASCII 72), turn on the LED:
|
||||
if (incomingByte == 'H') {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
// if it's an L (ASCII 76) turn off the LED:
|
||||
if (incomingByte == 'L') {
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Console Read example for YunShield/Yún
|
||||
|
||||
Read data coming from bridge using the Console.read() function
|
||||
and store it in a string.
|
||||
|
||||
To see the Console, pick your Yún's name and IP address in the Port menu
|
||||
then open the Port Monitor. You can also see it by opening a terminal window
|
||||
and typing:
|
||||
ssh root@ yourYunsName.local 'telnet localhost 6571'
|
||||
then pressing enter. When prompted for the password, enter it.
|
||||
|
||||
created 13 Jun 2013
|
||||
by Angelo Scialabba
|
||||
modified 16 June 2013
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ConsoleRead
|
||||
|
||||
*/
|
||||
|
||||
#include <Console.h>
|
||||
|
||||
String name;
|
||||
|
||||
void setup() {
|
||||
// Initialize Console and wait for port to open:
|
||||
Bridge.begin();
|
||||
Console.begin();
|
||||
|
||||
// Wait for Console port to connect
|
||||
while (!Console);
|
||||
|
||||
Console.println("Hi, what's your name?");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Console.available() > 0) {
|
||||
char c = Console.read(); // read the next char received
|
||||
// look for the newline character, this is the last character in the string
|
||||
if (c == '\n') {
|
||||
//print text with the name received
|
||||
Console.print("Hi ");
|
||||
Console.print(name);
|
||||
Console.println("! Nice to meet you!");
|
||||
Console.println();
|
||||
// Ask again for name and clear the old name
|
||||
Console.println("Hi, what's your name?");
|
||||
name = ""; // clear the name string
|
||||
} else { // if the buffer is empty Cosole.read() returns -1
|
||||
name += c; // append the read char from Console to the name string
|
||||
}
|
||||
} else {
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
102
arduino/libraries/Bridge/examples/Datalogger/Datalogger.ino
Normal file
102
arduino/libraries/Bridge/examples/Datalogger/Datalogger.ino
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
SD card datalogger
|
||||
|
||||
This example shows how to log data from three analog sensors
|
||||
to an SD card mounted on the YunShield/Yún using the Bridge library.
|
||||
|
||||
The circuit:
|
||||
* analog sensors on analog pins 0, 1 and 2
|
||||
* SD card attached to SD card slot of the YunShield/Yún
|
||||
|
||||
Prepare your SD card creating an empty folder in the SD root
|
||||
named "arduino". This will ensure that the Yún will create a link
|
||||
to the SD to the "/mnt/sd" path.
|
||||
|
||||
You can remove the SD card while the Linux and the
|
||||
sketch are running but be careful not to remove it while
|
||||
the system is writing to it.
|
||||
|
||||
created 24 Nov 2010
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
adapted to the Yún Bridge library 20 Jun 2013
|
||||
by Federico Vanzati
|
||||
modified 21 Jun 2013
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/YunDatalogger
|
||||
|
||||
*/
|
||||
|
||||
#include <FileIO.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize the Bridge and the Serial
|
||||
Bridge.begin();
|
||||
Serial.begin(9600);
|
||||
FileSystem.begin();
|
||||
|
||||
while (!SerialUSB); // wait for Serial port to connect.
|
||||
SerialUSB.println("Filesystem datalogger\n");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// make a string that start with a timestamp for assembling the data to log:
|
||||
String dataString;
|
||||
dataString += getTimeStamp();
|
||||
dataString += " = ";
|
||||
|
||||
// read three sensors and append to the string:
|
||||
for (int analogPin = 0; analogPin < 3; analogPin++) {
|
||||
int sensor = analogRead(analogPin);
|
||||
dataString += String(sensor);
|
||||
if (analogPin < 2) {
|
||||
dataString += ","; // separate the values with a comma
|
||||
}
|
||||
}
|
||||
|
||||
// open the file. note that only one file can be open at a time,
|
||||
// so you have to close this one before opening another.
|
||||
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
|
||||
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
|
||||
|
||||
// if the file is available, write to it:
|
||||
if (dataFile) {
|
||||
dataFile.println(dataString);
|
||||
dataFile.close();
|
||||
// print to the serial port too:
|
||||
SerialUSB.println(dataString);
|
||||
}
|
||||
// if the file isn't open, pop up an error:
|
||||
else {
|
||||
SerialUSB.println("error opening datalog.txt");
|
||||
}
|
||||
|
||||
delay(15000);
|
||||
|
||||
}
|
||||
|
||||
// This function return a string with the time stamp
|
||||
String getTimeStamp() {
|
||||
String result;
|
||||
Process time;
|
||||
// date is a command line utility to get the date and the time
|
||||
// in different formats depending on the additional parameter
|
||||
time.begin("date");
|
||||
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
|
||||
// T for the time hh:mm:ss
|
||||
time.run(); // run the command
|
||||
|
||||
// read the output of the command
|
||||
while (time.available() > 0) {
|
||||
char c = time.read();
|
||||
if (c != '\n') {
|
||||
result += c;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
Write to file using FileIO classes.
|
||||
|
||||
This sketch demonstrate how to write file into the YunShield/Yún filesystem.
|
||||
A shell script file is created in /tmp, and it is executed afterwards.
|
||||
|
||||
created 7 June 2010
|
||||
by Cristian Maglie
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/FileWriteScript
|
||||
|
||||
*/
|
||||
|
||||
#include <FileIO.h>
|
||||
|
||||
void setup() {
|
||||
// Setup Bridge (needed every time we communicate with the Arduino Yún)
|
||||
Bridge.begin();
|
||||
// Initialize the Serial
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
while (!SerialUSB); // wait for Serial port to connect.
|
||||
SerialUSB.println("File Write Script example\n\n");
|
||||
|
||||
// Setup File IO
|
||||
FileSystem.begin();
|
||||
|
||||
// Upload script used to gain network statistics
|
||||
uploadScript();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Run stats script every 5 secs.
|
||||
runScript();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
// this function creates a file into the linux processor that contains a shell script
|
||||
// to check the network traffic of the WiFi interface
|
||||
void uploadScript() {
|
||||
// Write our shell script in /tmp
|
||||
// Using /tmp stores the script in RAM this way we can preserve
|
||||
// the limited amount of FLASH erase/write cycles
|
||||
File script = FileSystem.open("/tmp/wlan-stats.sh", FILE_WRITE);
|
||||
// Shell script header
|
||||
script.print("#!/bin/sh\n");
|
||||
// shell commands:
|
||||
// ifconfig: is a command line utility for controlling the network interfaces.
|
||||
// wlan0 is the interface we want to query
|
||||
// grep: search inside the output of the ifconfig command the "RX bytes" keyword
|
||||
// and extract the line that contains it
|
||||
script.print("ifconfig wlan0 | grep 'RX bytes'\n");
|
||||
script.close(); // close the file
|
||||
|
||||
// Make the script executable
|
||||
Process chmod;
|
||||
chmod.begin("chmod"); // chmod: change mode
|
||||
chmod.addParameter("+x"); // x stays for executable
|
||||
chmod.addParameter("/tmp/wlan-stats.sh"); // path to the file to make it executable
|
||||
chmod.run();
|
||||
}
|
||||
|
||||
|
||||
// this function run the script and read the output data
|
||||
void runScript() {
|
||||
// Run the script and show results on the Serial
|
||||
Process myscript;
|
||||
myscript.begin("/tmp/wlan-stats.sh");
|
||||
myscript.run();
|
||||
|
||||
String output = "";
|
||||
|
||||
// read the output of the script
|
||||
while (myscript.available()) {
|
||||
output += (char)myscript.read();
|
||||
}
|
||||
// remove the blank spaces at the beginning and the ending of the string
|
||||
output.trim();
|
||||
SerialUSB.println(output);
|
||||
SerialUSB.flush();
|
||||
}
|
||||
51
arduino/libraries/Bridge/examples/HttpClient/HttpClient.ino
Normal file
51
arduino/libraries/Bridge/examples/HttpClient/HttpClient.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Yún HTTP Client
|
||||
|
||||
This example for the YunShield/Yún shows how create a basic
|
||||
HTTP client that connects to the internet and downloads
|
||||
content. In this case, you'll connect to the Arduino
|
||||
website and download a version of the logo as ASCII text.
|
||||
|
||||
created by Tom igoe
|
||||
May 2013
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/HttpClient
|
||||
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <HttpClient.h>
|
||||
|
||||
void setup() {
|
||||
// Bridge takes about two seconds to start up
|
||||
// it can be helpful to use the on-board LED
|
||||
// as an indicator for when it has initialized
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin();
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
while (!SerialUSB); // wait for a serial connection
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Initialize the client library
|
||||
HttpClient client;
|
||||
|
||||
// Make a HTTP request:
|
||||
client.get("http://www.arduino.cc/asciilogo.txt");
|
||||
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them:
|
||||
while (client.available()) {
|
||||
char c = client.read();
|
||||
SerialUSB.print(c);
|
||||
}
|
||||
SerialUSB.flush();
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Yún HTTP Client Console version for Arduino Uno and Mega using Yún Shield
|
||||
|
||||
This example for the YunShield/Yún shows how create a basic
|
||||
HTTP client that connects to the internet and downloads
|
||||
content. In this case, you'll connect to the Arduino
|
||||
website and download a version of the logo as ASCII text.
|
||||
|
||||
created by Tom igoe
|
||||
May 2013
|
||||
modified by Marco Brianza to use Console
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/HttpClient
|
||||
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <HttpClient.h>
|
||||
#include <Console.h>
|
||||
|
||||
void setup() {
|
||||
// Bridge takes about two seconds to start up
|
||||
// it can be helpful to use the on-board LED
|
||||
// as an indicator for when it has initialized
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin();
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
Console.begin();
|
||||
|
||||
while (!Console); // wait for a serial connection
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Initialize the client library
|
||||
HttpClient client;
|
||||
|
||||
// Make a HTTP request:
|
||||
client.get("http://www.arduino.cc/asciilogo.txt");
|
||||
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them:
|
||||
while (client.available()) {
|
||||
char c = client.read();
|
||||
Console.print(c);
|
||||
}
|
||||
Console.flush();
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Read Messages from the Mailbox
|
||||
|
||||
This example for the YunShield/Yún shows how to
|
||||
read the messages queue, called Mailbox, using the
|
||||
Bridge library.
|
||||
The messages can be sent to the queue through REST calls.
|
||||
Appen the message in the URL after the keyword "/mailbox".
|
||||
Example
|
||||
|
||||
"/mailbox/hello"
|
||||
|
||||
created 3 Feb 2014
|
||||
by Federico Vanzati & Federico Fissore
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/MailboxReadMessage
|
||||
|
||||
*/
|
||||
|
||||
#include <Mailbox.h>
|
||||
|
||||
void setup() {
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
// Initialize Bridge and Mailbox
|
||||
Bridge.begin();
|
||||
Mailbox.begin();
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
// Initialize Serial
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
// Wait until a Serial Monitor is connected.
|
||||
while (!SerialUSB);
|
||||
|
||||
SerialUSB.println("Mailbox Read Message\n");
|
||||
SerialUSB.println("The Mailbox is checked every 10 seconds. The incoming messages will be shown below.\n");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
String message;
|
||||
|
||||
// if there is a message in the Mailbox
|
||||
if (Mailbox.messageAvailable()) {
|
||||
// read all the messages present in the queue
|
||||
while (Mailbox.messageAvailable()) {
|
||||
Mailbox.readMessage(message);
|
||||
SerialUSB.println(message);
|
||||
}
|
||||
|
||||
SerialUSB.println("Waiting 10 seconds before checking the Mailbox again");
|
||||
}
|
||||
|
||||
// wait 10 seconds
|
||||
delay(10000);
|
||||
}
|
||||
71
arduino/libraries/Bridge/examples/Process/Process.ino
Normal file
71
arduino/libraries/Bridge/examples/Process/Process.ino
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Running process using Process class.
|
||||
|
||||
This sketch demonstrate how to run linux processes
|
||||
using a YunShield/Yún
|
||||
|
||||
created 5 Jun 2013
|
||||
by Cristian Maglie
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/Process
|
||||
|
||||
*/
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize Bridge
|
||||
Bridge.begin();
|
||||
|
||||
// Initialize Serial
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
// Wait until a Serial Monitor is connected.
|
||||
while (!SerialUSB);
|
||||
|
||||
// run various example processes
|
||||
runCurl();
|
||||
runCpuInfo();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Do nothing here.
|
||||
}
|
||||
|
||||
void runCurl() {
|
||||
// Launch "curl" command and get Arduino ascii art logo from the network
|
||||
// curl is command line program for transferring data using different internet protocols
|
||||
Process p; // Create a process and call it "p"
|
||||
p.begin("curl"); // Process that launch the "curl" command
|
||||
p.addParameter("http://www.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 Serial
|
||||
// A process output can be read with the stream methods
|
||||
while (p.available() > 0) {
|
||||
char c = p.read();
|
||||
SerialUSB.print(c);
|
||||
}
|
||||
// Ensure the last bit of data is sent.
|
||||
SerialUSB.flush();
|
||||
}
|
||||
|
||||
void runCpuInfo() {
|
||||
// Launch "cat /proc/cpuinfo" command (shows info on Atheros CPU)
|
||||
// cat is a command line utility that shows the content of a file
|
||||
Process p; // Create a process and call it "p"
|
||||
p.begin("cat"); // Process that launch the "cat" command
|
||||
p.addParameter("/proc/cpuinfo"); // Add the cpuifo file path as parameter to cut
|
||||
p.run(); // Run the process and wait for its termination
|
||||
|
||||
// Print command output on the SerialUSB.
|
||||
// A process output can be read with the stream methods
|
||||
while (p.available() > 0) {
|
||||
char c = p.read();
|
||||
SerialUSB.print(c);
|
||||
}
|
||||
// Ensure the last bit of data is sent.
|
||||
SerialUSB.flush();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
Blink
|
||||
Turns on an LED on for one second, then off for one second, repeatedly.
|
||||
|
||||
Most Arduinos have an on-board LED you can control. On the Uno and
|
||||
Leonardo, it is attached to digital pin 13. If you're unsure what
|
||||
pin the on-board LED is connected to on your Arduino model, check
|
||||
the documentation at http://www.arduino.cc
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
modified 8 May 2014
|
||||
by Scott Fitzgerald
|
||||
|
||||
modified by Marco Brianza to show the remote sketch update feature on Arduino Due using Yún Shield
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup() {
|
||||
checkForRemoteSketchUpdate();
|
||||
// initialize digital pin 13 as an output.
|
||||
pinMode(13, OUTPUT);
|
||||
}
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
void loop() {
|
||||
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
||||
delay(100); // wait for a second
|
||||
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
|
||||
delay(100); // wait for a second
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Running shell commands using Process class.
|
||||
|
||||
This sketch demonstrate how to run linux shell commands
|
||||
using a YunShield/Yún. It runs the wifiCheck script on the Linux side
|
||||
of the Yún, then uses grep to get just the signal strength line.
|
||||
Then it uses parseInt() to read the wifi signal strength as an integer,
|
||||
and finally uses that number to fade an LED using analogWrite().
|
||||
|
||||
The circuit:
|
||||
* YunShield/Yún with LED connected to pin 9
|
||||
|
||||
created 12 Jun 2013
|
||||
by Cristian Maglie
|
||||
modified 25 June 2013
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/ShellCommands
|
||||
|
||||
*/
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
void setup() {
|
||||
Bridge.begin(); // Initialize the Bridge
|
||||
SerialUSB.begin(9600); // Initialize the Serial
|
||||
|
||||
// Wait until a Serial Monitor is connected.
|
||||
while (!SerialUSB);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Process p;
|
||||
// This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then
|
||||
// sends the result to the grep command to look for a line containing the word
|
||||
// "Signal:" the result is passed to this sketch:
|
||||
p.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal");
|
||||
|
||||
// do nothing until the process finishes, so you get the whole output:
|
||||
while (p.running());
|
||||
|
||||
// Read command output. runShellCommand() should have passed "Signal: xx&":
|
||||
while (p.available()) {
|
||||
int result = p.parseInt(); // look for an integer
|
||||
int signal = map(result, 0, 100, 0, 255); // map result from 0-100 range to 0-255
|
||||
analogWrite(9, signal); // set the brightness of LED on pin 9
|
||||
SerialUSB.println(result); // print the number as well
|
||||
}
|
||||
delay(5000); // wait 5 seconds before you do it again
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
Temperature web interface
|
||||
|
||||
This example shows how to serve data from an analog input
|
||||
via the YunShield/Yún built-in webserver using the Bridge library.
|
||||
|
||||
The circuit:
|
||||
* TMP36 temperature sensor on analog pin A1
|
||||
* SD card attached to SD card slot of the YunShield/Yún
|
||||
|
||||
This sketch must be uploaded via wifi. REST API must be set to "open".
|
||||
|
||||
Prepare your SD card with an empty folder in the SD root
|
||||
named "arduino" and a subfolder of that named "www".
|
||||
This will ensure that the Yún will create a link
|
||||
to the SD to the "/mnt/sd" path.
|
||||
|
||||
In this sketch folder is a basic webpage and a copy of zepto.js, a
|
||||
minimized version of jQuery. When you upload your sketch, these files
|
||||
will be placed in the /arduino/www/TemperatureWebPanel folder on your SD card.
|
||||
|
||||
You can then go to http://arduino.local/sd/TemperatureWebPanel
|
||||
to see the output of this sketch.
|
||||
|
||||
You can remove the SD card while the Linux and the
|
||||
sketch are running but be careful not to remove it while
|
||||
the system is writing to it.
|
||||
|
||||
created 6 July 2013
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/TemperatureWebPanel
|
||||
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <BridgeServer.h>
|
||||
#include <BridgeClient.h>
|
||||
|
||||
// Listen on default port 5555, the webserver on the Yún
|
||||
// will forward there all the HTTP requests for us.
|
||||
BridgeServer server;
|
||||
String startString;
|
||||
long hits = 0;
|
||||
|
||||
void setup() {
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
// Bridge startup
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin();
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
// using A0 and A2 as vcc and gnd for the TMP36 sensor:
|
||||
pinMode(A0, OUTPUT);
|
||||
pinMode(A2, OUTPUT);
|
||||
digitalWrite(A0, HIGH);
|
||||
digitalWrite(A2, LOW);
|
||||
|
||||
// Listen for incoming connection only from localhost
|
||||
// (no one from the external network could connect)
|
||||
server.listenOnLocalhost();
|
||||
server.begin();
|
||||
|
||||
// get the time that this sketch started:
|
||||
Process startTime;
|
||||
startTime.runShellCommand("date");
|
||||
while (startTime.available()) {
|
||||
char c = startTime.read();
|
||||
startString += c;
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Get clients coming from server
|
||||
BridgeClient client = server.accept();
|
||||
|
||||
// There is a new client?
|
||||
if (client) {
|
||||
// read the command
|
||||
String command = client.readString();
|
||||
command.trim(); //kill whitespace
|
||||
SerialUSB.println(command);
|
||||
// is "temperature" command?
|
||||
if (command == "temperature") {
|
||||
|
||||
// get the time from the server:
|
||||
Process time;
|
||||
time.runShellCommand("date");
|
||||
String timeString = "";
|
||||
while (time.available()) {
|
||||
char c = time.read();
|
||||
timeString += c;
|
||||
}
|
||||
SerialUSB.println(timeString);
|
||||
int sensorValue = analogRead(A1);
|
||||
// convert the reading to millivolts:
|
||||
float voltage = sensorValue * (5000.0f / 1024.0f);
|
||||
// convert the millivolts to temperature celsius:
|
||||
float temperature = (voltage - 500.0f) / 10.0f;
|
||||
// print the temperature:
|
||||
client.print("Current time on the Yún: ");
|
||||
client.println(timeString);
|
||||
client.print("<br>Current temperature: ");
|
||||
client.print(temperature);
|
||||
client.print(" °C");
|
||||
client.print("<br>This sketch has been running since ");
|
||||
client.print(startString);
|
||||
client.print("<br>Hits so far: ");
|
||||
client.print(hits);
|
||||
}
|
||||
|
||||
// Close connection and free resources.
|
||||
client.stop();
|
||||
hits++;
|
||||
}
|
||||
|
||||
delay(50); // Poll every 50ms
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script type="text/javascript" src="zepto.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
function refresh() {
|
||||
$('#content').load('/arduino/temperature');
|
||||
}
|
||||
</script>
|
||||
|
||||
</head>
|
||||
<body onload="setInterval(refresh, 2000);">
|
||||
<span id="content">0</span>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
2
arduino/libraries/Bridge/examples/TemperatureWebPanel/www/zepto.min.js
vendored
Normal file
2
arduino/libraries/Bridge/examples/TemperatureWebPanel/www/zepto.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
88
arduino/libraries/Bridge/examples/TimeCheck/TimeCheck.ino
Normal file
88
arduino/libraries/Bridge/examples/TimeCheck/TimeCheck.ino
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Time Check
|
||||
|
||||
Gets the time from Linux via Bridge then parses out hours,
|
||||
minutes and seconds using a YunShield/Yún.
|
||||
|
||||
created 27 May 2013
|
||||
modified 21 June 2013
|
||||
By Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/TimeCheck
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#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() {
|
||||
Bridge.begin(); // initialize Bridge
|
||||
SerialUSB.begin(9600); // initialize serial
|
||||
|
||||
while (!Serial); // wait for Serial Monitor to open
|
||||
SerialUSB.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) {
|
||||
SerialUSB.print("0"); // adjust for 0-9
|
||||
}
|
||||
SerialUSB.print(hours);
|
||||
SerialUSB.print(":");
|
||||
if (minutes <= 9) {
|
||||
SerialUSB.print("0"); // adjust for 0-9
|
||||
}
|
||||
SerialUSB.print(minutes);
|
||||
SerialUSB.print(":");
|
||||
if (seconds <= 9) {
|
||||
SerialUSB.print("0"); // adjust for 0-9
|
||||
}
|
||||
SerialUSB.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();
|
||||
}
|
||||
|
||||
}
|
||||
51
arduino/libraries/Bridge/examples/WiFiStatus/WiFiStatus.ino
Normal file
51
arduino/libraries/Bridge/examples/WiFiStatus/WiFiStatus.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
WiFi Status
|
||||
|
||||
This sketch runs a script called "pretty-wifi-info.lua"
|
||||
installed on your Yún in folder /usr/bin.
|
||||
It prints information about the status of your wifi connection.
|
||||
|
||||
It uses Serial to print, so you need to connect your YunShield/Yún to your
|
||||
computer using a USB cable and select the appropriate port from
|
||||
the Port menu
|
||||
|
||||
created 18 June 2013
|
||||
By Federico Fissore
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/YunWiFiStatus
|
||||
|
||||
*/
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
void setup() {
|
||||
SerialUSB.begin(9600); // initialize serial communication
|
||||
while (!SerialUSB); // do nothing until the serial monitor is opened
|
||||
|
||||
SerialUSB.println("Starting bridge...\n");
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin(); // make contact with the linux processor
|
||||
digitalWrite(13, HIGH); // Led on pin 13 turns on when the bridge is ready
|
||||
|
||||
delay(2000); // wait 2 seconds
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Process wifiCheck; // initialize a new process
|
||||
|
||||
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua"); // command you want to run
|
||||
|
||||
// while there's any characters coming back from the
|
||||
// process, print them to the serial monitor:
|
||||
while (wifiCheck.available() > 0) {
|
||||
char c = wifiCheck.read();
|
||||
SerialUSB.print(c);
|
||||
}
|
||||
|
||||
SerialUSB.println();
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
|
||||
/*
|
||||
Arduino Yún First configuration sketch
|
||||
|
||||
Configures the YunShield/Yún WiFi and infos via the Bridge
|
||||
Works correctly if Line Ending is set as "NewLine"
|
||||
If your board has two USB ports, use the Native one
|
||||
|
||||
The circuit:
|
||||
Arduino YunShield
|
||||
(or any Yun model with firmware > 1.6.1)
|
||||
|
||||
created March 2016
|
||||
by Arduino LLC
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/YunFirstConfig
|
||||
*/
|
||||
|
||||
#include <Process.h>
|
||||
|
||||
#define MAX_WIFI_LIST 10
|
||||
|
||||
String networks[MAX_WIFI_LIST];
|
||||
String yunName;
|
||||
String yunPassword;
|
||||
|
||||
void setup() {
|
||||
SERIAL_PORT_USBVIRTUAL.begin(9600); // initialize serial communication
|
||||
while (!SERIAL_PORT_USBVIRTUAL); // do nothing until the serial monitor is opened
|
||||
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Hi! Nice to see you!"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("I'm your YunShield assistant sketch"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("I'll help you configuring your Yun in a matter of minutes"));
|
||||
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Let's start by communicating with the Linux processor"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("When LED (L13) will light up we'll be ready to go!"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Waiting..."));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("(in the meanwhile, if you are using the IDE's serial monitor, make sure that it's configured to send a \"Newline\")\n"));
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin(); // make contact with the linux processor
|
||||
digitalWrite(13, HIGH); // Led on pin 13 turns on when the bridge is ready
|
||||
|
||||
// Recover if the board is in AP mode - unused
|
||||
Process wifiList;
|
||||
bool master = false;
|
||||
wifiList.runShellCommand(F("iwinfo | grep \"Mode: Master\""));
|
||||
while (wifiList.available() > 0) {
|
||||
wifiList.read();
|
||||
master = true;
|
||||
}
|
||||
|
||||
// Get the list of reachable networks
|
||||
wifiList.runShellCommand(F("iwinfo wlan0 scan | grep ESSID | cut -d\"\\\"\" -f2"));
|
||||
|
||||
uint8_t num_networks = 0;
|
||||
uint8_t i = 0;
|
||||
char c;
|
||||
bool dropNet = false;
|
||||
|
||||
networks[0].reserve(32);
|
||||
|
||||
while (wifiList.available() > 0) {
|
||||
c = wifiList.read();
|
||||
if (c != '\n') {
|
||||
networks[i] += c;
|
||||
} else {
|
||||
// check if we already found networks[i] and eventually drop it
|
||||
for (uint8_t s = 0; s < i; s++) {
|
||||
if (networks[i].equals(networks[s])) {
|
||||
dropNet = true;
|
||||
}
|
||||
}
|
||||
if (i <= MAX_WIFI_LIST && dropNet == false) {
|
||||
networks[i++].reserve(32);
|
||||
} else {
|
||||
dropNet = false;
|
||||
networks[i]="";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
num_networks = i;
|
||||
|
||||
String encryption;
|
||||
String password;
|
||||
int chose = 0;
|
||||
|
||||
// If networks number is 0, start manual configuration
|
||||
if (num_networks == 0) {
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Oops, it seems that you have no WiFi network available"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Let's configure it manually"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("SSID of the network you want to connect to: "));
|
||||
networks[0] = getUserInput(networks[0], false);
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Password for the network you want to connect to: "));
|
||||
password = getUserInput(password, true);
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Encryption (eg WPA, WPA2, WEP): "));
|
||||
encryption = getUserInput(encryption, false);
|
||||
} else {
|
||||
// else print them prepending a number
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("It looks like you have "));
|
||||
SERIAL_PORT_USBVIRTUAL.print(num_networks);
|
||||
SERIAL_PORT_USBVIRTUAL.println(F(" networks around you "));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Which one do you want to connect to?\n"));
|
||||
for (i = 0; i < num_networks && i < MAX_WIFI_LIST; i++) {
|
||||
SERIAL_PORT_USBVIRTUAL.print(i);
|
||||
SERIAL_PORT_USBVIRTUAL.println(") " + networks[i]);
|
||||
}
|
||||
String selection;
|
||||
selection = getUserInput(selection, false);
|
||||
chose = atoi(selection.c_str());
|
||||
}
|
||||
|
||||
// Extract the selected network security
|
||||
bool openNet = false;
|
||||
wifiList.runShellCommand("iwinfo wlan0 scan | grep \"" + networks[chose] + "\" -A5 | grep Encryption | cut -f2 -d\":\"");
|
||||
while (wifiList.available() > 0) {
|
||||
c = wifiList.read();
|
||||
encryption += c;
|
||||
}
|
||||
|
||||
if (encryption.indexOf("none") >= 0) {
|
||||
openNet = true;
|
||||
encryption = "none";
|
||||
}
|
||||
if (encryption.indexOf("WPA2") >= 0) {
|
||||
encryption = "psk2";
|
||||
}
|
||||
if (encryption.indexOf("WPA") >= 0) {
|
||||
encryption = "psk";
|
||||
}
|
||||
if (encryption.indexOf("WEP") >= 0) {
|
||||
encryption = "wep";
|
||||
}
|
||||
|
||||
if (openNet == false && password.length() == 0) {
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("It looks like you need a password to connect to "));
|
||||
SERIAL_PORT_USBVIRTUAL.println(networks[chose]);
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Write it here: "));
|
||||
password = getUserInput(password, true);
|
||||
}
|
||||
|
||||
// Change hostname/root password
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("We are almost done! Give a name and a password to your Yun"));
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Name: "));
|
||||
yunName = getUserInput(yunName, false);
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Password: "));
|
||||
yunPassword = getUserInput(yunPassword, true);
|
||||
|
||||
// Select a country code
|
||||
String countryCode;
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("One last question: where do you live?"));
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Insert a two letters county code (eg IT, US, DE): "));
|
||||
countryCode = getUserInput(countryCode, false);
|
||||
|
||||
yunName.trim();
|
||||
yunPassword.trim();
|
||||
networks[chose].trim();
|
||||
password.trim();
|
||||
countryCode.trim();
|
||||
|
||||
// Configure the Yun with user provided strings
|
||||
wifiConfig(yunName, yunPassword, networks[chose], password, "YUN" + yunName + "AP", countryCode, encryption);
|
||||
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Waiting for the Yun to connect to the network"));
|
||||
}
|
||||
|
||||
bool Connected = false;
|
||||
bool serialTerminalMode = false;
|
||||
int runs = 0;
|
||||
|
||||
void loop() {
|
||||
if (!serialTerminalMode) {
|
||||
String resultStr = "";
|
||||
|
||||
if (!Connected) {
|
||||
SERIAL_PORT_USBVIRTUAL.print(".");
|
||||
runs++;
|
||||
}
|
||||
|
||||
// If it takes more than 20 seconds to connect, stop trying
|
||||
if (runs > 20) {
|
||||
SERIAL_PORT_USBVIRTUAL.println("");
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("We couldn't connect to the network."));
|
||||
SERIAL_PORT_USBVIRTUAL.println(F("Restart the board if you want to execute the wizard again"));
|
||||
resultStr = getUserInput(resultStr, false);
|
||||
}
|
||||
|
||||
// Check if we have an IP address
|
||||
Process wifiCheck;
|
||||
wifiCheck.runShellCommand(F("/usr/bin/pretty-wifi-info.lua | grep \"IP address\" | cut -f2 -d\":\" | cut -f1 -d\"/\"" )); // command you want to run
|
||||
while (wifiCheck.available() > 0) {
|
||||
char c = wifiCheck.read();
|
||||
resultStr += c;
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
|
||||
if (resultStr != "") {
|
||||
// We got an IP, freeze the loop, display the value and "spawn" a serial terminal
|
||||
Connected = true;
|
||||
resultStr.trim();
|
||||
SERIAL_PORT_USBVIRTUAL.println("");
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("\nGreat! You can now reach your Yun from a browser typing http://"));
|
||||
SERIAL_PORT_USBVIRTUAL.println(resultStr);
|
||||
SERIAL_PORT_USBVIRTUAL.print(F("Press 'Enter' key twice to start a serial terminal"));
|
||||
resultStr = getUserInput(resultStr, false);
|
||||
serialTerminalMode = true;
|
||||
//startSerialTerminal();
|
||||
SERIAL_PORT_HARDWARE.write((uint8_t *)"\xff\0\0\x05XXXXX\x7f\xf9", 11); // send "bridge shutdown" command
|
||||
delay(100);
|
||||
SERIAL_PORT_HARDWARE.println("\nreset\n\n");
|
||||
SERIAL_PORT_HARDWARE.flush();
|
||||
SERIAL_PORT_HARDWARE.println("\nreset\n\n");
|
||||
SERIAL_PORT_HARDWARE.write((uint8_t *)"\n", 1);
|
||||
}
|
||||
|
||||
} else {
|
||||
loopSerialTerminal();
|
||||
}
|
||||
}
|
||||
|
||||
String getUserInput(String out, bool obfuscated) {
|
||||
/*
|
||||
while (SerialUSB.available() <= 0) {}
|
||||
while (SerialUSB.available() > 0) {
|
||||
char c = SerialUSB.read();
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
*/
|
||||
while (SERIAL_PORT_USBVIRTUAL.available() <= 0) {}
|
||||
while (1) {
|
||||
char c = SERIAL_PORT_USBVIRTUAL.read();
|
||||
if (c == '\n' || c == '\r')
|
||||
break;
|
||||
else {
|
||||
if (c != -1) {
|
||||
out += c;
|
||||
if (obfuscated)
|
||||
SERIAL_PORT_USBVIRTUAL.print("*");
|
||||
else
|
||||
SERIAL_PORT_USBVIRTUAL.print(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
SERIAL_PORT_USBVIRTUAL.println("");
|
||||
return out;
|
||||
}
|
||||
|
||||
void wifiConfig(String yunName, String yunPsw, String wifissid, String wifipsw, String wifiAPname, String countryCode, String encryption) {
|
||||
Process p;
|
||||
|
||||
p.runShellCommand("blink-start 100"); //start the blue blink
|
||||
|
||||
p.runShellCommand("hostname " + yunName); //change the current hostname
|
||||
p.runShellCommand("uci set system.@system[0].hostname='" + yunName + "'"); //change teh hostname in uci
|
||||
|
||||
p.runShellCommand("uci set arduino.@arduino[0].access_point_wifi_name='" + wifiAPname + "'");
|
||||
|
||||
//this block resets the wifi psw
|
||||
p.runShellCommand("uci set wireless.@wifi-iface[0].encryption='" + encryption + "'");
|
||||
p.runShellCommand("uci set wireless.@wifi-iface[0].mode='sta'\n");
|
||||
p.runShellCommand("uci set wireless.@wifi-iface[0].ssid='" + wifissid + "'");
|
||||
p.runShellCommand("uci set wireless.@wifi-iface[0].key='" + wifipsw + "'");
|
||||
p.runShellCommand("uci set wireless.radio0.channel='auto'");
|
||||
p.runShellCommand("uci set wireless.radio0.country='" + countryCode + "'");
|
||||
p.runShellCommand("uci delete network.lan.ipaddr");
|
||||
p.runShellCommand("uci delete network.lan.netmask");
|
||||
p.runShellCommand("uci set network.lan.proto='dhcp'");
|
||||
|
||||
p.runShellCommand("echo -e \"" + yunPsw + "\n" + yunPsw + "\" | passwd root"); //change the passwors
|
||||
p.runShellCommand("uci commit"); //save the mods done via UCI
|
||||
p.runShellCommand("blink-stop"); //start the blue blink
|
||||
|
||||
p.runShellCommand("wifi ");
|
||||
}
|
||||
|
||||
long linuxBaud = 250000;
|
||||
|
||||
void startSerialTerminal() {
|
||||
SERIAL_PORT_USBVIRTUAL.begin(115200); // open serial connection via USB-Serial
|
||||
SERIAL_PORT_HARDWARE.begin(linuxBaud); // open serial connection to Linux
|
||||
}
|
||||
|
||||
boolean commandMode = false;
|
||||
void loopSerialTerminal() {
|
||||
// copy from USB-CDC to UART
|
||||
int c = SERIAL_PORT_USBVIRTUAL.read(); // read from USB-CDC
|
||||
if (c != -1) { // got anything?
|
||||
if (commandMode == false) { // if we aren't in command mode...
|
||||
if (c == '~') { // Tilde '~' key pressed?
|
||||
commandMode = true; // enter in command mode
|
||||
} else {
|
||||
SERIAL_PORT_HARDWARE.write(c); // otherwise write char to UART
|
||||
}
|
||||
} else { // if we are in command mode...
|
||||
if (c == '0') { // '0' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(57600); // set speed to 57600
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 57600");
|
||||
} else if (c == '1') { // '1' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(115200); // set speed to 115200
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 115200");
|
||||
} else if (c == '2') { // '2' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(250000); // set speed to 250000
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 250000");
|
||||
} else if (c == '3') { // '3' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(500000); // set speed to 500000
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 500000");
|
||||
} else if (c == '~') { // '~` key pressed?
|
||||
SERIAL_PORT_HARDWARE.write((uint8_t *)"\xff\0\0\x05XXXXX\x7f\xf9", 11); // send "bridge shutdown" command
|
||||
SERIAL_PORT_USBVIRTUAL.println("Sending bridge's shutdown command");
|
||||
} else { // any other key pressed?
|
||||
SERIAL_PORT_HARDWARE.write('~'); // write '~' to UART
|
||||
SERIAL_PORT_HARDWARE.write(c); // write char to UART
|
||||
}
|
||||
commandMode = false; // in all cases exit from command mode
|
||||
}
|
||||
}
|
||||
|
||||
// copy from UART to USB-CDC
|
||||
c = SERIAL_PORT_HARDWARE.read(); // read from UART
|
||||
if (c != -1) { // got anything?
|
||||
SERIAL_PORT_USBVIRTUAL.write(c); // write to USB-CDC
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
Arduino Yún USB-to-Serial
|
||||
|
||||
Allows you to use the YunShield/Yún processor as a
|
||||
serial terminal for the Linux side on the Yún.
|
||||
|
||||
Upload this to a YunShield/Yún via serial (not WiFi) then open
|
||||
the serial monitor at 115200 to see the boot process of Linux.
|
||||
You can also use the serial monitor as a basic command line
|
||||
interface for Linux 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
|
||||
'~' followed by '2' -> Set the UART speed to 250000 baud
|
||||
'~' followed by '3' -> Set the UART speed to 500000 baud
|
||||
'~' followed by '~' -> Sends the bridge's shutdown command to
|
||||
obtain the console.
|
||||
|
||||
The circuit:
|
||||
YunShield/Yún
|
||||
|
||||
created March 2013
|
||||
by Massimo Banzi
|
||||
modified by Cristian Maglie
|
||||
|
||||
This example code is in the public domain.
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/YunSerialTerminal
|
||||
|
||||
*/
|
||||
|
||||
long linuxBaud = 250000;
|
||||
|
||||
void setup() {
|
||||
SERIAL_PORT_USBVIRTUAL.begin(115200); // open serial connection via USB-Serial
|
||||
SERIAL_PORT_HARDWARE.begin(linuxBaud); // open serial connection to Linux
|
||||
}
|
||||
|
||||
boolean commandMode = false;
|
||||
|
||||
void loop() {
|
||||
// copy from USB-CDC to UART
|
||||
int c = SERIAL_PORT_USBVIRTUAL.read(); // read from USB-CDC
|
||||
if (c != -1) { // got anything?
|
||||
if (commandMode == false) { // if we aren't in command mode...
|
||||
if (c == '~') { // Tilde '~' key pressed?
|
||||
commandMode = true; // enter in command mode
|
||||
} else {
|
||||
SERIAL_PORT_HARDWARE.write(c); // otherwise write char to UART
|
||||
}
|
||||
} else { // if we are in command mode...
|
||||
if (c == '0') { // '0' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(57600); // set speed to 57600
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 57600");
|
||||
} else if (c == '1') { // '1' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(115200); // set speed to 115200
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 115200");
|
||||
} else if (c == '2') { // '2' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(250000); // set speed to 250000
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 250000");
|
||||
} else if (c == '3') { // '3' key pressed?
|
||||
SERIAL_PORT_HARDWARE.begin(500000); // set speed to 500000
|
||||
SERIAL_PORT_USBVIRTUAL.println("Speed set to 500000");
|
||||
} else if (c == '~') { // '~` key pressed?
|
||||
SERIAL_PORT_HARDWARE.write((uint8_t *)"\xff\0\0\x05XXXXX\x7f\xf9", 11); // send "bridge shutdown" command
|
||||
SERIAL_PORT_USBVIRTUAL.println("Sending bridge's shutdown command");
|
||||
} else { // any other key pressed?
|
||||
SERIAL_PORT_HARDWARE.write('~'); // write '~' to UART
|
||||
SERIAL_PORT_HARDWARE.write(c); // write char to UART
|
||||
}
|
||||
commandMode = false; // in all cases exit from command mode
|
||||
}
|
||||
}
|
||||
|
||||
// copy from UART to USB-CDC
|
||||
c = SERIAL_PORT_HARDWARE.read(); // read from UART
|
||||
if (c != -1) { // got anything?
|
||||
SERIAL_PORT_USBVIRTUAL.write(c); // write to USB-CDC
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user