Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl committed Mar 5, 2021
0 parents commit 3de7d3c
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv
.esphome
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Mick Vleeshouwer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
115 changes: 115 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
![Flexispot Desk with automation logos](images/header.png)
(*image source: [Windows Central](https://www.windowscentral.com/flexispot-e5-standing-desk-review)*)

## Turn your LoctekMotion/FlexiSpot desk into a smart desk

Recently I acquired a new standing desk from FlexiSpot. During assembly I noticed that the control panel had a RJ45 cable and a second RJ45 port, which sparked my interest. Can we connect my desk to the internet?

Most of the models Flexispot sells are using components from LoctekMotion. [LoctekMotion](https://www.loctekmotion.com/) is a manufacturer of lifting columns for height adjustable standing desks. On their website they mention a [bluetooth receiver](https://www.loctekmotion.com/shop/accessories/bt-desk-app/) that can be inserted to the control box, but I haven't been able to find this.

This repository will help you to connect your desk to the internet via the serial communication ports (RJ45), for example for use with [Home Assistant](https://www.home-assistant.io/). Think of scenarios like controlling your desk via voice or creating notifications when you sit for too long.
(or just because it is cool 🤓)

## Packages

> Use the information in this repository at your own risk and with caution. Tinkering with electronics always has risks.
| Name | Description |
| ------------------------------------- | -------------------------------------------------------------------------- |
| [Arduino](packages/arduino) | Custom code to control your desk via an ESP32/ESP8266 module via MQTT. |
| [ESPHome](packages/esphome) | Control your desk via an ESP32/ESP8266 module connected to Home Assistant. |
| [Raspberry Pi](packages/raspberry-pi) | Custom code to control your desk via a Raspberry Pi via Python. |

For more packaged solutions, see [similar projects](#similar-projects--research). Pull requests are welcome.

## Research

If you are interested in the internals of the LoctecMotion desk system, have a look at the research below which is composed of my own findings combined with findings of [similar projects](#similar-projects--research).

### Control Panels

At the time of writing, LoctekMotion sells [11 different control panels](https://www.loctekmotion.com/product/control-panel/). The features can differ per model, but it looks like the serial interface is pretty similar for the more advanced models.

The tables below will show a mapping of the RJ45 pinout to the pinout used by the control panel. Please note that all RJ45 pins are described in the following way;

![RJ-45 connector layout](images/RJ-45_connector.jpg)

In order to connect the control box to a Raspberry Pi and ESP32/ESP8266 chip I used a [RJ45 to RS232 adapter](https://www.allekabels.nl/rs232-kabel/4568/1041186/rj45-naar-rs232.html), but you simply can cut and split an ethernet cable as well.


#### [HS13A-1](https://www.loctekmotion.com/shop/control-panel/hs13a-1/)

- **Desk model**: Flexispot EK5
- **Control box**: CB38M2B(IB)-1
- **Source**: Printed on the PCB of the control box.

| RJ45 pin | Name | Cable Color |
| -------- | ---------- | ----------- |
| 8 | RESET SWIM | Brown |
| 7 | PIN 20 | White |
| 6 | RX | Purple |
| 5 | TX | Red |
| 4 | GND1 | Green |
| 3 | +5V (VDD) | Black |
| 2 | 29V+ | Blue |
| 1 | 29V- | Yellow |

#### [HS01B-1](https://www.loctekmotion.com/shop/control-panel/hs05a-1/)

- **Desk model**: Flexispot E5B
- **Control box**: unknown
- **Source**: [nv1t/standing-desk-interceptor](https://github.com/nv1t/standing-desk-interceptor)

| RJ45 pin | Name | Cable Color |
| -------- | --------- | ----------- |
| 8 | +5V (VDD) | Yellow |
| 7 | GND | Blue |
| 6 | TX | Black |
| 5 | RX | Green |
| 4 | PIN 20 | Red |
| 3 | (unknown) | Purple |
| 2 | SWIM | White |
| 1 | RES | Brown |


Other control panels / control boxes could be supported in the same way, but you would need to figure the RJ45 pinout mapping. Most control boxes have an extra RJ45 port for serial communication, but otherwise you would need to place your device in between the control panel and the control box.

### Retrieve current height

Based upon the great work of [minifloat](https://www.mikrocontroller.net/topic/493524), it became clear that the control panel utilises a [7-segment display](https://en.wikipedia.org/wiki/Seven-segment_display). The control box sends the height as [Binary Coded Decimal](https://www.electronics-tutorials.ws/binary/binary-coded-decimal.html), which is decoded in the control panel to drive the 7-segment display. **TODO CHECK**

Fortunately, this is very common in such devices and thus there is a lot of documentation on this topic. There is a great tutorial on [Electronics Tutorials](https://www.electronics-tutorials.ws/combination/comb_6.html), if you want to learn more about this notation.

Make sure you set the baud rate to 9600.

### Send a command

The control box only accepts commands when the 'screen is awake'. This can be simulated by setting PIN 20 to HIGH.

[TODO]

#### Command list

| Command name | Start | Length | Type | Payload | Checksum | End |
| ------------ | ----- | ------ | ---- | --------- | --------- | ---- |
| Wake up | `9b` | `06` | `02` | `00` `00` | `6c` `a1` | `9d` |
| Up | `9b` | `06` | `02` | `01` `00` | `fc` `a0` | `9d` |
| Down | `9b` | `06` | `02` | `02` `00` | `0c` `a0` | `9d` |
| M | `9b` | `06` | `02` | `00` `ac` | `ac` `b8` | `9d` |
| Preset 1 | `9b` | `06` | `02` | `04` `00` | `ac` `a3` | `9d` |
| Preset 2 | `9b` | `06` | `02` | `08` `00` | `ac` `a6` | `9d` |
| Preset 3 | `9b` | `06` | `02` | `10` `00` | `ac` `ac` | `9d` |
| Preset 4 | `9b` | `06` | `02` | `00` `01` | `ac` `60` | `9d` |

All bytes combined will become the command to send to the control box.

## Similar projects / research

While working on this project, I found out that I am not the only one with this idea. There are a few repositories on GitHub with great research which helped me kickstart this project.

- [grssmnn / ha-flexispot-standing-desk](https://github.com/grssmnn/ha-flexispot-standing-desk) - Home Assistant integration via MQTT (micropython)
- [Dude88 / loctek_IOT_box](https://github.com/Dude88/loctek_IOT_box) - Arduino code to control via Alexa and MQTT
- [nv1t / standing-desk-interceptor](https://github.com/nv1t/standing-desk-interceptor) - Research on intercepting commands from Flexispot desks
- [VinzSpring / LoctekReverseengineering](https://github.com/VinzSpring/LoctekReverseengineering#assumptions) - Research and Python samples

and a huge thanks to the [Tweakers.net](https://gathering.tweakers.net) community (Dutch), whom helped me to kickstart this project.
Binary file added images/GPIO-Pinout-Diagram-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/RJ-45_connector.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/arduino/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Arduino
66 changes: 66 additions & 0 deletions packages/arduino/flexispot_ek5.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <SoftwareSerial.h>

#define displayPin20 4 // D2 GPIO4
#define rxPin 12 // D5 GPIO12
#define txPin 14 // D6 GPIO14

SoftwareSerial sSerial(rxPin, txPin); // RX, TX
byte history[2];

// Supported Commands
byte wakeup[] = { 0x9b, 0x06, 0x02, 0x00, 0x00, 0x6c, 0xa1, 0x9d };
byte command_up[] = { 0x9b, 0x06, 0x02, 0x01, 0x00, 0xfc, 0xa0, 0x9d };
byte command_down[] = { 0x9b, 0x06, 0x02, 0x02, 0x00, 0x0c, 0xa0, 0x9d };
byte command_m[] = {0x9b, 0x06, 0x02, 0x20, 0x00, 0xac, 0xb8, 0x9d };
byte command_preset_1[] = { 0x9b, 0x06, 0x02, 0x04, 0x00, 0xac, 0xa3, 0x9d };
byte command_preset_2[] = { 0x9b, 0x06, 0x02, 0x08, 0x00, 0xac, 0xa6, 0x9d };
byte command_preset_3[] = { 0x9b, 0x06, 0x02, 0x10, 0x00, 0xac, 0xac, 0x9d };
byte command_preset_4[] = { 0x9b, 0x06, 0x02, 0x00, 0x01, 0xac, 0x60, 0x9d };


void setup() {
Serial.begin(115200); // debug serial
sSerial.begin(9600); // Flexispot EK5

// Turn desk in operating mode by setting controller pin20 to HIGH
// This will allow us to send commands and to receive the current height
Serial.println("Turn Operation Mode on");
pinMode(displayPin20, OUTPUT);
digitalWrite(displayPin20, LOW);

// Run command
Serial.println("Send command");
sSerial.write(command_down, sizeof(command_down));

// Wake up to get height
sSerial.write(wakeup, sizeof(wakeup));
}

void loop() {
while (sSerial.available())
{
byte in = sSerial.read();

// Start of packet
if(in == 0x9b) {
Serial.println();
}

// Second byte defines the message length
if(history[0] == 0x9b) {
int msg_len = in;
Serial.print("(LENGTH:");
Serial.print(in);
Serial.print(")");
}


// Get package length (second byte)
history[1] = history[0];
history[0] = in;

// Print hex for debug
Serial.print(in, HEX);
Serial.print(" ");
}
}
9 changes: 9 additions & 0 deletions packages/esphome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ESPHome (work in progress)

TODO Add note about pin out + 5V

## Installation

Please refer to the [ESPHome documentation](https://esphome.io/guides/getting_started_command_line.html).

You can use `flexispot_ek5.yaml` as a boilerplate for your own implementation. This implementation has been created for the ESP8266 nodemcu, but can easily be adopted for other platforms.
31 changes: 31 additions & 0 deletions packages/esphome/flexispot_ek5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
esphome:
name: flexispot_ek5
platform: ESP8266
board: nodemcuv2

wifi:
ssid: "Abraham Linksys"
password: "PASSWORD42"

# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Flexispot EK5 Fallback Hotspot"
password: "VGhp4HGdsorM"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
password: "1234"

ota:
password: "1234"

# Cover platform with position

# Switch platform (presets)

# Sensor (current height)
36 changes: 36 additions & 0 deletions packages/raspberry-pi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Raspberry Pi

The Raspberry Pi is a low cost device which is found in many households. A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose input/output) pins along the top edge of the board. These GPIO pins can be used to interact with the LoctekMotion control box.

In the end I believe the Raspberry Pi is overkill for this scenario, but if you have a spare one or if you want to experiment, give it a try. The Python script in this part is limited, but can easily be extended to fix your scenario.

## Prerequisites

- Raspberry Pi (with GPIO headers)
- Ethernet cable
- Dupont cables

## Installation

![GPIO Pinout Diagram](../../images/GPIO-Pinout-Diagram-2.png)

Connect the GND, TX, RX and PIN20 (see [README](../../README.md#control-panels)) to the Raspberry Pi. In the Python script we use the following pins, but you can adapt this to your wishes.

| PIN (GPIO) | Name |
| ------------ | ------ |
| 6 | GND |
| 8 (GPIO 14) | RX |
| 10 (GPIO 15) | TX |
| 12 (GPIO 18) | PIN 20 |

## Install

`sudo apt-get install python3-rpi.gpio`

Browse to the folder where you placed the `flexispot.py` file.

`python3 -m venv venv`

`source venv/bin/activate`

`python3 flexispot.py`
25 changes: 25 additions & 0 deletions packages/raspberry-pi/flexispot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SUPPORTED_COMMANDS = {
"up": bytearray(b'\x9b\x06\x02\x01\x00\xfc\xa0\x9d'),
"down": None,
"m": None,
"wake_up": None,
"preset_1": None,
"preset_2": None,
"preset_3": None,
"preset_4": None,
}

class LoctekMotion():

def __init__(self, serial):
self.serial = serial

def execute_command(self, command_name: str):
"""Execute command"""
command = SUPPORTED_COMMANDS.get(command_name)

if not command:
raise Exception("Command not found")

self.serial.write(command)

0 comments on commit 3de7d3c

Please sign in to comment.