DEV Community

Allen Tuh
Allen Tuh

Posted on

I hacked Sonoff RF Bridge to control my ceiling fan lights

I have the Home Assistant setup in my house. I installed 4 ceiling fan lights in my house. Each ceiling fan light is connected to a smart switch. However, only the light will turn on when I turn on the switch. To turn on the fan, I need to use the remote control. This isn't very pleasant for a person who likes conveniences such as me.

I must do something to satisfy my "laziness". Basically to be able to control the ceiling fan light, first, you need to find out the RF signals the remote control sends to the ceiling fan lights, then you need to be able to replicate these RF signals via other means or hardware. I came across a few solutions:

A. Duplicate the remote control circuit

This solution involves opening up my current remote controller to decode the RF signal used to control the fan, which is a NO-NO for me because my remote controller is sealed tight. I also do not have the resources to build a circuit board. However, if you are interested you can refer to this video tutorial.

B. Replace with Tuya WiFi smart remote control kit

This solution is more straightforward. You just need to buy a new remote control kit here. Tuya allows you to integrate directly with Home Assistant. Since my fans are new and I don't want to break my warranty, I will skip this solution.

C. Install Sonoff RF bridge

This solution is very challenging and has a lot of roadblocks. I was not aware of what I was getting myself into. Initially, I thought I just going to buy and install a Sonoff RF bridge and I am done. Boy, I was so wrong!

Journey to hack the Sonoff RF bridge

Sonoff RF bridge was only compatible with the Sonoff family of RF devices, so no luck with my ceiling fan lights. However, I came across several projects that hack the Sonoff RF bridge to work with more RF devices. These projects give me hope. I tried every single one of them and finally made one of them work with my ceiling fan lights. I will list down all the relevant projects, but will only emphasize the one that work for me.

  1. RFLink32 for Sonoff RF Bridge
  2. ESPurna
  3. Tasmota
  4. OpenMQTTGateway

OMG! It works!

After countless weeks of trying, I finally got OMG(OpenMQTTGateway) to work on the Sonoff RF bridge and control all my ceiling fan lights from Home Assistant. Here is the step-by-step guide:

Prerequisite

  1. Sonoff RF Bridge
  2. USB to serial adapter (for flashing firmware)
  3. Software-Defined Radio(SDR) (to decode the RF signal)
  4. Soldering kit
  5. Some programming knowledge

1. Hack the PCB board

If you bought a Sonoff RF bridge from the Internet, most likely it will be version R2 V2.2. If you have other versions, please refer here. Solder a jumper from the pin USBRXD to the target pin of the receiver chip and another one from the pin USBTXD to the target pin of the transmitter chip as shown in the photo below. I get the circuit modification from here.

Flip to the back side of the PCB and cut off the traces at the 2 points shown in the photo below.

2. Create the custom firmware

Obviously, we are going to load the OMG firmware. As of the moment only v0.9.16 works. Check out the v0.9.16 of the OMG repo from https://github.com/theengs/OpenMQTTGateway. OMG is developed with PlatformIO so setup your IDE accordingly (https://platformio.org/platformio-ide).
Create a prod_env.ini file to be able to connect to the house WiFi network and talk to the Home Assistant via MQTT protocol. Here is the example file I created.

[platformio]
default_envs = rfbridge-direct-1

[env:rfbridge-direct-1]
platform = ${com.esp8266_platform}
board = esp8285
lib_deps =
  ${com-esp.lib_deps}
  ${libraries.wifimanager8266}
  ${libraries.esppilight}
build_flags =
  ${com-esp.build_flags}
  '-DZgatewayPilight="Pilight"'
  '-DRF_RECEIVER_GPIO=4'
  '-DRF_EMITTER_GPIO=5'
  '-DLED_INFO=13'
  '-DLED_INFO_ON=0'
  '-DZsensorGPIOInput="GPIOInput"'
  '-DINPUT_GPIO=0'
  '-fcommon'
  '-DESPWifiManualSetup=true'
  '-DGateway_Name="OMGSonoff"'
  '-Dgw_password="password"'
  '-Dwifi_ssid="MYWIFI"'
  '-Dwifi_password="password"'
  '-DNetworkAdvancedSetup=true'
  '-DNET_IP="192.168.1.605"'
  '-DNET_MASK="255.255.255.0"'
  '-DNET_GW="192.168.1.254"'
  '-DBase_Topic="omgsonoff/"'
  '-DMQTT_USER="mqtt-user"'
  '-DMQTT_PASS="password"'
  '-DMQTT_SERVER="192.168.1.501"'
board_build.flash_mode = dout
board_build.ldscript = eagle.flash.1m64.ld ;this frees more space for firmware upload via OTA.
custom_description = RF gateway for the Sonoff RF Bridge requiring direct hack, relying on ESPilight library, [tutorial](https://1technophile.blogspot.com/2019/04/sonoff-rf-bridge-pilight-or-how-to.html).
custom_hardware = RFBridge v1
Enter fullscreen mode Exit fullscreen mode

Change the below settings accordingly:

  • Dwifi_ssid: your house's WiFi SSID
  • Dwifi_password: WiFi password
  • DNET_IP: Static IP for the Sonoff RF Bridge
  • DNET_MASK: Subnet mask
  • DNET_GW: Gateway IP
  • DMQTT_USER: MQTT user created in Home Assistant
  • DMQTT_PASS: MQTT user password
  • DMQTT_SERVER: MQTT server usually is the IP for your Home Assistant

After successfully building the OMG project, you will get the firmware file.

3. Flash the firmware

Sonoff is using the ESP8266 chip which makes flashing custom firmware possible. Solder 4 headers to the 4 programming pins on the PCB. Connect the serial adapter to the programming headers as shown below: Always ensure that the voltage setting on the adapter is 3.3V.

Download and install the ESPTool. Make a backup of the original firmware by running this command:

esptool.py --port /dev/cu.usbserial-21420 read_flash 0x00000 0x100000 esp-1MB-backup.bin
Enter fullscreen mode Exit fullscreen mode

Clear the firmware on the chip by running this command:

esptool.py erase_flash
Enter fullscreen mode Exit fullscreen mode

Flash the custom firmware to the chip by running this command:

esptool.py --chip esp8266 --port /dev/cu.usbserial-21420 write_flash --flash_size detect --flash_mode dout 0x00000 firmware.bin
Enter fullscreen mode Exit fullscreen mode

You can refer to this video tutorial. Plug in the Sonoff RF Bridge to the power supply and you should see the blue LED turn on.

4. Connect OMG to Home Assistant via MQTT

Install MQTT and Mosquitto broker add-on in Home Assistant. I will not go through the process here. Create the MQTT user in Home Assistant to allow OMG to connect to the Home Assistant with this user. The username and password MUST match the one we set in Step 2.

If everything is setup correctly, you will see this in the Mosquitto broker log.

5. Hack the ceiling fan light RF signal

Plug in the SDR into your Laptop and install Universal Radio Hacker (URH) software. Get your ceiling fan light remote control ready and record the RF signal with URH. Usually, the RF frequency is 433.92M.

Top comments (0)