Skip to content

Temperature, Humidity and Pressure#

We are using in this example the Bosh BME280 sensor, an environment sensor measuring temperature, humidity and pressure. This example illustrates the difference that exists between running a program and deploying it as an app with a configuration file.

Running a THP program#

To get data from the sensors, we refer to the sensor driver by importing it in the program with import <driver_name>. We also need to specify the GPIO pins and the relevant connection bus.

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"

Paste the program above in the Code tab of an online device and check the logged temperature, humidity and pressure in the Logs tab or in the Humio tab of the device.

Save the above toit program in a file named thp.toit, and execute the command toit run thp.toit to run the program on the device, and check the log in the CLI.

$ toit run thp.toit 2020-08-15T13:09:04.801533Z: <process initiated> Temperature is 23.1 C Humidity is 29% Atmospheric Pressure is 1006.0 Pa 2020-08-15T13:09:05.020756Z: <process terminated - exit code: 0>

And remember, running a program means that it will be uninstalled from the device once the program is done running.

Deploying a THP app#

We can use this program to measure the temperature, the relative humidity and the atmospheric pressure once every hour by deploying it using the following app specification file:

# filename: tutorials/thplog.yaml

# This job specification file runs the Hourly THP app on install and every hour after that.

name: Hourly THP
entrypoint: thplog.toit
  on_install: true
  on_interval: 1h

Deploy on your default device using the CLI with

toit deploy thp.yaml

The log will show the different measures every hour after the program was installed.

To uninstall an app, click on the Uninstall button.

To uninstall an app, run toit device ps to get the list of installed apps. Then use the app ID in the toit device uninstall <app_ID or app_name> command.