The previous example will only work if the computer running the browser has internet access. This is almost always possible on a developer machine, but there are situations where the user's computer might not have internet access (for example, out in the desert or on Mars, the coverage might not be that great). Luckily, we’ve got this covered as well. The Shellminator library can host the same WebTerminal you used earlier through its own web server, independent of the platform (there are a few minor differences, but we’ll go over them later).
This is really useful if you have a local network that’s not connected to the internet. However, you should know that this operation can be resource-intensive, but an ESP32 or a Pico W can handle it just fine. Starting from the previous example, all you need to do is import the required library:
Then, create an object from the ShellminatorWebServerThemedOffline
class. In its constructor, you’ll need to specify a port where the web server will be available. Since it’s a regular HTML server, **port 80* is recommended.
#define WEBSERVER_PORT 80
In the init
section, start the server (optionally, we log debug messages, but that’s not mandatory):
htmlServer.attachDebugChannel( &Serial );
htmlServer.begin();
Finally, in the loop
section, you’ll need to periodically update the web server:
If you’ve done everything right, all that’s left is to open a new tab in your browser and enter the device's IP address in the URL. You’ll see pretty much the same interface as the WebTerminal hosted in the documentation, except for a few differences:
- You don’t need to specify the WebSocket address since the WebTerminal can extract it from the URI.
- There are no tooltips to reduce the resources the page uses.
- The webpage has been minified to consume fewer resources.
Whole Code
#include "WiFi.h"
#define WEBSOCKET_PORT 443
#define WEBSERVER_PORT 80
char ssid[] = "Replace With Your SSID";
char pass[] = "Replace With Your Password";
uint8_t printBuffer[ 100 ];
int printBufferSize = sizeof( printBuffer );
uint8_t passwordHash[] = { 0xCC, 0xb4, 0x24, 0x83 };
const char logo[] =
" _____ __ ____ _ __ \r\n"
" / ___// /_ ___ / / /___ ___ (_)___ ____ _/ /_____ _____\r\n"
" \\__ \\/ __ \\/ _ \\/ / / __ `__ \\/ / __ \\/ __ `/ __/ __ \\/ ___/\r\n"
" ___/ / / / / __/ / / / / / / / / / / / /_/ / /_/ /_/ / / \r\n"
"/____/_/ /_/\\___/_/_/_/ /_/ /_/_/_/ /_/\\__,_/\\__/\\____/_/ \r\n"
"\r\n\033[0;37m"
"Visit on GitHub:\033[1;32m https://github.com/dani007200964/Shellminator\r\n\r\n"
;
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, pass);
Serial.print("Attempting to connect to Network");
while( WiFi.status() != WL_CONNECTED ){
Serial.print( '.' );
delay( 1000 );
}
shell.attachLogo( logo );
shell.enableBuffering( printBuffer, printBufferSize );
shell.setPassword( passwordHash, sizeof( passwordHash ) );
shell.begin( "arnold" );
.attachDebugChannel( &Serial );
htmlServer.begin();
ws.attachDebugChannel( &Serial );
ws.attachConnectCallback( userConnectedCallback );
ws.attachDisconnectCallback( userDisconnectedCallback );
ws.begin();
}
void loop(){
ws.update();
shell.update();
htmlServer.update();
delay( 2 );
}
shell.printLoginScreen();
}
shell.logOut();
}