Hey Google, set my desk to standing mode!
Tags: javascript, tutorial, node, googleassistant
Published: 2019-01-30 23:06:11

---
title: Hey Google, set my desk to standing mode!
published: true
description: how to hack any electrical device and connect it to the Google Assistant!
cover_image: https://cdn-images-1.medium.com/max/2560/1*-LgROW6iWc-lOxoN6Jmz7g.jpeg
canonical_url: https://medium.com/@wassimchegham/hey-google-set-my-desk-to-standing-mode-b21dcc40d4b5
tags: javascript, tutorial, node, googleassistant
---

Hi there, if you have been following my [recent tweets](https://twitter.com/manekinekko), you know how much I am enjoying my new standing desk: [The E2B by FlexiSpot](https://amzn.to/2UmHpyI)! So, on one late evening, I had an excess of creativity and I came up with a fun idea: What if I could use the Google Assistant to control my desk settings?

{% twitter 1088184875159560192 %}

So I decided to build a proof of concept and 5 hours later...

{% twitter 1088317216163016704 %}

Let me walk you through the process...

# Disclaimer

Before we start, a couple of things to note:

1. All the information provided here is provided on an "as is" and "as available" basis and you agree that you use such information entirely at your own risk. Under no circumstances will I be held responsible or liable in any way for any damages, losses, expenses, costs or liabilities whatsoever resulting from your use of the information and material presented here.
2. I am not a professional Electronic engineer, but I do have some knowledge - from university - which is enough to put together this proof of concept. However, if you know a better alternative to the circuit I built, please let me know. I'll be more than happy to upgrade my setup!

# High-Level Plan

Our plan for this project is as follow:

1. Reverse engineer the desk controller to understand how the system works.
2. Figure out a way to manually trigger the control signals without using the desk controller.
3. Figure out a way to programmatically trigger the control signals.
4. Hook up the Google Assistant to the system.

---

# #1 Understand how the desk controller works

Well, obviously if you need to understand how a device works, the first thing to do is to open it (**THIS WILL VOID THE WARRANTY**). Also, make sure you have a spare device (or parts) in case things go wrong (which it did for me, read the next part for more details!). So, thankfully I had already anticipated this scenario and bought a spare controller. I'd like to thank the folks over FlexiSpot who nicely gave me a discount code!

*By the way, if you want to buy the E2 standing desk, you can use the code "rffr1" to get a 20€ discount (only valid for the on the [Fexispot website](https://flexispot.com/) untill end of February 2019)!*

Let's have a look at the outside of the desk controller which looks like this:
Once opened, the internal [PCB](https://en.wikipedia.org/wiki/Printed_circuit_board) board shows the following design:

1[](https://cdn-images-1.medium.com/max/800/1*eP1GH15WTStywhVwcQsENg.jpeg)

Here is a basic explanation of each component illustrated in this PCB:

1. Up and Down buttons allow you to raise and lower the desk from 70cm to 120cm (42.1″ to 63″);
2. The Setting Modes button allows to memorize a certain height and store it into the M1, M2 or M3 slots (three programmable presets);
3. The Auto-tracking button is used to set the sit/stand timer system and helps alert you when it's time to stand;
4. The TM1650 is a LED microcontroller which takes care of the 8-segments display on the left ([see full specs](https://os.mbed.com/components/TM1637-LED-controller-32-LEDs-max-Keyboa/));
5. The STM8S (STM8S103F2) is the main microcontroller which is the brain of the whole system ([see datasheet here](https://www.st.com/resource/en/datasheet/stm8s103f2.pdf)).

Now we need to figure out the commands to use to manually control the desk without really using (can we?) the desk controller!

Let the fun begins!

---

## Attempt #1: Reverse engineer the STM8S microcontroller (tl;dr: Don't–Don't–Don't–Don't!!)

My first attempt was to try and reverse engineer the STM8S microcontroller so I could get the required signals that would instruct the motors to change position, and then I would model and send out those exact signals to the motors as if they were coming from the STM8S itself. Well, that was my plan!

However, while I was trying this process, I managed to damage the PIN 14 of the microcontroller which role was to lower the desk down (Down Button). Admittedly, even if I managed to get all the signals from the microcontroller, I would not have been able to lower the desk anymore - other than replacing the STM8S microcontroller unit which I would have done if I had a spare one!

![](https://cdn-images-1.medium.com/max/800/1*NDOpgDsKZef528YJbbHEnA.png)

So, I had to figure out another way. And actually, there was a much simpler way of doing exactly what I was looking for.


---

## Attempt #2: Hacking the M1, M2 and M3 buttons…? DUH!

It turns out there was a simpler method that would allow me to send the signals to the motors without touching (or burning!) the STM8S microcontroller. I simply had to hack into the M1, M2, and M3 buttons! These modes work as presets that store a specific height of the desk and when triggered, they instruct the desk to go to that exact height, from any position.

![](https://cdn-images-1.medium.com/max/800/1*PJrv_mbUfesdfWwuOyC2lA.png)

**This hacky method turns out to be more generic and can literally be applied to any electrical device which has button inputs.**

Now, I was ready for the next step: figuring out a way to simulate a push button so the microcontroller thinks that one of the M1, M2 or M3 buttons was pushed.

For that, I had to build an electronic switch circuit.

---

# #2. Manually trigger the control signals

Before we automate all the things, let's first try and test manually our approach. For that, I built a simple circuit which is an "NPN Transistor Switching Circuit". This transistor-based switch circuit is ideal for low voltage DC devices which is exactly what we are trying to do: remember that we are trying to simulate a push button which requires a few milli-amps to be triggered!

## NPN Transistor As A Switch Circuit - 101

To make it simple, the areas of operation for a transistor switch are known as the *Saturation Region* and the *Cut-off Region*. This means that we will use the transistor as a switch by driving it back and forth between its "fully-OFF" (cut-off) and "fully-ON" (saturation) regions. Here is a *very simplified* illustration of the operation regions of a typical bipolar transistor:

![](https://cdn-images-1.medium.com/max/800/1*xtXUHAQUUU3oxKDB9AVEag.png)

Having said that, let's now have a look at a typical "NPN Transistor As A Switch" circuit:

![](https://cdn-images-1.medium.com/max/800/1*NR81F0_hjbqFXe2VVydwwg.png)

In this circuit, we are using an LED as a load to demonstrate the switch process. And here is an "implementation" of such a circuit:

![](https://cdn-images-1.medium.com/max/800/1*1IESOpAhGkiGzbYquQGrnQ.png)

Very simple, right? Our circuit seems to work as expected.

Now let's move on and hook up the desk controller into this circuit:

![](https://cdn-images-1.medium.com/max/800/1*LvnQTvGfJ7lFMPhl8P_byw.png)

In the diagram above, J1, J2 and 3 are the jumpers that will be connected to the wires that we soldered into the desk controller, respectively, Mode 1, Mode 2 and Mode 3. J0 is the ground which will also be connected to the desk controller.
And here is the actual circuit on the breadboard:

![](https://cdn-images-1.medium.com/max/800/1*MDJa5mP4KmhCms0NyruPgQ.png)

Note that the V1, V2 and V3 (and GND which is not in this picture) wires are intended to remain loose for the moment.

---

# #3. Automatically trigger the push signals

In order to automatically trigger the "push button" signals that will fool the STM8S microcontroller, we will need a programmable microcontroller. For this, you can use any board you have (Arduino, Nanode, Teensy, etc…) or even a Raspberry PI; anything with GPIOs.

I initially used an Arduino MEGA 2560 board in order to experiment with the code but then I realized that I didn't have a Wifi shield (which is required for the next part of this project). So as a reference, here is the Arduino code:

{% gist https://gist.github.com/manekinekko/333c7f41b9d5654453708b3327c83d1c %}

So I ended up using one of my Onion Omega2+ wifi enabled boards (**SPOILER: it can run JavaScript!!!**).

![](https://cdn-images-1.medium.com/max/800/0*1SyJhYobfzWfsbeY.png)

Here is the final circuit. Please note that the V1, V2, and V3 wires are now connected to PIN 1, 2 and 3 of the Onion Omega2+ chip:

![](https://cdn-images-1.medium.com/max/800/1*Ye_2JHnk_yXC-HaVCbyLgA.png)

And here is the breadboard implementation:

![](https://cdn-images-1.medium.com/max/1000/1*p-vhgNk37bXNRu9O0WraUQ.png)

Now that everything is in place, all we need to do is to write a simple program that will toggle the GPIO PIN 1, 2 and 3: this will send a HIGH voltage up to 3.3V and a LOW voltage down to 0.4V.

So basically, when we set the output on PIN 1 to HIGH, this will deliver a 3.3V through the transistor-based switch circuit which in turn will saturate the transistor allowing it to close the switch and this will act as a push button. The STM8S will then receive the push signal from the M1 button and then instruct the desk motor accordingly. That's it!

Here is the actual Node.js code illustrating this:

{% gist https://gist.github.com/manekinekko/3afd35ca285503569d16ff983b114e96 %}

*I should mention that the Onion Omega2+ has a built-in shell command that allows controlling the GPIO pins. For instance, if we need to set the PIN 1 to OUTPUT mode with a HIGH voltage, we can run the following command: gpioctl dirout-high 1 But I was happy to find an NPM module that abstracts these operations.*

Now that our code is working as intended, we are going to expose a simple REST API that we will use to trigger the same commands but this time, over HTTP (this is required for the Google Assistant integration):

{% gist https://gist.github.com/manekinekko/a726ede6d255e6ec657774a8eb6c253a %}

We can now easily send a GET request to `http://192.168.80.84:1337/mode/1` and this will set the PIN 1 to HIGH for 800ms then set it back to LOW. The same thing applies to `mode/2` and `mode/3`.

One last thing to do is schedule this script to run automatically during the boot sequence. We do this by editing the `/etc/rc.local` script file ([as described in the documentation](https://docs.onion.io/omega2-docs/running-a-command-on-boot.html)):

```
node /root/desk-controller-assistant-server.js &

exit 0
```

*Please note that this command runs continuously since we are executing a node server. In order for the boot sequence to finish successfully (and reaches the exit 0), we need to fork the process by adding an ampersand (&) to the end of the command.*

Before moving to the next part, I highly recommend you allocate a static IP address to your device, this will help with the Google Assistant integration!


---

Here is the final hardware setup:

![](https://cdn-images-1.medium.com/max/800/1*1MUcKFhRzNymwPukDiPgRA.jpeg)

---

# #4 Hooking up the Google Assistant

## Pre-requisite

Make sure your Arduino or Onion Omega2+ is accessible from the Internet. For the purpose of this proof of concept, I simply used [ngrok](https://dashboard.ngrok.com/get-started) which allows me to expose a Webhook - from a local device in my local network, to the outside world - It's really the perfect tool for this experiment!

**One caveat though**: The Onion Omega2+ I used has only a 32MB of internal storage so I was unable to install ngrok on the device itself, instead, I installed ngrok on my Pixelbook.

*The Omega2+ has an SD card slot to extend the internal storage, I just didn't want to bother with that!*

Using ngrok, I opened up an HTTP tunnel on `http://198.168.80.84:1337` (the IP of the Omega2+):

```
ngrok http -subdomain=wassimchegham 192.168.86.84:1337
```

Once ngrok is up and running, I finally got a public URL:

```
https://wassimchegham.ngrok.io/
```

And now I can send HTTP requests to my device to set the desired mode:
```
https://wassimchegham.ngrok.io/mode/1
```

**⚠️ WARNING: ngrok will expose your local machine to the Internet. Make sure your turn it off if you are not using it.**

## Setting up the Google Assistant - the quickest way: IFTTT

This is where the infamous IFTTT comes in place. Here is the recipe I used:

IF...

![](https://cdn-images-1.medium.com/max/800/1*QMVRMx5qSotbMqNQcP6myw.png)

THEN...

![](https://cdn-images-1.medium.com/max/800/1*BH560JfuwnlNipL8HmrpBg.png)

DONE!

**I highly recommend reading the next part about the recommended way to integration IoT device with the Google Assistant.**

---

## Setting up the Google Assistant - the recommended way: The Smart Home API

If you are building a serious device integration with the Google Assistant, you will have to build a Smart Home Action:

*Smart Home Actions rely on Home Graph, a database that stores and provides contextual data about the home and its devices. For example, Home Graph can store the concept of a living room that contains multiple types of devices (a light, television, and speaker) from different manufacturers. This information is passed to the Google Assistant in order to execute user requests based on the appropriate context. [Read more](https://developers.google.com/actions/smarthome/).*

Creating a Smart Home Action requires you to implement the following command:
1. **action.devices.SYNC**: Requests the list of devices that the user has connected and are available for use.
2. **action.devices.QUERY**: Queries for the current states of devices.
3. **action.devices.EXECUTE**: Requests a command to execute on smart home devices. The new state should be provided in response if available. One EXECUTE intent can target multiple devices, with multiple commands.
4. **action.devices.DISCONNECT**: Informs your app when a user has unlinked the app account from the Google Assistant. After receiving a DISCONNECT intent, you should not report state for this user's devices.

I am not going to explain all the implementation details of each method. But, I should mention though that most of the code was adapted from the [Smart Washer codelab](https://codelabs.developers.google.com/codelabs/smarthome-washer/#0). So I highly recommend you make sure to follow that codelab for more details on how to deploy and sync your smart device with your Google Assistant.

*Shout out to [Nick Felker](https://medium.com/@fleker) and his team for putting together this codelab which saved me a lot of time 👏🏼*

Even though if every Smart Home Action logic is different, 99% of them will share the same boilerplate code (assuming they use of Node.js and Firebase):

{% gist https://gist.github.com/manekinekko/5491d2331ee364eac43da469fee1c109 %}


*To know more about the role of each method, please head over to the [official documentation](https://developers.google.com/actions/smarthome/create).*

One thing to note about the [Device Traits API](https://developers.google.com/actions/smarthome/traits/): When registering a new device (ie. a Smart home Action) with the Google Assistant (ie. the Home Graph), your device must be one of the supported devices ([see full list](https://developers.google.com/actions/smarthome/guides/)) and must have one of the supported traits ([see full list](https://developers.google.com/actions/smarthome/traits/)). In addition, every trait attribute such as Modes attributes ([see full list](https://developers.google.com/actions/reference/smarthome/traits/modes)) and Toggles attributes ([see full list](https://developers.google.com/actions/reference/smarthome/traits/toggles)) must also be one of the supported ones. So, obviously, a standing desk does not fit in any of those categories. I had then to make the Home Graph thinks that the standing desk is a… Switch (type: `action.devices.types.SWITCH`) with an ON/OFF feature (trait: `actions.devices.traits.OnOff`). This means that my desk can have only two states:

1. Turned On: will raise the desk (Mode 3).
2. Turned Off: will lower the desk (Mode 1).

Well! That should be fine for this proof of concept. But if you are building your own device integration, you have plenty of other Traits that you can combine together that would perfectly match your device functionalities.
When it comes to our specific code implementation, here is the complete implementation:

{% gist https://gist.github.com/manekinekko/8ada9f0daf136ec6ae2c785bf4ad33c6 %}

Here is the full project source if you'd like to improve the setup

{% github https://github.com/manekinekko/standing-desk-assistant %}

Basically, what the code does is: when we send commands to our Smart Home Action, we store each state in the Firebase Realtime database. Then, for every state change, we simply send the HTTP request to our local device through ngrok. Here is the high-level - self-explanatory - picture of the architecture:

![](https://cdn-images-1.medium.com/max/800/1*YauIyXGlP2k-wV4Z0HnQdQ.jpeg)

*For a more detailed explanation about how the Smart Home API works, please refer to the [official documentation](https://developers.google.com/actions/smarthome/).*

Now, let's connect and use our **[test] Standing Desk device** (ie. the Switch) to our Google Assistant:

![](https://cdn-images-1.medium.com/max/1000/1*Odh_9uoYFTZPFDk8AVxoWA.png)

*Remember to deploy a test version of your Smart Home Action from the Actions Console, in order to find in under the available devices list.*

For a more natural interaction with the actions, I created two routines that would allow me to raise and lower my desk - instead of turning on and off my desk:

![](https://cdn-images-1.medium.com/max/800/1*fom2SZY3P-67pkpjEcweLQ.png)


---

# Thank You!

That's it! I hope you enjoyed reading this post as much as I enjoyed writing it and building this fun project.

Until next time, take care ❤️


---

Follow me [@manekinekko](https://twitter.com/manekinekko) for more fun projects.