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 code as an app together with a specification file.

Connect the sensor

Follow the steps here to connect the BME280 sensor with your ESP32.

Run on an ESP32

To get data from the sensors, we make use of the bme280 sensor driver by importing it in the Toit program with import drivers.bme280.

We also need to specify the GPIO pins and the relevant connection bus.

// Save this program in a file named thp.toit
import gpio
import serial.protocols.i2c as i2c
import drivers.bme280

  bus := i2c.Bus
    --sda=gpio.Pin 21
    --scl=gpio.Pin 22

  device := bus.device 0x77

  bme := drivers.Bme280 device

  print "Temperature: $bme.read_temperature C"
  print "Humidity: $bme.read_humidity %"
  print "Pressure: $bme.read_pressure Pa"

Save the above Toit program in a file named thp.toit.

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.

Deploy on an ESP32

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
    on_install: true
    on_interval: 1h

Save the content of the above yaml specification into a file named thp.yaml located in the same directory as the thp.toit file from the previous step.

Deploy the "Hourly THP" app on your ESP32 with the CLI command

toit deploy thp.yaml

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.

The device is not required to be online when the app is deployed. When the device comes online, the app will be installed on the device and start it's scheduled execution.

View all apps installed (or waiting to be installed) on your ESP32 from the Apps tab

The Toit console Apps tab

Uninstall an app

To uninstall an app, click on the Uninstall button in the Apps tab of the device.