With the Toit platform, developers can treat an ESP32 as a regular computer, installing apps just as easy as on a computer or a phone. Using the high-level language Toit, it is easy to implement functionality and applications while the Toit platform ensures that the device keeps functioning no matter what. This project tests the Toit platform by connecting a BME280 sensor to an ESP32, and reading data from this sensor. The data is published on the Toit console log.
Hardware neededFor this project you need an ESP32 dev board. Most boards will work just fine, but in this project the following one is used:
https://www.digikey.com/en/products/detail/espressif-systems/ESP32-DEVKITC-32D/9356990
In addition, we need a BME280 sensor. The sensor at the link below is a suitable choice for this project:
https://www.sparkfun.com/products/14348
Get access to the Toit platformA free account can be created at toit.io. The first 100MB of data that flows from the ESP32 to the platform are for free. Follow the link below, and click "Try for free" on the web page:
When you sign up, an organisation is created in the Toit console and you'll get an email to set your password. You can then log on to the console and see your devices once you've claimed them (see next section).
Provision and claim your ESP32A quick guide on how to install the required Toit software on your computer, provision your ESP32, and subsequently claim it so that it shows up in the Toit console can be found here:
After claiming the device, it should show up in the list of devices within a few seconds. Click on the device for more information about it.
Below is a Fritzing diagram of how to hook up the BME280 sensor to the ESP32. Note that most ESP32 dev boards are too wide for a standard breadboard. Therefore, wires must be connected under the dev board. Since we're using I2C for this setup, we connect the wires as follows:
- SDA -> GPIO21
- SCL -> GPIO22
Finally, we of course need to connect 3.3V and GND.
To deploy an app, we need both some Toit code, and a YAML scheduling file.
The Toit code is relatively self-explanatory, see below. First we need libraries for GPIO, I2C, and sensor access. In the main function we setup the I2C bus with address 0x77 and set GPIO 21 and 22 to be SDA and SCL, respectively. We also create a sensor object that contains methods for reading data from the sensor. The print command prints data on the log in the Toit console, with formatting.
// Filename: bme280.toit
import gpio
import serial.protocols.i2c as i2c
import drivers.bme280
main:
bus := i2c.Bus
--sda=gpio.Pin.out 21
--scl=gpio.Pin.out 22
device := bus.device 0x77
bme := drivers.Bme280 device
bme.on
print "Temperature: $(%.1f bme.read_temperature) C"
print "Humidity: $(%.1f bme.read_humidity) %"
print "Pressure: $(%.1f bme.read_pressure / 100) hPa"
bme.off
We also need a YAML file. The YAML file takes care of the scheduling of the Toit code, i.e. when the code should be executed. We give it the name "Measure THP", point to the Toit file bme280.toit, and finally we instruct the system to run the code when we install the app, and subsequently every five seconds.
// Filename: bme280.yaml
name: "Measure THP"
entrypoint: bme280.toit
triggers:
on_install: true
on_interval: 5s
Toit Code and YAML file for printing time stampThe Toit code for printing a formatted time stamp on the log is as follows:
// Filename: time.toit
main:
time := Time.now.local
print "Current Time: $(%02d time.h):$(%02d time.m):$(%02d time.s) - $(%02d time.day)/$(%02d time.month)"
In the YAML file we choose to run this Toit code once we install the app, and then every five seconds.
// Filename: time.yaml
name: "Timestamp"
entrypoint: time.toit
triggers:
on_install: true
on_interval: 5s
Deploying the appsWith the Toit and YAML files above saved, we can deploy these apps on the ESP32 using the following commands, given that you named the ESP32 "Toit1" in the Toit console, and that the YAML filenames are "bme280.yaml" and "time.yaml":
$ toit device -d Toit1 deploy bme280.yaml
successfully deployed the job Measure THP on Toit1
$ toit device -d Toit1 deploy time.yaml
successfully deployed the job Timestamp on Toit1
Go to the Toit console and click on your device. The two installed apps should now be listed under the APPS tab. Note the "Uninstall" buttons on the far right. These can be used for uninstalling the corresponding app:
If we switch to the LOGS tab, we should see temperature, humidity, and barometric pressure data being published in the log.
You can now try to modify the code and re-deploy the app. Note that you do not have to uninstall an app before you re-deploy it.
Comments