I'm using a nano 33 iot with standard wifinina library. The nano is connected to my laptop's hotspot, and transmitts html web pages with client.println(). However when the page is getting large, it is only partially transmitted. In this case it breaks at the javascript part as the image below, so the interactive function of the page is broken.
I know it's a transmission problem because the breaking point changes when I refresh the page.
Why is it interrupted ? Are there timeout settings need to changed? How do I get the whole page transmitted?
// begin server
if (server_status == 2) {
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
// if you get a client,
Serial.println("\n== New ==");
String currentLine = "";
while (client.connected()) {
if (client.available()) // if there's bytes to read from the client,
{
char c = client.read(); // read a byte, then
//Serial.write(c); // print it out the serial monitor
if (c == '\n') // if the byte is a newline character
{
// end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
switch (update_page) {
case 1:
client.print(HTML_OK);
client.println(web_page);
update_page = 0;
break;
case 2:
client.println(web_page);
update_page = 0;
break;
default:
break;
}
// break out of the while loop:
break;
} else {
currentLine = ""; // if you got a newline, then clear currentLine:
}
} else if (c != '\r') {
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("PUT /led")) {
blink_timer = 0;
Serial.println(currentLine);
} else if (currentLine.endsWith("PUT /p1:")) {
Serial.println(currentLine);
} else if (currentLine.endsWith("PUT /p2:")) {
Serial.println(currentLine);
} else if (currentLine.endsWith("PUT /p3:")) {
Serial.println(currentLine);
} else if (currentLine.endsWith("PUT /p4:")) {
Serial.println(currentLine);
}
if (currentLine.endsWith("GET /home")) {
web_page = PAGE_MAIN;
update_page = 1;
Serial.println(currentLine);
} else if (currentLine.endsWith("GET /cal")) {
web_page = PAGE_CAL;
update_page = 1;
Serial.println(currentLine);
} else if (currentLine.endsWith("GET /test")) {
sprintf(buffer, "%.2f", double(then) / 1000000);
String mes = XMLheader;
mes += buffer;
mes.toCharArray(buffer, 512, 0);
web_page = buffer;
update_page = 2;
Serial.println(currentLine);
Serial.print("XML:");
Serial.println(web_page);
} else if (currentLine.endsWith("GET /updateTime")) {
sprintf(buffer, "<time>%u</time>", then / 1000000);
String mes = XMLheader;
mes += buffer;
mes.toCharArray(buffer, 512, 0);
web_page = buffer;
update_page = 2;
Serial.println(currentLine);
Serial.print("XML:");
Serial.println(web_page);
} else if (currentLine.endsWith("GET /updateCal")) {
sprintf(buffer, "<p1>%d</p1><p2>%d</p2><p3>%d</p3><p4>%d</p4>", p1, p2, p3, p4);
String mes = XMLheader;
mes += buffer;
mes.toCharArray(buffer, 512, 0);
web_page = buffer;
update_page = 2;
Serial.println(currentLine);
Serial.print("\nXML:");
Serial.println(web_page);
} else {
update_page = 1;
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
}
while (client.connected()) { // loop while the client's connected
delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out to the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
...
so they check against '\n' and if the previous line was empty ➜ that's a full empty line meaning the end of the header of the request
in your code you have
while (client.connected()) {
if (client.available()) // if there's bytes to read from the client,
{
char c = client.read(); // read a byte, then
//Serial.write(c); // print it out the serial monitor
if (c == '\n') // if the byte is a newline character
{
// end of the client HTTP request, so send a response: