Introduction: Remotely Shutdown or Restart a Computer With ESP8266 Device
Just to be clear here, we are shutting down YOUR computer, not someone else's computer.
The story goes like this:
A friend of mine Facebook messaged me and said he has a dozen computers running a bunch of math but every morning at 3 am they lock up. Since the computers are 30-minutes away, it's a huge nuisance to drive two towns over (we live in South Dakota) to power-cycle the computers. He asked, could I build him an IoT device that would allow him to restart the offending computer from the comfort of his cozy bed?
Never to miss out on a challenge, I agreed to put something together for him, this is that project.
Using two bit-shift registered, an ESP8266 ESP01, a handful of LEDs, and some homemade optoisolators, the entire project costs about $5 if you buy the parts from China on Ebay. Maybe $20 from Amazon.
This is a rather complex build with lots of fine soldering. Not counting my screw ups and re-soldering, it took me the better part of 20 hours to make but it turned out amazing and works perfectly.
Step 1: Prototype Board
Always start all projects with a prototype breadboard, it is the best way to determine if you have all of the components and they operate as expected. This project is a bit complicated so I highly recommend building it on a breadboard before moving forward.
The parts you will need are:
- One ESP8266 ESP01 (although any ESP8266 device would work)
- Two 8-bit shift registers, I used the 74HC595N
- 16 LEDs, I used straw hat white LEDs that operate at 3.3V. If you use others, you may need resistors.
- Three 3k3-ohm pulldown resistors
- Jumper wires & a breadboard
You will also need to build at least one optoisolator. I used black shrink tubing, a bright white LED, a 220-ohm resistor, and a photoresistor. Solder the 220-ohm resistor to the cathode of the LED and then seal the LED and the photoresistor inside the shrink tubing facing each other. But well get to those in a later step.
Follow the wiring diagram provided in the next step. The wiring is pretty straightforward.
Because the ESP8266 operates at 3.3V, make sure you power it appropriately.
Step 2: Schema Using Will-CAD
The schematic is pretty straightforward, we are following the standard wire-up of an 8-bit shift register. Since I'm using two 8-bit shift registers, they need to be chained together on their 'clock' and 'latch' pins.
Because the ESP01 only has two GPIO pins, we need to re-use the TX & RX as outputs, which works fine for our purposes. You could use an ESP-12 or another version that has more than two GPIO pins if you want more control. But that will add another $2 to the cost of the project - which is just crazy-talk.
We need to keep our 8-bit shift registers and ESP01 pins pulled high on boot so they don't do weird things or go into program mode. I used three 3k3 resistors, larger or smaller values would work as well. This value was derived from the guides talking about leveraging alternative pins on ESP01.
- TX --> clock pin --> 3k3 pullup
- RX --> latch pin --> 3k3 pullup
- 00 --> serial data --> 3k3 pullup
- 02 --> floating
8-bit Shift Register (74HC595H)
- VCC --> 3.3V
- OE --> 3.3V (this is the enable pin)
- GND --> GND
- CLR --> GND (this is keeps the clear pin from clearing)
- And the LEDs, those go to ground
Step 3: ESP8266 Code
The ESP8266 code is pretty straightforward. Unfortunately, the editor in Instructables is pretty useless, so you'll want to get the code directly from Github.
"SensorBase" class, is available here, it is required if you want to "use" my code:
There are a few things to note. The code is pretty well documented.
- I am a very lazy developer, so I put all of the reusable ESP8266 code into a class called "SensorBase". You can find that on Github too, link above.
- You must type in your MQTT broker's server, username, password, and port. Those can be found a little further down when we create the CloudMQTT service.
- You do NOT need to follow my topic syntax format, however, I would recommend following it.
- There is nothing clever about this code, it is very pragmatic.
Step 4: Perfboard Layout
This project will be installed at a mini-datacenter so I decided to just use perfboard for the final design. Perfboard works great for projects like this and is easy to layout using a piece of custom graph paper. Here you will see my layout, of course, you may choose to do it differently.
My project needed two 8-bit shift registers, so I started with their positioning in the middle. I knew that my connectors to optoisolators would be simple female headers for now, even though that is not an ideal solution.
I love LEDs, and this neededto have an LED for each optoisolator circuit. I knew the test phase would be infinitely easier if I could get instant feedback directly on the board but I also knew that those LEDs would huge pain to solder. And they where. I didn't have anything smaller than 5mm LEDs so I had to stagger them. My final design did a zig-zag pattern of the cathodes because I didn't want to run the anodes over the ground wires. This proved to be a good design. The LED wires join up above the 8-bit shift registers and run on the top of the board with shielded wires for simplicity.
For power, I wanted to run it from an old USB cable so it can be powered directly from one of the computers. This will work fine because the USB ports are typically powered even if the computer is off. I used an LM317 linear voltage regulator to reduce the power to 3.3V. A 3.3V regulator would have worked as well, but I didn't have one.
To avoid too many wires crossing I did run a few wires on the top side of the perfboard, something that I try to avoid. Keep in mind that the through-holes are conductive, so use shielded wires to avoid shorts. Those connections taking place on the top of the board are shown in dashed lines on my diagram.
Step 5: Soldered Board
My final soldered board turned out really well. As expected, the LEDs on the top took a lot of work to get soldered correctly without any shorts. After you have soldered the LEDs and headers, using your multimeter to determine if you have any shorts. It is best to find out now.
Other than the LEDs, everything else went pretty well. I did have to redo a few connections but with some patients, some debugging, and a little re-soldering, everything will work fine.
You'll see from this photo that I have hooked up the optoisolators as well, which I used 8-wire CAT-5 cable. The reason is that it is super-cheap, easy to splice, and is well marked. More about those optoisolators in the next step.
Step 6: Making Optoisolators
Of course, you do not need to make your own optoisolators, many commercial versions are available for pennies a piece and would operate better since they would drive the computer's power lines directly without any resistance. But, I didn't have any optoisolator so I had to make mine using an LED, resistor, and photoresistor.
After confirming that within a sleeve of black shrink tubing, the "off" resistance with less than my meter could read and the "on" resistance was a few thousand ohms, I did a final test on an old motherboard. It worked perfectly for me. I suspect some computers may be more or less sensitive but on the motherboards that I have tested, this configuration worked fine.
You will want to use a really bright white LED to get the maximum light into the photoresistor. I didn't try a lot of options, but the bright white LED and a 220-ohm resistor definitely work well.
Step 7: CloudMQTT Setup
Any MQTT service, or similar IOT service like Blynk, would work but I choose to use CloudMQTTfor this project. I have used CloudeMQTTT for many projects in the past and since this project will be handed over to a friend, it makes sense to create a new account that can also be transferred.
Create a CloudMQTT account and then create a new "instance", choose the "Cute Cat" size since we are only using it for control, no logging. CloudMQTT will provide you with a server name, username, password, and port number. (Note that the port number is not the standard MQTT port). Transfer all of these values into your ESP8266 code in the corresponding locations, making sure that the case is correct. (seriously, just copy/paste the values)
You can use the "Websocket UI" panel on CloudMQTT to see your device's connections, button pushes, and, in the odd scenario that you get an error, an error message.
You WILL need these settings when configuring the Android MQTT client as well, so note the values if you need to. Hopefully, your password is not too complicated to type in on your phone, you cannot set that in CloudMQTT.
Step 8: MQTT Android Client
Any Android (or iPhone) MQTT client would work but I like MQTT Dash. MQTT Dash is easy to use, very responsive and has all of the options that you'll need.
Once installed, set up an MQTT Server, populate the server, port, username, and password with your instance's values, NOT your login information to CloudMQTT. You can use any client name that you want.
If you typed everything correctly, it will automatically connect to your MQTT server and show you a blank screen, since you have not setup any buttons, text, or messages yet. On the blank screen, you see a "+" in the top right corner, click it, and then select "Select/Button". We are going to add one "Select/Button" per computer, so 8 or 16 or less.
If you DID get a connection error, you have one of the values wrong. Go back and double check.
Each computer will use the topic that corresponds to the values specified in your code, if you followed my conventions, then they will be "cluster/rack-01/computer/01". It is critical that you change the "on" and "off" values to match our code, instead of "0" and "1", use the values "on" and "off" respectively. I would also recommend using QOS(1) since we will be expecting a confirmation from the server.
After adding one, you can long-press and use the "clone" option to create a bunch, then just change their name and topic.
Step 9: Getting Your ESP8266 on Wifi
Using the ESP8266 Wifi Managermodule, it is a breeze to get our device on Wifi. If you used my SensorBase class, it is already built in. If not, simply follow the instructions on the Wifi Manager's page.
Wifi Manager will attempt to connect to your SSID on boot, which it cannot since you've never told it your SSID, so it will automatically go into access point mode (or AP Mode) and serve up a simple web page asking for your SSID & Password. Using your phone or laptop, connect the newly-available wireless network with the SSID name "ESP_xxxxxx" where the "xxxxxx" is some random (not really random) sequence. (Full instructions can be found on the Wifi Manager page.)
Once connected, bring up your web browser and point it to 192.168.4.1, type in your SSID & Password, and click save.
You are now on the internet and your IoT device has the "I" part working!
Step 10: Final Connection & Testing
To hook everything up, locate your computer's power button cable where it meets the motherboard. You should see two rows of headers with a bunch of wires and connectors. Typically, they are labeled pretty well. Unplug your switch and plug up the optoisolator plug. I put some "Dupont" plugs on mine, so they plugged in just like the power cable. Polarity on this end doesn't matter but make sure you have the polarity of the other end correct -- the one going to your custom board.
And it works perfectly. Using the MQTT Dash client (or similar tool), you can remotely power your computers.
Press the corresponding check button on your app and once the app hears back from the MQTT server with the "off" message, the button will change back to unchecked.
This has been running for a few weeks without any issues. We did notice that the duration of time to pull the button low on the computers needed to be extended, we ended up with 1 full second. This value can be exposed as a settable value through the MQTT server or you can hardwire the value, depending on your desire.
Good luck and let me know how yours turned out.
We have a be nice policy.
Please be positive and constructive.
How did you use the 8-wire CAT-5 cable? and can it be directly used on the GPIO pins of ESP8266 if we do not require any LEDs?