April 21, 2019

ePaper to display weather information


This project was to use an ePaper display to display weather information in my lounge room. Really it was an experiment to see what you could do with ePaper, because the ePaper module was an impulse buy while looking around Waveshare's web site.

The display module is a 4.2" monochrome display. Given I wanted this to connect by WiFi, it made sense to use their ESP8266 based driver board.

After connecting up the display to the driver, I powered it on to see the test mode. That seemed to work OK so I tried some code to display the current time.


You cannot really see from the photo, but the display is very thin. Thinner than LCD.

I had thought about designing and 3D printing a case for the display, but that's a bit above my Blender/OpenSCAD skill level. There are a couple of cool cases for similar boards on Thingiverse though.

In the end I repurposed a 6x4" photo frame. I cut a mask out of paper (just as a test), and it kind of looks OK.

The final version of the code has the time and date in the top left; the weather conditions in the centre left; the network status on the bottom left; and then the indoor and outdoor temperature on the right. You can see the source code (along with details on how it gets the temperature updates) at GitHubhttps://github.com/eb3nezer/mqtt_epaper

So of course now I have a photo frame with a micro-controller hanging out the back. I had a look around to see if anyone had designed a suitable case, but didn't see anything, so I ended up designing one myself.

I went through a few prototypes:


This is the final model:



The screw holes allow it to be mounted onto the stand of the photo frame:



If you are interested, the design for the case is available at https://www.thingiverse.com/thing:3576172

April 10, 2019

WiFi controlled fan - build

Warning: Home appliances have high voltages inside. Danger. Don't attempt a project like this unless you know what you're doing, and take appropriate safety precautions.

See also:
Inside of fan
In the picture above you can see the inside of the fan. Important components:
  • Rotary switch that used to control power to the motor windings (top left)
  • Capacitor to help motor start (black rectangle in top left)
  • Fan motor (behind the fan in the centre - the wires for the motor run up from the fan, obscured by the blade in this picture)
  • AC power cord (bottom)
  • 5V power supply (black rectangle in bottom left - 5V side not yet connected in this picture)
  • Relay board (top right)
  • NodeMCU board soldered to protoboard (bottom right - not yet connected to power supply or rotary switch)
Originally the capacitor was in the top right, but I needed to move it somewhere to make space.

Starter capacitor

This fan also had a mechanical timer switch in the top left. You could turn this, and it would supply power for (say) 30 minutes, and then disconnect the power.

Clockwork timer switch

I figured given this will be run by my home automation system I didn't need a clockwork timer any more, so maybe I could remove it, and move the capacitor there.

Out with the dremel tool. You know you're committed once you start chopping up appliances that were previously fully functional!


With the timer gone, now there was plenty of space to move the capacitor there. Fortunately the capacitor had a long-ish cable.

I needed to figure out how the existing wiring harness connected, plus I knew I would need to get AC power from somewhere. It was time to investigate what was behind these insulation bundles.


Removing the cable tie and the insulation revealed some crimp connectors.


Here brown is active and blue is neutral. I need to patch some wires in here to take AC power for my 5V power supply, so I'm going to need to take those connectors off.

I had a look in my tool box to see if I had any to replace them with, but I don't. Generally I don't trust crimp connectors, and so I solder them anyway. I added my wires to get an AC feed for my power supply, soldered the joins together, added heat shrink, and then put them back in their insulation bundle.

Then the other ends of those wires I added go to the AC in terminals of the 5V power supply.


Again covered with heat shrink, as I don't want any live wires hanging around. Make sure you don't solder to the wrong side of the power supply!

Here is the power supply back inside the fan.


It is loose for the moment, but I will hot glue it into the corner later.


Here you can see how the back of the rotary switch was connected. The connector on the bottom is the input (AC active), and the top 3 connectors are for the 3 different speeds. I tested which colour is connected for which speed so that I know how to wire up the relay outputs later.

The active wire previously connected first to the clockwork timer, and then to the rotary speed switch. With the timer removed I just joined the wires up in its place and put heat shrink over the join.


Now instead of the rotary speed switch, the active wire needs to go to the relay board to be switched on to each of the motor windings.


Here the active wire connects to the common terminal of the first relay, and then to the common terminals of each of the other relays.

