Modifications

Sauter à la navigation Sauter à la recherche
11 743 octets ajoutés ,  12 novembre 2013 à 11:36
Ligne 225 : Ligne 225 :  
== Programming the 32U4 over WiFi ==
 
== Programming the 32U4 over WiFi ==
 
{{traduction}}
 
{{traduction}}
 +
When your Yún is on the same network as your computer, you can connect to it wirelessly to program it.
 +
 +
After configuring your Yún, connect to the network you specified in the configuration settings. Open the Arduino IDE.
 +
 +
Under the ''Tools > Port'' menu, you should see an entry that lists your Yún's name and its IP address. Under the ''Board'' menu, select Arduino Yún.
 +
 +
Open the Blink example (''File > Examples > 01Basics > Blink'') and upload the sketch to the board. You will be prompted for the administrator password. Use the one you entered in the configuration screen.
 +
 +
Once the program is uploaded, the 32U4 processor will restart. You should see the LED connected to pin 13 blinking.
 +
 +
== Using the onboard Ethernet ==
 +
When you connect the Yun to a wired network with an ethernet cable, it will try to connect automatically via DHCP. The board will show up on the ports menu just as it would over WiFi.
 +
 +
If you want to connect the Yun directly to your computer, either configure the computer's interfaces to have a static IP address, or act as a DHCP server.
 +
 +
Note: the ethernet interface is eth1, not eth0
 +
 +
== Communicating with Linino via Bridge ==
 +
 +
The Bridge library enables communication between Arduino and Linino. There are several different utility classes that facilitate different kinds of communication between the two, described below, and more in depth on the Bridge library reference pages.
 +
 +
xxx IMAGE
 +
 +
== La Console ==
 +
 +
The Console, based on Bridge, enables you to send information from the Yún to a computer just as you would with the serial monitor, but wirelessly. It creates a secure connection between the Yún and your computer via SSH.
 +
 +
Load the following onto your Yún :
 +
The WiFi and Ethernet interfaces, USB host, and SD card are all connected to the AR9331. The Bridge library allows you to work with these devices, as well as run scripts and communicate with web services.
 +
 +
<nowiki>#include <Console.h>
 +
 +
const int ledPin = 13; // the pin that the LED is attached to
 +
int incomingByte;      // a variable to read incoming serial data into
 +
 +
void setup() {
 +
  // initialize serial communication:
 +
  Bridge.begin();
 +
  Console.begin();
 +
 +
  while (!Console){
 +
    ; // wait for Console port to connect.
 +
  }
 +
  Console.println("You're connected to the Console!!!!");
 +
  // initialize the LED pin as an output:
 +
  pinMode(ledPin, OUTPUT);
 +
}
 +
 +
void loop() {
 +
  // see if there's incoming serial data:
 +
  if (Console.available() > 0) {
 +
    // read the oldest byte in the serial buffer:
 +
    incomingByte = Console.read();
 +
    // 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);
 +
    }
 +
  }
 +
}</nowiki>
 +
 +
To see the Console, select your Yún's name and IP address in the Port menu. The Yún will only show up in the Ports menu if your computer is on the same LAN as the Yún. If your board is on a different network, you won't see it in the Ports menu. Open the Port Monitor. You'll be prompted for the Yún's password.
 +
 +
You can also see the Console by opening a terminal window and typing
 +
 +
<nowiki>ssh root@yourYunsName.local 'telnet localhost 6571'</nowiki>
 +
 +
then pressing enter.
 +
 +
Note: If you are using Windows, you must install a terminal emulator. PuTTY is a reasonable choice, but you will have to enter the two commands above separately.
 +
 +
Type 'H' to turn the LED on pin 13 on and type 'L' to turn it off.
 +
 +
== Process ==
 +
The Process commands allow you to run Linux processes on Linino through the Arduino.
 +
 +
In the following example, The Linino will connect to a server with curl, and download some ASCII text. It prints the text a serial connection.
 +
<nowiki>#include <Process.h>
 +
 +
void setup() {
 +
  // Initialize Bridge
 +
  Bridge.begin();
 +
 +
  // Initialize Serial
 +
  Serial.begin(9600);
 +
 +
  // Wait until a Serial Monitor is connected.
 +
  while (!Serial);
 +
 +
  // run various example processes
 +
  runCurl();
 +
}
 +
 +
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://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();
 +
    Serial.print(c);
 +
  }
 +
  // Ensure the last bit of data is sent.
 +
  Serial.flush();
 +
}</nowiki>
 +
 +
== Using Bridge to pass information between processors ==
 +
 +
Bridge allows you to pass information between the two processors using a key/value pairing.
 +
 +
This example 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.
 +
 +
When running this example, make sure your computer is on the same network as the Yun.
 +
 +
When you have have programmed the board, you can request the value on a pin, write a value to a pin, and configure a pin as an input or output.
 +
 +
When the REST password is turned off, you can use a browser with the following URL structure :
 +
