Secure Your Home With Toit

by Qvistiger in Circuits > Electronics

461 Views, 3 Favorites, 0 Comments

Secure Your Home With Toit

hackathon.png

The Toit platform transforms an ESP32 into a regular computer, where installing apps is just as easy as it is on a computer or a mobile phone. Using the high-level language Toit you can build applications and install them on the device while the device keeps on working no matter what.

In this project, you will build with Toit your own smart home security system that can detect intruders in your home. The project consists of three levels, each extending the previous one. You will learn how to write Toit applications and how to install these on your ESP32. You will also learn how to send sensor data to an external client service.

Supplies

Supplies for the light sensor alarm:

  • 1 ESP32
  • 2 x Breadboards
  • 1x 10kΩ resistor
  • 1x photoresistor
  • 5 jumper wires
  • a micro-B USB cable to connect the ESP32 to your computer

Supplies for the magnetic door switch alarm:

  • The setup from above
  • 1x magnetic door switch (formally known as a reed switch)
  • 3x jumper wires
  • 1x 10kΩ resistor
  • 1x LED diode

To work with Toit:

  • Create a free account at toit.io.
  • Install Toit on your computer.
  • Install the Toit VS Code extension in Visual Studio Code if you prefer working without using a command line interface.

Install the Toit Firmware on Your ESP32

Screenshot from 2021-09-06 09-41-08.png

Once you are signed up, you can access the Toit console. Provision your ESP32 with Toit using the Serial page. Follow the instructions given on the page, and once you are done, you can see your device online (using your WiFi) on the Devices page. From now on, all communication with the ESP32 will be done over-the-air.

Light Sensor Alarm

step1schematic.png

This part of the project assumes that night intruders will use a flashlight to orient themselves when entering your home. We can thus use a photoresistor, i.e., a variable resistor that changes its resistance based on the light level, to detect the entrance of intruders in your house.

Use the schematic above to build your device. We use the GPIO pin 34 in this example.

Write and Deploy the Light Sensor App

Once you are done building, you will deploy your first Toit app on the device. A Toit app is made of two different files: a .toit file with the program you want to run, and a .yaml file that specifies when the program should run.

Copy and paste the code found here in VSCode, and use the CLI or the left-hand side menu in VSCode to deploy the app on the device. Then verify in the Toit console that the Light detector app is in the list of installed apps.

Magnetic Door Switch Alarm

step2schematic.png

Another way of detecting intruders is by monitoring the door, which can either be open or closed. We will use an LED to show the state of the door. Use the schematic above to add the LED and the magnetic door switch to your device.

Then write your app in VS Code using the toit and yaml source code from here, and deploy your app as you previously did. Using the Toit console, you can verify in the Apps tab of your device that you now have two apps running in parallel on your device, isolated from each other.

Smart Home Alarm

step2pubsub2.png

You can write your own backend or client application to communicate, fetch and store data from devices using the Toit API. But first, we need to create two new PubSub subscriptions. This can be done either via the Toit CLI or from the Toit console.

  • Either, using the CLI, execute the two commands:

toit pubsub subscription create cloud:door/in MyPubsubTopic

toit pubsub subscription create cloud:door/out MyPubsubTopic

  • Or, from the Toit console, on the PubSub page, click Create subscription. For the first subscription, choose cloud:door/in as the topic and MyPubsubTopic as the name. Do the same for cloud:door/out.

Uninstall and Reinstall Apps

Screenshot from 2021-09-06 10-59-19.png

Then, fetch from here the toit and yaml source code needed to write your third app. Note that the third app uses the same GPIO pins as the second, so you can simply uninstall the second app "Door checker" for the third app "Smart Door" to work.

Write a Python Script Acting As Backend

Screenshot 2021-09-06 at 08.42.25.png

In this step, we will write a simple CLI-based Python script that prints the state of the door when it is triggered. This script will act as an external client application that receives data from your device. For the sake of simplicity the script just runs in a terminal window on your computer.

Follow the README file to set up the Python environment and execute the script.

Reach out if you have any questions or ideas for my next Toit project. Hope you enjoyed following along.