The wires from the rotary switch to the motor were not quite long enough, so I needed to extend them, and then cover the join with heat shrink.


I had to buy a hair dryer for the heat shrink. I keep the hair dryer in the bathroom, but I should keep it in the tool cupboard, because I only ever use it for heat shrink.

Now to connect the motor wires to the relays.


Below the relays you can see the opt-isolators, diodes (to protect from back-current) and indicator LEDs.

The black wire goes to the oscillator motor, and then red for high speed, white for medium speed, and blue for low speed.

At this point all my AC wires are connected and insulated. (OK only mostly insulated. More on that later.)

Now it's time to connect up the low voltage logic wiring. I attached the NodeMCU board to some proto board. Although not necessary, I find this useful in terms of soldering.


Here you see red and black on the right to connect to +3.3V/0V for the relay board, and 4 wires for each of the logic level inputs to the board. (One each for oscillate, low, medium and high.)

Now to connect the other ends of the wires to the terminals on the relay board.


The relay board has screw terminals. The high voltage side was fine, but I kept having problems with the low voltage wires falling out. I ended up tinning the ends of the wires with solder, and this helped them stay in the screw terminals more securely.

I don't have the 5V power supply or the speed switch connected up yet, but it's probably worth doing a test to see that everything connects, and that I can still control the relays from inside Home Assistant. Prior to this I only tested the design on a breadboard.


While all my connectors are insulated, the underside of the relay board has exposed connectors from the through-hole components. Although I'm not connecting the AC here, I don't want any short circuits, so I improvised with some paper. (For the finished product I have some electrical insulation tape covering the under side of the relay board, with a layer of insulating foam covering that.)

At power on I get a power light on the relay board. So far so good.

The WiFi connect sequence for my code does a slow flash of the onboard LED while it connects to WiFi, and then quick flash while it logs in to MQTT. Once connected OK the LED goes off. All goes according to plan.

Now to test from Home Assistant. Power on, low power, and the low power relay clicks on! A test of the speed and oscillate combinations shows that the relays are working as expected!

Now to complete the wiring, I soldered wires to the 5V out of the power supply, and connected them to Vin and Ground on the MCU. Note that the NodeMCU board has an on-board voltage regulator, so a supply of more than 3.3V is OK for the Vin pins.

This is how the rotary switch looks.


The red wire goes to +3.3V, which connects to the terminal for high speed. Then a 1k resistor between high and medium, 1k between medium and low. There is actually another 1k resistor between the low terminal and the black wire, but it is hidden inside the heat shrink. I needed the voltage for low to be different to the voltage for off, so I didn't want low to connect directly to 0V. The orange wire connects to the "rotor" of the switch, which connects to the analog pin on the MCU.

Thus the resistors act as a voltage divider. The voltage on high speed is close to 3.3V, with less for medium, and less for low. I checked what reading the ESP8266 reads for each setting so that the code can determine where you positioned the switch, and if you changed it. Note that the ESP8266 itself wants a voltage between 0 and 1V for the analog pin, but the NodeMCU board lets you supply a voltage between 0 and 3.3V. This is handy, as the supply voltage is 3.3V.

You know what? I think I'm ready. Screw it all back together, for a test.

Be careful with AC wiring! We stood clear, turned on the power, and prepared for the release of magic smoke. With the smoke retained, I went to Home Assistant to try to get some airflow.


Thanks heaps to my assistant, and to the maker community at my workplace.

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!

WiFi controlled fan - Options

My quest (spoiler: ultimately successful) to smart-ify a pedestal or box fan.

Spoiler: The finished product

Overview

I have a Xiaomi smart socket that could turn my fan on or off at the wall, and to be honest that's quite useful, but I wanted more. I wanted to be able to control the speed remotely as well. Fans are not that complicated right? It should be easy enough to modify the internals of a fan to support this.

My list of requirements:

  • The solution must work with a fan that is domestically available, and ideally not too expensive.
  • I need to be able to turn it on and off remotely, as well as control the speed remotely.
  • It should integrate with Home Assistant.
  • Ideally the existing controls on the fan should also work, so it can be controlled manually or remotely.
The first phase of this project was to find out how the speeds of these fans are normally controlled, and how they might be controlled by a micro-controller.

Pulse Width Modulation (PWM) for DC

