Combining Wire.h and Bridge example Error

Hi,

I’m trying to use the bridge example on my Arduino Yun. The example works perfectly. However I was short on I/O pins so I bought the MCP2017 I/O expander shield:

http://productdata.numato.com/assets/downloads/arduinoandshields/ioexpander/IOExpanderUserManualV9.pdf

I’ve written my code so that when I type in arduino.local/arduino/digital/16/1 it should turn pin 0 on the U4 chip on.

However when I try and connect I get a Could not connect to YunServer 146 Connection refused error.

My code below:

/*

Possible commands created in this shetch:

“/arduino/digital/3” → digitalRead(3)
“/arduino/digital/13/1” → digitalWrite(3, HIGH)

*/

#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <Wire.h>
#include <MCP23017.h>

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send

BridgeServer server;
MCP23017 Chip_U2;
MCP23017 Chip_U4;

void setup() {

/*

  • There are two on board MCP23017(U2 and U4) IO Expander ICs on this Shield
  • Select jumper positions of AD1=AD2=AD3=0, to set the Address of U2 as 0x20
  • Select jumper positions of AD4=0; AD5=0; AD6=1, to set the Address of U4 as 0x21
  • Digital Pins 0 - 7 belongs to PORTA and 8 - 15 belongs to PORTB of U2
  • Digital Pins 24 - 27 belongs to PORTA and 16 - 23 belongs to PORTB of U4
    */
    Wire.begin(); // Begin I2C bus
    //// Set Chip Addresses
    Chip_U2.begin (B000);
    Chip_U4.begin (B001);
    //// Initialize chip
    Chip_U2.init();
    Chip_U4.init();

/*

  • The following code starts I2C communication with MCP23017(U2).Please refer MCP23017 datasheet
  • for register addresses. Sets all GPIOs of this IC to OUTPUT
    */
    Chip_U2.pinMode(0,OUTPUT); /// Set all pins to output on chip U2
    Chip_U2.pinMode(1,OUTPUT);
    Chip_U2.pinMode(2,OUTPUT);
    Chip_U2.pinMode(3,OUTPUT);
    Chip_U2.pinMode(4,OUTPUT);
    Chip_U2.pinMode(5,OUTPUT);
    Chip_U2.pinMode(6,OUTPUT);
    Chip_U2.pinMode(7,OUTPUT);
    Chip_U2.pinMode(8,OUTPUT);
    Chip_U2.pinMode(9,OUTPUT);
    Chip_U2.pinMode(10,OUTPUT);
    Chip_U2.pinMode(11,OUTPUT);
    Chip_U2.pinMode(12,OUTPUT);
    Chip_U2.pinMode(13,OUTPUT);
    Chip_U2.pinMode(14,OUTPUT);
    Chip_U2.pinMode(15,OUTPUT);
    //////////////////////
    Chip_U4.pinMode(0,OUTPUT); /// Set all pins to output on chip U4
    Chip_U4.pinMode(1,OUTPUT);
    Chip_U4.pinMode(2,OUTPUT);
    Chip_U4.pinMode(3,OUTPUT);
    Chip_U4.pinMode(4,OUTPUT);
    Chip_U4.pinMode(5,OUTPUT);
    Chip_U4.pinMode(6,OUTPUT);
    Chip_U4.pinMode(7,OUTPUT);
    Chip_U4.pinMode(8,OUTPUT);
    Chip_U4.pinMode(9,OUTPUT);
    Chip_U4.pinMode(10,OUTPUT);
    Chip_U4.pinMode(11,OUTPUT);
    ///////////////
    Chip_U2.digitalWrite(0,LOW); /// Set all pins to LOW on chip U2
    Chip_U2.digitalWrite(1,LOW);
    Chip_U2.digitalWrite(2,LOW);
    Chip_U2.digitalWrite(3,LOW);
    Chip_U2.digitalWrite(4,LOW);
    Chip_U2.digitalWrite(5,LOW);
    Chip_U2.digitalWrite(6,LOW);
    Chip_U2.digitalWrite(7,LOW);
    Chip_U2.digitalWrite(8,LOW);
    Chip_U2.digitalWrite(9,LOW);
    Chip_U2.digitalWrite(10,LOW);
    Chip_U2.digitalWrite(11,LOW);
    Chip_U2.digitalWrite(12,LOW);
    Chip_U2.digitalWrite(13,LOW);
    Chip_U2.digitalWrite(14,LOW);
    Chip_U2.digitalWrite(15,LOW);
    //////////////////////
    Chip_U4.digitalWrite(0,LOW); /// Set all pins to LOW on chip U2
    Chip_U4.digitalWrite(1,LOW);
    Chip_U4.digitalWrite(2,LOW);
    Chip_U4.digitalWrite(3,LOW);
    Chip_U4.digitalWrite(4,LOW);
    Chip_U4.digitalWrite(5,LOW);
    Chip_U4.digitalWrite(6,LOW);
    Chip_U4.digitalWrite(7,LOW);
    Chip_U4.digitalWrite(8,LOW);
    Chip_U4.digitalWrite(9,LOW);
    Chip_U4.digitalWrite(10,LOW);
    Chip_U4.digitalWrite(11,LOW);
    Chip_U4.digitalWrite(12,LOW);
    Chip_U4.digitalWrite(13,LOW);
    Chip_U4.digitalWrite(14,LOW);
    Chip_U4.digitalWrite(15,LOW);
    Bridge.begin();

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

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();

if (pin<16) {
Chip_U2.digitalWrite(pin,value); //U2 addressing 1:1 straight write
}
else if (pin>=16) {
Chip_U4.digitalWrite(pin-16,value); //U4 addressing needs to be adjusted
}

else {

if (pin<16) {
value = Chip_U2.digitalRead(pin);
}
else if (pin>=16) {
value = Chip_U4.digitalRead(pin-16);
}
}

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

With the same shield on if I try and run the regular Bridge example it works no problems

Thanks in advance

I don't know the answer to your specific question, but, have you ever heard of for statements? Copying and pasting one line of code 15 times, and editing each line to operate on a different pin makes no sense, when a for statement can iterate over all the pins.

// 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"

Does the readStringUntil() call remove the / from the stream? If it does, wouldn't the value that is in command be "/digital", if the Arduino is being sent the kind of string that you suppose it is supposed to handle?

What value DO you get for command? Does it actually cause the function that that snippet of code is from to be called?

What value is actually in pin?

What value IS the next character read from the stream? Since you didn't store it, how can you deal with the situation where the character is not a /? You can't compare the character to anything else.

With the same shield on if I try and run the regular Bridge example it works no problems

So, the issue has nothing to do with the bridge. It has everything to do with what you are getting from the bridge, which you don't print to the serial port, or with how you are using the data.

You are correct, I need to clean the code up more. It's still messy. I believe the problem is actually the hardware connection. since the Yun and UNO have different pin mapping for SDA and SCL. Because I tried to run the test code that comes with the I/o Expander and even that doesnt work. I will have to figure out how to correctly wire this shield with my Yun.

Thanks