avatarAttila Vágó

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7942

Abstract

s and share the code, let me list out the acceptance criteria.</p><ul><li><b>It had to recognise three colours</b>, and based on those being correct, it had to move a LEGO mechanism. The order of colours was important for the core security of the lock. The actual LEGO build itself, <b>did not have to be beautiful, just functional</b>. This was my MVP — a concept I will write about very soon, so <a href="https://attilavago.medium.com/subscribe"><b>subscribe!</b></a> All the next items in this list are to be considered additional features.</li><li>It had to <b>keep track of the number of unsuccessful attempts</b>, and take action after a hard-coded number of tries. I set that to three, because three is a magic number and it’s a prime number (<i>I verified that with Siri!</i>), and weirdos like me like prime numbers.</li><li>It also had to <b>account for situations when the lock was new and needed installing for the first time</b>, so a setup mode had to be coded into the software.</li><li>A <b>reset situation</b> also felt like a standard enough feature request, so I had to allow for a solution to enable the user to trigger that mode, BUT only when the lock was unlocked — another security feature.</li><li>It also had to feature <b>a self-locking tongue</b>. You know, the one where you just pull the door behind you and locks itself.</li><li>It had to <b>have a “security pin”,</b> in this case I chose a colour I wanted to be ignored by the sensors, while that colour would be unknown to the potential intruder, making the key even more secure.</li><li>Speaking of keys, <b>it had to allow for variability</b>, to make it impossible (or at least very difficult) to copy.</li></ul><p id="795f">You can appreciate how the six additional features are both welcome, <b>quite necessary for a superior and more secure user experience</b>, and something closer to resembling a production device.</p><h2 id="e327">So, how does it work?</h2><p id="d567">I’ll explain in plain English the overall user journey with some additional technical insight, after which you’re welcome to take a look at the code. I think this way you’ll understand better what I was going for and how the code achieves that.</p><figure id="5cbe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*X7J7xwwUcSb7hFKqwkRmlw.jpeg"><figcaption></figcaption></figure><p id="7412">Imagine that you just bought a new smart lock. It automatically locks behind you as you close the door, so that’s neat, but of course, you’ll also want to unlock it, so once you installed it and turned it on for the first time, it recognises it’s a new lock, and asks to be set up. You twist the colour cubes on your key to the desired combination and insert it. <b>The three colour/light sensors pick the three colours up and save them to three variables. The next time the lock is initiated, it will recognise that those three variables have already been set</b>, so it won’t ask to be set up again, but will proceed to the unlock routine, provided your key still is set to the same colour combination.</p><p id="cb12">Should the key be set to another combination, it will <b>alert you that you’re trying to attempt to unlock with an incorrect colour sequence</b>. It will advise you to try again and also alert you how many tries you have left. Should you run out of tries, <b>it initiates a burglar alarm — another security feature</b>.</p><p id="a8ad">Of course, you may at times decide you want to reset the colour combination to something else. Maybe red-green-yellow is far too last year for you, and you’d like to go for blue-brown-black instead! Well, you can do that. While the door is open and the lock is turned on, <b>you can push back on the tongue, which triggers the touch sensor, at which point the lock knows it’s meant to switch into reset mode</b>. At this point, it will ask for the new colour combination and set it. The next time you want to unlock the door, you can use the new combination.</p><p id="82b9">An aspect where I could have further improved on security here, is perhaps first asking for the existing colour combination, and only should that be correct, allow for a reset, however I decided not to, as it felt a bit over the top for a toy prototype.</p><p id="79ae">You can watch the entire user journey (except the reset part) either on my <a href="https://www.instagram.com/reel/CmreiGsj8a8/?utm_source=ig_web_copy_link">Instagram</a> or <a href="https://www.tiktok.com/@bricksnbrackets/video/7181879577516592390?is_from_webapp=1&amp;sender_device=pc&amp;web_id=7181880531481544198">TikTok</a>, and of course, you’re welcome to read through the code as well. It’s less than 100 lines!</p> <figure id="069f"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.tiktok.com%2Fembed%2Fv2%2F7181879577516592390&amp;display_name=tiktok&amp;url=https%3A%2F%2Fwww.tiktok.com%2F%40bricksnbrackets%2Fvideo%2F7181879577516592390%3Fis_from_webapp%3D1%26sender_device%3Dpc%26web_id%3D7181880531481544198&amp;image=https%3A%2F%2Fp16-sign-va.tiktokcdn.com%2Fobj%2Ftos-maliva-p-0068%2Fae19cd6978a44a769f21006654cb494f_1672161673%3Fx-expires%3D1672189200%26x-signature%3DzX1MsjSpjtcgfO5Xx6X%252FJPmuODQ%253D&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tiktok" allowfullscreen="" frameborder="0" height="700" width="340"> </div> </div> </figure></iframe></div></div></figure><div id="7dec"><pre><span class="hljs-keyword">from</span> ev3dev2.motor <span class="hljs-keyword">import</span> LargeMotor, OUTPUT_A, SpeedPercent <span class="hljs-keyword">from</span> ev3dev2.sensor <span class="hljs-keyword">import</span> INPUT_1, INPUT_2, INPUT_3, INPUT_4 <span class="hljs-keyword">from</span> ev3dev2.sensor.lego <span class="hljs-keyword">import</span> TouchSensor, ColorSensor <span class="hljs-keyword">import</span> time

<span class="hljs-keyword">from</span> ev3dev2.sound <span class="hljs-keyword">import</span> Sound spkr = Sound()

spkr.speak(<span class="hljs-string">'Starting smart lock...'</span>)

lockMotor = LargeMotor(OUTPUT_A) keyOne = ColorSensor(INPUT_1) keyTwo = ColorSensor(INPUT_2) keyThree = ColorSensor(INPUT_3) readWriteToggle = TouchSensor(INPUT_4) alarmSoundPath = <span class="hljs-string">'/home/robot/smartlock/spaceship_alarm.wav'</span> incorrectKeyMessage = <span class="hljs-string">'Warning. Incorrect key.'</span> alarmMessage = <span class="hljs-string">'Sorry, you failed three times, triggering home burglar alarm now.'</span> tryAgainMessage = <span class="hljs-string">'Please try again.'</span> unlockMessage = <span class="hljs-string">'Unlocking door now.'</span> setupColoursMessage = <span class="hljs-string">'Please insert your desired color combination key.'</span> newLockMessage = <span class="hljs-string">'New lock detected. Entering setup mode.'</span> newLockSetupConfirmationMessage = <span class="hljs-string">'Key color combination saved. Have a safe home.'</span> resetMessage = <span class="hljs-string">'Entering reset mode.'</span> resetConfirmationMessage = <span class="hljs-string">'Key color combination updated. Have a safe home.'</span> initiateUnlockRoutineMessage = <span class="hljs-string">'Please insert correct key.'</span>

isKeyOne = <span class="hljs-literal">None</span> isKeyTwo = <span class="hljs-literal">None</span> isKeyThree = <span class="hljs-literal">None</span> failedTriesCount = <span class="hljs-number">0</span>

keyOneColor = <span class="hljs-literal">None</span> keyTwoColor = <span class="hljs-literal">None</span> keyThreeColor = <span class="hljs-literal">None</span> ignoredColor = <span class="hljs-string">'White'</span>

<span class="hljs-keyword">if</span> readWriteToggle.is_pressed: spkr.speak(resetMessage)

Options

spkr.speak(setupColoursMessage) keyOneColor = keyOne.color_name keyTwoColor = keyTwo.color_name keyThreeColor = keyThree.color_name spkr.speak(resetMessage) <span class="hljs-keyword">elif</span> keyOneColor == <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> keyTwoColor == <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> keyThreeColor == <span class="hljs-literal">None</span>: spkr.speak(newLockMessage) spkr.speak(setupColoursMessage) keyOneColor = keyOne.color_name keyTwoColor = keyTwo.color_name keyThreeColor = keyThree.color_name spkr.speak(newLockSetupConfirmationMessage)

timer = time.time() + <span class="hljs-number">60</span> * <span class="hljs-number">1</span> <span class="hljs-comment"># user has 60 seconds to unlock door</span> <span class="hljs-keyword">while</span> time.time() < timer: spkr.speak(initiateUnlockRoutineMessage) <span class="hljs-keyword">if</span> keyOne.color_name == keyOneColor: isKeyOne = <span class="hljs-literal">True</span> <span class="hljs-keyword">elif</span> keyOne.color_name != ignoredColor <span class="hljs-keyword">and</span> isKeyOne == <span class="hljs-literal">None</span> : isKeyOne = <span class="hljs-literal">False</span> spkr.speak(incorrectKeyMessage) <span class="hljs-keyword">if</span> keyTwo.color_name == keyTwoColor: isKeyTwo = <span class="hljs-literal">True</span> <span class="hljs-keyword">elif</span> keyTwo.color_name != ignoredColor <span class="hljs-keyword">and</span> isKeyTwo == <span class="hljs-literal">None</span> : isKeyTwo = <span class="hljs-literal">False</span> spkr.speak(incorrectKeyMessage) <span class="hljs-keyword">if</span> keyThree.color_name == keyThreeColor: isKeyThree = <span class="hljs-literal">True</span> <span class="hljs-keyword">elif</span> keyThree.color_name != ignoredColor <span class="hljs-keyword">and</span> isKeyThree == <span class="hljs-literal">None</span> : isKeyThree = <span class="hljs-literal">False</span> spkr.speak(incorrectKeyMessage)