For a fan that uses a DC motor, PWM might be a good solution. I'm planning on some kind of ESP8266 module as the MCU, and a circuit using the ESP8266's PWM output to switch a suitable MOSFET should work nicely. (I have used this approach for LED lighting in the past.)

While I'm sure they do exist, I looked around for a cheap fan with a DC motor, but didn't find one. I suspect this is partly because fans with 3-speed AC motors are cheap, very common, and they do the job.

If I were to find one, I would still like to build this kind of control. Your maximum speed is when your PWM is at 100% duty cycle. You'd need to do some testing to find out what your minimum duty cycle is for the fan you have. Below a certain minimum and you won't have enough torque to get it started.

PWM for AC

Dimmer circuits often use PWM, but you can't just vary the duty cycle at the 3kHz that your MCU supports. You need to vary it in phase with the AC (50 or 60 Hz). It is possible to get triacs that can switch higher AC voltages (240V for me).

I did explore this option a bit, but there were a few show stoppers for me:
  • Motors that have windings that suit this kind of control are not commonly used in cheap desktop cooling fans. (Common in ceiling fans though.)
  • In order to vary the duty cycle in phase, you need a circuit to detect when the voltage crosses zero, so you know when to turn your triac on and off. This is not such a complex circuit, but there's a high voltage side to this circuit, so a bit of extra care is involved. The former point was more of a problem really.

An AC motor with distinct windings

These kinds of motors are very common. The fan has a concrete number of speeds (generally 3). Each speed has its own winding inside the motor. The switch (or knob) selects which winding to supply power to.

Fan Motor Windings: Gene Haynes http://waterheatertimer.org/How-to-wire-3-speed-fan-switch.html
What if I swap out the speed switch for a set of relays (controlled by the MCU)? The relays would switch power to the windings. Then if I wire up the original switch as a voltage divider, I can get the MCU to detect the switch position by measuring the voltage. Thus the MCU can change the speed if someone moves the switch.

Sounds like a plan.

February 24, 2018

Adding WiFi to an LED strip

A while ago I saw a 5m roll of RGBW LED strip lights at Aldi. The pack included the 5m of lights, along with a 12V power supply and an IR controller for effects.

My reading online told me that it should be relatively simple to connect up some MOSFET transistors to an ESP8266 module so that the different LED colours could be controlled independently. The ESP8266 has WiFi of course, so with the correct software this should integrate easily with my home automation system (Home Assistant).

This post documents my build.

The LED strip was a special buy at Aldi, but there are plenty of products like this available. e.g. This is an Arlec one. It is 3m and has only RGB, but the concept is similar.

I found this great page on Adafruit describing a suitable circuit. https://learn.adafruit.com/rgb-led-strips/usage

They suggest using IRLB8721 MOSFETs. Their circuit is for an Arduino which uses 5V logic, but they suggest that these transistors switch on at about 2.2V, so they are fine for the ESP8266's 3.3V logic.

To test this I bought a few and set up a test circuit on a breadboard.


My test circuit used only the red channel, and my code used PWM to change the value of one of the pins every couple of seconds. The red LEDs got brighter and dimmer as expected.

Initially I had included a 5V voltage regulator chip. The ESP8266 board I'm using works fine with 5V, but I didn't think it would like the 12V that the LED power supply put out. As it turns out, this board can actually work fine with up to about 20V (it has its own onboard voltage regulator), so my final design omitted the voltage regulator.

The next step was to build something more permanent. I ended up using some prototyping board.


I bought a 12V barrel connector at Jaycar that connects to the original power supply that came with the LED strip. It connects to the Vin on the ESP8622 board. The digital outputs from the ESP8266 connect to pin 1 of the MOSFETs (the "base"). I used red, green, blue and white wire to remind me of which channels these will go to on the LEDs. If you only have an RGB strip, then you don't need the white channel.

Here is the completed board.


The negative wire from the power supply goes to pin 3 of the MOSFETs (the "emitter"), and then pin 2 of the MOSFETs (the "collector") goes to the LED strip. I re-used one of the joining connectors that came with the LED strip to connect them to my prototyping board. The end that connects to the LED strip is also hot-glued so it won't come unclipped.

If I had thought about it a bit more I would have put some kind of connector in the middle. The LEDs are going to be mounted on the skirting board in my lounge room, so this means the circuit board has to be right next to them. Oh well.

