Skip to content

Instantly share code, notes, and snippets.

@emorydunn
Last active Sep 29, 2021
Embed
What would you like to do?
Sonoff L1 ESPHome Component
esphome:
name: led_strip_1
platform: ESP8266
board: esp8285
includes:
- custom_components/sonoff_l1.h
wifi:
captive_portal:
# Logging must be disabled in order for the ESP to comunicate with the Nuvotron
# logger:
# hardware_uart: UART0_SWAP
# level: VERBOSE
# Enable Home Assistant API
api:
ota:
light:
- platform: custom
lambda: |-
auto light_out = new SonoffL1();
App.register_component(light_out);
return {light_out};
lights:
- name: "LED Strip 1"
effects:
- lambda:
name: Colorful Gradient (Smooth)
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeGradient();
- lambda:
name: Colorful Breath (Fade)
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeBreath();
- lambda:
name: RGB Gradient
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeRGBGradient();
- lambda:
name: RGB Pulse (Strobe)
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeRGBPulse();
- lambda:
name: RGB Breath
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeRGBBreath();
- lambda:
name: RGB Strobe (Flash)
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeRGBStrobe();
- lambda:
name: Sound Reactive
lambda: |-
auto light_out = new SonoffL1();
light_out->setModeSync();
#include "esphome.h"
class SonoffL1 : public Component, public LightOutput {
#define SONOFF_L1_MODE_COLORFUL 1 // [Color key] Colorful (static color)
#define SONOFF_L1_MODE_COLORFUL_GRADIENT 2 // [SMOOTH] Colorful Gradient
#define SONOFF_L1_MODE_COLORFUL_BREATH 3 // [FADE] Colorful Breath
#define SONOFF_L1_MODE_DIY_GRADIENT 4 // DIY Gradient (fade in and out) [Speed 1- 100, color]
#define SONOFF_L1_MODE_DIY_PULSE 5 // DIY Pulse (faster fade in and out) [Speed 1- 100, color]
#define SONOFF_L1_MODE_DIY_BREATH 6 // DIY Breath (toggle on/off) [Speed 1- 100, color]
#define SONOFF_L1_MODE_DIY_STROBE 7 // DIY Strobe (faster toggle on/off) [Speed 1- 100, color]
#define SONOFF_L1_MODE_RGB_GRADIENT 8 // RGB Gradient
#define SONOFF_L1_MODE_RGB_PULSE 9 // [STROBE] RGB Pulse
#define SONOFF_L1_MODE_RGB_BREATH 10 // RGB Breath
#define SONOFF_L1_MODE_RGB_STROBE 11 // [FLASH] RGB strobe
#define SONOFF_L1_MODE_SYNC_TO_MUSIC 12 // Sync to music [Speed 1- 100, sensitivity 1 - 10]
public:
int mode = SONOFF_L1_MODE_COLORFUL;
void setup() override {
// nothing to do here
Serial.begin(19200);
}
LightTraits get_traits() override {
// return the traits this light supports
auto traits = LightTraits();
// Use Supported Color Modes for 2021.8 and later
traits.set_supported_color_modes({light::ColorMode::RGB});
// Use Individual traits for earlier versions
// traits.set_supports_brightness(true);
// traits.set_supports_rgb(true);
// traits.set_supports_rgb_white_value(false);
// traits.set_supports_color_temperature(false);
return traits;
}
void write_state(LightState *state) override {
// This will be called by the light to get a new state to be written.
float red, green, blue;
// use any of the provided current_values methods
state->current_values_as_rgb(&red, &green, &blue);
// Convert to 0-255
int redValue, greenValue, blueValue;
redValue = floor(red * 255);
greenValue = floor(green * 255);
blueValue = floor(blue * 255);
bool ledState;
state->current_values_as_binary(&ledState);
float brightnessPercent;
state->current_values_as_brightness(&brightnessPercent);
// Convert to 0-100
int brightness = floor(brightnessPercent * 100);
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"switch\":\"%s\",\"light_type\":1,\"colorR\":%d,\"colorG\":%d,\"colorB\":%d,\"bright\":%d,\"mode\":%d"),
millis(), millis()%1000,
ledState ? "on" : "off",
redValue, greenValue, blueValue,
brightness,
SONOFF_L1_MODE_COLORFUL);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_COLORFUL_GRADIENT
void setModeGradient() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_COLORFUL_GRADIENT
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_COLORFUL_BREATH
void setModeBreath() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_COLORFUL_BREATH
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_RGB_GRADIENT
void setModeRGBGradient() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_RGB_GRADIENT
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_RGB_PULSE
void setModeRGBPulse() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_RGB_PULSE
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_RGB_BREATH
void setModeRGBBreath() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_RGB_BREATH
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_RGB_STROBE
void setModeRGBStrobe() {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_RGB_STROBE
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
// SONOFF_L1_MODE_SYNC_TO_MUSIC
void setModeSync(int sensitive = 10, int speed = 50) {
char buffer[140];
snprintf_P(buffer, sizeof(buffer), PSTR("AT+UPDATE=\"sequence\":\"%d%03d\",\"mode\":%d,\"sensitive\":%d,\"speed\":%d"),
millis(), millis()%1000,
SONOFF_L1_MODE_SYNC_TO_MUSIC,
sensitive,
speed
);
Serial.print(buffer);
Serial.write(0x1B);
Serial.flush();
}
};
@hans99
Copy link

