Sonoff iFan02 Controller (No Firmware change required)

Sonoff iFan02 in particular seems to be difficult and somewhat dangerous to flash, since it must be connect to mains power during the procedure. Although the steps below are not ideal for everyone, they at least offer a method for controlling the iFan02 module via HomeKit and if you have the ability to also create HomeKit automations (e.g. You have an iPad that supports the latest iOS or you have an Apple TV 4 or later) you can also control it with Hubitat. Hopefully at some point, someone will be able to port this Homebridge plugin to a standalone Node application and then write a driver for direct control from Hubitat.

Install and initial configuration of Homebridge

  1. Install Homebridge. There are hundreds of guides out there. It's not hard.

  2. Add the homebridg-ewelink plugin

    sudo npm -g install homebridge-ewelink

  3. Add homebridge-ewelink to your config.json file

SAMPLE Config.json file

{
    "bridge": {
        "name": "Homebridge",
        "username": "XX:XX:XX:XX:XX:XX",
        "port": 51826,
        "pin": "123-45-678"
    },
    
    "description": "Your description here",

    "accessories": [
    ],

    "platforms": [
        {
        "platform" : "eWeLink",
        "name" : "eWeLink",
        "authenticationToken" : "obtain-with-Charles",
        "apiHost" : "us-api.coolkit.cc:8080",
        "webSocketApi" : "us-pconnect3.coolkit.cc"
        }
    ]
}

Note: Use eu-pconnect3.coolkit.cc for the webSocketApi value if you are in Europe

  1. Follow the next steps below to obtain the authentication token from the EWeLink app. After you have the token value, return to the Config.json file and replace "obtain-with-Charles" with your unique authentication token.

Obtaining the EWeLink authentication token

In order for the Homebridge plugin to communicate with the EWeLink web socket, it is necessary to obtain the authentication token from the EWeLink iOS app.

  1. Install the EWeLink app on your iOS device
  • IMG_6336
  1. Ensure your Sonoff devices are registered and working with the native app. Keep the app logged into your account.

  2. Download and install Charles - Web Proxy Debugging Application on your computer.

  3. Start Charles . The application will run in trial mode for 30 days, with timed delays and a 30 minute timeout as an added and unnecessary annoyance.

  4. From the Proxy menu, choose SSL Proxy Settings...

  • 14%20PM
  1. Enter the following two locations into the SSL Proxy Settings

    a) us-ota.coolkit.cc or eu-ota.coolkit.cc if you are located in Europe. Use 8080 for the port number, but for now, after adding the location, you must then uncheck the box next to it so it remains disabled for now.

    b) Your computer's IP address. Use 8888 for the port number

EXAMPLE

  • 17%20PM
  1. In iOS, tap Settings > WiFi and tap the "i" to the right of the WiFi network that both your phone and computer are connected to.

  2. Scroll to the bottom and tap Configure Proxy

  3. Tap Manual and enter your computer's IP for the Server and 8888 for the port number.

  • IMG_6309
  1. Tap Save

  2. On your iOS device, open a browser and enter https://chls.pro/ssl in the address bar, but first ensure Charles is still running on your computer and is in the foreground. Demo mode times out after 30 minutes.

  3. You will be prompted to allow the website to open Settings. Tap Allow

  • IMG_6311
  1. Install Profile will open. Tap Install and enter your phone's passcode when prompted
  • IMG_6312

  • IMG_6313

  1. A warning message will be shown. Tap Install again
  • IMG_6314
  1. Tap Install again
  • IMG_6315
  1. Tap Done
  • IMG_6316
  1. Tap General > About and then scroll to the bottom and tap Certificate Trust Settings
  • IMG_6318
  1. Enable full trust for the root certificate you just installed.
  • IMG_6319
  1. If the EWeLink application is running, be sure to exit it.

  2. On your computer, again go to the Proxy menu, choose SSL Proxy Settings... and check the box to enable us-ota.coolkit.cc:8080 or eu-ota.coolkit.cc:8080 respective of your geographic location and the click OK

  • 25%20PM
  1. Launch the EWeLink application on your iOS device and on you computer, you should see https://us-ota.coolkit.cc:8080 or https://eu-ota.coolkit.cc:8080 respectively, appear in the column on the left of the Charles application.
    Note: If this is the first time you have used the application, you may be presented with a dialogue box asking you to allow the incoming data. You will need to OK the dialogue, then exit and restart the EWeLink app to get the data you need

  2. Click on either https://us-ota.coolkit.cc:8080 or https://eu-ota.coolkit.cc:8080 (depending on your location)

  3. Expand the otaother directory and then click on app (cn.itead.ota.queryinfo)

  • 39%20PM
  1. Select Contents at the top of the preview window and Headers at the bottom of the preview area. Then scroll down until you see Authorization
  • 54%20PM%20copy
  1. The alpha numeric value that follows the word Bearer is the authentication token used to securely communicate with the web socket.

EXAMPLE
f2bd4270af3e233a5bbc9873v32et2994c968771

  1. Return to your Config.json file and paste your authentication token in place of the words "obtain-with-Charles"

  2. Disable full trust for the Charles Proxy root certificate on your iOS device, but leave the Profile installed in case you need to repeat the process. The authentication token will always need to be captured again if you log out of the EweLink app.

  • IMG_768777BF452D-1
  1. On your Homebridge server, go to /usr/local/lib/node_modules/homebridge-ewelink and edit the file index.js

  2. Replace the entire contents of index.js with the content from this repository

  3. Find and replace the following in the index.js file

                           case 'PSF-B04-GL' :
                           switchesAmount = 2;
                           break;
                           case 'PSB-B04-GL' :
                           switchesAmount = 2;
                           break;
                           case 'PSF-A04-GL' :
                           switchesAmount = 4;
                           break;