My software is a post in itself (coming soon). There are a couple of great examples of code for ESP8266 that uses MQTT to do PWM control for LEDs. I was able to enhance someone else's code to get something that works well.

For now, here's the final product in use.


I have a bit of an overrun on the right, which I have now trimmed to size.

I am very happy with the finished product. It integrates well with my home automation system (letting me set colour and brightness), and it is extremely reliable.

If you are thinking of making something like this yourself, on top of the LEDs and power supply you'll need:
  • ESP8266 module. I paid about AU$5 for mine.
  • IRLB8721 MOSFETs. These were about $2 each. You need one per channel, so for a single colour LED strip you only need 1. For RGB you need 3. For RGBW you need 4.
  • A barrel connector to suit your power supply. About $2.
Note that some LED strips that you can buy come without power supplies. It is probably worth getting some that come with a suitable supply, as otherwise finding a cheap 12V supply with a suitable current output might be difficult.



March 15, 2017

Button Box for Home Assistant

Here I show you how to make a simple button box that can be used to control lights, set scenes, or other automation things connected to your Home Assistant system.

 


It works by detecting short or long presses of 4 momentary push buttons, and sending an appropriate message over MQTT. The software detects "short" and "long" presses of each button. If you press and release in less time than a "long" press, then it is "short".

You set up a corresponding automation rule in Home Assistant to take action based on the incoming MQTT message. You can have different rules for long and short presses of each button.

Why build this?

I have a Home Assistant system running controlling the lighting in my flat. While the web UI for Home Assistant is excellent, sometimes it's handy to just have a button to press.

I already had most of the components to build this project, and so ... why not?

Hardware

My project is based on a Freetronics EtherTen board. I used this because I had a few lying around. It's an Arduino compatible board with Wiznet W5100 chip. The same sketch would work with any similar board.

I purchased 4 momentary push buttons. Essentially I just wanted something with a definite action. I ended up with these red and black buttons from Jaycar.

Connections

The sketch is designed to use 4 digital inputs with the inbuilt pullup resistors enabled. The buttons are wired to connect the digital input to ground when the button is pressed. In the photo the black wire is connected to ground.


The other connection for the buttons goes to a free digital input. Partly I chose ones that were a little bit apart so that I didn't need to put heat shrink on the pins, and partly because this board uses a couple of the inputs to talk to the ethernet hardware.

I used some breadboard connecting wires. I cut off one end and soldered the wires to the buttons, and the other end goes into the sockets on the Arduino. The wires used to have black insulating plastic around the pins, but I removed it so that they fit in the case.


The connections look like this:



The only other connections are power (micro-USB) and the ethernet cable.

Software

The Arduino sketch is included here. The README shows which part of the code should be configured to match your situation.

Want more or less buttons? No problem. Just choose pins, and also a bit mask (powers of 2).

Of course you need to configure;
  • The address of your MQTT server
  • The login and password for your MQTT server
  • The base topic that you will send updates on (this should match your automation script in Home Assistant)
  • The topic names for each button. (I use b1, b2, r1, r2)
  • How long you need to hold the button for to be a "long" press. I chose 1 second.
  • The topic that Home Assistant sends status updates on. Initially I thought I might use discovery, so I was watching for the startup message, but for momentary buttons, discovery actually makes it harder to configure.
  • If your board doesn't assign a MAC address, then you need to make one up. If it is written on the board, then use the appropriate one.
After startup, the software tries to connect to the MQTT server repeatedly if disconnected. Then it watches for button presses, and sends a payload of either "short" or "long" to the appropriate topic when you press the button.

Automation rules

On the Home Assistant side, set up an automation rule listening to the appropriate topic and payload. Your rule can do whatever you think is appropriate.

I have my buttons configured this way:
  • Short press of black 1 turns on one of the lounge lights.
  • Long press of black 1 turns on all the lounge lights.
  • Short press of red 1 turns off all the lounge lights.
  • Long press of red 1 turns off all lights.
etc

The automation rule will end up looking like this in your configuration.yaml