<span class="hljs-keyword">if</span> isKeyOne <span class="hljs-keyword">and</span> isKeyTwo <span class="hljs-keyword">and</span> isKeyThree:
    spkr.speak(unlockMessage)
    lockMotor.on_for_rotations(SpeedPercent(<span class="hljs-number">10</span>), <span class="hljs-number">1</span>)
    isKeyOne = <span class="hljs-literal">None</span>
    isKeyTwo = <span class="hljs-literal">None</span>
    isKeyThree = <span class="hljs-literal">None</span>
    failedTriesCount = <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span>:
    spkr.speak(tryAgainMessage)
    failedTriesCount = failedTriesCount + <span class="hljs-number">1</span>
    <span class="hljs-keyword">if</span> failedTriesCount == <span class="hljs-number">3</span>:
        spkr.speak(alarmMessage)
        spkr.play_file(alarmSoundPath)
        timer = <span class="hljs-number">0</span>
    <span class="hljs-keyword">else</span>:
        spkr.speak(<span class="hljs-string">'You have '</span> + <span class="hljs-built_in">str</span>(<span class="hljs-number">3</span> - failedTriesCount) + <span class="hljs-string">'attempts left.'</span>)</pre></div><h2 id="6f7d">Closing thoughts</h2><p id="7b47">I am beyond ecstatic to have been able to sit down over the course of two evenings, and whip up a little prototype like this. <b>LEGO Mindstorms was one of the first things I wrote about on Medium roughly 7 years ago</b>, and I really wish I had more time to play around with robotics.</p><div id="cec9" class="link-block">
      <a href="https://readmedium.com/ev3-robotics-with-python-mac-924dc381958">
        <div>
          <div>
            <h2>EV3 Robotics with Python (Mac)</h2>
            <div><h3>Aaaand I am back to writing. Now bare with me, this is absolutely going to be worth your time, and you will get python…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*zeztPvs5mhHw0FU016hSpw.jpeg)"></div>
          </div>
        </div>
      </a>
    </div><p id="c330">Speaking of robotics, somehow <b>for me,</b> <b>this is the space where Python makes more sense</b>. While in my day-to-day job I have plenty of exposure to Python in the form of server-side code and Django, I feel much more excited about the language in the context of tinkering and prototyping.</p><div id="ee87" class="link-block">
      <a href="https://levelup.gitconnected.com/pyscript-another-futile-attempt-to-dethrone-javascript-9c70bdcc1dfd">
        <div>
          <div>
            <h2>PyScript — Another Futile Attempt To “Dethrone” JavaScript?</h2>
            <div><h3>What point is PyScript trying to make? Pure genius or another useless fad? Let’s find out…</h3></div>
            <div><p>levelup.gitconnected.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*LNDgHb85o88XasE-)"></div>
          </div>
        </div>
      </a>
    </div><p id="a961">It was also <b>quite interesting to act as a product owner on a project where I had to define what was acceptable as an MVP</b> and what was more fit to be a feature built on top of that initial minimum viable product. Learning a lot from the LockPickingLawyer in terms of lock security also helped me make more informed decisions around the design of the key and some of the coding.</p><p id="9969">I have to stress this again, The LEGO Group is making a huge mistake by discontinuing the Mindstorms theme. <b>I think EV3 was to date the best, most fun and most accessible robotics prototyping and programming tool. </b>It will be sorely missed, and boy am I happy to own not one, not two, but three sets! Glad I listened to my gut feeling back in 2015 and 2016!</p><p id="94f0"><b><i>So, what do you think about my LEGO smart lock and how would you improve on the security aspect of it?</i></b></p><figure id="ffdf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*X7T2r0RC0J9ZE9qSkDoYsQ.jpeg"><figcaption></figcaption></figure><figure id="b1e9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*W0UQimSXfEHnSuLz4oDl9g.jpeg"><figcaption></figcaption></figure><figure id="dc6d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*s6gb-Mds5jV--oWWcmE1wQ.jpeg"><figcaption></figcaption></figure><p id="16c6"><i>Attila Vago — Software Engineer improving the world one line of code at a time. Cool nerd since forever, writer of codes and blogs. Web accessibility advocate, LEGO fan, vinyl record collector. Loves craft beer! <a href="https://attilavago.medium.com/my-200th-article-hello-its-time-we-met-3f201ad1303"><b>Read my Hello story here!</b></a><b> <a href="https://attilavago.medium.com/subscribe">Subscribe</a> </b>and/or<b> <a href="https://attilavago.medium.com/membership">become a member</a> </b>for more stories about <a href="https://medium.com/@attilavago/list/lego-all-the-things-083f80bd3c51"><b>LEGO</b></a><b>, <a href="https://medium.com/@attilavago/list/technology-tech-news-a2d2d509b856">tech</a>, <a href="https://medium.com/@attilavago/list/coding-software-development-d123369e3636">coding</a> and <a href="https://medium.com/@attilavago/list/accessibility-4b67c1d08ef3">accessibility</a></b>! For my less regular readers, I also write about <a href="https://medium.com/@attilavago/list/the-random-stuff-96bfc5a222e5"><b>random bits</b></a> and <a href="https://medium.com/@attilavago/list/writing-writing-tips-f83ef5e79de5"><b>writing</b></a>.</i></p></article></body>

How To Build A Smart Lock With Python And EV3dev

High-level instructions on how to build a working smart lock with LEGO’s EV3 brick and some simple Python code.

One smart LEGO EV3 brick, some 150 mostly Technic pieces, a laptop, and some good ol’ Python. That was all I needed to take everything I’ve learnt from the LockPickingLawyer over on YouTube and apply it to a smart-lock of my own! Seriously, check out his channel, you’ll learn a lot about lock safety and mechanisms. Now, while I’m sure he’d find plenty of flaws in my design, I did have this idea a few weeks ago of creating a smart door lock that had a variable colour-coded key.

If your first question is why colour-coded, I think my answer will be as standard as it could be from an engineer: because I could. Seriously, not joking. I could, and because I don’t know of any keys that are both variable and colour-coded. The reason I could, was because, well, I have a bunch — three to be more exact — of LEGO Mindstorms EV3 smart-bricks, and a good number of related sensors and motors, so it made sense to prototype the idea with LEGO.