hans99 commented Sep 6, 2020

Really good, thanks! Any chance you started working on the "effects"? (or am I just not understanding something?)

@emorydunn
Copy link
Author

emorydunn commented Sep 12, 2020

Really good, thanks! Any chance you started working on the "effects"? (or am I just not understanding something?)

As it happens, I have! Not all of them seem to work correctly, and there's probably a better way to implement them, but I'll put everything up.

@hans99
Copy link

hans99 commented Sep 13, 2020

Really good, thanks! Any chance you started working on the "effects"? (or am I just not understanding something?)

As it happens, I have! Not all of them seem to work correctly, and there's probably a better way to implement them, but I'll put everything up.

Great! Yes, there must be a better way but your working implementation is better than mine non-working ;)
I also see some problem using them as you state - so I will just use SW-effects, works fine for me.

@rickalm
Copy link

rickalm commented Sep 18, 2020

This is great, do you have any information on how to put the Spider Z (Sonoff L1) into programming mode other than mashing the chip pins to ground GPIO0 at PowerOn ?

I was going to try the Swim or Res pads when I get a working image

@emorydunn
Copy link
Author

emorydunn commented Sep 18, 2020

Unfortunately you have to short the pin on the chip to ground, it’s kind of a pain. One thing I did find is that I needed to keep the pin shorted for the entire flashing sequence.

There’s a bit more info on my blog post about it.

@ShonP40
Copy link

ShonP40 commented Apr 13, 2021

Hey, first of all, great work on this! But, will there be any updates in the future to add receiving support from the remote?

@emorydunn
Copy link
Author

emorydunn commented Apr 13, 2021

Thanks, I'm glad people are finding it useful! I don't use the remote, so I probably won't be making any updates.

@ShonP40
Copy link

ShonP40 commented Apr 13, 2021

I’ve used this in the past but then decided to see if Tasmota’s integration is any better.
Ended up sticking with Tasmota for a couple months.
Decided to move back today due to the fact that I can setup software effects easier.

@eucciferri
Copy link

eucciferri commented May 18, 2021

Thank you very much for sharing this code, Emory!! :)

@atomicus
Copy link

atomicus commented Jul 13, 2021

I'm having trouble compiling it with

config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:27:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml:28:24: error: could not convert '{light_out}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::light::LightOutput*>'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:36:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:41:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:46:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:51:28: error: expected type-specifier before 'SonoffL1'
Compiling .pioenvs/l1-strip/lib4d9/ESP8266WiFi/ESP8266WiFiAP.cpp.o
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:56:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:61:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:66:28: error: expected type-specifier before 'SonoffL1'
config/l1-strip.yaml: In lambda function:
config/l1-strip.yaml:29:3: warning: control reaches end of non-void function [-Wreturn-type]
Compiling .pioenvs/l1-strip/lib4d9/ESP8266WiFi/ESP8266WiFiGeneric.cpp.o
*** [.pioenvs/l1-strip/src/main.cpp.o] Error 1

Any ideas?

