Is my door locked? If you leave your home, and you're unsure whether you locked your front door, this is a smarter way to check whether your front door is locked or unlocked.

In this codelab, you'll learn how to use an infrared sensor to see whether a door is locked or unlocked in realtime.

What You'll Build

Prerequisites

What You'll Need

What You'll Learn

Install the viam-micro-server on ESP32

Let's start by flashing the viam-micro-server to the ESP32 dev board. This will allow us to connect the Viam machine to Wifi so that we can subsequently add custom firmware over-the-air (OTA) by changing the machine configuration.

  1. Connect your ESP32 board to your computer with a data cable. Make sure the cable is capable of data transfer (not just charging).
  2. In the Viam app under the LOCATIONS tab, enter a name for a new machine, and Add machine. add machine
  3. Under the CONFIGURE tab, click the View setup instructions button. view setup instructions
  4. Select the platform you want to run on, ESP32.
  5. Select the platform of the computer you're using to flash the ESP32. The example shown below will be for a Mac, but the steps to follow for other operating systems is similar. select ESP32 platform
  6. Follow the instructions to download the file for the viam-micro-server installer used to flash and monitor logs on your ESP32.
  7. Download the file for the machine credentials used to connect the ESP32 to the Viam app.
  8. In the terminal window, navigate to the directory where the previous files downloaded. In the example shown below, files were downloaded to the ~/Downloads directory of the computer.
    $ cd ~/Downloads
    
  9. Copy the installation command from the setup instructions in the Viam app.
  10. Paste it into your terminal window at the command line prompt. Replace the <WIFI-SSID> placeholder (including the angle brackets<and>) with your own SSID. code snippet
  11. Run the command. Enter your WiFi password when prompted. If you receive a warning that the program is unsigned, go to Settings -> Privacy & Security and allow the application to run.
  12. When prompted to select your connection type, select /dev/cu.*. In most cases, including flashing firmware on the ESP32, you'll typically use cu (call-up) instead of tty (teletype). select connection type
  13. In the Viam app, wait for confirmation that your machine has successfully connected, and status is "Live". This might take a few minutes to complete depending on your connection.
  1. The IR sensor has three wires: black (GND), red (VCC), and a third wire for signal (OUT). Color conventions can vary, so double-check your sensor's labels or datasheet. IR sensor
  2. Use the jumper wires to connect the IR sensor to the ESP32 headers.

    HW104 IR Sensor Pin

    Connects To (ESP32)

    Description

    VCC

    Pin 1 (3.3V)

    Power for the sensor

    GND

    Pin 3 (GND)

    Ground connection

    OUT

    Pin 18 (GPIO18)

    Signal output (LOW = detect)

    The OUT wire is the signal output from the sensor. It sends a LOW (0V) signal when it detects an object, and stays HIGH (3.3V or 5V) when nothing is in front of it.

Now that we've connected the Viam machine to WiFi, we can add custom firmware using the over-the-air (OTA) service.

Add custom firmware via OTA service

  1. Under the CONFIGURE tab, select JSON mode. select JSON mode
  2. Paste the following JSON configuration. Notice we are using an IR sensor and an OTA service that requires a URL from which to fetch the new firmware. Refer to the documentation for more details about configuring OTA updates.
    {
      "components": [
        {
          "name": "IR",
          "api": "rdk:component:sensor",
          "model": "hw201",
          "attributes": {
            "meta": "0 is locked, 1 is unlocked",
            "pin": 18
          }
        }
      ],
      "services": [
        {
          "name": "OTA",
          "api": "rdk:service:generic",
          "model": "rdk:builtin:ota_service",
          "attributes": {
            "url": "https://github.com/ehhong/micro-rdk-with-IR-sensor/releases/download/v0.0.5/rusty-door-ota.bin",
            "version": "v0.0.5"
          }
        }
      ]
    }
    
    JSON configuration
  3. Click Save to apply your configuration changes. This may take a moment for the device to update.

Test the IR sensor

  1. Notice an IR sensor component in the left sidebar that corresponds to a panel with more details on the right. There is also an OTA service in the left sidebar that corresponds to a panel on the right.
  2. Find the IR panel, and then expand the TEST section to view the readings detected by the IR sensor. See what happens when you point the sensor towards the IR tape (or similar reflective surface). getReadings

In the next step, let's position the detector so that a value of 0 indicates a locked state, and 1 is unlocked.

Now that the detector is working as expected, let's position it near the door lock to dial in the sensitivity settings.

  1. Mount the device next to the door you want to monitor, pointing the sensor in the direction of the door lock. overhead view of detector
  2. Add IR tape (or similar reflective material) to the deadbolt. side view of detector
  3. Test it again to make sure the sensor placed in the new position captures the readings accurately and reliably. A value of 0 should indicate a locked state, and 1 is unlocked.

Remote access to door lock status

Now that your sensor is set up, you may not want to log in to the Viam app every time you want to check whether your door is locked or unloacked. There's a few options to consider:

What you learned

Real-world applications and projects for IR sensors

IR sensors are widely used for their simplicity, reliability, and low power consumption. Here are some real-world use cases where IR lock-state detection is useful:

Building advanced features with Viam and IR sensors

Now that you have an IR sensor that detects whether a door is locked or unlocked based on reflected light, you can:

Related Viam resources