jesserockz / athom-configs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add DDP Support - To sync with WLED

incaqueen opened this issue · comments

Thank you for your work on this! I got this up and running on my newly received Athom 15W bulbs.

I noticed a few other configs online have added DDP support, to allow syncing with WLED.
https://github.com/tony-fav/esphome-sync/tree/main/ddp
https://www.youtube.com/watch?v=d0ow9qJT8Ak

I combined your code with the one from tony-fav. I tested it and it works! Take a look and consider adding this and E131 support as well:

substitutions:
  name: athom-15w
  friendly_name: "Athom RGBCT Light"
  project_name: "athom.rgbct-light"
  project_version: "1.0"

esphome:
  name: "${name}"
  name_add_mac_suffix: false
  platform: ESP8266
  board: esp8285
  project:
    name: "${project_name}"
    version: "${project_version}"

api:

ota:

logger:

web_server:
  port: 80

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap: {} # This spawns an AP with the device name and mac address with no password.

captive_portal:

#dashboard_import:
#  package_import_url: github://athom-tech/athom-configs/athom-rgbct-light.yaml

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

sensor:
  - platform: uptime
    name: "${friendly_name} Uptime Sensor"

switch:
  - platform: restart
    id: restart_switch
    name: "${friendly_name} Restart"

output:
  - platform: esp8266_pwm
    id: red_output
    pin: GPIO4
  - platform: esp8266_pwm
    id: green_output
    pin: GPIO12
  - platform: esp8266_pwm
    id: blue_output
    pin: GPIO14
  - platform: esp8266_pwm
    id: white_output
    pin: GPIO5
  - platform: esp8266_pwm
    id: ct_output
    inverted: true
    pin: GPIO13

light:
  - platform: rgbct
    restore_mode: RESTORE_DEFAULT_ON
    name: "${friendly_name}"
    id: my_light
    red: red_output
    green: green_output
    blue: blue_output
    white_brightness: white_output
    color_temperature: ct_output
    cold_white_color_temperature: 153 mireds
    warm_white_color_temperature: 500 mireds
    color_interlock: true
    effects:
      - lambda:
          name: DDP
          update_interval: 0s
          lambda: |-
            // statics in light effects are like globals in Arduino
            static float scaled_r = 0.0;
            static float scaled_g = 0.0;
            static float scaled_b = 0.0;
            static std::unique_ptr<WiFiUDP> ddp_udp;
          
            // allocate and start UDP
            // this is like the start() in an Arduino sketch
            if (!ddp_udp) {
              ddp_udp = make_unique<WiFiUDP>();
              if (!ddp_udp->begin(4048)) {   // always listen on DDP port
                return;
              }
            }
            
            // the rest is like the loop() in an Arduino sketch
            // read UDP payload
            std::vector<uint8_t> payload;
            while (uint16_t packet_size = ddp_udp->parsePacket()) {
              payload.resize(packet_size);
              if (!ddp_udp->read(&payload[0], payload.size())) {
                continue;
              }
            }
        
            // ignore small payload
            if (payload.size() < 2) {
              return;
            }

            // do the thing
            float r = (float)payload[10]/255.0f;
            float g = (float)payload[11]/255.0f;
            float b = (float)payload[12]/255.0f;
            
            float m = 0.0f;
            if ( (r>=g) && (r>=b) ) { m = r; }
            else if ( g >= b )      { m = g; }
            else                    { m = b; }
            
            if (m != 0.0f) {
              scaled_r = r/m;
              scaled_g = g/m;
              scaled_b = b/m;
            } else {
              scaled_r = 0.0f;
              scaled_g = 0.0f;
              scaled_b = 0.0f;
            }
            auto call = id(my_light).turn_on();
            call.set_transition_length(0);
            call.set_brightness(m); 
            call.set_color_mode(ColorMode::RGB);
            call.set_rgb(scaled_r, scaled_g, scaled_b);
            call.set_publish(false);
            call.set_save(false);
            call.perform();

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"
      disabled_by_default: true

Thats awesome @incaqueen. ESPHome already supports e131 as a built in component, but ddp could be something that could be added still.

https://esphome.io/components/light/index.html#e1-31-effect

Going to close this as its not really an issue, but I will keep it in mind and might add ddp to ESPHome myself if I get some time.