[SOLVED] Ethernet in Accessory mode doesn't work !?

Hello experts,

I'm so new to ADK programming, any comment or suggestion is really appreciated.
What i want to do is to make Mega ADK (+Ethernet shield) can get IP address from DHCP server controlling by Android device in accessory mode.
So firstly i find relating sample source code that closest to my requirement, what i chose are;

  • DemoKit
  • DHCPChatServer

Each of them works very well when I try execute it individually but doesn't work when i integraged them together.
My problem is : "After AndroidAccessory "powerOn" is executed, getting IP via DHCP function will no longer work."

Here is some part of my code

void setup_ethernet(){
  
  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  Serial.println(ip);
  
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 54321)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void setup()
{
        // Open serial communications and wait for port to open:
        Serial.begin(9600);
         while (!Serial) {
          ; // wait for serial port to connect. Needed for Leonardo only
        }

	Serial.println("\r\nStart");
        
	init_leds();
	init_relays();
	init_buttons();
	//init_joystick( 5 );

	// autocalibrate OFF
	touch_robot.set_CS_AutocaL_Millis(0xFFFFFFFF);

	servos[0].attach(SERVO1);
	servos[0].write(90);
	servos[1].attach(SERVO2);
	servos[1].write(90);
	servos[2].attach(SERVO3);
	servos[2].write(90);


	b1 = digitalRead(BUTTON1);
	b2 = digitalRead(BUTTON2);
	b3 = digitalRead(BUTTON3);
	b4 = digitalRead(JOY_SWITCH);
	c = 0;

	acc.powerOn();

        //setup ethernet using DHCP
        setup_ethernet();
}

void loop()
{
        .....
}

Anyone has idea? how can i get rid of this problem?.
Thank you in advance for your help :slight_smile:

Newbie

Both the Ethernet shield and the ADK's USB Host use the SPI bus, so it's possible that the bus arbitration isn't right.

Thank you for your reply, dxw00d !!!! :slight_smile:

Yes, you are right!
Ethernet shield and the ADK's USB Host use the same SPI pins;
50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS)

In this case, is there a way i can correct bus arbitration?

One more thing, according to the source code above what i noticed is that after acc.powerOn() is executed,
Ethernet module that use spi interface becomes not working even i rewrite a program that used to work perfectly on it. (like arduino sample code - DHCPChatServer)

This is extremely weired. To resolve this issue i have to install Arduino IDE in other PC and write dummy program on it to clear memory then switch back and write my program again.

Just FYI.

Ethernet shield and the ADK's USB Host use the same SPI pins;
50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS)

Not correct: The Ethernet Shield uses pin 10 for SS and not pin 53. The question is: does the DemoKit correctly set the SS line for the USB host interface?

Thank you for your reply pylon!!

The reason why i use those pins are already mentioned here; (not in ICSP header)
http://forums.adafruit.com/viewtopic.php?f=31&t=22175

and schematic i'm using as reference is;

My Ethernet shield - WIZ820io

For SS pin of USB host interface issue, it was programmatically set by Max3421e libarary,
which you may see it in /libraries/USB_Host_Shield/Max3421e_constants.h

#if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
  #define SCK_PIN   52
  #define MISO_PIN  50
  #define MOSI_PIN  51
  #define SS_PIN    53
#endif

Pls let me know if my understanding is not correct :frowning:

not in ICSP header

That forum thread is about the Arduino Ethernet Shield. There the SCLK, MISO and MOSI signals are used only from the ICSP header in newer revisions to be compatible with UNO and MEGA2560. The SS is always on pin 10, the Ethernet library expects it there and only there. So connect your ethernet module's SS pin to pin 10 and it will probably work.

The MAX3421e uses the signal of pin 53 but it's internally connected.

OMG!!!!!

It's working now !!!!!!!!!!!!!!
Thank you so much PYLON!!, you are really my pylon XD XD XD XD

Let me explain a bit why it had been not working.
Previously, to make SS works on pin 53, i had modified W5200 library (new w5100.h) from

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRB  |=  _BV(4); };
  inline static void setSS()     { PORTB &= ~_BV(4); };
  inline static void resetSS()   { PORTB |=  _BV(4); };

To

#if defined(__AVR_ATmega1280__)
  inline static void initSS()    { DDRB  |=  _BV(4); };
  inline static void setSS()     { PORTB &= ~_BV(4); };
  inline static void resetSS()   { PORTB |=  _BV(4); };
#elif defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRB  |=  _BV(0); };
  inline static void setSS()     { PORTB &= ~_BV(0); };
  inline static void resetSS()   { PORTB |=  _BV(0); };

ฺฺBy this change, i can use ethernet with no problem but......
when it comes to integrate with ADK USB Host, it will no longer work.
So i follow your suggestion by changing it back and also apply SS to pin 10 instead of 53 as below.

[Arduino] Pin52 - SCLK [WIZnet820io]
[Arduino] Pin50 - MISO [WIZnet820io]
[Arduino] Pin51 - MOSI [WIZnet820io]
[Arduino] Pin10 - nSS [WIZnet820io]

Now it works like a CHARM !! :stuck_out_tongue_closed_eyes: :stuck_out_tongue_closed_eyes:
Thank you so much, i had been stuck on this problem for 2 days but with you help, i can solve it eventually :smiley: :smiley:

Newbie++