---
title: "Implementing Smart lock | Simuli(STM32+W5500)"
url: "https://maker.wiznet.io/praveen_k/projects/implementing-smart-lock-simuli-stm32-w5500/"
markdown_url: "https://maker.wiznet.io/praveen_k/projects/implementing-smart-lock-simuli-stm32-w5500/md"
type: "UCC: User Created Content"
author: "Simuli docs"
author_url: "https://docs.simuli.co/stm32-projects/smart-lock/prerequisites-for-smart-lock"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Simuli docs"
original_url: "https://docs.simuli.co/stm32-projects/smart-lock/prerequisites-for-smart-lock"
license: "Apache License 2.0 (Apache-2.0)"
published: "2022-10-31"
language: "en"
hardware: ["WIZnet W5500"]
likes: 4
views: 1277
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Implementing Smart lock | Simuli(STM32+W5500)

> Smart lock using Simuli Virtual Lab (STM+W5500)

Original author: Simuli docs (source: https://docs.simuli.co/stm32-projects/smart-lock/prerequisites-for-smart-lock)

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))

WIZnet parts: W5500 ([Datasheet](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/datasheet?utm_source=maker&utm_medium=project&utm_campaign=w5500), [product hub](https://maker.wiznet.io/products/w5500/))

## Article

Simuli Virtual Lab

First, log in to your Simuli account and navigate to the Simuli Virtual Lab. Here we will create a new emulated instance of the STM32. Click on the `Launch` button under STM32 to get started.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2Fd7xRNIS034U9LVd1wYtG%2Fvlab_raspi.png?alt=media&token=26c37b72-f19e-490c-91be-1671a0fd899a)

This will open the configuration menu. First, provide a name for the project. Then, we will add the `W5500` Ethernet Module by selecting them from the list of available components. Finally, review that the name for the project and the selected sensors are correct and click on the `Launch` button.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FeOdxtgScJgqj6kVfQQpR%2Fvlab-lock-set1.png?alt=media&token=97c075e6-bee0-43a9-93ff-4495b1f146a3)

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2Fi98EYg3oe3qFIRPq4MuG%2Fvlab-lock-set2.png?alt=media&token=acf42517-2900-481e-b2ff-1aee43918c64)

Once we have clicked on the Launch button, a new instance of the STM32 will be created. It can take a few minutes, so be patient.

Now that our instance is being created, let's move forward to create an Adafruit IO account and configure it.

Adafruit IO

Go to the [Adafruit IO website](https://io.adafruit.com/), if you already have an account, you can directly log in and skip to the configuration part. If this is your first time at Adafruit IO, click on the `Get Started for Free` button at the top. This will take you to a signup page, fill all the details and click on the `CREATE ACCOUNT` button.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M9rAzfyk09Mm-FclXVO%2F-McJ5qKjnYTNH0ZsPhNT%2F-McJD-3tWI1fgMDyp_Gn%2Fadaio_signup.png?alt=media&token=917aac07-c42d-4a3a-9ab3-455fa997635f)

Once the account has been created, go back to the [Adafruit IO website](https://io.adafruit.com/). Let's start by creating our feeds. Feeds act as our MQTT topics, where we can either publish our data or subscribe to receive the data. In this case, we will create 1 feed, for publishing our real-time values.

Click on `Feeds`, and then click on `view all` . This will open up a list of all the feeds currently set up for our account. Let's create a feed named lock.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M9rAzfyk09Mm-FclXVO%2F-McJ5qKjnYTNH0ZsPhNT%2F-McJHICygOAKFWdUOXEX%2Fadaio_feeds.png?alt=media&token=a370d4b6-53c6-42cc-b88d-26a332d53a18)

Click on the `New Feed` button, this opens up a pop-up, let's name the feed **lock**.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FEZPV8GDQkxxq0yHpeSiI%2Fada-lock1.png?alt=media&token=437883f0-b748-46b0-8c26-4a8429fbae11)

Now we will create a dashboard from which we will publish values. Click on `Dashboards`, and then click on `view all` . This will open up a list of all the dashboards currently set up for our account. Let's create a dashboard named keypad.

Click on the `New Dashboard` button, this opens up a pop-up, let's name the dashboard **keypad**.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2F8pLBdeuDAoPNENyxaAWd%2Fada-lock2.png?alt=media&token=10f36c2b-4fa1-424b-b6fa-f836ec47304f)

Now click on the dashboard to set up a keypad. Click on the Settings button and click on `Create New Block`.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2Fe42rMXzfyMOfJ8oYUyRA%2Fada-pad1.png?alt=media&token=82b8df04-dcdc-4d85-8830-825cfddc7e6e)

Click on `Number Pad` from the options available and then select the feed you want to subscribe the data to.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FqSOxBLjCkZXa7CuT1Slv%2Fada-pad2.png?alt=media&token=9d2045a4-8dad-4d63-8fd0-ba44e59dc951)

Once the keypad is configured, our Adafruit IO setup is complete. We will now set up our STM32.

## **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](https://docs.platformio.org/en/latest/core/userguide/index.html).

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.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FAeUujJZdd1wIZcwy68XA%2Fvlab-lock-stm1.png?alt=media&token=d0aa64f5-1ad9-45d2-88e7-5329a3cf58c9)

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](https://github.com/simuli-lab/STM32-Smart_lock/blob/main/lock.ino) 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.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M9rAzfyk09Mm-FclXVO%2F-Me_m4a0AaScgqwwrXIE%2F-Me_z8rtzlE4JIgmuOHY%2Fadaio_key.png?alt=media&token=90c3aa90-0a89-4938-8022-00051645717d)

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.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FrPb0b6MqVXnA54VujxPx%2Fvlab-lock-libsearch.png?alt=media&token=76eda5ce-7df4-4e11-893b-3b94c39711f4)

Type the following command in the terminal:

pio lib install 1092

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FmfeP9PSkTfCf8QWj9XYI%2Fvlab-lock-libinst1.png?alt=media&token=b5513f30-8b66-46f7-92da-a76f7e2b3cb9)

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

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FSZAxufuVH2RGEo0O98EO%2Fvlab-lock-libsearch2.png?alt=media&token=8d354eb2-80cb-4fc5-a699-07ac34a0e853)

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FnHDau2mupBYpITsBDfK1%2Fvlab-lock-libinst2.png?alt=media&token=6c3598e9-1879-4fb1-b14b-244aa32d67db)

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.

load-firmware

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.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2F3ZQ3WWiLzUBmOkHFfWFu%2Fvlab-lock-stm2.png?alt=media&token=b2ce0cb0-7ee5-486c-bb30-4393116ea521)

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.

![](https://2903658835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M9rAzfyk09Mm-FclXVO%2Fuploads%2FoL1OcEfYRy0J8HTsaAie%2Fvlab-lock-stmout.png?alt=media&token=4ee761c4-3249-46d6-be8c-506080832030)

---

Source: https://maker.wiznet.io/praveen_k/projects/implementing-smart-lock-simuli-stm32-w5500/
