Introduction: Remotely Shutdown or Restart a Computer With ESP8266 Device
To be clear here, we are shutting down YOUR computer, not someone else's computer.
The story goes like this:
A friend of mine on 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 worked 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 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-ohmpulldown resistors
- Jumper wires & a breadboard
You will also need to build at least one optoisolator. I usedblack shrink tubing, a bright white LED, a 220-ohm resistor, and a photoresistor. Solder the 220-ohm resistor to the LED's cathode and then sealed the LED and the photoresistor inside the shrink tubing facing each other. But we'll 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 with 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 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 perf board 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 cause huge pain to solder. And they were. I didn't have anything smaller than 5mm LEDs, so I had to stagger them. My final design made 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 board's top with shielded wires for simplicity.
For power, I wanted to run it from an old USB cable to 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 ran a few wires on the top side of the perf board, which 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 apiece 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 LEDto get the maximum light into the photoresistor. I didn't try many 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 CloudeMQTT 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 only use 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, ensuring that the case is correct. (seriously, 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 set up 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 will 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, they would be "cluster/rack-01/computer/01". It would be best to 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 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, 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.
Participated in the
Arduino Contest 2017
Participated in the
Remote Control Contest 2017
Question 5 years ago on Step 5
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?
Reply 4 years ago
I wanted to control 16 devices and the ESP8266 only has two usable IO pins (generally). The bit shift registers were used to expand the IO pins as just output pins.
If you were to use a NodeMCU device, you would have more IO pins available, still not 16 but more. You could drive it directly from the chips IO pins if you want.
I used the CAT-5 cable to connect the pins on the shift register with my optocouplers. It worked well because there are 8 pairs and I needed 8 pairs, but any wire would work.
5 years ago
Clever design. You could probably use this as a fun way to prank someone on april fools.
Reply 5 years ago
That was actually discussed.
Control only one computer makes it significantly easier and smaller. Could tuck it right inside the case and no one would be the wiser.
5 years ago
Wouldn't it be better to figure out why the computers lock up? That's not supposed to happen, and something is surely going on if that always happens at 3:00 am. Maybe there's a larger problem underhead, or the cleaning lady pulls the plug, or what.
Reply 5 years ago
haha - true!
These computers are running crypto-mining with a custom OS. We are investigating why they are hanging but this keeps us up and running for right now.
5 years ago
I like to know system theory of operation.
What are the 8-bit shift registers used for?
Working in IT I'd like to review the system event logs and see what you are interrupting at 3 am. Could be tape backups or updates or patch installs or virus scans or...
But cool stuff!
I like your opto isolator, totally commando!
Reply 5 years ago
Since the ESP01 only has a few output pins, the bit-shift register allows me to essentially create a bunch more.
The reason that the computers are hanging is because they are doing crypto-mining and using a custom OS. We are evaluating why they are hanging, but this gets us past that for now.