icon

Toit.io | IoT Platform Series #2

0 40530 Easy

Use web dashboard and CLI to control and monitor an IoT Device (ESP32). Written in an IoT specific language, toitlang.

projectImage

Things used in this project
 

Espressif ESP32 Development Board - Developer Edition 

 

What is Toit?

 

Created by former Google Employees, Toit was a concept to make IoT Devices easily accessible, to monitor and control for enterprise uses. To simply secure the code on our ESP32 microcontrollers with lightweight containers and orchestrate them through our cloud API.

It uses sandbox containers, where our applications run isolated from the system, and each other, on the devices. Communicate with our cloud through a simple API (CLI), we take care of the rest.
 

Get PCBs for Your Projects Manufactured

projectImage

You must check out PCBWAY for ordering PCBs online for cheap!

You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop.

 

Platform

We own our data and we get to store it wherever we want. Feed data from our devices directly into your own system using HTTPS or MQTT, or let the platform help us get our bits to and from our devices. It is as simple as that.

Deploy code, access logs, update firmware, and install applications on your devices. Let your data flow into your own system, and share it with your customers. They just handle your microcontrollers.

Get full visibility into your device fleet with logs covering connectivity, code execution, and crash reports. Trace the bug, fix it and redeploy, all within minutes. Assign your devices into groups and deploy updates on a group-by-group basis.

All these high-level complex features, with the help of Toit.

 

Toit Language

The Toit platform is built on the free and open-source object-oriented Toit language, so we write applications in a high-level, memory-safe language and let the battery-optimized virtual machine execute them efficiently on your ESP32. Fast to develop, safe to run. Let us call it a language for the IoT.

There are two ways to get started with setting up and using the Toit Cloud - Toit Web Console and CLI using Jaguar.

 

 

 

Web Console

With a visual presentation, it gets easier to understand the basic function and concept of the IoT Platform. Make sure the browser supports the Serial interface over the browser. Ideally, Google Chrome is the perfect one to use the platform on.

Head to https://console.toit.io/ and Sign In to the Toit Console. And make sure you're on the Devices tab, click on the Set up ESP32 button.

projectImage

Now follow the steps as mentioned on the page. Make sure the below step is performed on a browser with a serial peripheral interfacing feature.

projectImage

For any kind of error, the dashboard shall handle them all. And you can follow the provided steps to continue.

Provide WiFi credentials the device will connect with -

projectImage

After providing the credentials, the device provisioning step will begin -

projectImage

After it completes, we can view the device name provided, and buttons to proceed further -

projectImage

Let us start with writing code.

To get started with simple things, let us control the LED on our ESP32. For that, go to the right-hand section in the Examples > GPIO > Blinking LED.

projectImage

Pin 2 of ESP32 is usually the built-in LED so we can change it to that in the code. And, change the time duration of the blinking as well. You can access the below code and other codes from here.

CODE
import gpio

/// Built-in LED.
LED ::= 2

main:
  led := gpio.Pin LED --output
  while true:
    print "blink"
    led.set 1
    sleep --ms=100
    led.set 0
    sleep --ms=100

Now click on Run on device.

projectImage

We can see the toit console letting us know of the execution. As well as the LED blinks till the code is running.

projectImage

Another code to test for monitoring the ESP32, using the adc pin check.

CODE
import gpio
import gpio.adc

/// The PIN for the photo resistor.
PHOTO_RESISTOR_PIN ::= 32

/// Lighting threshold. Change the threshold to fit your lighting conditions.
THRESHOLD ::= 2.2

main:
  pin := gpio.Pin PHOTO_RESISTOR_PIN
  while true:
    sensor := adc.Adc pin
    print sensor.get*100
projectImage

You can see that the files are saved in the .toit extension.

 

CLI using Jaguar

Jaguar is a small Toit application that runs on your ESP32. It uses the capabilities of the Toit virtual machine to let you update and restart your ESP32 code written in Toit over WiFi. Change your code in your editor, update it on your device, and restart it all within seconds. No need to flash over the serial, reboot your device, or wait for it to reconnect to your network.

For more information visit the GitHub Repo (open source). Download the precompiled binary to directly get started -

For macOSFor WindowsFor Linux

After the download is complete, run the installer to install and add the file to the environment PATH. After the installation is done, open CMD or Powershell and enter command - jag setup

projectImage

Now let us flash and begin with the procedure to connect Toit Cloud with ESP32 using jaguar.

Enter the command to flash the firmware on ESP32 - jag flash and then select the port our ESP32 is connected with, wifi username, and password. Next enter the command flash

projectImage

Copy the below code and save it in a file with the extension '.toit'. Example - control.toit. You can access the below code and other codes from here.

CODE
import gpio

/// Built-in LED.
LED ::= 2

main:
  led := gpio.Pin LED --output
  while true:
    print "blink"
    led.set 1
    sleep --ms=100
    led.set 0
    sleep --ms=100
projectImage

Now let us test the program. Run the command jag run control.toit (or whatever filename is used). and we shall see the ESP32 responding to the program as per the code. We can also use jag watch control.toit to ask Jaguar to keep watching your Toit code on disk and to live reload it when it changes.

projectImage

Let us try and grab data from the ESP32 now. Copy the code below and save it in another file. Example - monitor.toit

CODE
import gpio
import gpio.adc

/// The PIN for the photo resistor.
PHOTO_RESISTOR_PIN ::= 32

/// Lighting threshold. Change the threshold to fit your lighting conditions.
THRESHOLD ::= 2.2

main:
  pin := gpio.Pin PHOTO_RESISTOR_PIN
  while true:
    sensor := adc.Adc pin
    print sensor.get*100

Now, this time is necessary to run the program in a specific manner. One hand, jag watch monitor.toit command needs to be run on a Code editor terminal (Visual Studio) while the other command jag monitor (not filename) on a normal CLI. The scenario we shall see would look like this along with the command on top)

 

projectImage

We can see the values read over the serial monitor when the watch command was used.

projectImage

Now that we are done, let us find one more (not so) interesting thing. When we use the run or watch command, alongside the [jaguar] prompt, there is an INFO present, provided with a local IP with the port address. If we open that on our browser, the info is available there.

License
All Rights
Reserved
licensBg
0