Honestly, The LEGO Group is being an absolute eejit for discontinuing what was to date probably the best and coolest prototyping tool ever created for those interested in robotics and automation. I’m certainly going to hang on to everything I have now that they’ve been discontinued completely. Eejits, I tell you. Eejits.

What’s a variable colour-coded key?

Well, I might have just made the term up myself, but essentially, it’s a programmable key, where you can literally change the configuration of the key, giving you a broad range of options. In my case, I went with seven colours: white, red, green, blue, yellow, brown and black, where white gets ignored, just to confuse someone wanting to “pick” the lock. Let’s call it a “security pin”. 😉 Either way, this gives us 343 permutations according to omnicalculator.com, because colours can repeat.

The variable aspect of it, which, I think, is possibly the cleverest part of all this little prototype, is in allowing the key elements to rotate freely. This essentially means, that as soon as you take the key out of the lock, the colours naturally get mixed up, so anyone grabbing the key from you would have just as hard a time getting in later if they had 343 individual keys to try out.

Add to that the “security pin”, which in my design is factory set to white, and you have yourself what, I think, is a pretty decent locking system for a toy prototype. I mean, I know keypad locks that are ten times less secure. Just look at this Smith & Locke Medium Duty Push-Button Lock sold for 32 bucks, and used in a ton of places. I’ve seen office doors locked with this crap. These types of locks are so bad, even the order of the combination doesn’t matter! 🙀 In mine, however, it does. Say for instance you choose the colours red, blue, and yellow. Try to open the lock with a yellow, red and blue combination, and it will tell you to sod off.

For all intents and purpose, it’s a mechanically programmable key, with built-in self-obfuscation.

Prerequisites for building an EV3 LEGO smart lock

You’ll obviously need a few key components, and I wasn’t lying in the introduction; that is kind of all you need, but let’s get into a bit more details, and allow me to clarify a few aspects.

  • An EV3 brick. This is essential. It cannot be another edition of LEGO’s smart brick, and you’ll see soon why. It has to be the one from set #31313 or the official education edition set #45544. You only need one smart brick for this prototype. You can also get it separately, but not for much longer.
  • You’ll also ideally need three light/colour sensors. You can also do it with one, but then you’ll have to get very clever with your programming, as you’ll need to keep track of colour sequences and such. These will read the various colours from the key, and allow us to set the colour key in setup and reset mode.
  • A large EV3 servo motor. This will drive the lock’s tongue and has enough power to move that, regardless of the spring putting tension on it.
  • Finally, you’ll need a touch sensor. This will allow us to push back on the tongue to trigger the reset mode, in case we want to change the colour combination.
  • Some LEGO bricks, ideally Technic, but frankly, as it always is with LEGO, your imagination is the limit, so be that Technic or System bricks, you’ll still manage to recreate the prototype. If you use System bricks, I would recommend substituting the Technic spring with a rubber band.
  • Basic Python knowledge. I will perhaps soon write a companion article for this, introducing folks to the very basics of Python, but for now, you can learn these over on Codecademy or the many resources linked in this FreeCodeCamp article.
  • A computer. It can be something as simple and cheap as a Raspberry Pi, and as premium and expensive as a Mac. A Windows machine will also do just fine. I used my trusted M2 MacBook Air.
  • A microSD or microSDHC card (2 GB or larger, though not larger than 32 GB — a limitation imposed by the EV3 brick).
  • In terms of software, you’ll only need two things: VSCode and EV3dev. Additionally to VSCode, you’ll want to install the ev3dev-browser plugin. This will allow you to upload your project effortlessly to the brick.

OK, I know that looks like a lot of prerequisites, but trust me, it’s not, especially when you think that you’ll end up building a working smart lock!

Requirements…

My goal expanded from a simple recognition of colours which would trigger moving the lock tongue, to a few more things around the general concept of a smart lock, so before I explain how it all works and share the code, let me list out the acceptance criteria.

  • It had to recognise three colours, and based on those being correct, it had to move a LEGO mechanism. The order of colours was important for the core security of the lock. The actual LEGO build itself, did not have to be beautiful, just functional. This was my MVP — a concept I will write about very soon, so subscribe! All the next items in this list are to be considered additional features.
  • It had to keep track of the number of unsuccessful attempts, and take action after a hard-coded number of tries. I set that to three, because three is a magic number and it’s a prime number (I verified that with Siri!), and weirdos like me like prime numbers.
  • It also had to account for situations when the lock was new and needed installing for the first time, so a setup mode had to be coded into the software.
  • A reset situation also felt like a standard enough feature request, so I had to allow for a solution to enable the user to trigger that mode, BUT only when the lock was unlocked — another security feature.
  • It also had to feature a self-locking tongue. You know, the one where you just pull the door behind you and locks itself.
  • It had to have a “security pin”, in this case I chose a colour I wanted to be ignored by the sensors, while that colour would be unknown to the potential intruder, making the key even more secure.
  • Speaking of keys, it had to allow for variability, to make it impossible (or at least very difficult) to copy.

You can appreciate how the six additional features are both welcome, quite necessary for a superior and more secure user experience, and something closer to resembling a production device.

So, how does it work?

I’ll explain in plain English the overall user journey with some additional technical insight, after which you’re welcome to take a look at the code. I think this way you’ll understand better what I was going for and how the code achieves that.

Imagine that you just bought a new smart lock. It automatically locks behind you as you close the door, so that’s neat, but of course, you’ll also want to unlock it, so once you installed it and turned it on for the first time, it recognises it’s a new lock, and asks to be set up. You twist the colour cubes on your key to the desired combination and insert it. The three colour/light sensors pick the three colours up and save them to three variables. The next time the lock is initiated, it will recognise that those three variables have already been set, so it won’t ask to be set up again, but will proceed to the unlock routine, provided your key still is set to the same colour combination.

Should the key be set to another combination, it will alert you that you’re trying to attempt to unlock with an incorrect colour sequence. It will advise you to try again and also alert you how many tries you have left. Should you run out of tries, it initiates a burglar alarm — another security feature.

Of course, you may at times decide you want to reset the colour combination to something else. Maybe red-green-yellow is far too last year for you, and you’d like to go for blue-brown-black instead! Well, you can do that. While the door is open and the lock is turned on, you can push back on the tongue, which triggers the touch sensor, at which point the lock knows it’s meant to switch into reset mode. At this point, it will ask for the new colour combination and set it. The next time you want to unlock the door, you can use the new combination.

An aspect where I could have further improved on security here, is perhaps first asking for the existing colour combination, and only should that be correct, allow for a reset, however I decided not to, as it felt a bit over the top for a toy prototype.

You can watch the entire user journey (except the reset part) either on my Instagram or TikTok, and of course, you’re welcome to read through the code as well. It’s less than 100 lines!

from ev3dev2.motor import LargeMotor, OUTPUT_A, SpeedPercent
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3, INPUT_4
from ev3dev2.sensor.lego import TouchSensor, ColorSensor
import time

from ev3dev2.sound import Sound
spkr = Sound()

spkr.speak('Starting smart lock...')

lockMotor = LargeMotor(OUTPUT_A)
keyOne = ColorSensor(INPUT_1)
keyTwo = ColorSensor(INPUT_2)
keyThree = ColorSensor(INPUT_3)
readWriteToggle = TouchSensor(INPUT_4)
alarmSoundPath = '/home/robot/smartlock/spaceship_alarm.wav'
incorrectKeyMessage = 'Warning. Incorrect key.'
alarmMessage = 'Sorry, you failed three times, triggering home burglar alarm now.'
tryAgainMessage = 'Please try again.'
unlockMessage = 'Unlocking door now.'
setupColoursMessage = 'Please insert your desired color combination key.'
newLockMessage = 'New lock detected. Entering setup mode.'
newLockSetupConfirmationMessage = 'Key color combination saved. Have a safe home.'
resetMessage = 'Entering reset mode.'
resetConfirmationMessage = 'Key color combination updated. Have a safe home.'
initiateUnlockRoutineMessage = 'Please insert correct key.'

