REMOTE CONSOLE
Remote console for IoT devices
Remote console is a unique Thinger.io feature to allow remote access on your devices. It allows to create web terminals to interact with the device like in a serial interface. It also provides features to create interactive terminals on any Arduino compatible device.
This feature is specially useful for remote diagnosis, show logs on devices, manage device configuration, or any other functionality, as the console can be easily extended to include new commands.
Remote console for IoT devices

Web Serial Interface

By default, the Thinger.io Console can work as a web serial interface, similar to the Arduino Serial Monitor where the device write logs to serial interface.
To use Thinger.io Console it is required to include the ThingerConsole.h file and create a instance over thing reference.
1
#include <ThingerConsole.h>
2
3
ThingerConsole console(thing);
Copied!

Writing to Console

Then it is possible to use the console instance for logging to the remote terminal by using the same interface available in the Arduino Serial class, i.e, using println, printf, etc. For example, the following code will show a log every second:
1
unsigned long last_log = 0;
2
void loop() {
3
thing.handle();
4
auto current = millis();
5
6
if(current-last_log>=1000){
7
last_log = current;
8
console.printf("printing on serial interface: %lu\r\n", current);
9
}
10
}
Copied!
When the terminal is connected in the Thinger.io console, the device will start to send the configured log. If there is no any terminal connected to the device, the device will skip sending any data over the wire.
Writing to Thinger.io web console
It is possible to check if the console is connected, so the associated loging code can be omitted, which can be useful if the log involves doing some calculation, reading a sensor, etc. The above example can be improved adding an extra check
1
unsigned long last_log = 0;
2
void loop() {
3
thing.handle();
4
5
// check if console is connected
6
if(console){
7
auto current = millis();
8
if(current-last_log>=1000){
9
last_log = current;
10
console.printf("printing on serial interface: %lu\r\n", current);
11
}
12
}
13
}
Copied!
If web console is not connected, the device will not send any data over the wire.

Reading from Console

Reading from serial can be done in the same way it is done over Arduino Serial interface, for example, the following code sample will check if there is any data available using the available method on the console (in the same way it is used on Arduino Serial) , and will print back to the console:
1
void loop() {
2
thing.handle();
3
if(console.available()){
4
console.setTimeout(100);
5
String input = console.readStringUntil('\n');
6
console.print("> I received: ");
7
console.println(input);
8
}
9
}
Copied!
Reading from Thinger.io remote console

Interactive terminals

It is possible to create interactive terminals easily for handling custom commands with arguments. For working with the interactive terminal mode it is required to create and register commands on the console instance.

Create Commands

Creating a new command in the console can be done on the setup function. It is required to call the function command over the console instance. This function requires two arguments: the command name and the command function. There is an optional parameters that is the command description, that can be useful to specify command arguments or any other help about the command.
In the following sections are presented some command examples covering different requirements, like writing to console, reading arguments, or reading from console.

Millis Command Example

The most simple example of a command can be defined as the following, where it is registered a command with the name millis, and a function that just print the result of the millis() function over the console terminal. It also includes a description in the third argument with this content.
1
void setup() {
2
// any other code
3
4
// console commands
5
console.command("millis", [&](int argc, char* argv[]){
6
console.println(millis());
7
}, "get current time millis");
8
}
9
10
void loop() {
11
thing.handle();
12
}
Copied!
This will result in a prompt with an interactive terminal in the console, with the ability to execute the millis function defined above.
Millis Command Example
The function definition with the syntax [&](int argc, char* argv[]{}is a C++ lambda function, but it is possible to use any othervoid function(int argc, char* argv[]){}

Log Command Example

There are some cases where commands require to be running during long periods, i.e., to print an ongoing log. In the following example, the command will print a sample log until the command is canceled or the console is closed.
1
void setup() {
2
console.command("log", [&](int argc, char* argv[]){
3
while(console.command_running()){
4
console.printf("Running log at %lu\r\n", millis());
5
delay(1000);
6
}
7
}, "show logs");
8
}
Copied!
To work with long-running commands it is required to frequently check the command_running method, that will return false if the console has been closed, or the command was cancelled.
To cancel a running command on the terminal just press Ctrl+C
Log Command Example

GPIO Command Example

Some commands may need to accept arguments, i.e., turn on/off a given gpio, print a given string to a display, modify a threshold, etc. For this reason, all commands receive argc and argv parameters, like in any standard C/C++ main function. argc determines the number of arguments, and argv is an array of char* which holds all parsed parameters. Any command will receive at least one argument, which is the command name.
In the following example, it is created a command to modify digital state of a given GPIO. This function accepts two parameters, which are the GPIO pin number and the desired state.
1
void setup() {
2
console.command("gpio", [&](int argc, char* argv[]){
3
if(argc<3){
4
console.error("missing parameters");
5
}else{
6
int pin = atoi(argv[1]);
7
pinMode(pin, OUTPUT);
8
digitalWrite(pin, strcmp(argv[2],"on")==0);
9
console.printf("%d turned %sn", pin, argv[2]);
10
}
11
}, "<pin> <on|off> turn on/off a given gpio");
12
}
Copied!
GPIO Command Example

Hello Command Example

Commands may require also to read console inputs interactively while they are running. In the following example it is outlined a simple command that ask for a name to say it hello.
1
void setup() {
2
console.command("hello", [&](int argc, char* argv[]){
3
console.print("please, enter your name: ");
4
console.flush();
5
while(console.command_running() && !console.available());
6
String name = console.readStringUntil('\n');
7
console.printf("hello %s\r\n", name.c_str());
8
}, "hello command");
9
}
Copied!
Once the command is executed it will prompt the user for its name, waiting until it is ready. After typing the name and pressing enter, then it will display the name with a hello.
Interactive Command Example

Terminal Prompt

By default, when the interactive terminal is enabled it will show a prompt with the device identifier. In the following capture, it shows the esp32 name which is the identifier for the device.
Default Console Prompt based on the device ID (esp32)
The prompt name can be modified by using the set_prompt("my prompt") method on the console instance. For example:
1
void setup(){
2
console.set_prompt("Arduino");
3
}
Copied!
Will result on a modified prompt with the Arduino name.
Custom Prompt

Default Commands

By default, Thinger.io adds some general utily commands.

Help command

Help command will display all registered commands with an associated description.
Help command to see the registered commands

Reboot command

Thinger.io client implements a command that will reboot your device, as in a normal computer.
Reboot command

Clear command

It can be useful to clear the console sometimes, so, it is a clear console command to clear all screen content.
Clear Command

SSH Connection

Console can be accessed over the Thinger.io web console, but there is also a possibility for connecting devices over standard SSH connections from the Internet (no local network required). This feature is a work in progress and will be released soon as a plugin!
Last modified 2mo ago