Become an IoT developer in 15 minutes
Tags: Makers, IoT, Microcontrollers, Esp32, Toit

Become an IoT developer in 15 minutes

From signing up to a working IoT product in 15 minutes? It is possible with Toit. Toit Follow Apr 23 · 4 min read

Implementing IoT solutions in 2021 is not exactly pleasant: Developing in C is complex and error-prone and updates on remote devices are too risky. We knew that to make IoT development efficient, we had to start from scratch with a new language, a robust and efficient virtual machine, and great developer tools. Everything Toit is optimized for iterative app deployment and seamless firmware updates.

To demonstrate how easy it is to become an IoT developer with Toit, we will show you how to set up a minimalist weather station measuring temperature, humidity, and atmospheric pressure — all within minutes of signing up.

Hardware

Here is what you need to get started:

You just have to connect the sensor to the ESP32 and the ESP32 to your computer, which is done in a few steps. Then you are good to go.

We connect the Qwiic cable to the BME280 breakout board with the red wire to 3.3V (3V3) and the black wire to Ground. We chose to connect the blue (I2C SDA) to the GPIO pin 21 and the yellow (I2C SCL) to pin 22.

Sign up

Sign up on the Toit website and follow the instructions that will guide you through Toit installation and the provisioning of your device with the latest Toit firmware. You should be up and running within 5 minutes, and see your newly claimed device in the console. We called ours the-15-minutes-weather-station , so if you pick something else note it down.

Validate your setup

The BME280 sensor is connected to the microcontroller via GPIO pins, and the sensor supports both I2C and SPI connection modes. All the code needed to handle the pins and the connection is already included in the Toit libraries, so that the only thing left to do is to write apps.

Once your ESP32 is available in the list of devices, select it and go to the Code tab to paste the following program, written to check that the sensor is correctly connected:

Click on “Run on device” and check the output. If the sensor is not connected correctly, you will get an error message that will help you fix your hardware (did you use the right pins?).

No error message? Your sensor is properly connected and you are good to go!

As you can see from the program above, a simple Toit program follows a structured skeleton:

First, you import the libraries you need;

Then you define your variables, functions, and classes.

Write your first program

With the bus correctly configured, make the following additions to the program:

In the import section, tell the program where to find the driver for the sensor with import drivers.bme280

In the main function, use the BME280 I2C address found in the sensor datasheet to configure the device via device := bus.device 0x77 and construct the driver using bme := drivers.Bme280 device

Finally, in main use the API provided by the driver to read out the data and print it to the logs.

Voilà! You have written your first Toit program. You can see the full program on GitHub. Paste it in the Code tab of your device, and see the data in the output window below.

In the Code tab of the device, you can read the output of a program right below the code that generated it. This makes it easy to try things out and improve your Toit programs.

Deploy your first app

Now that your program is working, you can create your first app.

An app consists of two files: a .toit file with a program like you just wrote, and a .yaml file that designates the program and specifies when it should run. Copy all the code from the Code tab above and put it into a file called weather-station.toit . Then create a weather-station.yaml file next to it with this content:

In this example, the app is called THP—short for temperature, humidity, and pressure. It will run the weather-station.toit program at installation and every 30 seconds after that.

Remember that the weather-station.toit file should be in the same folder as the corresponding .yaml file. Now to deploy your app on your aptly named device (you did note the name down, right?), run the following command using the Toit command line interface from that folder:

toit device -d the-15-minutes-weather-station deploy weather-station.yaml

Your app is now installed on your device and you can see it in the list of apps in the console. It runs and produces log entries every 30 seconds, just like you told it to.

And here you are, an IoT developer in Toit with a minimalist weather station, all within 15 minutes of getting started.

Want to keep going? Find the full code for this app and more on GitHub. Check the list of drivers that are included in the Toit libraries or find one in our registry, and get hacking! Sign up on toit.io or reach out to our team directly at contact@toit.io.