April 07, 2019

Wifi controlled fan - Design

A word of warning: If you proceed with making this project yourself, you run the risk of electrocuting yourself and dying. Fan motors use high voltages inside. More than enough to give you a serious shock.

See my previous post on what I'm trying to do, and why I chose this kind of fan.

My prototype circuit uses a NodeMCU ESP8266 board, and a 4-way 3.3V relay board to control the fans. The relay board should be able to safely switch the AC voltage your fan uses. (240V in my case.)

The MCU connects to the 3.3V relay board. The inputs for each of the relays connect to one of the digital data pins on the MCU. When the MCU sets that pin to high, the relay turns on. When the MCU sets that pin to low, the relay turns off. The relays will act in place of the original switch on the fan. Instead of sending power to the appropriate motor winding by turning the knob on the fan, the MCU will turn on the relay that connects to that motor winding.

My fan also has a switch for a little motor that makes the louvres on the front of the fan rotate, so I use the 4th relay to turn this motor on or off.

The fan is designed to work with my home automation system. I use Home Assistant, which includes an MQTT message bus. The code will connect to my MQTT bus and listen for updates, changing the relays as appropriate. Home Assistant provides a fan component that can send messages on MQTT to appropriate topics depending on the features of your fan. My code is written to support a fan that has 3 speeds ("high", "medium" and "low"), that can turn on and off (obviously), and that can turn the "oscillate" motor on and off.

My fan uses a 4-way rotary switch. (One of those settings is off, where it doesn't connect to anything.) My circuit diagram above shows 3 switches. How this actually connects will depend on what kind of switch your fan has, but you could mock up something on a breadboard to prototype.

Some fans have push buttons, where pushing one button in pops out any other button that was previously pushed in. In this case there are generally 4 buttons, where 3 of them connect to the 3 motor windings and the 4th button connects to nothing (for off).

The theory seems sound, so I ordered some parts.

These things are cheap and commonly available, but for the sake of illustration I have used Ali Express links.

I wired this up on a breadboard to test. Note that in this picture the speed control switch wasn't connected, so no 1k resistors.

The relay board needs +3.3V and ground from the MCU. I then connected the input lines on the relay board to digital output pins on the MCU. (If you were using an MCU based on a 5V system, like an Arduino, you could use a relay board that needed 5V, which are also readily available.)



Note that although the ESP8266 module runs on 3.3V, the NodeMCU board has an onboard voltage regulator. You can power the board from up to 12V pretty safely. I used a 5V power supply, knowing that this would work fine.

While prototyping I just powered the board from the micro-USB connector connected to the PC.

My ESP8266 code is available under a GPL 3.0 license. You can see it here: https://github.com/eb3nezer/mqtt-fan

Check out the README.md in the code to see what configuration changes you might need to make, depending on how you wire up your circuit. It also describes how the code will set up as a WiFi access point initially to let you complete the rest of the configuration via your web browser.

This is designed to work with Home Assistant, and so you need to add the fan component to Home Assistant. The configuration looks like:

fan:
  - platform: mqtt
    state_topic: "ha/boxfan/on/state"
    command_topic: "ha/boxfan/on/set"
    oscillation_state_topic: "ha/boxfan/osc/state"
    oscillation_command_topic: "ha/boxfan/osc/set"
    speed_state_topic: "ha/boxfan/speed/state"
    speed_command_topic: "ha/boxfan/speed/set"
    name: Box Fan

Note that the topics you configure in Home Assistant should match the topics you set in the web UI for the fan's access point.

Once installed in Home Assistant the control looks like this;


If you don't include the oscillation topics, then Home Assistant won't give you a control to turn oscillate on/off.

Time to test. Power on turns on the "high" relay. Change the speed to medium turns off the "high" relay, and turns on the "medium" relay. Turn on the Oscillate switch, and the oscillate relay turns on. Power off turns off all relays.

Success! Time to cut up a fan!

No comments: