developit / ama

Ask me stuff
29 stars 0 forks source link

How did you automate your Autonomous standing desk? #13

Open stvnwn opened 3 years ago

stvnwn commented 3 years ago

I noticed that you did so from your comment on this GitHub issue. Honestly, I'd be satisfied if you only shared a photo of your custom keypad, just so I know that this project is viable for someone new to microcontrollers.

developit commented 3 years ago

Hi @stvnwn! Sorry for not seeing this. I actually hadn't worked with Microcontrollers for at least a decade when I got back into them and automated the desk.

Buying the cable

The desk has a keypad connected to the motor driver using a cable that looks like ethernet, but which is actually 10 wires instead of 8 - it's a 10P10C connector. I bought a cable+connector on Amazon for $5. Autonomous only uses 4 of the 10 wires (so it's kindof annoying they went with a 10P connector instead of something like a phone jack, which would have been like 5¢).

The microcontroller

Then I bought an ESP8266 chip from Amazon (3 for $12), and flashed Espruino onto it. Espruino is awesome - you plug in your chip via USB, open the Espruino Web IDE, write some JavaScript, and hit "flash" or "send".

Detour: bundling and minifying the JS before flashing

I got a little into the weeds and built a whole Rollup-based bundler + command-line flashing tool instead of using the IDE because I wanted to squeeze a lot more code onto the device. It's called espz (video) - you can try it out if you're daring, the only requirement is that you've flashed the Espruino base image (kinda like an OS) using their guide. It can even send code to the chip over wifi! The npm package has some docs: https://www.npmjs.com/package/espz

Making ESP8266 talk to the desk

From there, I found Stefan's exceptional reverse-engineered protocol description. With that information in hand, I wrote up some JavaScript for the microcontroller that listens for data from the desk's motor controller and can send it commands like UP/DOWN/M1/M2/etc: https://gist.github.com/developit/d610e45a522810b5287db61e554ae9c9#file-desk-js

Making the desk talk to the ESP8266 (for longer than 2 seconds)

I had to do a bunch of performance testing to find the right way to handle the barrage of data from that motor controller in Espruino. Most of my naive attempts couldn't process the data fast enough, and when that happens Espruino starts buffering the received data. That immediately fills the device's memory and it crashes (or overheats!). Two tricks are required to make this work:

  1. in Espruino, "jumping" through a string using indexOf() is way faster than a regex or character loop.
  2. the motor controller sends tons of duplicate data - skipping repeated messages saves a lot of compute time.

Exposing a REST API and Web GUI

Then I wrote a bit more code that turns the ESP8266 into a web server (it's actually pretty good at this!). The web server hosts an API and a little controller/keypad web app that hits that API and allows me to operate the desk. Web server + API: https://gist.github.com/developit/d610e45a522810b5287db61e554ae9c9#file-index-js UI (just an HTML file with some JS in it): https://gist.github.com/developit/d610e45a522810b5287db61e554ae9c9#file-ui-html

Powering it up

Conveniently, when you plug the connector wire into the motor controller, it provides power over one of the wires. It provides 5v, but the ESP8266 chip is 3.3v. Normally that would fry the chip, but as it turns out the ESP8266 is 5v tolerant. This means it boots and runs as long as the wire is connected to the motor controller - no battery or wall adapter needed!

Here's a video of it working (using the UI on my computer for so I could record from my phone): https://twitter.com/_developit/status/1367842479115034624

jtszalay commented 3 years ago

The files reference a utils.js that’s not present. Would you be able to share that?

vkhurana commented 3 years ago

Could you also share the wiring diagram? Which pins on the ESP did you connect to?

vipial1 commented 2 years ago

Hey @developit , amazing job creating the project. I would like to create an integration for HomeAssistant and your project would help a lot. I have only one concern, as I understand in your code, once you connect the microcontroller to the motor, the desk buttons are not working anymore. I would like to keep them working so the solution could be convert your microcontroller in a kind of man in the middle…connected to the motor and to the buttons. Every command from the motor, would be interpreted by the micro (as it does right now) and forwarded to the buttons. And every command from the buttons would be directly forwarded to the motor (not need to interprete them, we will do it with the answer from the motor). What do you think? I could create the HomeAssistant integration but would need some help on this part.

developit commented 2 years ago

Hiya @vipial1 - a Home Assistant integration would be awesome, I would 100% use that! Want to DM me on Twitter so I have your info? I might be able to help.

Regarding the buttons, you're right that the microcontroller replaces the default desk buttons, but that is actually just laziness on my part! Because of the way the wire protocol works, you can actually just keep the controller module connected by soldering a female RJ50 connector - both the microcontroller and desk button controller can connect to the same 5 wires. I can make a diagram if that was unlcear haha.

I'm actually about to set my desk back up in a new house, and my plan was to fix this issue!

vkhurana commented 2 years ago

@developit a diagram would really help! I had the same idea as @vipial1 using an ESP32 (since it has three serial ports) - one to listen from the desk and one to transmit to the keypad.

If it can be done as you described, that will be better.

Best

vipial1 commented 2 years ago

@developit I sent you my email via Twitter. Please, take a look

developit commented 2 years ago

Migrating to a GH repo: https://github.com/developit/desky/discussions/1

piitaya commented 2 years ago

I have a aum world advanced standing desk that use the same controller and keypad. I will look to port it to esphome to have home assistant integration but it's very hard to find a female RJ50 10PC10 😭 (shipping to France 😅)

vipial1 commented 1 year ago

Here the home assistant integration: https://github.com/vipial1/desky-ha

backslashV commented 11 months ago

Hi there, I have a similar desk with an RJ50 keypad connector. I'd like to use an Upsy Desky to make the desk smart, but that device uses an RJ45 connector. Can anyone suggest a way to identify what each of these 5 pins do so I can make a custom RJ50 to RJ45 adapter? PXL_20231018_215534606.jpg

developit commented 11 months ago

Maybe check with Upsy Desky? That looks like the RJ50 from my desk, but I have no idea what pins the RJ45 controllers use.

backslashV commented 11 months ago

Maybe check with Upsy Desky? That looks like the RJ50 from my desk, but I have no idea what pins the RJ45 controllers use.

Thanks. I've already checked with the Upsy Desky developer. He wasn't able to help. Is there a way to determine what each pin in the picture is? I can then ask the Upsy Desky owner what pins on the RJ45 he uses.

borger commented 9 months ago

I have a aum world advanced standing desk that use the same controller and keypad. I will look to port it to esphome to have home assistant integration but it's very hard to find a female RJ50 10PC10 😭 (shipping to France 😅)

Did this port to esphome ever happened @developit ?