* [http://myArduinoYun.local/arduino/digital/13 http://myArduinoYun.local/arduino/digital/13] : calls digitalRead(13);
 +
* [http://myArduinoYun.local/arduino/digital/13/1 http://myArduinoYun.local/arduino/digital/13/1] : calls digitalWrite(13,1);
 +
* [http://myArduinoYun.local/arduino/analog/9/123 http://myArduinoYun.local/arduino/analog/9/123] : analogWrite(9,123);
 +
* [http://myArduinoYun.local/arduino/analog/2 http://myArduinoYun.local/arduino/analog/2] : analogRead(2);
 +
* [http://myArduinoYun.local/arduino/mode/13/input http://myArduinoYun.local/arduino/mode/13/input] : pinMode(13, INPUT);
 +
* [http://myArduinoYun.local/arduino/mode/13/output http://myArduinoYun.local/arduino/mode/13/output] : pinMode(13, OUTPUT);
 +
 +
You can use the curl command from the command line instead of a browser if you prefer.
 +
 +
You need to include the Bridge, YunServer, and YunClient libraries :
 +
<nowiki>#include <Bridge.h>
 +
#include <YunServer.h>
 +
#include <YunClient.h></nowiki>
 +
 +
Instantiate a server enabling the the Yun to listen for connected clients.
 +
 +
YunServer server;
 +
 +
In '''setup()''', start serial communication for debugging purposes, and turn the built-in LED on pin 13 high while Bridge begins. '''Bridge.begin()''' is blocking, and should take about 2 seconds to complete. Once Bridge starts up, turn the LED off.
 +
 +
<nowiki>void setup() {
 +
  Serial.begin(9600);
 +
  pinMode(13,OUTPUT);
 +
  digitalWrite(13, LOW);
 +
  Bridge.begin();
 +
  digitalWrite(13, HIGH);</nowiki>
 +
 +
In the second part of '''setup()''', tell the instance of YunServer to listen for incoming connections only coming from localhost. Connections made to Linino will be passed to the 32U4 processor for parsing and controlling the pins. This happens on port 5555. Start the server with '''server.begin()'''.
 +
 +
<nowiki>  server.listenOnLocalhost();
 +
  server.begin();
 +
}</nowiki>
 +
 +
In '''loop()''', you'll create an instance of the YunClient for managing the connection. If the client connects, process the requests in a custom function (described below) and close the connection when finished.
 +
 +
Putting a delay at the end of '''loop()''' will be helpful in keeping the processor from doing too much work.
 +
 +
<nowiki>void loop() {
 +
  YunClient client = server.accept();
 +
 +
  if (client) {
 +
    process(client);
 +
    client.stop();
 +
  }
 +
 +
  delay(50);
 +
}</nowiki>
 +
 +
Create a function named '''process''' that accepts the YunClient as its argument. Read the command by creating a string to hold the incoming information. Parse the REST commands by their functionality (digital, analog, and mode) and pass the information to the appropriately named function.
 +
 +
<nowiki>void process(YunClient client) {
 +
  String command = client.readStringUntil('/');
 +
 +
  if (command == "digital") {
 +
    digitalCommand(client);
 +
  }
 +
  if (command == "analog") {
 +
    analogCommand(client);
 +
  }
 +
  if (command == "mode") {
 +
    modeCommand(client);
 +
  }
 +
}</nowiki>
 +
 +
Create a function to deal with ''digital'' commands. Accept the client as the argument. Create some local variables to hold the pin and value of the command.
 +
 +
<nowiki>void digitalCommand(YunClient client) {
 +
  int pin, value;</nowiki>
 +
 +
Parse the client's request for the pin to work with using '''client.parseInt()'''.
 +
 +
If the character after the pin is a "/", it means the URL is going to have a value of 1 or 0 following. This value will assign a value to the pin, turning it HIGH or LOW. If there is no trailing "/", read the value from the specified pin.
 +
 +
<nowiki>  pin = client.parseInt();
 +
 +
  if (client.read() == '/') {
 +
    value = client.parseInt();
 +
    digitalWrite(pin, value);
 +
  }
 +
  else {
 +
    value = digitalRead(pin);
 +
  }</nowiki>
 +
 +
Print the value to the client and update the datastore key with the current pin value.
 +
 +
By wrapping the value to the client in '''F()''', you'll be printing form the flash memory. This helps conserve space in SRAM, which is useful when dealing with long strings like URLs.
 +
 +
The key will be the pin, and type. For example ''D2'' will be saved for for digital pin 2. The value will be whatever value the pin is currently set to, or was read from the pin.
 +
 +
<nowiki>  client.print(F("Pin D"));
 +
  client.print(pin);
 +
  client.print(F(" set to "));
 +
  client.println(value);
 +
 +
  String key = "D";
 +
  key += pin;
 +
  Bridge.put(key, String(value));
 +
}</nowiki>
 +
 +
Set up a function to handle analog calls in the same fashion, except setting the key to A instead of D when working with the analog input pins :
 +
 +
<nowiki>void analogCommand(YunClient client) {
 +
  int pin, value;
 +
 +
  pin = client.parseInt();
 +
 +
  if (client.read() == '/') {
 +
    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);
 +
 +
    String key = "D";
 +
    key += pin;
 +
    Bridge.put(key, String(value));
 +
  }
 +
  else {
 +
    value = analogRead(pin);
 +
 +
    client.print(F("Pin A"));
 +
    client.print(pin);
 +
    client.print(F(" reads analog "));
 +
    client.println(value);
 +
 +
    String key = "A";
 +
    key += pin;
 +
    Bridge.put(key, String(value));
 +
  }
 +
}</nowiki>
 +
 +
Create one more function to handle pin mode changes. Accept the YunClient as the argument, and create a local variable to hold the pin number. Read the pin value just as you did in the digital and analog functions.
 +
 +
<nowiki>void modeCommand(YunClient client) {
 +
  int pin;
 +
  pin = client.parseInt();</nowiki>
 +
 +
Check to make sure the URL is valid
 +
 +
<nowiki>  if (client.read() != '/') {
 +
    client.println(F("error"));
 +
    return;
 +
  }</nowiki>
 +
 +
If it's a valid URL, store the URL as a string. If the mode is an ''input'' or ''output'', configure the pin and report it to client. If the string doesn't match those values, return an error.
 +
 +
<nowiki>  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);
 +
}</nowiki>
 +
 +
You can find more details on this example on the Bridge Example page.
    
== Guide en vidéo ==
 
== Guide en vidéo ==
29 917

modifications

Menu de navigation