Yún Serial Terminal

This example allows you to use the Yún's 32U4 processor or the microcontroller of the board attached to the shield as a serial terminal for the Linux processor.

Upload this to a Yún device via USB (not over WiFi) then open the serial monitor at 115200bps to see the boot process of the Linux processor. 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.

Hardware Required

  • Yún board or shield

Circuit

There is no circuit for this example.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

Create a variable to hold the baud rate at which the processors will communicate and a variable to determine if entering command mode or not.

long linuxBaud = 250000;
boolean commandMode = false;

Open the serial connection to the computer and Linux in setup()

void setup() {
  Serial.begin(115200);    
  Serial1.begin(linuxBaud);
}

In the loop() copy data from one serial connection to the other. If the "~" character is passed, enter command mode.

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 Linux
      }
    } 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 if (c == '2') {          //     '2' key pressed?
        Serial1.begin(250000);        //        set speed to 250000
        Serial.println("Speed set to 250000");
      } else if (c == '3') {          //     '3' key pressed?
        Serial1.begin(500000);        //        set speed to 500000
        Serial.println("Speed set to 500000");
      } else if (c == '~') {
        Serial1.write((uint8_t *)"\xff\0\0\x05XXXXX\x0d\xaf", 11);
        Serial.println("Sending bridge's shutdown command");
      } else {                        //     any other key pressed?
        Serial1.write('~');           //        write '~' to Linux
        Serial1.write(c);             //        write char to Linux
      }
      commandMode = false;            //     in all cases exit from command mode
    }
  }
  if (Serial1.available()) {          // got anything from Linux?        
    char c = (char)Serial1.read();    // read from Linux  
    Serial.write(c);                  // write to USB-serial
  }
}

Below is the complete code:

/*
  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
  }
}

See Also

  • Bridge Library - Your reference to the Bridge Library
  • Bridge – Simple REST style calls to access analog and digital pins
  • Console Ascii Table – A complete ASCII table printed to the Console
  • Console Pixel – Turn an LED on and off through the Console
  • Console Read - Read data coming from bridge using the Console.read() function
  • Data Logger - Log data from three analog sensors to an SD card.
  • File Write - How to write file into the Yún filesystem.
  • Http Client - A basic HTTP client that connects to the internet and downloads content.
  • Http Client Console - HTTP client that connects, downloads content and shows it using WiFi and Console.
  • Mailbox Read Message - How to read the messages queue, called Mailbox, using the Bridge library.
  • Process - How to run linux processes using an Yún.
  • Remote Due Blink - How to upload remotely a sketch on DUE boards.
  • Shell Commands - How to run linux shell commands using a Yún.
  • Temperature Web Panel - How to serve data from an analog input via the Yún's built-in webserver.
  • Time check - Gets the time from Linux via Bridge then parses out hours, minutes and seconds.
  • WiFi Status - Prints information about the status of your wifi connection.
  • Yún First Configuration - Easily configure your Yún device using Serial Monitor and USB port.


Last revision 2016/05/25 by SM

Share