Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server.avaiable() randomly stalls the MCU for X-seconds #20

Open
RayanR opened this issue Apr 19, 2021 · 0 comments
Open

Server.avaiable() randomly stalls the MCU for X-seconds #20

RayanR opened this issue Apr 19, 2021 · 0 comments

Comments

@RayanR
Copy link

RayanR commented Apr 19, 2021

when connecting top the server using a browser, it works fine untill it suddenly grinds to a halt and stops updating. After some searching, it seems the server halts for a while and then it starts running again. This behaviour results in lots of timeout errors

`
byte mac[] = {0x04, 0xe9, 0xe5, 0x0d, 0xd6, 0x4b};
IPAddress ip(10, 116, 114, 134); // IP address, may need to change depending on network

EthernetServer server(80); // create a server at port 80
EthernetClient client;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
uint32_t ethping = 0;
String loc = "loop";

void ethernet_init() {
// start the Ethernet connection and the server:
Ethernet.begin(mac); // DHCP
//Ethernet.begin(mac,ip); // Static IP

// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
if (Serial)Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
error_blink(ETHERNET_NO_HARDWARE);
do {
if (Serial)Serial.println("Please connect Ethernet shield");
error_blink();// do nothing, no point running without Ethernet hardware
}
while (Ethernet.hardwareStatus() == EthernetNoHardware);
error_blink(PULSE);
}
if (Ethernet.linkStatus() != LinkON) {
if (Serial)Serial.println("Ethernet cable is not connected.");
error_blink(ETHERNET_NO_CABLE);
do {
if (Serial)Serial.println("Please connect Ethernet cable");
error_blink();// do nothing, no point running without Ethernet cable
}
while (Ethernet.hardwareStatus() == LinkOFF);
error_blink(PULSE);
}

// start the server
server.begin();
if (Serial) {
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
}

void ethernet_client() {
client = server.available(); // try to get client

if (client) { // got client?
boolean currentLineIsBlank = true;
if (Serial) Serial.println("Client");
while (client.connected()) {
loc = "ethc";
can0.events();
error_blink();
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) {
if (Serial) Serial.println("XML request");
loc = "ethx";
// 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, "HVPC_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
Settings();
// send XML file containing input states
XML_response(client);
}
else { // web page request
if (Serial) Serial.println("Page request");
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
File webFile = SD.open("index.htm"); // open web page file
if (webFile) {
error_blink(SENDING_FILE);
loc = "ethf";
char file_buffer[ETH_BUFFER_SIZE];
int avail;
while (avail = webFile.available()) {
int to_read = min(avail, ETH_BUFFER_SIZE);
if (to_read != webFile.read(file_buffer, to_read)) {
break;
}
client.write(file_buffer, to_read);
}
webFile.close();
error_blink(PULSE);
}
}
// display received HTTP request on serial port
if (Serial) Serial.println(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
if (Serial) Serial.println("cleared");
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
client = 0;
Ethernet.maintain();
if (Serial)
{
Serial.println("Client stopped");
Serial.println();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant