ESP32 WIFI/MQTT Based PC Power Control Prototype

Admir Cosic
5 min readMay 30, 2021

--

As all true IT nerds I have a smart home, it is a smart home system that i have invested a lot of hours into building. It is a mix of different techs and units and consists of 4 rpi’s and an unspecified number of different ESP8266/ESP32 units that turn things on or off, collect data and measurements such as temperature, humidity and other interesting information.

So when the Pandemic started since we could not go out as much I started several projects that I had been putting on hold for the last 2 years. One of those is building a workstation PC into my desk. Which is something i will document extensively and write a story about, but that is in the future.

Anyway during the planning of that project i realized one thing, “wouldn't it be really cool if i could integrate the ability to turn my workstation on or off info my smart home system? And then as an extra bonus I could integrate it with google home and turn it on/off with voice commands?”

The requirement's

The idea is actually very simple, use an ESP32 as an MQTT controlled device that if it receives a message on a certain topic trips a 5V relay, shorts the power pins on the motherboard and the PC powers on.

The basic requirement's are as follows:

  1. It must be controlled via MQTT so i can incorporate it into my smart home setup.
  2. It must also be able to be controlled via a simple web interface as a backup solution.
  3. It must support changing power state (ON/OFF)
  4. It must support doing a hard shutdown (Not that I have ever needed to do one with my workstation but still a good to have thing)

The hardware

The brain of the unit is a small ESP32 30 pin version which is pictured below.

ESP32 30-pin

Main features of ESP32
Following are the main features of an ESP32 :

  • It has onboard 18 Analog to digital converts ADCs. Each ADC is 12 bit SAR technology-based.
  • 2 digital to analog converts DACs.
  • It integrates 9 touch sensors.
  • For communication, it has 2 UART communications channels, 2 I2C communications interfaces, two I2S channels and one CAN communication interface.
  • It has 16 pulse width modulation channels.
  • It also has a cryptographic hardware acceleration module for various cryptographic algorithms like RSA, AES.

For our purposes we will use 3 GPIO pins that we will connect to the relay. One pin will be used to trigger the relay and the other two are the ground and 3.3V pins. which we will connect to the relay to power the magnets inside of it so they will open and close and thus short the pins that are on the motherboard.

As mentioned above to short the pins on the motherboard and trigger the computer to start we will use a simple 5V relay that the EPS 32 controls.

5V relay

So what is a relay?

The relay is simply like a switch that can be controlled from outside by electric current. It has an electro magnet inside. When you give it electricity, the magnet connects the key ends and then conducts electricity as pictured below.

How a relay functions

Assembly

Connect 3 jumper cables from the GPIO pins on the ESP32 to the pins on the right side of the relay as below.

  • D14 → S (Signal)
  • 3.3v → 5V Vcc
  • GND → GND

Please note you are free to change the D14 pin to which ever one you want, but also remember that you have to change the code that you will upload to the ESP32 to use that specific pin instead.

On the left side of the relay you should see three pins named “NC, “C” and “NO”. C is the pin that the current comes from. NC and NO are abbreviations for “Normally Closed” and “Normally Open”.

If you plug a cable into the NC port, no electric current is transmitted while the relay is not working. It is transmitted when the relay operates. NO has the opposite logic.

On my X99 Huananzhi motherboard the current normally flows in the power pins and to turn it on you short the pins and i think it should be the same on your motherboard so we will attach cables to the “NO” and “C” ports. And then we will splice the ends into the cable leading from the front power button to the motherboard pins.

If your motherboard goes on automatically when you are finished with the unit and have attached the cables to the motherboard then yours is most likely the opposite, do not worry you cannot fry your motherboard or something like that if you attach them wrong.

You should end up with something similar to the picture below when it is assembled.

Example of assembled unit during testing

The code

The code is very simple, it allows the user to control the unit both via a simple web interface and also via MQTT messages. Which makes it easy to integrate with your smart home system.

Complete functioning source code is available in my GitHub repo linked below. There are 3 versions of the code that I have made available.

  • WIFI (WIFI only version)
  • MQTT (MQTT only version)
  • WIFI_AND_MQTT (WIFI and MQTT version)

Clone it and use it as a base for your own project or alternatively just flash it to the ESP32 and use it as is. :)

Planned improvements

This is not the final version of the unit. It is more of a prototype currently. So there are some improvements planned the major ones are listed below.

  • Modify MQTT code so it publishes to the home assistant config topic so it gets automatically added to home assistant.
  • Create a case for the unit, currently I have it sitting in a small round plastic container which works but it really does need a case.

If you have any suggestion's of other improvements that would be cool, please write them in the comments below.

--

--

Admir Cosic
Admir Cosic

Written by Admir Cosic

I am a developer who is passionate about stuff like IOT and home automation. Few tech that I enjoy working in are .net core, C#, Typescript and Azure.