Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sebastienvermeille/eec73fd0f0ab6a8f2a27ed1bd6252218 to your computer and use it in GitHub Desktop.
Save sebastienvermeille/eec73fd0f0ab6a8f2a27ed1bd6252218 to your computer and use it in GitHub Desktop.
mqtt:
broker: "< ip >"
port: 1883
username: "<username>"
password: "<password>"
discovery: true
topic_prefix: "megadesk"
on_message:
- topic: "megadesk/set/height"
then:
- lambda: |-
int raw_value = atoi(x.c_str()); // Convert MQTT message to integer
if (raw_value >= 299 && raw_value <= 6640) {
ESP_LOGI("MQTT", "Setting desk height (raw) to: %d", raw_value);
// Update the UI state
id(megadesk_height_raw).publish_state(raw_value);
// Use the same set_action code as the slider
char buf[20];
sprintf(buf, "<=%d.", raw_value);
id(uart_desk).write_str(buf);
ESP_LOGI("MQTT", "Sent UART command: %s", buf);
} else {
ESP_LOGW("MQTT", "Invalid raw height value: %d", raw_value);
}
# If you get errors compiling this with esphome, make sure you are on a recent version.
# 'esphome version' will output the version you are on.
#
# If you want to compile in the Home Assistant esphome addon, you need to copy megadesk.h into your esphome folder
# on your Home Assistant platform.
esp32:
board: esp32-c3-devkitm-1
framework:
type: esp-idf
version: recommended
esphome:
name: megadesk
comment: megadesk-companion
platformio_options:
board_build.flash_mode: dio # If you don't specify this using esp-idf framework, the device will boot-loop.
includes:
- megadesk.h
on_boot:
priority: -100
then:
- delay: 1s
- uart.write: "<C0.0."
- delay: 1s
- uart.write: "<R0.11."
- delay: 1s
- uart.write: "<R0.12."
#####
# You need to make sure tx_pin and rx_pin are set properly for your device.
# For 5-pin Megadesks, this will not need changing.
#
# If you have a 3-pin megadesk, try swapping them in case of a mixup on RX/TX
# when you soldered the wires.
#####
uart:
id: uart_desk
baud_rate: 115200
tx_pin: GPIO6
rx_pin: GPIO7
logger:
hardware_uart: USB_SERIAL_JTAG # Logging over the USB-C port (WiFi will always log as well)
api:
password: ""
ota:
- platform: esphome
password: ""
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Companion Fallback"
password: "megadesk"
captive_portal:
web_server:
port: 80
sensor:
- platform: custom
lambda: |-
auto megadesk = new Megadesk(id(uart_desk));
App.register_component(megadesk);
return { megadesk->raw_height, megadesk->min_height, megadesk->max_height };
sensors:
- id: megadesk_raw
internal: true
on_value:
then:
- component.update: megadesk_height_inches
- component.update: megadesk_height_cm
- component.update: megadesk_height_raw
- name: "Megadesk Minimum Height"
- name: "Megadesk Maximum Height"
number:
- platform: template
name: "Megadesk Height (inches)"
id: megadesk_height_inches
min_value: 23
max_value: 47
step: 0.53
mode: slider
update_interval: never
unit_of_measurement: 'inches'
#NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
lambda: |-
return ((((id(megadesk_raw).state - 299) * (47 - 23)) / (6914 - 299)) + 23);
set_action:
- number.set:
id: megadesk_height_raw
value: !lambda "return int((((x - 23) * (6914 - 299)) / (47 - 23)) + 299);"
- platform: template
name: "Megadesk Height (cm)"
id: megadesk_height_cm
min_value: 58.42
max_value: 118.745
step: 0.53
mode: slider
update_interval: never
unit_of_measurement: 'cm'
#NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
lambda: |-
return ((((id(megadesk_raw).state - 299) * (119.38 - 58.42)) / (6914 - 299)) + 58.42);
set_action:
- number.set:
id: megadesk_height_raw
value: !lambda "return int((((x - 58.42) * (6640 - 299)) / (119.38 - 58.42)) + 299);"
- platform: template
name: "Megadesk Height (raw)"
id: megadesk_height_raw
# internal: true
min_value: 299
max_value: 6640
step: 1
mode: slider
update_interval: never
lambda: |-
return id(megadesk_raw).state;
set_action:
- uart.write: !lambda |-
char buf[20];
sprintf(buf, "<=%i,.", int(x));
std::string s = buf;
return std::vector<unsigned char>( s.begin(), s.end() );
button:
- platform: template
name: "Desk Position 2"
on_press:
then:
- uart.write: "<L0,2."
- platform: template
name: "Desk Position 3"
on_press:
then:
- uart.write: "<L0,3."
- platform: template
name: "Desk Position 4"
on_press:
then:
- uart.write: "<L0,4."
- platform: template
name: "Desk Position 5"
on_press:
then:
- uart.write: "<L0,5."
- platform: template
name: "Toggle Minimum Desk Height"
on_press:
then:
- uart.write: "<L0,11."
- uart.write: "<R0,11."
- platform: template
name: "Toggle Maximum Desk Height"
on_press:
then:
- uart.write: "<L0,12."
- uart.write: "<R0,12."
- platform: template
name: "Recalibrate Desk"
on_press:
then:
- uart.write: "<L0,14."
- platform: template
name: "Reboot Megadesk"
on_press:
then:
- uart.write: "<L0,15."
- platform: template
name: "Toggle Audio feedback"
on_press:
then:
- uart.write: "<L0,17."
- platform: template
name: "Toggle both-button mode"
on_press:
then:
- uart.write: "<L0,18."
interval:
- interval: 300s
then:
- uart.write: "<C0.0."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment