I ported my webserver from a Arduino Due with w5500 ethernet shield to Teensy 4.1.
I use the NativeEthernet library and i got things running. But it is not stable yet. Now and then this error occurs: SendErr: -20
And after a while the connection got lost and i have to reset the Teensy.
My code:
I use the NativeEthernet library and i got things running. But it is not stable yet. Now and then this error occurs: SendErr: -20
And after a while the connection got lost and i have to reset the Teensy.
My code:
Code:
#include <SPI.h>
#include <NativeEthernet.h>
#include <fnet.h>
#include <SD.h>
int chipSelect = BUILTIN_SDCARD; //Teensy interne SD card
IPAddress ip( 192, 168, 0, 177 );
IPAddress gateway( 192, 168, 0, 1);
IPAddress subnet( 255, 255, 255, 0 );
EthernetServer server(80);
#define REQ_BUF_SZ 60 // size of buffer used to capture HTTP requests
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
File webFile; // the web page file on the SD card
uint8_t mac[6];
void setup() {
SD.begin(BUILTIN_SDCARD);
Ethernet.setStackHeap(1024 * 128);
Ethernet.setSocketSize(1460 * 8); //Set buffer size
Ethernet.setSocketNum(8); //Change number of allowed sockets
teensyMAC(mac);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield niet gevonden of defect...");
while (true) {delay(1); }// do nothing, no point running without Ethernet hardware
}
if (Ethernet.linkStatus() == LinkOFF) {Serial.println("Ethernetkabel niet verbonden...");
}
server.begin();
Serial.print("Server op adres: ");
Serial.println(Ethernet.localIP()); Serial.println();
}
void loop() {
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// limit the size of the stored received HTTP request
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if ((strstr(HTTP_req, "Home=A")) || (strstr(HTTP_req, "Home=D"))) {
// send rest of HTTP header
client.println("Content-Type: text/xml"); client.println("Connection: keep-alive"); client.println();
}
else { // web page request
// send rest of HTTP header
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println("Refresh: 300");
client.println();
// open requested web page file
if (strstr(HTTP_req, "GET / ") || strstr(HTTP_req, "GET /Home.htm")) {webFile = SD.open("Home.htm"); } // open web page file HOME
if (strstr(HTTP_req, "GET / ") || strstr(HTTP_req, "GET /HomeTA.htm")) {webFile = SD.open("HomeTA.htm"); }
if (strstr(HTTP_req, "GET /Home1.htm")) {webFile = SD.open("Home1.htm"); }
if (strstr(HTTP_req, "GET /Home2.htm")) {
webFile = SD.open("Home2.htm"); }
if (strstr(HTTP_req, "GET /Home2a.htm")) {webFile = SD.open("Home2a.htm"); }
if (strstr(HTTP_req, "GET /Home3.htm")) {webFile = SD.open("Home3.htm"); }
if (strstr(HTTP_req, "GET /Home4.htm")) {webFile = SD.open("Home4.htm"); }
if (strstr(HTTP_req, "GET /Home4T1.htm")) {webFile = SD.open("Home4T1.htm"); }
if (strstr(HTTP_req, "GET /Home4T2.htm")) {webFile = SD.open("Home4T2.htm"); }
if (strstr(HTTP_req, "GET /Home4T3.htm")) {webFile = SD.open("Home4T3.htm"); }
if (strstr(HTTP_req, "GET /Home4T4.htm")) {webFile = SD.open("Home4T4.htm"); }
if (strstr(HTTP_req, "GET /Home4T5.htm")) {webFile = SD.open("Home4T5.htm"); }
if (strstr(HTTP_req, "GET /Home45.htm")) {webFile = SD.open("Home45.htm"); }
if (strstr(HTTP_req, "GET /Home50.htm")) {webFile = SD.open("Home50.htm"); }
// send web page to client
if (webFile) {
const int bufSize = 2048;
byte clientBuf[bufSize];
int clientCount = 0;
while (webFile.available())
{ clientBuf[clientCount] = webFile.read();
clientCount++;
if (clientCount > bufSize-1)
{ client.write((const uint8_t *)clientBuf, bufSize);
clientCount = 0;
}}
// final < bufSize byte cleanup packet
if (clientCount > 0) client.write((const uint8_t *)clientBuf, clientCount);
webFile.close(); // close the file
}}
// display received HTTP request on serial port
//Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} //end webserver
}
// sets every element of str to 0 (clears array) //webserver
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {return 0;}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {return 1;}
}
else {found = 0;}
index++;}
return 0;
}
void teensyMAC(uint8_t *mac) {
static char teensyMac[23];
#if defined(HW_OCOTP_MAC1) && defined(HW_OCOTP_MAC0)
Serial.println("using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0");
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
#define MAC_OK
#else
mac[0] = 0x04;
mac[1] = 0xE9;
mac[2] = 0xE5;
uint32_t SN=0;
__disable_irq();
#if defined(HAS_KINETIS_FLASH_FTFA) || defined(HAS_KINETIS_FLASH_FTFL)
Serial.println("using FTFL_FSTAT_FTFA - vis teensyID.h - see https://github.com/sstaub/TeensyID/blob/master/TeensyID.h");
FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
FTFL_FCCOB0 = 0x41;
FTFL_FCCOB1 = 15;
FTFL_FSTAT = FTFL_FSTAT_CCIF;
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
SN = *(uint32_t *)&FTFL_FCCOB7;
#define MAC_OK
#elif defined(HAS_KINETIS_FLASH_FTFE)
Serial.println("using FTFL_FSTAT_FTFE - vis teensyID.h - see https://github.com/sstaub/TeensyID/blob/master/TeensyID.h");
kinetis_hsrun_disable();
FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
*(uint32_t *)&FTFL_FCCOB3 = 0x41070000;
FTFL_FSTAT = FTFL_FSTAT_CCIF;
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
SN = *(uint32_t *)&FTFL_FCCOBB;
kinetis_hsrun_enable();
#define MAC_OK
#endif
__enable_irq();
for(uint8_t by=0; by<3; by++) mac[by+3]=(SN >> ((2-by)*8)) & 0xFF;
#endif
#ifdef MAC_OK
sprintf(teensyMac, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(teensyMac);
#else
Serial.println("ERROR: could not get MAC");
#endif
}