replace with

                            case 'PSF-B04-GL' :
                            switchesAmount = 4;
                            break;
                        //  case 'PSB-B04-GL' :
                        //  switchesAmount = 2;
                        //  break;
                        //  case 'PSF-A04-GL' :
                        //  switchesAmount = 4;
                        //  break;
  1. Start Homebridge. It may give an error, and that's OK. If it does continue onto the next step. If you don't get an error, then continue onto Configuring HomeKit to control all of the Sonoff iFan02 functions. The first time I did this with a single Sonoff iFan 02, I could not start Homebridge with the "switchesAmount" value set to 4 without getting an error, but this added the switches in homebridge needed and everything functioned when I set the values back to 2. When I added a second iFan02, I set the value to 4 to get all the switches to show up in HomeKit, but now HomeKit runs with the value set to 4 and does not give an error. :face_with_raised_eyebrow:

  2. If you get an error when "switchesAmount" is set to 4, then stop Homebridge and edit the Index.js file again with the following changes, again making sure you've changed all 3 occurrences

                            case 'PSF-B04-GL' :
                            switchesAmount = 2;
                            break;
                        //  case 'PSB-B04-GL' :
                        //  switchesAmount = 2;
                        //  break;
                        //  case 'PSF-A04-GL' :
                        //  switchesAmount = 4;
                        //  break;
  1. Start homebridge once again, and it should not give an error this time. Now under Default Room in HomeKit, you'll have 4 buttons, one for the light and speeds 1-3.

Configuring HomeKit to control all of the Sonoff iFan02 functions

  1. One of the buttons in HomeKit under Default Room will be the light and will be labelled as CH 1 in HomeKit. Change its name in HomeKit to "Fan Light"

  2. Another button in Default Room will be labelled as CH 2 in HomeKit. Change it's name to "Fan ON" or "Level 1"

  3. Another one of the buttons will be named CH 3, when this button is tapped by itself, it does nothing, but when you tap "Fan ON" or "Level 1" at the same time, you will get fan level 2 and you can confirm this by watching the EWeLink app change. Change that button name to "Fan Level 2"

  4. Finally, the last button labelled CH 4 is for level 3 and acts the same as the level 2 button, in that you must have both it and the Level 1 button enabled at the same time to get level 3. Change that button name to "Fan Level 3"

  5. To get the Smart Speed function to activate, you simply turn on all three of the level buttons. Control sequence and timing is important for consistent operation. Follow the next steps to configure HomeKit automations and Hubitat triggered rules. Using these methods, you can turn the fan on to any level, and you can even include the fan light in your automations if you wish.

Controlling with Hubitat

The Sonoff iFan02 when controlled via Homebridge is very particular about the order in which switch combinations are enable and disabled. The module itself also has a built in delay on startup, which I believe is a relay that is designed to momentarily increase the fan speed to max, and then lower it to the desired level to prevent motor burnout. As a result, stable operation and accurate switching between levels requires delays due to the switch combinations required with this setup. For this reason, HomeKit scenes and automations of the switch sequences are not appropriate. Triggered rules are therefor used instead, with Momentary switches as the trigger. Fan OFF is handled with a separate triggered rule.

  1. Install the Homebridge Hubitat app and Homebridge Plug-in from @tonesto7

  2. Create three virtual switches for fan levels 1 through 3 - Example: Fan L1, Fan L2 and Fan L3

  3. Add the three virtual switches Fan L1, Fan L2 and Fan L3 to the Homebridge Hubitat app

  4. Stop and restart Homebridge

  5. In HomeKit, create automations to link the three virtual switches to their respective fan levels. When the virtual switch turns ON, the corresponding fan level in HomeKit should also turn on and when the virtual switch turns OFF, so too should the corresponding fan level in HomeKit.

EXAMPLE

  1. Create five virtual switches in Hubitat and configure each to turn off after 1 second - Example: Fan OFF, Fan Level 1, Fan Level 2, Fan Level 3, Fan Smart Speed.

  2. Create a triggered rule for OFF and each of the four levels available, using the following rules as a guide.

    NOTE: You may add longer delays, but testing has shown that removing or shortening delays leads to inconsistent operation when changing fan speeds.

Fan OFF
11%20PM

Fan Level 1
40%20PM

Fan Level 2
09%20PM

Fan Level 3
29%20PM

Fan Smart Speed
02%20PM

[Edit] Added additional info added to step 31 under Install and initial configuration of Homebridge. Please provide feedback if you have success with the "switchesAmount" value in the index.js file set to 4 permanently or did you have to change the value back to 2 in order to get Homebridge to run without error?

1 Like

Wow! Nice write-up!

But... for now... I think I'll stick with my Hampton Bay Zigbee Fan Controller. :slight_smile:

1 Like

Thanks. I'm just working through the control from Hubitat. Tricky. I haven't even gotten to the issues of fan speed others have reported and posted fixes for. It's a ways from usable at this stage. You're right to stay with the Hampton Bay at this stage.

1 Like

And, their cloud is down right now. A disadvantage of this method.

[Edit] Nope. It's my modem. Their cloud is working fine.

Main post updated with method for Hubitat control. The example rules shown in the main post were tested many times, switching back and forth between modes, and from OFF to ON for each mode.

Download the Hubitat app