Skip to content

Run a program on a device#

There are two ways to execute code on a device:

  • Running programs

    Running a program means that the Toit code runs once on an online device. Once the program is finished running, or if the program lasts longer than 2 minutes, the program is removed from the device.

  • Deploying applications

    Deploying an application onto a device is done using an app specification file. This means that the Toit code will be executed multiple times over the lifecycle of the device as an application.

Running programs on a device#

This option makes it easy to run small snippets of Toit code on a device. The device needs to be online since the installation and execution of the code happens synchronously such that you get feedback right away.

Using the Code tab of an online device, a "Hello, World" Toit program can be run by clicking Run on Device.

runcodedevice

Check the output to see that it has printed "Hello, World!". The output is also shown in the logs tab of the device, and in the Humio tab.

Refreshing the browser page will delete the program you just wrote. Similarly, exiting the tab while your program is running will stop its execution.

After writing a program, for example in VSCode, save it as a .toit file. Find a candidate device with the toit devices command or use a simulator with toit simulator start.

Set the device or simulator as your default device with toit device use <name>.

Then execute using toit run <toit-file>, which can include the path where the Toit file was saved (for example Desktop/hello.toit).

Access the log for the previous 5 minutes with toit device logs -f 5m.

The program is run once, and uninstalled automatically after its completion. Programs are automatically terminated and uninstalled after two minutes of execution.

Running a program on a device does not allow the device to go into deep sleep as it needs to remain online for the program to run, even if it is set to make a measurement only once a day. Therefore the toit run command is great for running small snippets of code on a device but not built for running actual long-lived applications. For this reason, the Toit cloud will terminate any running program after 2 minutes of execution. Long-lived apps need to be deployed with a yaml file describing when and how often the application must run.