automation:
  - alias: Turn lounge lights 1 on when black 1 short pressed
    trigger:
      platform: mqtt
      topic: myhome/buttons/b1
      payload: short
    action:
      service: light.turn_on
      entity_id: group.loungelights1
  - alias: Turn all lounge lights on when black 1 long pressed
    trigger:
      platform: mqtt
      topic: myhome/buttons/b1
      payload: long
    action:
      service: light.turn_on
      entity_id: group.loungelights
  - alias: Turn off all lights when red 1 long pressed
    trigger:
      platform: mqtt
      topic: mhome/buttons/r1
      payload: long
    action:
      service: light.turn_off
      entity_id: group.all_lights



September 24, 2016

Wall rack for laptops - an Ikea hack

The need:

At work we have WiFi everywhere. If you go to a meeting you take your laptop with you. If you go to do some work at someone else's desk on another floor, you take your laptop with you. If you need the bathroom, well you will probably take your laptop with you, but where do you put it?

The solution:

Enter the Hack Rack.



Given an idea, a 24 hour company hack-a-thon, a team of keen hackers, we set out to design, build and install 13 laptop racks around the office.

This is a modified Ikea Spontan magazine rack. I'll cover some of the steps we took to make it.

We started with the base Spontan.


The Spontan is made of metal (with an enamel coating). While it is designed for magazines and newspapers, it is quite strong, and can hold a laptop fairly easily. One of the problems though is that the metal surfaces may scratch the body of our precious laptops. We needed a padding solution.

We want padding on the "inside" of the rack, so the part that you can't see. This must extend down to where the bottom edge of the laptop will rest.

We also need padding on some of the front facing surfaces, as the laptop will rest against these as well.

Enter Reverse Garbage. Reverse Garbage gets industrial discards that are useful for re-purposing. Perfect.

My first find was a roll of high density foam that is self adhesive on one side.


This is the edge of a larger sheet (presumably). The roll I picked was about 15cm wide, and several metres long. The white part is removed to reveal the self adhesive backing. We cut pieces of this to size, and stuck it on the "inside" surfaces of the rack.


For the front facing surfaces my first thought was to find some kind of thick fabric. I ended up with 3 great purchases from Reverse Garbage.

The first was an offcut of a roll of "event carpet". They had 3 colours (blue, pink and green) of which I grabbed some blue. The roll was about 2m x 4m, which set me back $10.


Next was a book of fabric samples for chairs. I figured we could cut up each sheet into 2 strips, which would make an interesting design. Here's are a couple.

 

Third was a sheet of polyurethane. These look like offcuts from making thongs (flip-flops). They are quite thin (2-3mm) but provide enough padding so that a laptop won't scratch.


Attaching the covering to the front facing surfaces ended up being more of a challenge than I had expected. I researched glues that would stick to metal, and 2 part epoxies like Araldite seemed to fit the bill.


The problem of course is that with 13 racks to build and mount in 24 hours, mixing up and applying a 2 part epoxy is going to take a lot of time.

We experimented with hot glue, but it didn't stick well to the rack.

We ended up getting some sandpaper and roughing up the front facing surfaces that needed padding, and then using the hot glue. I wasn't totally satisfied with the bond, but there won't be a lot of lateral pressure, so it should stick pretty well. I think if any need re-sticking I will use Araldite.


Of course beware that hot glue sets fairly fast. You need to glue in sections.

Here's our first prototype with the green polyurethane on the front.

 

The chair covering ended up being more fiddly than expected. I was quite proud of the finished product, but we only made one.


In the end the blue "event carpet" was our favourite.


I thought mounting the racks might be a challenge. The rack weighs about 1kg. With 3 laptops attached the total weight could be 5-8kg. We needed to attach the racks to plasterboard (drywall). On the advice of my good friend Mark I investigated various attachment strategies on YouTube. It seemed like some kind of toggle screw would be the best.



The toggle sits sideways inside the anchor screw. You screw this into the wall. It has a sharp point and is self drilling. Then you place whatever you want to mount in place, and screw in the holding screw. As the holding screw goes in, the toggle flips sideways inside the wall, and tightens against the inside of the wall. Each screw can hold 20kg, so more than enough for what I need.

All that's needed now is some testing...


And a label so that people know what these strange racks on the walls outside the company bathrooms are for...


So how did we do? For our hack-a-thon we were awarded the non-technical prize! Thanks heaps to Alex, Malcom, Ricardo and Yuting, and especially for Alex and her video production work for the presentation!