IoT Bytes

Bits and Bytes of IoT

MQTT Based Shutdown Button for WD My Cloud

 

Pradeep Singh | 2nd April 2016

wdmycloud

WD My Cloud is a great NAS device from Western Digital with remote access capabilities. It allows you to save everything in one place and access it from anywhere with your PC, Mac, smartphone or tablet. As most people would like their data to be available 24×7, it makes more sense to always keep it on. Another reason for keeping it on, is that most NAS devices take long time to come online after boot process or will perform at a slower speed because of the initial indexing. Considering these points WD didn’t add a physical Power Off or Restart button to this device. However there are options for Shutdown and Restart on web management interface.

Now, not everybody want to use it 24×7, as there are people who want to use WD My Cloud as a secondary backup device and will turn it on once in a while. There are also some people who believe that keeping the Magnetic Disks always on will increase the chances of failure or will give more time to hackers to try and intrude into your system.

So here in this article, I am going to explain how to add an Internet enabled physical switch to your WD My Cloud. Before you start let me tell you it’s not a straight forward method. You must have some knowledge about Linux, ESP8266/NodeMCU and MQTT.

Before you begin –

Here is a quick video to show what are you going to get at the end of this article –

 

Prerequisites:

  1. WD My Cloud
  2. WiFi Network with Internet Connection
  3. ESP8266 with NodeMCU Firmware
  4. AMS1117 SOT-223 Voltage Regulator
  5. Capacitors (470µf (1), 220µf (1), 0.1µf (2)), Resistor (220 Ohm)
  6. USB Cable, LED, Tactile Push Button
  7. Female Berg strip, General Purpose PCB, Soldering Iron, Wires etc

 

This article is divided into 2 parts –

Part 1 will cover WD My Cloud device configuration to execute a python script which will listen to MQTT messages and perform the operation on the device.

Part 2 will cover MQTT Publisher/Initiator using a Mobile App or ESP8266 based physical button.


PART 1: Setup WD MY Cloud listen to MQTT Messages and make REST Calls

1. Login to WD My Cloud web interface using url http://wdmycloud.local/UI.  If your browser doesn’t resolve “wdmycloud.local” replace it with IP Address of your My Cloud device.

2. Navigate to “Settings –> Network” and enable “SSH”.

3. ssh to WD My Cloud using root account (Default password is “welc0me”) using Putty or similar tool.

4. Create a new folder named as “automation” and change directory using following commands –

mkdir /automation
cd /automation

5. Download Python PIP package using curl command –

curl https://bootstrap.pypa.io/get-pip.py > get-pip.py

6. Install PIP using following python command –

python get-pip.py

7. Install Paho MQTT Client using following following command –

pip install paho-mqtt

8. Install Python Requests module using following command –

pip install requests

9. Install xml.etree.ElementTree (only if required, should be available be default)

10. Download “wd_my_cloud_mqtt_listener.py” python code file from GitHub Octocatto your computer and change the values for variables USER_NAME, PASSWORD and MQTT_TOPIC. You may want to create a separate account for My Cloud web UI and assign that to user and password variables. Use a unique MQTT Topic (For Ex: “Country/State/City/Area/Street/HouseNo/SomeUniqueKeyword”). Following is the portion of code that needs to be changed –

##==================================================##
##---------------- SET VARIABLES -------------------##
##==================================================##
##--- Change following 3 variables ---
USER_NAME = "put UI User Name Here" 
PASSWORD = "put UI User Password Here"
MQTT_TOPIC = "put MQTT Topic here"

11. Upload this updated “wd_my_cloud_mqtt_listener.py” file to your share folder on WD My Cloud.

12. Go back to your ssh session (with root user) and copy this file from your share folder (check under /shares directory) to “/automation” directory which we created in step # 4 using linux “cp” command. For Ex (Do Not use this command as it is. Replace “pradeep” with your share name) –

cp /shares/pradeep/wd_my_cloud_mqtt_listener.py /automation/

13. Now create a cron job on WD My Cloud. Type following command in ssh shell to edit “crontab” –

sudo crontab -e

Go to end in this file and add following line –

@reboot sudo python /automation/wd_my_cloud_mqtt_listener.py &

Now reboot WD My Cloud with “sudo reboot” command and wait till My Cloud comes back. After reboot run following command to test if this script is running in the background –

ps aux | grep /automation/WD_My_Cloud_MQTT_Without_Print.py

