Implementing the Smart Lock

The STM32 instances come with support for Platform IO Core (CLI). We will be using it for working on our projects. To know more about Platform IO Core (CLI) and the different commands go to the Platform IO documentation.

First, we need to initialize a new project using Platform IO. Let's create a new folder called Smart-Lock which will hold all our code related to the project and then open the newly created folder in the terminal. Use the following commands:

mkdir /workspace/Smart-Lock/
cd /workspace/Smart-Lock/

Now we will initialize a new project using Platform IO. Type the following command in the terminal:

pio project init --board nucleo_f411re

The pio project init command is used to initialize the project. Then we need to specify the board which we want to use with the --board flag.

Once the project init command has run successfully, we will see some new folders inside the Smart-Lock folder. We will store the files related to the project in the src folder.

Create a new file called main.cpp inside the src folder. This file will hold the code for the Smart Home Control program. Click on the main.cpp file to open it up in our code editor. Now, go to the Github link and paste the following code inside the main.cpp file.

In the above code, replace "yourusername" with your Adafruit IO username and "youradafruitiokey", with your Adafruit IO key which is present under My Key in Adafruit IO.

Before we can compile our code, we need to install a few libraries. First, let's search for the Adafruit MQTT library. Go to your terminal and type:

pio lib search Adafruit MQTT

Platform IO will now search for the relevant libraries and provide us with the results. Here we can see that we got the library we were looking for. We can see the Library ID at the top, in this case it is 1092. We will use this ID to install the library.

Type the following command in the terminal:

pio lib install 1092

This will install the Adafruit MQTT Library along with all of its dependencies. Now we need to install the Ethernet library. Follow the same steps as before to install it via Platform IO.

pio lib search Ethernet #This is optional
pio lib install 872

Now we are ready to compile our code. Save the main.cpp file. We will again use Platform IO to compile our code. Go to your terminal and type the following command.

pio run

The first time this command is run, Platform IO will download and install the relevant framework and toolchains required for the STM32. This process will take a few seconds.

Now Platform IO will compile our code and output a firmware.bin file. We need to copy this firmware file from the .pio/build/nucleo_f411re to /workspace. Usually you would need to use the cp command, but we have created a custom command called load-firmware for ease of use. Just write the following command and the firmware.bin file will be copied to the workspace folder.


Once we have copied the firmware.bin file, all we need to do is to reset the STM32 by pressing the red button above the STM32.

After the reset, the STM32 will load the new firmware, now enter a 4 digit numerical password on the keypad if the password is correct it will print "Code Match" and switch ON a LED otherwise it will print "Code Unmatch" and switch OFF the LED.

You can also use the Arduino IDE to work with the STM32. Follow our guide on Using STM32 with Arduino IDE and IoTIFY Virtual Lab.

Last updated