Sonoff Ifan04 - ESPHome working code

I saw a number of people here had trouble getting the iFan04 into flash mode, how did you solve it? I’ve tried different USB cables, installing all recommend drivers for ESP and the adapter I’m using, but no luck.

When I plug just the adapter into my PC it recognizes it, but as soon as I try to connect the iFan04 nothing comes up. The LED comes on and it starts going into pairing mode if I just connect it to the adapter and plug it into my PC, but nothing happens when trying to hold the power button while giving it power.

Any ideas?

I had no issues doing it - but did have to hold the button on the ifan04 when plugging in the usb to the computer.

Ya that’s what I’ve been doing. Was there any LED or sound indication that it was in flashing mode?

Nope - I usually test it’s in the right mode by dumping the firmware first. I have had some challenges identifying the device on the computer.

From that single frontside picture the USB TTL device doesn’t have a regulator on it which means you might have missed that it is basically a requirement for this board. Sonoff Ifan04 - ESPHome working code - #61 by NonaSuomy

i have used that same adapter to flash all three of my boards and didn’t have any issues.

Registered here just to thank you for this!

It finally worked after spending the entire afternoon trying to flash it. I tried with a Prolific TTL and later switched to a Arduino UNO as an adapter, both with an external bench power supply. In the end the problem was forgetting to hv the 3 grounds together.

TY

Did you ever try this? Any luck?

Nope, seems they made an updated version of it https://www.tindie.com/products/bugrovs2012/i2c-mosfet-trailing-edge-ac-dimmer-light/
but here is some future depreciated code using a custom component if you have one and want to give it a go. This is for their 4 channel, possible that it is the same to control the two channel or maybe contact them and see if they will make a proper component to make it work. Krida i2c dimmer by dcsim0n · Pull Request #5773 · esphome/esphome · GitHub

4channel_krida.h

#include "esphome.h"
using namespace esphome;

class MyCustomFloatOutput : public Component, public FloatOutput {
  public:

    int i2c_addr;
    const int ch_addr[4] = {128, 129, 130, 131}; //AC dimmer channels
    int channel; // from 0 to -3
    int error;
    bool debug;

  void setup() override {

    debug = 0; // set debug off/on
    i2c_addr = 39; // set i2c address

    Wire.begin();
    Wire.beginTransmission(i2c_addr);
    for (int i = 0; i < 4; i++){ // loop through all channels
        Wire.write( ch_addr[i] );
        Wire.write(100); //off
    }
    error = Wire.endTransmission();
    if (debug == 1){log_err(error);} //debug

  }

  void write_state(float state) override {   // state is the amount this output should be on, from 0.0 to 1.0
    
    int value = state * 1024; // convert it to an integer first
    value = map(value, 0, 1024, 100, 0); //convert to 0-100%
  
    Wire.beginTransmission(i2c_addr);
    Wire.write( ch_addr[channel] );
    Wire.write(value);
    Wire.endTransmission();
    error = Wire.endTransmission();
    if (debug == 1){log_err(error);}
  }

    void log_err(int error){
    if (error==0){ ESP_LOGD ("custom", "I2C Transmissio: success");} // 0: success.
    else if (error==1){ ESP_LOGD ("custom", "I2C Transmissio: data too long to fit in transmit buffer");} // 1: data too long to fit in transmit buffer.
    else if (error==2){ ESP_LOGD("custom", "I2C Transmissio: received NACK on transmit of address");} // 2: received NACK on transmit of address.
    else if (error==3){ ESP_LOGD("custom", "I2C Transmissio: received NACK on transmit of data");} // 3: received NACK on transmit of data.
    else if (error==4){ ESP_LOGD("custom", "I2C Transmissio: other error");} // 4: other error.
    else if (error==5){ ESP_LOGD("custom", "I2C Transmissio: timeout");} // 5: timeout
    }

};

krida_4CH_AC_Dimmer.yaml

esphome:
  name: test-4ch
  includes:
	#path to your library
    - "/config/esphome/libraries/4channel_krida.h"
esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "xxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Name"
    password: "xxxxxxxxxxxxx"

captive_portal:

i2c:
    sda: D2
    scl: D1
    #scan: true

