Building a Remote Door Lock with AWS IoT

I thought it’d be fun to build an IoT Remote Door lock and figure out how to do something a bit more interesting with AWS IoT Core at the same time.

Now, I’ve played a bit with Amazon’s IoT Button for fun, and built an ESP8266-based temperature sensor using Mongoose OS and a DHT22 temp sensor. Building a remote controlled door latch would be an interesting way to learn how to extend the capabilities of both a bit more. Let’s diagram out our Evil-Plan™ so there’s a clear idea of what we will be building.

So here’s the Evil-Plan™…

Roughly translated, the IoT Button publishes a “ButtonPress” event to its MQTT topic on AWS IoT Core when it is pressed. This message is propagated to a listening ESP8266 micro-controller. The micro-controller then flips the input to a relay which controls an electronic latch, causing it to either lock or unlock, depending on the previous state. In theory, it seems pretty sound, but there’s only one way to find out if this works.

Let’s get started by standing up the micro-controller.

Preparing and Building Mongoose OS Firmware

The brains of the Remote Lock is an Espressif ESP8266 NodeMCU micro-controller board with Mongoose OS. It’s probably the easiest way to get an IoT device working with AWS IoT, and Amazon even has a great tutorial on getting this to work.

To build a Mongoose OS application, the file hierarchy has to be set up correctly. The files “mos.yml” and “init.js” are created and placed into the following hierarchy structure.


This is the contents of remote-lock/mos.yml. The “mos.yml” metafile describes the overall application, including dependencies and dependency versions. The “libs” section is especially important to note; It is here that the needed libraries for the app are listed, without which some functionalities will not work.

author: Lim Wei Chiang
description: AWS Remote lock
# arch: PLATFORM
version: 1.0
manifest_version: 2019-08-10

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

  - js
  - aws
  - mqtt

  - fs

  # common mgos libs
  - origin:
  - origin:
  - origin:
  - origin:
  - origin:
  - origin:
  - origin:

  # libs necessary for the current app
  - origin:
  - origin:
  - origin:
  - origin:

The following is the Javascript code of the main execution thread, it goes into remote-lock/fs/init.js. The “fs” directory holds all the files that need to be loaded into flash memory of the ESP8266. The code subscribes the ESP8266 to an MQTT topic, and toggles the signal on a GPIO pin when it receives an MQTT message. This in turn controls a relay to toggle the the electronic latch between a locked and unlocked state.

/*** Global Constants ***/
let SYS_STARTUP_DELAY = 1; // in seconds
let LOCK_CTL_GPIO = 4; /* Pin D2 on the ESP8266 (NodeMCU Layout) board */
let MQTT_PATH = "remote-lock";
let DEVICE_ID = Cfg.get('');

/*** Global Variables ***/
let lock_state = 0; // , '0' = Locked, '1' = Unlocked

