Controlling Elgato Key Light under Ubuntu with Ruby

Recently I’ve acquired Elgato Key Light. It is a WiFi controllable LED lighting panel.

The panel uses 160 LEDs to provide up to 2800 lumens of brightness and a color range of 2900-7000K. While you can control it from a mobile device, doing it directly from the shell makes the whole experience way more convenient.

Key Light officially does not support Linux, however it uses ESP32, and it does run an unprotected HTTP server that accepts JSON commands. You can not only turn it on and off but also adjust both light temperature and brightness with simple JSON requests.

Here’s the code that I use to control my lights written in Ruby:

Sponsored
Sponsored
class="brush: ruby; title: ; notranslate"> #!/usr/bin/env ruby require 'uri' require 'net/http' require 'json' require 'yaml' # Set your lights IPs LIGHTS = %w[ 192.168.0.199 ] COMMAND = ARGV[0] DETAIL1 = ARGV[1].to_i DETAIL2 = ARGV[2].to_i # Sends a json request to a given elgato light def dispatch(ip, payload, endpoint, method) uri = URI("http://#{ip}:9123/elgato/#{endpoint}") req = method.new(uri) req.body = payload.to_json res = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(req) end puts res.body end def on(light_ip) dispatch( light_ip, { 'numberOfLights': 1, 'lights': [{ 'on': 1 }] }, 'lights', Net::HTTP::Put ) end def off(light_ip) dispatch( light_ip, { 'numberOfLights': 1, 'lights': [{ 'on': 0 }] }, 'lights', Net::HTTP::Put ) end def temperature(light_ip, value) raise "Needs to be between 143 and 344, was: #{value}" if value < 143 || value > 344 dispatch( light_ip, { 'numberOfLights': 1, 'lights': [{ 'on': 1, 'temperature': value }] }, 'lights', Net::HTTP::Put ) end def brightness(light_ip, value) raise "Needs to be between 3 and 100, was: #{value}" if value < 3 || value > 100 dispatch( light_ip, { 'numberOfLights': 1, 'lights': [{ 'on': 1, 'brightness': value }] }, 'lights', Net::HTTP::Put ) end def info(light_ip) dispatch( light_ip, {}, 'accessory-info', Net::HTTP::Get ) end def command(command, light_ip) case command when 'on' on(light_ip) when 'off' off(light_ip) when 'temperature' temperature(light_ip, DETAIL1) when 'brightness' brightness(light_ip, DETAIL1) when 'theme' temperature(light_ip, DETAIL1) brightness(light_ip, DETAIL2) when 'info' info(light_ip) else raise "Unknown COMMAND #{COMMAND}" end end LIGHTS.each { |light_ip| puts command(COMMAND, light_ip) }

You can place this code in /usr/local/bin under elgato name with executable permissions and then you can just:

elgato on # turn your lights on
elgato off # turn your lights off
elgato info # get info on your lights
elgato brightness 50 # set brightness to 50%
elgato temperature 280 # make  light temperature quite warm

The post Controlling Elgato Key Light under Ubuntu with Ruby appeared first on Closer to Code.

Ubuntu Server Admin

Recent Posts

Meet the Canonical Ceph team at Cephalocon 2024

Photo by Jeton Bajrami on Unsplash Date: December 4-5th, 2024 Location: Geneva, Switzerland In just…

12 hours ago

The transition to in-house software development in the automotive industry

Who will win the race to the perfect SDV? The automotive industry is experiencing a…

12 hours ago

Profile-guided optimization: A case study

Software developers spend a huge amount of effort working on optimization – extracting more speed…

12 hours ago

Ubuntu Weekly Newsletter 866

Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…

1 day ago

Ubuntu vs Debian: Linux Distributions Compared Deep Dive

Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…

2 days ago

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

4 days ago