output:
- platform: custom
  type: float
  lambda: |-
    auto my_custom_float_output = new MyCustomFloatOutput();
    App.register_component(my_custom_float_output);
    my_custom_float_output->channel = 0;
    return {my_custom_float_output};
  outputs:
    id: ch_1

- platform: custom
  type: float
  lambda: |-
    auto my_custom_float_output = new MyCustomFloatOutput();
    App.register_component(my_custom_float_output);
    my_custom_float_output->channel = 1;
    return {my_custom_float_output};
  outputs:
    id: ch_2

- platform: custom
  type: float
  lambda: |-
    auto my_custom_float_output = new MyCustomFloatOutput();
    App.register_component(my_custom_float_output);
    my_custom_float_output->channel = 2;
    return {my_custom_float_output};
  outputs:
    id: ch_3
    
- platform: custom
  type: float
  lambda: |-
    auto my_custom_float_output = new MyCustomFloatOutput();
    App.register_component(my_custom_float_output);
    my_custom_float_output->channel = 3;
    return {my_custom_float_output};
  outputs:
    id: ch_4
    
    
fan:
  - platform: speed
    output: ch_1
    name: "4ch 1"
    
  - platform: speed
    output: ch_2
    name: "4ch 2"
    
  - platform: speed
    output: ch_3
    name: "4ch 3"
    
  - platform: speed
    output: ch_4
    name: "4ch 4"  

Looks like someone was using that to control a fan as well…

I later built some Analog led strip lights and was going to modify the fan lamp shroud to contain those instead.

Follow my later I2C stuff where I found the right I2C pins on the back of the board for connection details.

Hi everybody!
did somebody create a esphome custom component for the I2C MOSFET dimmer from krida?

that’s the arduino code:

would be nice to control the device with esp32 as I2C master

unfortunately no info from seller. little more guides and support couldn’t do so much harm :wink: especially regarding home assistant & esphome known as really very popular, more or less nothing to find …

@haydon So my Ifan04 has been functioning great with esphome since completing the process describe above here. And I’ve even got it managed through the esphome addon in HA now. So all good there.

I’m encountering an error when trying to use the device in HA Scenes, and wondered if you might have any suggestions. When I try to activate a Scene which has the Ifan04 fan set to a speed greater than 0 (i.e. on vs. off), the following error is thrown:

Preset mode is not valid, valid preset modes are: .

This wasn’t happening when I first set the device up in scenes and tested them. But it is happening now, and so the fan doesn’t run with the other fans I’m trying to synchronize it to.

Any thoughts on how I might further troubleshoot this?

I actually bought the one above previously for another project and have been unsuccessful with it. The regulator and CH340 is a key note thanks! I’d love to buy from Amazon instead of Ali to avoid the very long wait. I think this one has the regulator. Would someone be willing to confirm or deny my intuition here? Amazon.com: DSD TECH SH-U07B USB to TTL Adatper with CH340C Chip (2PCS) : Electronics

https://www.amazon.com/gp/product/B00IJXZQ7C/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

This is the one I used for all my flashing. Confirmed it works for:
sonoff s31 / s31 lite
sonoff ifan04 (SONOFF iFan04-L WiFi Ceiling Fan Light Controller, APP Control & Remote Control, Work with Alexa & Google Home, No Hub Required(2.4G WiFi) (1 Pack): Amazon.com: Tools & Home Improvement) (fixed didn’t realize this was the L variety)
Levoit 300s air purifier.

Hope this helps!

Awesome, thank you! I have the ifan04-L (previously ordered the exact link you sent) is there any version differences I should take into account before giving this a shot?

I don’t believe so - it worked first time for me. Note I do not use the remote so can’t comment on that.

1 Like

My USB - TLL adapter came in and I took your Yaml and updated it with my own information. When compiling I get this error. Has anyone seen it before?

Have you confirmed the yank is valid?

This is what kept if from being valid. I currently have it commented out and it validated and loaded fine. I noticed it was from the section that translates the remote actions back to HA so I figured I could go without it for a while. For some reason it was hanging up when others seemed to get it easily (IE you haha)

sorry what i mean is run your yaml file through a yaml validator

Yep, I validated it. It checks out when I comment out the bottom section.