function mqttSubHandler(conn, topic, msg){
  print("MQTT Received:");

  if (lock_state === 0)
  else if (lock_state === 1) {

function toggleLock(){
  lock_state = 0;
  GPIO.write(LOCK_CTL_GPIO, lock_state);

function toggleUnlock(){
  lock_state = 1;
  GPIO.write(LOCK_CTL_GPIO, lock_state);

/*** Main ***/
Sys.usleep(SYS_STARTUP_DELAY * 1000000); // Delay startup in usecs
GPIO.set_mode(LOCK_CTL_GPIO, GPIO.MODE_OUTPUT); // Set GPIO pin to use, and method
MQTT.sub(MQTT_TOPIC, mqttSubHandler); // Subscribe for event

We use the files above to build the firmware for the micro-controller. This step uses the Mongoose OS ‘mos’ command, usually at ~/.mos/bin/mos. While in remote-lock/, build the firmware using the ‘mos build’ command.

$ ~/.mos/bin/mos build --platform ESP8266
 Connecting to, user test
 Uploading sources (2261 bytes)
 Firmware saved to ~/.mos/remote-lock/build/

Flashing Firmware and Connecting the Remote Lock to Internet

Once the firmware finishes building, it needs to be flashed to the board. I have my board connected via USB to a MacBook. While in remote-lock/, flash the firmware using the ‘mos flash’ command.

$ ~/.mos/bin/mos flash
 Loaded remote-lock/esp8266 version 1.0 (20190929-145628)
 Using port /dev/cu.SLAB_USBtoUART
 Opening /dev/cu.SLAB_USBtoUART @ 115200…
 Connecting to ESP8266 ROM, attempt 1 of 10…
   Connected, chip: ESP8266EX
 Running flasher @ 921600…
   Flasher is running
 Flash size: 4194304, params: 0x024f (dio,32m,80m)
      2320 @ 0x0 -> 0
    262144 @ 0x8000 -> 86016
       128 @ 0x3fc000 -> 0
      4096 @ 0x7000
      8192 @ 0x8000
      4096 @ 0x14000
     73728 @ 0x19000
    737280 @ 0x100000
      4096 @ 0x3fb000
 Wrote 827408 bytes in 9.46 seconds (683.09 KBit/sec)
      2320 @ 0x0
      4096 @ 0x7000
    262144 @ 0x8000
    733200 @ 0x100000
      4096 @ 0x3fb000
       128 @ 0x3fc000
 Booting firmware…
 All done!

That looks good! Next, lets set up Wi-Fi connectivity so the ESP8266 can reach the Internet.. I’ve substituted my SSID and Wi-Fi password in the example, of course. 😄

$ ~/.mos/bin/mos wifi myWiFiSSID 'WiFiPassword'
 Using port /dev/cu.SLAB_USBtoUART
 Getting configuration…
 Setting new configuration…

Once it’s online, we’ll need to register the ESP8266 with Amazon AWS. IoT Core has a built in Certificate Authority (CA) of its own, which is useful to generate certificates for IoT devices quickly. Mongoose OS makes this even easier with its built-in enrolment feature that “automagically” registers, generates and uploads SSL certificates, and links a default “allow-all” IoT policy to the ESP8266. Let’s enrol the ESP8266 with the “mos aws-iot-setup” function.

$ ~/.mos/bin/mos aws-iot-setup --aws-region us-east-1
 Using port /dev/cu.SLAB_USBtoUART
 AWS region: us-east-1
 Connecting to the device…
   esp8266 62019422AB37 running remote-lock
 Generating ECDSA private key
 Generating certificate request, CN: esp8266_22AB37
 Asking AWS for a certificate…
 Certificate info:
   Subject : CN=esp8266_22AB37
   Issuer  : OU=Amazon Web Services Inc. L=Seattle ST=Washington C=US
   Serial  : [REMOVED]
   Validity: [REMOVED]
   Key algo: ECDSA
   Sig algo: SHA256-RSA
   ID      : [REMOVED]
   ARN     : [REMOVED]
 AWS region: us-east-1
 Attaching policy "mos-default" to the certificate…
 2019/09/29 23:21:02 This operation, AttachPrincipalPolicy, has been deprecated
 Attaching the certificate to "esp8266_22AB37"…
 Writing certificate to aws-esp8266_22AB37.crt.pem…
 Uploading aws-esp8266_22AB37.crt.pem (1141 bytes)…
 Writing key to aws-esp8266_22AB37.key.pem…
 Uploading aws-esp8266_22AB37.key.pem (227 bytes)…
 Updating config:
   aws.thing_name = 
   mqtt.enable = true
   mqtt.server = [REMOVED]
   mqtt.ssl_ca_cert = ca.pem
   mqtt.ssl_cert = aws-esp8266_22AB37.crt.pem
   mqtt.ssl_key = aws-esp8266_22AB37.key.pem
 Setting new configuration…

Right, so that should work, and we need to tie this to the AWS IoT Button.

Glueing Everything Together

Old trusty AWS IoT Button. It’s Wi-Fi connected too! (I love Wi-Fi and I cannot lie…)

I’ll be honest, because of my job as a Wi-Fi engineer, I love things that connect to Wi-Fi. The AWS IoT Button is just one of those things. This has already been on-boarded previously, so I really just need to assign an action to the button when it is pressed.

Here, the Action Policy at AWS IoT is set to match and redirect any published MQTT messages from the IoT Button sent to “thing/AWS-Button-AB12” to the topic “remote-lock/esp8266_22AB37”. This is the topic being subscribed to by the ESP8266 micro-controller, which then uses any received message as a trigger to lock or unlock.

Match and Republish MQTT messages from the IoT Button

It’s fairly simple, and could actually be simplified further by having the Remote Door Lock subscribe directly to the “thing/AWS-Button-AB12” topic. I kept the two MQTT topics separate in case I wanted to easily attach a trigger other than the IoT Button later.

Testing, One, Two, Three…

Now for a quick prayer, and button click…

It’s alive….

Voila! A quick press of the IoT button toggles the Remote Latch, and allows me to lock or unlock any door that this is installed on. In fact, I’ll probably install it on a locker I have at work. 🙂

Yes! Intel NUC 8i5BEH accepts 64 GB RAM!

So my 4-year old Intel NUC 5I5MYHE which I have been using as an ESXi server finally decided to give up the ghost. While on the lookout for a replacement, I came across William Lam’s excellent post at where he tested 2x 32GB SODIMMs on his Hades Canyon NUC (supported), and found that it was also possible to run 64GB of RAM on his older 6th Gen NUC (not technically supported). He speculated that later generations of NUCs would be capable of running 64GB RAM too.

After a bit more research, I wound up choosing the NUC 8i5BEH because it had 4 physical cores, and with hyper-threading could present up to 8 vCPUs on ESXi. God knows I’ve been needing at least 6 vCPUs for the longest time to run some lab VMs. The only unknown was whether the NUC 8i5 would support the Samsung DDR4 32GB DIMMs (P/N M471A4G43MB1), and whether it could finally support 2x 32GB for 64GB RAM. More RAM is always a good thing, right?

I bought the NUC locally in Singapore, but had to get the RAM module from Amazon US. It simply wasn’t available anywhere else here. Finally, when everything arrived, it was time to unbox and start assembling.

Fresh from the store
Unboxed NUC
Post install: The single 32GB DIMM is installed together with an mSATA SSD, which was recovered from the late NUC5I5.

Assembly done, I tried booting up the NUC and immediately ran into issues. I didn’t manage to capture a screenshot, but booting ESXi 6.7u1 would always fail when it was loading some drivers. With nothing left to lose, I thought a BIOS upgrade might help. I downloaded version 0066 for NUC8i5, and proceeded to run the upgrade.

Flashing the BIOS from 0051 to 0066

After that, ESXi booted up without issues, and went straight to work with 32GB RAM installed. No fuss!

NUC8i5 running ESXi with 32GB of RAM

In any case, the first gamble of using the 32GB DIMM paid off. I immediately ordered another 32GB DIMM off Amazon, which took an agonizing 9 days to arrive. It was somewhat my fault, I wasn’t around for the first few delivery attempts.

Ta-da! NUC shown with second 32GB SODIMM before installation
Both 32GB DDR4 SODIMMs installed

So, the moment of truth: Does the Intel NUC8i5BEH support 64GB of RAM? Happily, the answer was “Yes”.

That’s 64 JeeBees of goodness right there, folks.

I’ve been running this for a few days with multiple VMs powered on, and this baby has been rock solid so far. Definitely a very viable home lab solution!

vSphere Metro Storage Cluster Networking: Part 3

This post has been much delayed for a number of reasons, namely because some feasible solutions became End of Sale, while others, based on field experience were not practically seen or deployed. In the meantime, other newer solutions which can address some of the issues we discussed earlier have now become available, so here is Part 3.

So back in Part 1, I blogged about considerations for the L2 DCI link for a vSphere Metro Cluster. In Part 2, I covered the potential routing pitfalls of stretching L2 networks across sites.

In Part 3, I’m going to discuss the methods which can be used to workaround the some of issues which we talked about in Part 2. Just to recap, the issues with stretched networks were:

  • Asymmetrical traffic flow across DC sites
  • Inability of network services (eg firewalls) to handle asymmetric traffic flow
  • Lack of VM site-awareness for optimized routing
  • Inefficient use of the DCI

VMware NSX Distributed Firewall with Asymmetrical Traffic Flows

In Part 2, I mentioned that it is possible for a VM to move between sites, with the result being that traffic to the VM (ingress traffic) could come in on say DC1, while traffic from the VM (egress traffic) could exit on DC2. Such a situation would cause issues with traditional firewalls, since these need to see traffic flows in both directions in order to allow or deny traffic correctly.

vMSC Invalid Firewall State

Perimeter Firewalls do not see consistent flow state

In the diagram above, the firewall at DC1 sees the “in” state of the flow from both User 1 and User 2 to VM1, which happens to have vMotioned to DC2. Assuming we’ve tweaked the setup for local egress, the VM will send traffic out via the DC2 router. As a consequence, the firewall at DC2 sees only the “out” state of the flow. This means that firewalls at both sites would observe any or all of the following issues and start dropping traffic because of state inconsistencies:

  • Incomplete TCP handshake / termination
  • Inconsistent sequence numbers
  • Unidirectional traffic flow

With NSX for vSphere, it’s actually possible to deploy a stateful firewall at the VM level using the Distributed Firewall (DFW) feature. NSX DFW works by having security policy defined centrally via NSX, which is then pushed down to corresponding VMs for enforcement at the micro level. With this being the case, we’ve brought the firewall closer to the VM itself by enforcing policy at the vNIC level.

NSX DFW sees flow state

NSX Distributed Firewall sees full flow state

Looking at the diagram above, the network ingress and egress paths of traffic to the VM are still inconsistent. However, the firewall enforcement point is at the vNIC level, which is tied to the VM. At the vNIC level, the DFW will always observe all traffic entering and exiting the VM. The DFW filter will have full information on the network traffic flows of the VM, and be able to appropriately apply stateful firewall policies, regardless of where the VM is or moves to, or how traffic arrives and departs from it. We’ve effectively resolved the problem of stateful perimeter firewalls not working due to not seeing the full traffic flow, by moving the firewall to the VM vNIC.

Other Methods

It bears mentioning that there are/were other methods of addressing some of the other network considerations that come with stretching networks. When writing both Part 1 and 2, I  considered writing more on these methods, however it appears that they are not quite feasible in the real world. Here is just a summary of what might have been.

Locator ID Separation Protocol (LISP): As you may have realized, there doesn’t seem to be a solution which has VM site awareness, so there is no way to optimize ingress routing to VMs according to which site they are located on (potentially also reducing DCI traffic). The fact is, LISP was supposed to address this issue, by being able to insert granular routes to VMs depending on where they resided. The biggest challenge with utilizing LISP in order to optimize ingress routing to the VM is that it requires ISPs to support LISP within their infrastructure. It is quite rare to come across such ISPs in the real world. Also, LISP plays a lot with insertion of host routes, which is its own set of network black magic.

DNS Optimization with Cisco ACE Load Balancers: Cisco also developed an orchestration solution utilizing its global and local load balancers to dynamically update DNS A records to point to wherever a VM was vMotioned to. This would enable new connections to directly reach the VM at it’s new location, thus also ensuring new connections do not have to traverse the DCI. It’s really quite a creative hack, though unfortunately the Cisco ACE product line was EoS’ed not long after the solution was published.

vSphere Distributed Virtual Switch: Packet analysis using ERSPAN

Packet analysis is invaluable in troubleshooting network issues and network monitoring. While packet analysis used to be used only in the domain of physical networks, that is no longer the case.

The vSphere Distributed Virtual Switch is now able to produce dumps of specific virtual network traffic and transport using ERSPAN to packet monitoring consoles. Yes, that’s right, using the Distributed Virtual Switch you can monitor network traffic in the virtual realm even if the traffic doesn’t actually hit the physical wire.

I didn’t quite see much material covering this so far, so I thought I’d show how this would work. For this blog post, I used the following:

  • Distributed Virtual Switch (vSphere Enterprise Plus)
  • Wireshark installed in a monitoring console (my personal laptop)
  • A VM which we want to monitor (a Windows 7 VM which is my jump box VM)

Let’s start with setting up Wireshark for packet capturing on the monitoring console. Opening Wireshark, go to Capture -> Interfaces.

That should open up a list of interfaces which we can capture from. Now I’d like to capture using the “Local Area Connection”, though it’s probably a good idea to find out what the IP address for that interface is. We’ll need to set it as a receiver for ERSPAN captured traffic. Click on “Options”.

We look out again for the “Local Area Connection” and note the IP address associated with the chosen receiving interface. In the case, it’s We’ll checkbox the interface, and then click on “Start”.

Just like that, Wireshark will start dumping out all the traffic it gets on the interface. In this case, we only want to monitor traffic capture via ERSPAN on the Distributed Virtual Switch. Since ERSPAN encapsulates traffic in GRE, that’s what we’ll filter for. Type in “gre” into the filter field and click on “apply”, which should immediately filter out all the “noise” packets.