Weather station
In this tutorial we are using an ESP32 with the Bosch BME280 sensor; an environmental sensor measuring temperature, the relative humidity, and the atmospheric pressure.
This example illustrates the difference between running a Toit program once on an ESP32 and deploying the same program as a long-lived app by using an app specification file.
The code examples shown in this tutorial are also available here.
Connect the sensor


Follow the steps here to connect the BME280 sensor with your ESP32 using a Qwiic cable.
Install package
If you haven't installed the Toit CLI and the Visual Studio Code language extension for Toit yet, follow this guide before proceeding with the tutorial.
In order to get data from the sensor we make use of the BME280 driver available as a package from pkg.toit.io.
Open Visual Studio Code, and create a new file. Paste the following Toit program into the file and save the file as: weather_station.toit
:
/** Program measuring temperature, relative humidity, and atmospheric pressure with a BME280. */ import gpio import i2c import bme280 main: bus := i2c.Bus --sda=gpio.Pin 21 --scl=gpio.Pin 22 device := bus.device bme280.I2C_ADDRESS_ALT driver := bme280.Driver device print "Temperature: $driver.read_temperature C" print "Humidity: $driver.read_humidity %" print "Pressure: $driver.read_pressure Pa"
Then, open a terminal window in VSCode and execute the following commands in order to install the BME280 driver package locally:
Run Toit program
If you want to know more about what is happening in each step in the Toit program above, see here.
Open the Toit console, find your ESP32 in the list of devices, copy and paste the program above into the Code tab your online ESP32 with the BME280 sensor connected, press the Run button and go to the Logs tab of the device to see the measured temperature, humidity and pressure values.
Running a Toit program in this way means that it will be uninstalled from the device once the program has terminated.
Deploy app
We can use the same Toit program to repeatedly measure the temperature, humidity, and pressure every 30 seconds by deploying it on the ESP32 using the following app specification file:
# filename: weather_station.yaml # This app specification file executes the 'Weather Station' app on install # and subsequently every 30 seconds. name: Weather Station entrypoint: weather_station.toit triggers: on_install: true on_interval: 30s
Save the content of the above yaml specification into a file named weather_station.yaml
located in the same directory as the weather_station.toit
file from the previous
step.
Deploy the Weather Station app on your ESP32 with the CLI command
If you haven't set your default device in the CLI, add -d device-name
to
the deploy command, where device-name
is the name of your ESP32.
The device is not required to be online when the Weather Station app is deployed. When the device comes online, the app will be installed on the device and start it's scheduled execution.
View logs
The device log will show the three measurements every 30 seconds after the program has been installed on the device.
View the device log in the Toit console in the Logs tab of the device.
Uninstall app
View all installed apps (or waiting to be installed) on your device from the Apps tab in the Toit console.
To uninstall an app, click on the Uninstall button in the Apps tab of the device.