Khoi Hoang's EthernetWebServer on Teensy4.1 with MagJack -- anyone successful?

Status
Not open for further replies.

rothemich

New member
Has anyone successfully run the khoi-prog EthernetWebServer example program (https://github.com/khoih-prog/EthernetWebServer/tree/master/examples/AdvancedWebServer v1.3.0) on a Teensy4.1 with MagJack ethernet? I’m trying the unaltered example code (other than patches and debug) without success.

Using the Teensyduino IDE (Arduino 1.8.13, Teensyduino 1.53), I’ve:
  • verified I’m using NativeEthernet
  • verified I’m using Functional-Vlpp
  • updated the khoi-prog patches for teensy4 (boards.txt & https://github.com/khoih-prog/Ether...er/Packages_Patches/hardware/teensy/avr/cores)
  • confirmed I get a valid ping return for 192.168.1.70
  • put in a series of debug printlns, to ultimately trace it to what appears to be a failure in Stream.cpp upon exiting the readStringUntil(‘\r’) after reading a newline character
  • verified virtually identical results from Chrome browser

I see this serial output:
Code:
Starting AdvancedWebServer_NativeEthernet on TEENSY 4.1 with Custom Ethernet using Teensy 4.1 NativeEthernet Library
EthernetWebServer v1.3.0
[ETHERNET_WEBSERVER] ======== USE_NATIVE_ETHERNET ========
[ETHERNET_WEBSERVER] Default SPI pinout:
[ETHERNET_WEBSERVER] MOSI: 11
[ETHERNET_WEBSERVER] MISO: 12
[ETHERNET_WEBSERVER] SCK: 13
[ETHERNET_WEBSERVER] SS: 10
[ETHERNET_WEBSERVER] =========================
[ETHERNET_WEBSERVER] Board : TEENSY 4.1 , setCsPin: 10
=========================
Currently Used SPI pinout:
MOSI:11
MISO:12
SCK:13
SS:10
=========================
Using mac index = 2
Connected! IP address: 192.168.1.70

When I launch a URL against 192.168.1.70, it immediately drops the connection (ping), and I get into what I suspect is an infinite loop (since the board starts to heat up). I’ve done some debugging trying to narrow it down:

Stepping into Parsing-impl.h (from the khoi-prog EthernetWebServer), which iteratively calls client.readStringUntil(char, size_t) in the teensy4 patched stream.cpp, I see the following:

Code:
After retrieving:
	GET / HTTP/1.1
client.readStringUntil(‘\r’) yields
	Host: 192.168.1.70
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	Upgrade-Insecure-Requests: 1
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safa 
client.readStringUntil(‘\n’) yields
	ri/605.1.15
client.readStringUntil(‘\r’) yields
	Accept-Language: en-us
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	Accept-Encoding: gzip, deflate
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	Connection: keep-alive
client.readStringUntil(‘\n’) yields
	<newline>
client.readStringUntil(‘\r’) yields
	<newline>
And then drops ping

The khoih-prog updates seem to suggest this should work, but I may be debugging something that is known incompatible so first wonders if others have tried with success.

Thanks.
 
Can you try this new code

Code:
#include <SPI.h>
#include <NativeEthernet.h>

#define USING_STATIC_IP       true

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

#if USING_STATIC_IP
  IPAddress ip        (192, 168, 2, 127);
  IPAddress gateway   (192, 168, 2, 1);
  IPAddress subnet    (255, 255, 255, 0);
#endif

EthernetServer server(80); //server port

EthernetClient client;

String readString;

//////////////////////

#define BOARD_NAME    "Teensy 4.1 with NativeEthernet"

#define ETHERNET_SS_PIN     10

#define BUFFER_SIZE         400
char htmlBuffer[BUFFER_SIZE];

void handleRoot()
{
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int day = hr / 24;

  hr = hr % 24;

  Serial.print(F("R"));

  snprintf(htmlBuffer, BUFFER_SIZE - 1,
           "<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AdvancedWebServer %s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>Hi from EthernetWebServer!</h2>\
<h3>on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);

  //server.send(200, F("text/html"), htmlBuffer);
  Serial.println(htmlBuffer);

  client.println(htmlBuffer);
}

//////////////////////

void drawGraph()
{
  String out;
  out.reserve(3000);
  char temp[70];

  Serial.print(F("D"));

  out += F("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n");
  out += F("<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n");
  out += F("<g stroke=\"black\">\n");
  int y = rand() % 130;

  for (int x = 10; x < 300; x += 10)
  {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
  }
  out += F("</g>\n</svg>\n");

  //server.send(200, F("image/svg+xml"), out);
  Serial.println(out);

  client.println(out);
}

//////////////////////

void setup()
{
  //enable serial data print
  Serial.begin(115200);
  while (!Serial);

  Serial.print("Advanced EthernetWebServer on ");
  Serial.println(BOARD_NAME);

  Ethernet.init(ETHERNET_SS_PIN);

#if USING_STATIC_IP
  // Static IP
  //Ethernet.begin(mac, ip, gateway, subnet);
  Ethernet.begin(mac, ip);
#else
  // DHCP
  Ethernet.begin(mac);
#endif

  server.begin();

  Serial.print(F("Connected! IP address: "));
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // Create a client connection
  client = server.available();

  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {
        handleRoot();
        drawGraph();

        //stopping client
        client.stop();
      }
    }
  }
}


Selection_769.png
 

Attachments

  • Selection_768.png
    Selection_768.png
    18.2 KB · Views: 58
Last edited:
Yes, @khoih-prog, that works brilliantly. I'll look forward to unraveling just what changes you made to get it to work, and thank you for your excellent library! 100% satisfied (for now :) )
 
Status
Not open for further replies.
Back
Top