Skip to content

Commit

Permalink
Consistent mesh management
Browse files Browse the repository at this point in the history
Add mesh.begin() after renew to bring more consistency to the ethernet examples. Replace client.write() with client.print() function in main html example
  • Loading branch information
TMRh20 committed Nov 21, 2021
1 parent 5a60e61 commit 44e456b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ void loop() {
if ( ! mesh.checkConnection() ) {
Serial.println("*** RENEW ***");
//refresh the network address
mesh.renewAddress();
if ( ! mesh.renewAddress() ){
mesh.begin();
}

} else {

Expand All @@ -146,8 +148,8 @@ void loop() {
Serial.print((char)client.read());
}
// Send an HTML response to the client. Default max size/characters per write is 90
client.write( "HTTP/1.1 200 OK\n Content-Type: text/html\n Connection: close \nRefresh: 5 \n\n");
client.write( "<!DOCTYPE HTML>\n <html> HELLO FROM ARDUINO!</html>");
client.print( "HTTP/1.1 200 OK\n Content-Type: text/html\n Connection: close \nRefresh: 5 \n\n");
client.print( "<!DOCTYPE HTML>\n <html> HELLO FROM ARDUINO!</html>");
client.stop();

Serial.println(F("********"));
Expand Down
31 changes: 15 additions & 16 deletions examples/InteractiveServer_Mesh/HTML.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ static const PROGMEM char credits_html[] =
*/
void sendPage(EthernetClient& _client, const char* _pointer, size_t size ) {
for (uint16_t i = 0; i < size; i++) {
char c = pgm_read_byte(_pointer++);
_client.write(&c, 1);
_client.print((char)pgm_read_byte(_pointer++));
}
}

Expand All @@ -122,14 +121,14 @@ void main_page(EthernetClient& _client) {

if (!led_state) {
sprintf_P(bf, PSTR("<tr><td bgcolor=%s>\n"), lColor);
_client.write(bf);
_client.print(bf);
sprintf_P(bf, PSTR("LED is %s</td></tr>\n"), lState);
} else {
sprintf_P(bf, PSTR("<tr><td> </td><td bgcolor=%s>\n"), lColor);
_client.write(bf);
_client.print(bf);
sprintf_P(bf, PSTR("LED is %s</td></tr>\n"), lState);
}
_client.write(bf);
_client.print(bf);

// Send the 2nd half of the page
static const char* html_pointer2 = main_html_p2;
Expand Down Expand Up @@ -165,27 +164,27 @@ void stats_page(EthernetClient& _client) {
char buffer[45];

strncpy_P(buffer, PSTR("HTTP/1.1 200 OK\nContent-Type: text/html\n"), 45);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("Connection: close\n\n<!DOCTYPE HTML>\n<html>\n"), 45);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("<head><style>body{background-color:linen;}\n"), 45);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("td{border: 1px solid black;}</style></head>\n"), 45);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("<body><table><tr><td> Uptime</td><td>\n"), 45);
_client.write( buffer );
_client.print( buffer );
sprintf_P(buffer, PSTR("%u days, %lu hours %lu minutes %lu"), days, hours, minutes, seconds);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("seconds</td></tr><tr><td>UIP Buffer Size"), 45);
_client.write( buffer );
_client.print( buffer );
sprintf_P(buffer, PSTR("</td><td>%u bytes</td></tr><tr><td>User "), UIP_BUFSIZE);
_client.write( buffer );
_client.print( buffer );
sprintf_P(buffer, PSTR("Output<br>Buffer Size</td><td>%u bytes"), OUTPUT_BUFFER_SIZE);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("</td></tr></table><br><br>"), 45);
_client.write( buffer );
_client.print( buffer );
strncpy_P(buffer, PSTR("<a href='/'>Home</a></body></html>"), 45);
_client.write( buffer );
_client.print( buffer );

}

Expand Down
7 changes: 4 additions & 3 deletions examples/InteractiveServer_Mesh/InteractiveServer_Mesh.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ void loop() {
mesh_timer = millis();
if ( ! mesh.checkConnection() ) {
//refresh the network address
mesh.renewAddress();
if ( ! mesh.renewAddress() ) {
mesh.begin();
}
}
}

Expand All @@ -93,6 +95,7 @@ void loop() {
// If a request is received with enough characters, search for the / character
if (size >= 7) {
char slash[] = {"/"};
client.setTimeout(10000);
client.findUntil(slash, slash);
char buf[3] = {" "};
if (client.available() >= 2) {
Expand Down Expand Up @@ -121,9 +124,7 @@ void loop() {
}
}
// Empty the rest of the data from the client
//while (client.waitAvailable()) {
client.flush();
//}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ void loop() {
mesh_timer = millis();
if ( ! mesh.checkConnection() ) {
//refresh the network address
mesh.renewAddress();
if ( ! mesh.renewAddress() ) {
mesh.begin();
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion examples/SLIP_InteractiveServer/SLIP_InteractiveServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void loop() {
mesh_timer = millis();
if ( ! mesh.checkConnection() ) {
//refresh the network address
mesh.renewAddress();
if ( ! mesh.renewAddress() ) {
mesh.begin();
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion examples/SimpleClient_Mesh/SimpleClient_Mesh.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ void loop() {
if (millis() - mesh_timer > 30000) { //Every 30 seconds, test mesh connectivity
mesh_timer = millis();
if ( ! mesh.checkConnection() ) {
mesh.renewAddress();
if ( ! mesh.renewAddress() ) {
mesh.begin();
}
}
}

Expand Down

0 comments on commit 44e456b

Please sign in to comment.