Hello,
I am looking for some help troubleshooting a problem that presents itself when I attempt to use an Ethernet shield and USB host shield together. I have changed the CS pin of the Ethernet shield and do not believe it is a pin conflict problem.
When running my code with just the ethernet and without the USB shield it works as expected. Network connections can be made. This is what I get in the serial monitor.
Serial conneced.Start
Ethernet Beginserver is at 192.168.160.58
When I insert the USB host shield between the Arduino and the Ethernet shield the USB host operates correctly but the ethernet shield does not initialize correctly or work. This is what the serial monitor shows in this case.
Serial conneced.Start
Ethernet Beginserver is at 0.0.0.0
DN >04<
ASCII: a
UP >04<
Hardware:
Elegoo Mega 2560 (Arduino Mega 2560)
Arduino USB Host Shield Rev 3 https://store.arduino.cc/usa/arduino-usb-host-shield
W5500Ethernet Shield http://wiki.seeed.c/W5500_Ethernet_Shield_v1.0/
I disconnected the header pin from digital pin 10 and connected it to digital pin 11 on the ethernet shield for all tests.
I replaced the ICSP header on the USB host shield with longer ones so they could connect with the Ethernet shield when it is stacked on top. I have tested continuity of icsp pins between all devices.
Code below.
I appreciate your help.
/* ==================================================================================================================================================
LIBRARIES LIBRARIES
==================================================================================================================================================== */
#include <Wire.h> //Wire for OLED display
#include <SPI.h> //SPI for Ethernet Shield
#include <EthernetUdp2.h> //Ethernet W5500
#include <EthernetClient.h> //Ethernet W5500
#include <Dhcp.h> //Ethernet W5500
#include <EthernetServer.h> //Ethernet W5500
#include <Ethernet2.h> //Ethernet W5500
#include <util.h> //Ethernet W5500
#include <Dns.h> //Ethernet W5500
//#include <Twitter.h> //Ethernet W5500
/*=============================USB=================================*/
#include <hidboot.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
class KbdRptParser : public KeyboardReportParser
{
void PrintKey(uint8_t mod, uint8_t key);
protected:
void OnControlKeysChanged(uint8_t before, uint8_t after);
void OnKeyDown (uint8_t mod, uint8_t key);
void OnKeyUp (uint8_t mod, uint8_t key);
void OnKeyPressed(uint8_t key);
};
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
MODIFIERKEYS mod;
*((uint8_t*)&mod) = m;
Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
Serial.print(" >");
PrintHex<uint8_t>(key, 0x80);
Serial.print("< ");
Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
Serial.print((mod.bmRightShift == 1) ? "S" : " ");
Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
};
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
Serial.print("DN ");
PrintKey(mod, key);
uint8_t c = OemToAscii(mod, key);
if (c)
OnKeyPressed(c);
}
void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
MODIFIERKEYS beforeMod;
*((uint8_t*)&beforeMod) = before;
MODIFIERKEYS afterMod;
*((uint8_t*)&afterMod) = after;
if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
Serial.println("LeftCtrl changed");
}
if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
Serial.println("LeftShift changed");
}
if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
Serial.println("LeftAlt changed");
}
if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
Serial.println("LeftGUI changed");
}
if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
Serial.println("RightCtrl changed");
}
if (beforeMod.bmRightShift != afterMod.bmRightShift) {
Serial.println("RightShift changed");
}
if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
Serial.println("RightAlt changed");
}
if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
Serial.println("RightGUI changed");
}
}
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
Serial.print("UP ");
PrintKey(mod, key);
}
void KbdRptParser::OnKeyPressed(uint8_t key)
{
Serial.print("ASCII: ");
Serial.println((char)key);
};
USB Usb;
//USBHub Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
KbdRptParser Prs;
/*==========================ETHERNET==============================*/
//byte ip[] = { 192, 168, 1, 5 }; //assign IP address to shield
byte subnet[] = { 255, 255, 255, 0 }; // <= SETUP (subnet of shield)
byte gateway[] = { 192, 168, 160, 1 }; // <= SETUP (gateway of shield)
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
//This sketch uses W5500 Ethernet Shield,Seeeduino V4.2,Grove-Temp&Humi,
//Base Shield V2 Sensor and Micro SD Card to design a temperature and humidity collection station.
//attach the temperature and humidity sensor to base shield D5 grove port.
//It publishes the temperature and humidity data to webpage
//and refresh every 5 seconds, store the data into SD card datalog.txt file.
//#include <Ethernet.h>
// Please update IP address according to your local network
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
;
#else
byte mac[] = { 0x2C, 0xF7, 0xF1, 0x08, 0x0A, 0x24 }; // <= SETUP (mac address of shield) listed on sticker on shield
#endif
IPAddress clientIp(192, 168, 160, 58); // <= SETUP (IP address of shield) not sure if different from line above
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
/* ==================================================================================================================================================
SETUP SETUP
==================================================================================================================================================== */
void setup() {
/*============================Serial===============================*/
Serial.begin(115200);
//while (!Serial) {
//; // wait for serial port to connect. Needed for Leonardo only
//}
Serial.print("Serial conneced.");
/*==============================USB===============================*/
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("OSC did not start.");
delay( 200 );
HidKeyboard.SetReportParser(0, &Prs);
/*==========================ETHERNET==============================*/
Ethernet.init(11); //Set ethernet shhield spi pin to 11 insttead of default 10 because USB host shield uses pin 10
Ethernet.begin(mac,clientIp); // initialize Ethernet device //,ip,gateway,subnet
server.begin(); // start to listen for clients
Serial.print("Ethernet Begin");
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}//End Setup
/* ==================================================================================================================================================
LOOP LOOP
==================================================================================================================================================== */
void loop() {
Usb.Task();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
The first thing you should do in setup is set all CS pins HIGH (disable all device SPI) .
void setup() {
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(4, HIGH);
// rest of setup
I'm not certain this is your problem, but it could be.
The problem could lack of power. Try powering the mega using its barrel jack.
Thanks SurferTim and gdsports.
I added those lines to setup to disable SPI. While it did not fix my problem it is still good to have in there.
I tried with an external power supply connected, still no luck.
I thought I read something about a hardware issue with the USB host shield. Has anyone heard anything about that?
I just decided to meter the ICSP header, I don't have a scope. The only thing that stuck out to me is the reset pin. With the USB host shield it read 2.7v. With the Ethernet shield the reset pin was at 4.97v. This would lead me to believe that I have a defective USB host shield. Any thoughts?
Edit: I modified my code to allow me to connect a jumper between reset and 5v just to see if that was causing issues for the Ethernet shield. Results were the same.
I did some troubleshooting:
I connected the ethernet shield to the mega with the exception of pin 10.
Then I connected only some of the USB shield pins to the the ethernet shield and mega.
I found that when the SDA and SCL and IOREF pins are connected on the USB host shield the ethernet shield will not work. I tried rebooting several times and each time this is what I got.
Serial conneced.
Start
Ethernet Begin
server is at 0.0.0.0
Serial conneced.
Start
So I stacked the USB host shield on top only connecting Reset, 3.3v,5v,GND,Vin,7,8,9, jumper to pin 10 on mega, and the ICSP header. I have some success. USB and ethernet seem to initialize...the first time.
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
But if I hit reset on the mega I get different results each time.
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
Ethernet Begin
server is at 192.128.128.24
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
Ethernet Begin
server is at 192.128.0.24
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
Ethernet Begin
server is at 192.168.160.58
Serial conneced.
Start
Ethernet Begin
server is at 192.128.128.24
Serial conneced.
Start
Ethernet Begin
server is at 192.128.128.24