Toggle Wi-Fi Light when using Zoom with JavaScript

As a remote employee I am often sharing my video on Zoom while working. I may be pair programming, in a meeting, or just checking in with a co-worker. This happens numerous times throughout the day, but itโ€™s hard to predict when Iโ€™m on.

Problem

Being on Zoom with a family at home can be interesting, especially when you work in your bedroom closet like I do. I usually donโ€™t mind if my wife or kids come into my office when Iโ€™m on a Zoom call unless Iโ€™m on with a client or during an important meeting. So, how is my family supposed to know when Iโ€™m on a conference call? Even if they were to come into my office, its not always obvious that Iโ€™m sharing my video.

Solution

So, I ended up writing a program that uses the Wemo Insight Wi-Fi Switch to toggle a Night Light outside my office when I launch or exit Zoom! In addition, I bought some red light bulbs to make the light easier to see.

Interfacing with Wemo

I used the node package wemo-client (by @timonreinhard) to interface with my Wemo. The following code is all that I needed to communicate with my Wemo device.

const Wemo = require( "wemo-client" );
const wemo = new Wemo( {
  port: 1234,
  discover_opts: {
    unicastBindPort: 1235
  }
} );

wemo.discover( function( device ) {
  console.log( `Wemo Device Found: ${ device.friendlyName }` );

  var client = wemo.client( device );

  client.on( "error", function( err ) {
    console.log( `Error: ${ err.code }` );
  } );

  client.on( "binaryState", function( value ) {
    console.log( `Light State changed to: ${ !!value }` );
  } );

  checkZoom( client ); // Will expand on this method down below
} );

Detecting Zoom and Updating Wemo

I used the node package process-exists (by @sindresorhus) to determine if I was currently running Zoom. Since I wasnโ€™t aware of any hook to determine when an app launches or exists, I just used a setTimeout to re-check for Zoom every so often. When I found that the state of the application changed, it was pretty easy to call the setBinaryState method to toggle the wemo light. Finally, to help me know when the light is toggled, I added the node package node-notifier (by @mikaelbrevik) to alert me when the light turns on or off.

const processExists = require( "process-exists" );
const notifier = require( "node-notifier" );
const path = require( "path" );
const INTERVAL = 60000;

let isRunning;

function checkZoom( client ) {
  setTimeout( function refresh() {
    processExists( "zoom.us" ).then( exists => {
      if ( isRunning === exists ) { return };
      isRunning = exists;
      updateLight( client, isRunning );
    });
    setTimeout( refresh, INTERVAL );
  }, INTERVAL );
}

function updateLight( client, flag ) {
  notify( `Turn ${ flag ? "on" : "off" }` );
  client.setBinaryState( +flag );
}

function notify( message ) {
  notifier.notify( Object.assign( {
    title: "Wemo Zoom",
    icon: path.join( __dirname, "zoom.png" ),
    sound: true
  }, { message } ) );
}

Other ideas

Going forward, instead of checking for the Zoom process, Iโ€™d rather find a way to detect my computerโ€™s webcam usage. I havenโ€™t quite figured out how to do that yet, but I think that would be a better solution.

Conclusion

This little project was not only fun, but it has become an extremely handy tool for my family while Iโ€™m working. In addition, the kids really enjoy seeing the light turn on and off. The wemo light can also be controlled with Alexa, the Wemo app on my phone, or you could create a recipe on IFTTT;

If you enjoyed this post, please consider sharing it with others via the following Twitter or Reddit buttons. Also, feel free to checkout my egghead.io profile page for addition free and subscription lessons, collections, and courses. As always, you can reach out to me on Twitter at @elijahmanor. Thanks and have a blessed day!

Reddit