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 code as an app together with a specification file.
Follow the steps here to connect the BME280 sensor with your ESP32.
To get data from the sensors, we make use of the bme280 sensor driver by importing it in the Toit program with
// Save this program in a file named thp.toit import gpio import serial.protocols.i2c as i2c import drivers.bme280 main: bus := i2c.Bus --sda=gpio.Pin 21 --scl=gpio.Pin 22 device := bus.device 0x77 bme := drivers.Bme280 device bme.on print "Temperature: $bme.read_temperature C" print "Humidity: $bme.read_humidity %" print "Pressure: $bme.read_pressure Pa" bme.off
Save the above Toit program in a file named
Copy and paste the program above into the Code tab of an online ESP32 with the temperature sensor, press the Run on device button and go to the Logs tab of the device to see the logged temperature, humidity and pressure from the ESP32.
Running a Toit program in this way means that it will be uninstalled from the device once the program has terminated.
We can use the same Toit program to repeatedly measure the temperature, humidity, and pressure once every hour by deploying it on the ESP32 using the following app specification file:
# filename: thp.yaml # This app specification file executes the 'Hourly THP' app on install # and subsequently every hour. name: Hourly THP entrypoint: thp.toit triggers: on_install: true on_interval: 1h
Save the content of the above yaml specification into a file named
located in the same directory as the
thp.toit file from the previous
Deploy the "Hourly THP" 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 log will show the three measurements every hour after the program has been installed on the device.
View all apps installed (or waiting to be installed) on your ESP32 from the Apps tab
To uninstall an app, click on the Uninstall button in the Apps tab of the device.