Create a SmartThings Edge Driver for an IoT bulb

Objective

Learn how to create and customize an Edge Driver for an IoT bulb to seamlessly interoperate on the SmartThings platform.

Overview

SmartThings is a platform for IoT devices to communicate within its ecosystem, enabling smarter living solutions that simplify everybody else's way of life. There are multiple methods to connect an IoT device to the SmartThings platform, one of which is through a SmartThings hub.

Hub connected devices connect to a SmartThings-compatible hub using Matter, Zigbee, Z-Wave, or LAN protocols. The SmartThings-compatible hub allows devices that utilize these protocols to integrate within the SmartThings platform, permitting users to view and control devices from the SmartThings app to automate actions and more. The connection from a SmartThings device to a SmartThings hub is made possible with Edge Drivers.

SmartThings Edge Drivers serve as translators between the protocols used by the device and the SmartThings platform. These drivers enable the devices to run locally on the hub, offering many benefits including speed, reliability, and enhanced functionality.

Learn more about Edge Drivers in the SmartThings Edge Architecture section.

Set up your environment

You will need the following:

  • Host PC running on Windows 10 (or higher) or Ubuntu 20.04 (x64)
  • Visual Studio Code (latest version recommended)
  • Devices connected on the same network:
    • Android mobile device with SmartThings app installed (with Android 10 or higher)
    • SmartThings Station (or SmartThings Hub) onboarded with Samsung account
    • Philips Hue Bulb (SmartThings connected device)

Sample Code

Here is a sample code for this Code Lab. Download it and start your learning experience!

Edge Driver Sample Code
(6.1 KB)

Install SmartThings CLI

You need to install SmartThings CLI as this is the main tool for developing apps and drivers for SmartThings Edge Drivers.

To install SmartThings CLI, open a web browser and download the smartthings.msi installer from the latest release.

Open the SmartThings CLI Setup in the downloaded file, then click Next.

Accept the license agreement terms, then click Next.

Select the destination path for installation and click Next.

To begin the installation process, click Install.

Complete the setup by clicking Finish.

To verify if SmartThings CLI is installed correctly, open the Command Prompt and run this command:

smartthings --version

View and run available commands for SmartThings CLI with this command:

smartthings --help

For a full list of commands, visit the SmartThings CLI Commands.

Start your project

After downloading and extracting the sample code containing the project files, click File > Open Folder in Visual Studio Code to open it.

Locate the sample code directory and click Select Folder.

Once finished, the project files are seen on the Explorer menu.

Set the bulb's color configuration

In init.lua, under the device_init function, write the code below to set the bulb's colors and its transition time:

local ColorControl = clusters.ColorControl

  local PHILIPS_HUE_COLORS = {
    {0xED, 0xC4}, -- red
    {0xAE, 0xE3}, -- blue
    {0x2C, 0xC3}, -- yellow
    {0x53, 0xD3}, -- green
    {0xCA, 0x08}, -- white
  }
  
  local index = 1
  
  local TRANSITION_TIME = 0 --1/10ths of a second
  -- When sent with a command, these options mask and override bitmaps cause the command
  -- to take effect when the switch/light is off.
  local OPTIONS_MASK = 0x01
  local OPTIONS_OVERRIDE = 0x01

  device:send(ColorControl.server.commands.MoveToHueAndSaturation(device, PHILIPS_HUE_COLORS[5][1], PHILIPS_HUE_COLORS[5][2], TRANSITION_TIME, OPTIONS_MASK, OPTIONS_OVERRIDE))

  local timer = device.thread:call_on_schedule(1, function()
    local hue = PHILIPS_HUE_COLORS[index][1]
    local sat = PHILIPS_HUE_COLORS[index][2]
    device:send(ColorControl.server.commands.MoveToHueAndSaturation(device, hue, sat, TRANSITION_TIME, OPTIONS_MASK, OPTIONS_OVERRIDE))
    index = (index + 1) % 6
    if index == 0 then
      index = 1
    end
  end, "color_schedule_timer")

Save the file and open either the Command Prompt or Terminal.

Build and upload your Edge Driver

In the Terminal, type the following command to build and upload your Edge Driver package to the SmartThings Cloud:

smartthings edge:drivers:package

Create a private channel

Create a new channel for your Edge Driver and enter the following channel details:

smartthings edge:channels:create
Channel name: SmartThings Edge Driver Channel
Channel description: Channel for SDC2024
Channel terms of service URL: www.smartthings.com

Enroll the SmartThings Hub in your channel

Enroll your hub in your newly created channel and select the corresponding channel and hub:

smartthings edge:channels:enroll

Assign the Edge Driver to your channel

Assign your driver to the created channel:

smartthings edge:channels:assign

Install the Edge Driver to your hub

Install the created Edge Driver from your channel to your own hub:

smartthings edge:drivers:install

Control the bulb via SmartThings app

On your mobile phone, launch the SmartThings app and tap the + icon.

Once you're on the Add device page, tap Scan nearby. Make sure that the Philips light bulb is turned on.

Wait for the light bulb to be visible. Once visible, tap Done.

Now, observe the blinking and changing colors of your bulb.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can create an Edge Driver for SmartThings devices that can be integrated into the SmartThings ecosystem!

To learn more about SmartThings hub connected devices and Edge Drivers, visit: