expanding on Yun bridge example

Hi everyone, new to the world of arduino so I'm having a bit of a play around with the examples to begin with to see if I can make the arduino do my bidding.

To the point of my post: I have been trying to add to the digital outputs of the bridge example on the yun, however I'm unsure how to get pins 0 and 1 to work, they both seem to be constantly on at about half brightness using a breadboard with some LEDs just to prove pins are doing what I tell them (image attached of the setup)

I have successfully turned pins 0 and 1 off using the blink example, it seems to be something to do with using the webpage control as to why it's not working to me.

If anyone can steer me in the right direction that would be much appreciated, my modified code is as follows:

/*
Arduino Yún Bridge example

This example for the Arduino 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

*/

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

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

void setup() {
// Bridge startup
pinMode(13, OUTPUT);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(13, LOW);
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
Bridge.begin();
// digitalWrite(13, HIGH);
// digitalWrite(0, HIGH);
// digitalWrite(1, HIGH);
// digitalWrite(2, HIGH);
// digitalWrite(3, HIGH);
// digitalWrite(4, HIGH);
// digitalWrite(5, HIGH);
// digitalWrite(6, HIGH);
// digitalWrite(7, 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
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);
}

Pins 0 and 1 are the serial transmit / receive lines between the 32U4 running the sketch, and the Linux processor. So you can control those pins when running a simple sketch like blink, but as soon as you call Bridge.begin() they are taken over for the bridge communications. The pins can't do two functions at once (serial and digital output) so something will lose.

Thanks Shapeshifter, I guess I'll have to keep that in mind when I get to project building too, slightly annoying to lose 2 outputs.

I suppose there is no way around this if I want to keep using a webpage interface to the yun?

No, there is not. Unfortunately pins 0 and 1 are required for communication between the two processors if you use Bridge library like using the webpage interface.

Bummer! Oh well, I'll just have to work around it. Thanks for the help.

There isn't much point to using a Yun without using the serial interface to talk to the Linux side - that's why the serial lines are permanently tied together between the two processors. If you didn't need the Linux side, you could save quite a bit of money and use an Uno or Leonardo.

That said, even if you were to forgo using the bridge and the Linux side, you will still likely have problems using pin 0 for anything. That's because it's being driven by the Linux side serial port, and you will have two devices trying to drive the same pin. Not good. You could also have some issues with port 1: anything you drive onto that pin is going to be fed into the serial input of the Linux side, potentially confusing it.

Net result: don't ever plan on doing anything with pins 0 and 1, let them do their thing interfacing with the Linux processor.

Yeah I definitely need the linux side of the yun to interface with it for my planned project via a webpage, the next catastrophe for me to deal with is building said webpage interface.... But I think the questions I have on that at this stage are for somewhere other than here.

Appreciate the help, keep up the good work!