Great News - The desk is movable by software!
Summary of what I did the last few days
I already updated my repository on GitHub: GitHub - MhouneyLH/esphome_custom_components: A collection of custom components for esphome.
The desk can move up and down. Finally. I used the G-Pin
(the second one) for showing the controller-box that something has to move. I augmented my adapter-construction with another white-cable for the touchpad-tx (in the code this is remote-uart
). With that one I can write the corresponding messages via uart.
But how does the desk know, if it should go up or down?
I have defined a function called move_to(target_height)
. It checks the boundaries, sets the new target-height and updates the current-operation based on the current-height:
void Desktronic::move_to(const float height_in_cm)
{
if (height_in_cm < MIN_HEIGHT || height_in_cm > MAX_HEIGHT)
{
ESP_LOGE(TAG, "Moving: Height must be between 72.0 and 119.0 cm");
return;
}
target_height_ = height_in_cm;
current_operation = must_move_up(height_in_cm) ? DESKTRONIC_OPERATION_RAISING : DESKTRONIC_OPERATION_LOWERING;
}
The main-loop looks like this (no need to move, if we are idling anyway):
void Desktronic::loop()
{
read_desk_uart();
if (current_operation == DesktronicOperation::DESKTRONIC_OPERATION_IDLE)
{
read_remote_uart();
return;
}
move_to_target_height();
}
The read_desk_uart()
-method updates the current_height_
. That’s pretty important for the later check, if the target-height is reached or if the desk should move further more. This check you will see in the move_to_target_height()
-method.
The interesting part is the move_to_target_height()
-method. Summarized, I do some checks at the beginning, check wether I have to move up or down and do that until I reach the wished height (inside of the target-boundaries). The target boundaries are defined with a deviation from the target height of 0.6cm. The move_pin_
is the G-Pin
in this case, which has to be high when moving:
void Desktronic::move_to_target_height()
{
if (!move_pin_)
{
ESP_LOGE(TAG, "Moving: Move pin is not configured");
return;
}
if (!remote_uart_)
{
ESP_LOGE(TAG, "Moving: Remote UART is not configured");
return;
}
if (!isCurrentHeightValid())
{
ESP_LOGE(TAG, "Moving: Height must be between 72.0 and 119.0 cm");
move_pin_->digital_write(false);
current_operation = DesktronicOperation::DESKTRONIC_OPERATION_IDLE;
return;
}
move_pin_->digital_write(true);
switch (current_operation)
{
case DesktronicOperation::DESKTRONIC_OPERATION_RAISING:
ESP_LOGE(TAG, "Moving: Up");
for (int i = 0; i < REMOTE_UART_SEND_MESSAGE_COUNT; i++)
{
remote_uart_->write_array(REMOTE_UART_MESSAGE_MOVE_UP, REMOTE_UART_MESSAGE_LENGTH);
}
break;
case DesktronicOperation::DESKTRONIC_OPERATION_LOWERING:
ESP_LOGE(TAG, "Moving: Down");
for (int i = 0; i < REMOTE_UART_SEND_MESSAGE_COUNT; i++)
{
remote_uart_->write_array(REMOTE_UART_MESSAGE_MOVE_DOWN, REMOTE_UART_MESSAGE_LENGTH);
}
break;
default:
return;
}
if (isCurrentHeightInTargetBoundaries())
{
ESP_LOGE(TAG, "Moving: Finished");
move_pin_->digital_write(false);
current_operation = DesktronicOperation::DESKTRONIC_OPERATION_IDLE;
}
}
I was also able to reduce the flash memory to 44%. I removed unneeded header files as well as yaml configurations that are useless now. My yaml file now looks like this:
esphome:
name: esphome-web-755eb2
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: ...
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: ...
gateway: ...
subnet: ...
external_components:
- source:
type: git
url: https://github.com/MhouneyLH/esphome_custom_components
ref: develop
refresh: 4s
components: [ desktronic ]
uart:
- id: desk_bus
tx_pin: 5 # D1
rx_pin: 4 # D2
baud_rate: 9600
- id: remote_bus
tx_pin: 3 # RX
rx_pin: 1 # TX
baud_rate: 9600
desktronic:
id: my_desktronic
desk_uart: desk_bus
remote_uart: remote_bus
height:
name: Desk Height
move_pin:
number: 14 # D5
up:
name: Up Button
down:
name: Down Button
memory1:
name: Memory1 Button
memory2:
name: Memory2 Button
memory3:
name: Memory3 Button
switch:
- id: move_switch
name: ↑↓
platform: gpio
pin:
number: 2 # D4
inverted: true
on_turn_on:
then:
- lambda: id(my_desktronic).move_to(80.0);
# - lambda: id(my_desktronic).stop();
binary_sensor:
- id: is_moving_bsensor
name: Desk is moving
platform: template
lambda: return id(my_desktronic).current_operation != desktronic::DESKTRONIC_OPERATION_IDLE;
For more information about the code, etc. you can check the mentioned repository. With this state of the project, I accomplished a major (and the most difficult intermediate step) of the planned project. Thank you, especially @Mahko_Mahko and @ssieb!
The next steps and goals
- currently it is not possible to see the current height or other stuff on the touchpad, when the remote-tx is connected to the chip.
→ potential solution: use a third uart-bus for sending the remote-tx-data separately. - I want to create a more beautiful ui in HomeAssistant.
- I want to connect a weight-sensor to the home-assistant eco-system. The weight-sensor lays under my chair and if I sit down, the desk automatically goes down.
- add 2 methods with which I can adjust the height of the desk one by one. (so I dont have to give it a fixed height everytime)
- improving the hardware circumstances: At the moment, I have soldered the chip on improperly. I should now do this again properly because I noticed in between that there were some loose contacts.