isKeyOne = None
isKeyTwo = None
isKeyThree = None
failedTriesCount = 0

keyOneColor = None
keyTwoColor = None
keyThreeColor = None
ignoredColor = 'White'

if readWriteToggle.is_pressed:
    spkr.speak(resetMessage)
    spkr.speak(setupColoursMessage)
    keyOneColor = keyOne.color_name
    keyTwoColor = keyTwo.color_name
    keyThreeColor = keyThree.color_name
    spkr.speak(resetMessage)
elif keyOneColor == None or keyTwoColor == None or keyThreeColor == None:
    spkr.speak(newLockMessage)
    spkr.speak(setupColoursMessage)
    keyOneColor = keyOne.color_name
    keyTwoColor = keyTwo.color_name
    keyThreeColor = keyThree.color_name
    spkr.speak(newLockSetupConfirmationMessage)

timer = time.time() + 60 * 1 # user has 60 seconds to unlock door
while time.time() < timer:
    spkr.speak(initiateUnlockRoutineMessage)
    if keyOne.color_name == keyOneColor:
        isKeyOne = True
    elif keyOne.color_name != ignoredColor and isKeyOne == None :
        isKeyOne = False
        spkr.speak(incorrectKeyMessage)
    if keyTwo.color_name == keyTwoColor:
        isKeyTwo = True
    elif keyTwo.color_name != ignoredColor and isKeyTwo == None :
        isKeyTwo = False
        spkr.speak(incorrectKeyMessage)
    if keyThree.color_name == keyThreeColor:
        isKeyThree = True
    elif keyThree.color_name != ignoredColor and isKeyThree == None :
        isKeyThree = False
        spkr.speak(incorrectKeyMessage)

    if isKeyOne and isKeyTwo and isKeyThree:
        spkr.speak(unlockMessage)
        lockMotor.on_for_rotations(SpeedPercent(10), 1)
        isKeyOne = None
        isKeyTwo = None
        isKeyThree = None
        failedTriesCount = 0
    else:
        spkr.speak(tryAgainMessage)
        failedTriesCount = failedTriesCount + 1
        if failedTriesCount == 3:
            spkr.speak(alarmMessage)
            spkr.play_file(alarmSoundPath)
            timer = 0
        else:
            spkr.speak('You have ' + str(3 - failedTriesCount) + 'attempts left.')

Closing thoughts

I am beyond ecstatic to have been able to sit down over the course of two evenings, and whip up a little prototype like this. LEGO Mindstorms was one of the first things I wrote about on Medium roughly 7 years ago, and I really wish I had more time to play around with robotics.

Speaking of robotics, somehow for me, this is the space where Python makes more sense. While in my day-to-day job I have plenty of exposure to Python in the form of server-side code and Django, I feel much more excited about the language in the context of tinkering and prototyping.

It was also quite interesting to act as a product owner on a project where I had to define what was acceptable as an MVP and what was more fit to be a feature built on top of that initial minimum viable product. Learning a lot from the LockPickingLawyer in terms of lock security also helped me make more informed decisions around the design of the key and some of the coding.

I have to stress this again, The LEGO Group is making a huge mistake by discontinuing the Mindstorms theme. I think EV3 was to date the best, most fun and most accessible robotics prototyping and programming tool. It will be sorely missed, and boy am I happy to own not one, not two, but three sets! Glad I listened to my gut feeling back in 2015 and 2016!

So, what do you think about my LEGO smart lock and how would you improve on the security aspect of it?

Attila Vago — Software Engineer improving the world one line of code at a time. Cool nerd since forever, writer of codes and blogs. Web accessibility advocate, LEGO fan, vinyl record collector. Loves craft beer! Read my Hello story here! Subscribe and/or become a member for more stories about LEGO, tech, coding and accessibility! For my less regular readers, I also write about random bits and writing.

LEGO
Smart Home
Technology
Python
Programming
Recommended from ReadMedium