@emorydunn
Copy link
Author

emorydunn commented Jul 14, 2021

What version of ESPHome are you running? It's possible something broke in a recent update that I'm not using yet.

@atomicus
Copy link

atomicus commented Jul 14, 2021

1.19.4.

Not a problem for me to switch back to one you've used, compile and flash. Can you tell me what's the version you've used (more or less)

@atomicus
Copy link

atomicus commented Jul 14, 2021

Ok, so, downgrading did not help.

the problem was that sonoff_l1.h file that was copied to led_strip_1/src/sonoff_l1.h by esphome/platformio was empty. Not sure why, but I've filled it with correct contents and it worked.

@sirronb
Copy link

sirronb commented Aug 18, 2021

Hi Emory,
As of ESPHome Current version 2021.8.0 of today, the following errors are being reported when attempting to update devices with this version of ESPHome:

In file included from src/main.cpp:25:0:
src/sonoff_l1.h: In member function 'virtual esphome::light::LightTraits SonoffL1::get_traits()':
src/sonoff_l1.h:29:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_brightness'
traits.set_supports_brightness(true);
^
src/sonoff_l1.h:30:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_rgb'
traits.set_supports_rgb(true);
^
src/sonoff_l1.h:31:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_rgb_white_value'
traits.set_supports_rgb_white_value(false);
^
src/sonoff_l1.h:32:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_color_temperature'
traits.set_supports_color_temperature(false);
^
Compiling /data/led_strip_1/.pioenvs/led_strip_1/lib4d9/ESP8266WiFi/WiFiServerSecureBearSSL.cpp.o
*** [/data/led_strip_1/.pioenvs/led_strip_1/src/main.cpp.o] Error 1
========================= [FAILED] Took 18.90 seconds =========================

This website shows what the issue could be ...
https://esphome.io/api/classesphome_1_1light_1_1_light_traits.html

Will appreciate you looking into this when your time permits.
Many thanks. Keep well and be safe.
Rgds
sirronb

@sirronb
Copy link

sirronb commented Aug 18, 2021

Hey Emory,
Found the issue with the help from @JeffResc who did the changes for the D1-Dimmer.
This needs to be changed in the "sonoff_l1.h" code above...

LightTraits get_traits() override {
// return the traits this light supports
auto traits = LightTraits();
traits.set_supports_brightness(true);
traits.set_supports_rgb(true);
traits.set_supports_rgb_white_value(false);
traits.set_supports_color_temperature(false);
return traits;

to this ...

LightTraits get_traits() override {
// return the traits this light supports
auto traits = LightTraits();
traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
return traits;
}

Works perfectly from here onward.

Thx and go well and be safe.
Best rgds
Sirronb

@emorydunn
Copy link
Author

emorydunn commented Aug 19, 2021

@sirronb thanks so much for looking into that. I haven't updated yet, so I'll incorporate your changes if they're backwards compatible. It might be time to convert this to a proper repo, especially if there need to be multiple versions of the config.

@sirronb
Copy link

sirronb commented Aug 19, 2021

@emorydunn, Thanks for your response. I'd just like to add that the program compiled perfectly in ESPHome, but after uploading (ota) and testing it out on the L1, there was no response. So something has changed causing the sonoff L1 hardware not to respond as would be expected.
Hence, a new approach will have to be looked into.

@kancelott
Copy link

kancelott commented Aug 29, 2021

@sirronb

From the deprecated functions, it seems to be an RGB light. Maybe give the code below a try?

I ran into a similar problem with my (entirely different brand of) lights. An incorrect colour mode seemed to not allow my light to turn on at all. The other possible values are here: https://esphome.io/api/namespaceesphome_1_1light.html#ae15e8fc701ad0efd46c3dfeefa408c7a

LightTraits get_traits() override {
// return the traits this light supports
auto traits = LightTraits();
traits.set_supported_color_modes({light::ColorMode::RGB});
return traits;
}

@emorydunn
Copy link
Author

emorydunn commented Sep 4, 2021

I finally got around to updating ESPHome and testing this out. Looks like @kancelott's change does the trick.

@eucciferri
Copy link

eucciferri commented Sep 4, 2021

Thanks guys! It worked perfect for me too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment