Code for 2 by 16 LCD based on the Yun

First, if this post is in the wrong area, I apologize for it and the moderator can move it.

I am relatively new to Arduino. I have using a starter kit to learn. There seems to be many good examples of hardware combinations for Uno. The Yun has good examples, but can be limited. After using the Uno to display information on LCD (type 1602A). I thought it would be great to do it remotely on Yun using the wifi network. It would be a good demonstration of web connectivity. After 32 characters are entered, the console receives a message showing the text on the display and how to clear it if there is an error.

I had some luck and managed to get everything to work. Here is the code for others use:

/*
 USB Message Board Modified 
 for use with Yun board.
 I am also trying to make the user input easier.
 
  The circuit:
 * digital pin 12 to LCD RS (pin 4) 
 * digital pin 11 to LCD Enable (pin 6) 
 * digital pin 10 to LCD D4 (pin 11)
 * digital pin 9 to LCD D5 (pin 12)
 * digital pin 8 to LCD D6 (pin 13)
 * digital pin 7 to LCD D7 (pin 14)
 * LCD R/W (pin 5) to ground
 * 2.2K resistor from LCD VO (contrast, pin 3) to ground
 * LCD VSS (pin 1) to ground
 * LCD VDD (pin2) to +5 volts
 * LCD A (pin 15) to +5 volts
 * LCD K (pin 16) 330 ohm to ground, This limits current through Backlight LED
 
 This does work with my module
 Visble ASCII codes are between 20 and 126
 
 To see the Console, pick your YUn'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.
 
 Legal Boiler Plate
 Created 23 Dec. 2014 by Gregory Majewski
 This example code is in the public domain without any warranty.
 */

// include the library code:
#include <LiquidCrystal.h>
#include <Console.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
//buffer for message, global used in both setup and loop
char messageText[] = "Arduino         Rules           "; 

void setup() {
  Bridge.begin();   // Initialize Bridge
  Console.begin();  // Initialize Console
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.clear();
  for (int i= 0; i < 32; i++) {
    if (i == 16) {lcd.setCursor(0,1);} // go to second line
    lcd.write(messageText[i]);
    }
    while(!Console); // Wait for the Console port to connect, do after initial message display
}

void loop() {
  static int maxChars = 31;
  static int maxOnrow = 15;
  static int countChar = 0;
  static int numLine =0;
  if (Console.available() > 0) {
    char inChar = Console.read();
       if (inChar == '#') {//clear display character #
        lcd.clear();
        countChar = 0;
        numLine = 0;
       }
       int posChar = countChar; 
       if (countChar > maxOnrow) { // go to second line
       numLine = 1;
       posChar = countChar - 16; // adjust position to be o to 15
       } 
       if ((inChar > 31) && (inChar < 127) && (inChar != '#')) { //normal print characters minus #
        messageText[countChar] = inChar;
        lcd.setCursor(posChar, numLine);
        lcd.write(inChar);
        countChar ++;
       }
       if (countChar > maxChars) {
        Console.println("");
        Console.println("");
        Console.println("No more characters can be displayed ");
        Console.println("This the text that is displayed");
        Console.println(messageText);
        Console.println("If it is not right, clear display by typing # and try again");
        countChar = 0; //zero countChar so only println once
        numLine = 0;
        // need to empty buffer of any extra characters
        do {
          inChar = Console.read();
        } while (Console.available() > 0);
        delay(750); //so typist can see error messages 
       }
   }
}

Hopefully, this will be of use to somebody. Who knows, maybe there will be an “ultimate starter kit for the Yun”.

Thanks for sharing it :slight_smile:

Nice, I suggest you for a next step, mixing your example with this one:

// Possible commands are listed here:
//
// "digital/13"     -> digitalRead(13)
// "digital/13/1"   -> digitalWrite(13, HIGH)
// "analog/2/123"   -> analogWrite(2, 123)
// "analog/2"       -> analogRead(2)
// "mode/13/input"  -> pinMode(13, INPUT)
// "mode/13/output" -> pinMode(13, OUTPUT)

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;

void setup() {
  delay(60000);  //para que el boot del openwrt no tenga problemas
  
  Serial.begin(9600);

  // Bridge startup
  pinMode(13,OUTPUT);
  digitalWrite(13, HIGH);
  Bridge.begin();
  digitalWrite(13, LOW);

  // 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
  YunClient 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(YunClient 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(YunClient 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(YunClient 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(YunClient 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);
}

This way you can upload text to lcd 16x2 from any gadget with browser connected to the wifi (laptop, cellphone, tablet).

OK, Mart256, I will look at the code and try it out. It will be a bit later. The current project is to figure out how to set the Yun to ad hoc (aka peer to peer) wifi mode. Thank you for the code.