For this project you need an ESP32 dev board. Most dev boards with ESP32 will work just fine, but in this project the following one is used:
In addition, we need a BME280 sensor. The sensor at the link below is a suitable choice for this project:
Get access to the Toit platform
A 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 ESP32
A 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.
Hooking up the hardware
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.
Toit code and YAML file for BME280
To deploy a Toit app on the ESP32, 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
The 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 corresponding 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 apps
With 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...Read more »