This concludes first part of this article.


PART 2: Shutdown Initiator or Button

OK, you have just configured WD My Cloud to accept MQTT Message and perform a graceful shutdown using REST Calls. Now you can use any MQTT client to publish a shutdown message to MQTT topic (MQTT_TOPIC) you selected in Step # 10. You should send 0,1 or 2 for Shutdown, Restart or Unmount USB Drive respectively.

Now you can either use your Mobile Phone (using any MQTT App) or ESP8266 based physical switch to shutdown WD My Cloud NAS.

Option 1: Shutdown using Mobile App

You can use any MQTT Client App on your Android or IOS device and test this code. All you need to do with your MQTT Mobile App is to Subscribe to the Topic and Publish the Code. You can use following settings to shutdown WD My Cloud –

  • MQTT Broker: iot.eclipse.org
  • MQTT Port: 1883
  • MQTT Keep-Alive: 45
  • Topic: Subscribe to Topic you selected in Step #10 for MQTT_TOPIC variable in python code file.
  • Payload or Message: 0

*Use defaults for all other settings.

Option 2: Create a ESP8266 based Physical Switch

This is a little difficult option as you will have to make a ESP8266 module based circuit to send MQTT message on press of a button. Though this method will take some time and effort, but it is the most convenient option as this circuit can be placed near the WD My Cloud and can also be powered directly from My Cloud’s USB Port.

Follow following steps to make ESP8266 based Shutdown button for WD My Cloud NAS –

1. Follow this Link and flash ESP8266 with NodeMCU firmware.

2. Download the Lua code from GitHub Repository Octocat and change WIFI_SSID, WIFI_PASSWORD and MQTT_PUBLISH_TOPIC variables. Push this code into ESP8266 as explained in initial setup article (Refer Step 1).

This code must be saved on ESP8266 with “init.lua” file name. After booting, ESP8266/NodeMCU will attempt to run init.lua from flash memory.

Following is the code block which needs to be changed –

-----------------------------------------------
--- Variables Block ---
-----------------------------------------------
--- CHANGE FOLLOWING 3 VARIABLES ---
WIFI_SSID = "your wifi SSID"
WIFI_PASSWORD = "your wifi password"
MQTT_PUBLISH_TOPIC = "your MQTT Topic"

3. Now your ESP8266 is ready with Firmware. Connect it with 3.3v Power Supply and add a momentary push button between GPIO0 and Ground.

Following is a simple reference circuit diagram –

MY Cloud Shutdown Button

You can use AMS1117 based 3.3v Power Supply with this module and attach it to USB port of WD My Cloud NAS which can provide 5V power. Following schematic shows advanced version of this module with 5V to 3.3V Voltage Regulator and status LED –

shutdownButtonWithPowerSupply

This is how my module looks like –

IMG_0301

4 thoughts on “MQTT Based Shutdown Button for WD My Cloud

  1. Thanks for the great article! Going to give it a shot this weekend,, stumbled upon this looking for a way to send requests to My Cloud from outside my network to broadcast WOL magic packets on my LAN. A python script seems like the way to go, unless you have some other suggestion?

    Like

    1. I never tried WOL start with My Cloud. If WOL is supported by My Cloud’s NIC, you may add a compute board (RaspberyPi?) in your LAN to host Python script which can be invoked remotely using MQTT Protocol to generate WOL packet.

      If WOL is not supported by My Cloud, you may create a relay based switch with ESP8266. To power on My Cloud remotely, simply turn off the relay for couple of seconds and then turn it on.

      Like

  2. Hello Pradeep
    Very interested in your above article on the WD Cloud shutdown. As a retired engineer I would like to attempt this project but little experience on Linux, ESP8266/NodeMCU and MQTT. Where can I gain enough knowledge about these things in order to complete this on and perhaps get a bit more involved. Do i need a linux machine to do the coding or can it be done via windows?
    Many thanks

    Like

    1. Hello Stan Bruce,
      I am glad to hear that you are interested in this project. For this button, you may not need to do any coding as I have shared the code on GitHub (for links please refer to this article). Yes, you can use Windows machine to write your own code. Notepad++ would be a good starting point for writing smaller code modules on Windows. You may also have a look at my website (https://iotbytes.wordpress.com/) for information on ESP8266/NodeMCU and MQTT, or simply google for some tutorials. Let me know in case you need any help with this.
      Thank you.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: