Teensy 4.0 + W5500 ethernet module

Lavanya rajan

Well-known member
Hi all,

I am using a W5500 Ethernet module interfaced with a Teensy 4.0 via SPI. My goal is to send data from the controller to a cloud database using HTTP requests.

Before implementing the cloud communication, I wanted to first verify basic internet connectivity from the device. For this purpose, I tried to connect to the Google website (www.google.com) on port 80 using the Ethernet library.

The Ethernet initialization appears to be successful, and the device receives a valid static IP address. However, when attempting to connect to the server using, the connection fails. Any suggestions on how to debug or resolve this problem would be greatly appreciated.

Code:
/*
 Web client
*/

#include <SPI.h>
#include <Ethernet.h>

char server[] = "www.google.com";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

#define MYIPADDR 172,18,7,165
#define MYIPMASK 255,255,0,0
#define MYDNS 172,18,90,50
#define MYGW 172,18,90,50

EthernetClient client;

unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;

void setup() {

  Serial.begin(115200);
  delay(1000);

  Serial.println("Begin Ethernet");

  SPI.begin();
  Ethernet.init(29);   // CS pin for W5500

  // Static IP configuration
  IPAddress ip(MYIPADDR);
  IPAddress dns(MYDNS);
  IPAddress gw(MYGW);
  IPAddress sn(MYIPMASK);

  Ethernet.begin(mac, ip, dns, gw, sn);

  delay(1000);

  // Check hardware
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield not found!");
    while (true) delay(1);
  }

  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable not connected.");
  }

  Serial.print("Local IP : ");
  Serial.println(Ethernet.localIP());

  Serial.print("Subnet Mask : ");
  Serial.println(Ethernet.subnetMask());

  Serial.print("Gateway IP : ");
  Serial.println(Ethernet.gatewayIP());

  Serial.print("DNS Server : ");
  Serial.println(Ethernet.dnsServerIP());

  Serial.println("Ethernet Successfully Initialized");

  Serial.println("Connecting to server...");

  if (client.connect(server, 80)) {
    Serial.println("Connected!");

    client.println("GET /get HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();

  } else {
    Serial.println("Connection failed");
  }

  beginMicros = micros();
}

void loop() {

  int len = client.available();

  if (len > 0) {

    byte buffer[80];

    if (len > 80) len = 80;

    client.read(buffer, len);

    if (printWebData) {
      Serial.write(buffer, len);
    }

    byteCount = byteCount + len;
  }

  if (!client.connected()) {

    endMicros = micros();

    Serial.println();
    Serial.println("Disconnecting...");

    client.stop();

    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");

    float seconds = (float)(endMicros - beginMicros) / 1000000.0;

    Serial.print(seconds, 4);

    float rate = (float)byteCount / seconds / 1000.0;

    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");

    Serial.println();

    while (true) {
      delay(1);
    }
  }
}

Serial Monitor Resposne:
Begin Ethernet
Local IP : 172.25.3.210
Subnet Mask : 255.255.0.0
Gateway IP : 172.18.90.57
DNS Server : 172.18.90.50
Ethernet Successfully Initialized
Connecting to server...
Connection failed


Disconnecting...
Received 0 bytes in 0.0000, rate = nan kbytes/second
 
May I suggest trying it with the QNEthernet library and see if you can connect? Happy to provide some guidance if you need. Note that I'm not suggesting permanently switching, because the regular "Ethernet" library is probably a little faster. (It just doesn't have as many features. 😝)

If neither library works, the problem could be something else.

Uncomment the QNETHERNET_DRIVER_W5500 line in src/qnethernet_opts.h in the library's directory. Also, verify that the connection settings in src/qnethernet/drivers/driver_w5500_config.h are correct.
 
Try running the WebClient example. In Arduino IDE, click File > Examples > Ethernet > WebClient. It sends a slightly different HTTP request. Perhaps that difference matters?
 
Try running the WebClient example. In Arduino IDE, click File > Examples > Ethernet > WebClient. It sends a slightly different HTTP request. Perhaps that difference matters?
I modified the above said example for static IP and the serial response is, still HTTP response is not getting printed and also only once it gets connect if I keep loop running
Code:
Local IP : 172.18.7.165
Subnet Mask : 255.255.0.0
Gateway IP : 172.18.90.50
DNS Server : 172.18.90.50
Ethernet Successfully Initialized
connecting to www.google.com...
connected to 142.251.222.164

disconnecting.
Received 0 bytes in 0.0069, rate = 0.00 kbytes/second
 
Back
Top