A few weeks back I discovered that the stepper motors on my 3D printer were warm to the touch, even when not in use. I asked on the facebook group and learned that the motors are kept powered to prevent the extruder from being accidentally moved. This doesn't work for me, as since the printer is on a different floor and operated remotely, I like to leave it powered on. But I didn't like the idea of having the motors powered. So I decided to look for a remotely controllable power switch.
My first idea was to build one. I already had an always-on Raspberry Pi controlling the 3D printer, and you can buy high current relay modules and integrate them to the Pi. A couple of people voiced their concerns over this however, as a wiring mistake would lead to high voltage frying the Raspberry Pi. But then I came across the itead Sonoff Wifi Controlled Power switch.
I just cut the power cord and wire the Sonoff to the AC Live and Neutral wires in between the supply and the device being powered (Note: The earth wire needs to be run around it, as the Sonoff doesn't have a pass through connector for it). The Sonoff connects to your WiFi network, and lets you switch on and off the power to the device from an android app. And the best part, it's only USD5.85 !
Since this was a neater solution (no wires to the Pi) I ordered two units (one for the 3D printer, and another to play with). It supports up to 10A of current, which from other people's measurements is more than enough for my 3D printer.
I spent about 10 minutes trying to get the eWeLink software android app to pair with the device. It requires registration on a cloud based app (I guess so you can control it outside of your network). I wasn't to happy about letting an external party have control over my power switch. In the end though, I didn't bother with it as I found an "alternative" firmware for the Sonoff, called Sonoff-Tasmota, written by Theo Arends. This adds a webserver on the sonoff itself, so you can just browse to it from pc or mobile device (no app required) and turn the device on or off.
It doesn't stop there, as the Sonoff-Tasmota firmware also adds support for MQTT, a messaging protocol that lets you connect to a IOT message broker. This allows the Sonoff to notify other devices and systems on it's current state (on or off) and also be controlled by the other systems.
I installed the Mosquitto message broker on my Raspberry Pi, and with a few scripts I was able to add a power control option to my 3D Printer's Octoprint menu.
Oh, and if you're wondering, there's also a little button on the Sonoff itself, that can be used to manually toggle the power on or off.
Here it is in action:
Long Story - Build Notes
Here's the big picture of what how it all connects up.
To install the Sonoff-Tasmota firmware on the Sonoff device requires getting ones hands dirty.
First of all, header pins have to be soldered on the Sonoff board (thank you Itead for making this possible). This exposes the TTL Serial port of the controller. An FTDI USB to TTL Serial dongle is then connected to the header pins as below. It's important to set the FTDI dongle to use 3.3V as 5V will fry the chip on the Sonoff. Also, you should not have any AC wires or devices connected at this point in time:
To install the firmware, you'll need to first install the arduino sketch IDE:
- Browse to https://www.arduino.cc/en/Main/Software
- Under "Download the Arduino IDE" choose "Linux 64 bits" (my desktop PC runs Ubuntu) followed by "Just download"
- In a terminal:
- cd /usr/local
- tar xvf ~/Downloads/arduino-1.8.2-linux64.tar.xz
- ln -s arduino-1.8.2 arduino
- cd arduino-1.8.2
- ./install.sh (this doesn't seem to work for me)
- Download desktop icon
- wget -O /usr/local/arduino/arduino.png "http://www.freeiconspng.com/uploads/arduino-icon-2.png"
- Create the following content in the file ~/.local/share/applications/arduino-sketch.desktop
- [Desktop Entry]
- Set permissions in a terminal:
- chmod 755 ~/.local/share/applications/arduino-sketch.desktop
- Open ~/.local/share/applications and double click the Arduino Sketch icon
- Right click on the icon in the Ubuntu launcher and select "Lock to Launcher"
Now you can launch the Arduino Sketch IDE from the icon in your Launcher. Next we have to teach the Arduino Sketch IDE about the ESP8266 chip, which is the CPU at the heart of the Sonoff.
- Launch Arduino Sketch
- Under File -> Preferences -> Settings
- Enter "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into Additional Board Manager URLs
- If there are existing URLs there, separate them with commas.
- Press OK
- Tools -> Boards -> Boards Manager
- Scroll down and click on "ESP8266 by ESP8266 Community"
- Install button appears, choose latest version and press button.
- Click Close.
Finally, clone the Sonoff-Tasmota firmware from the github repository. In a terminal:
- cd ~/src
- cd Sonoff-Tasmota
- cp -r sonoff /usr/local/arduino
- mkdir -p /usr/local/arduino/portable/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/ld
- cp "./arduino/version 2.3.0/tools/sdk/ld/eagle.flash.1m0.ld" /usr/local/arduino/portable/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/ld
- cp arduino/version\ 2.3.0/boards.txt /usr/local/arduino/portable/packages/esp8266/hardware/esp8266/2.3.0/
- cp -r lib/* /usr/local/arduino/libraries/
- For OTA Support, you need php webserver and the OTA web page
- sudo apt-get install apache2 php libapache2-mod-php
- sudo cp -r api /var/www/html/
- sudo service apache2 reload
- To push OTA file from IDE to web server:
- cp arduino/espupload.py /usr/local/arduino/portable/packages/esp8266/hardware/esp8266/2.3.0/tools/
- Set host:
- sed -i -e 's/HOST_ADDR.*/HOST_ADDR = "127.0.0.1"/' /usr/local/arduino/portable/packages/esp8266/hardware/esp8266/2.3.0/tools/espupload.py
Although you can configure the Wifi APN into the firmware later, it's easier to just code it into the firmware as the default value:
- In Arduino Sketch, open the file user_config.h
- Change the values for STA_SSID1 and STA_PASS1 to your Wifi APN.
- If you have a second Wifi APN, set it in STA_SSID2 and STA_PASS2.
- File -> Save
Now we are ready to build the firmware:
- Launch the Arduino Sketch
- File -> Open -> /home/shahada/src/Sonoff-Tasmota/sonoff/sonoff.ino
- Tools -> Boards -> Generic ESP8266 Module
- Tools -> Flash Size -> 1M (No SPIFFS)
- Sketch -> Verify and Compile
To flash in the firmware:
- make sure there is no AC connected to the Sonoff
- Disconnect the VCC (blue) wire to the Sonoff
- Connect your FTDI adapter to the PC. It should light up.
- Select Tools -> Port -> /dev/ttyUSB0 (or wherever your FTDI serial port maps to)
- Hold down the button on the Sonoff and connect back the VCC (blue) wire. This will power on the Sonoff and put it in firmware flashing mode.
- Sketch -> Upload
- The console will show the firmware being uploaded
- The rx and tx lights on the FTDI will blink rapidly
- Once it has reached 100%, wait a second or two, and disconnect the USB cable to the FTDI.
- Wait a further few seconds, reconnec the USB cable to the FTDI.
- Select Tools -> Serial Monitor.
- This time do not hold down the Sonoff button.
- If all goes well, you will see it connect to your wifi and report it's IP address:
You can now browse to your Sonoff at the IP above:
- You may want to go into your router and assign fixed IP addresses on your LAN based on the Sonoff's MAC address in the DHCP settings.
You can now control the Sonoff to switch the device On or Off by pressing the Toggle button from the web page. The green light on the Sonoff indicates if the power to the device is on or not (of course, since we haven't plugged in AC power, there is no power to supply).
For now, leave the Sonoff connected to the FTDI adapter, and do not connect AC power.
Setting up an MQTT Message Broker
If you want to automate many things, you probably should aggregate all your Sonoff switches to a Message Broker. This lets other systems communicate with the Sonoff. Since I already had a Raspberry Pi running 24/7 to control my 3D printer (running Octoprint), I decided to install the MQTT server on the Raspberry Pi as well. I used a software called Mosquitto for this.
(Note: If you're reading this in 2018 or later, you're probably using a newer version of OctoPi than I am. If so, please see the comment by CinciJeff below.)
- ssh into your pi, and execute the following:
- sudo apt-key add mosquitto-repo.gpg.key
- cd /etc/apt/sources.list.d/
- sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
- sudo apt-get update
- sudo apt-get install mosquitto mosquitto-clients python-mosquitto
- cd /etc/mosquitto
- sudo cp /usr/share/doc/mosquitto/examples/mosquitto.conf.gz .
- sudo gunzip mosquitto.conf.gz
- edit /etc/mosquotto.conf and append "include_dir conf.d"
- create the file /etc/mosquitto/conf.d/mosquitto-sha.conf:
- pid_file /var/run/mosquitto.pid
- user mosquitto
- persistence true
- persistence_location /var/lib/mosquitto/
- port 1883
- log_dest none
- log_dest syslog
- allow_anonymous false
- password_file /etc/mosquitto/passwd
- Create Mosquito accounts in a terminal (you will be prompted for passwords):
- sudo mosquitto_passwd -c /etc/mosquitto/passwd shahada
- sudo mosquitto_passwd /etc/mosquitto/passwd sonoff
- sudo mosquitto_passwd /etc/mosquitto/passwd octoprint
- Restart mosquitto
- sudo systemctl restart mosquitto.service
Configure Sonoff for MQTT
Browse to the Sonoff IP, and under Configuration -> Other, set a web admin password and give it a friendly name (Note: you will have to login next time as user admin and this password):
Next under Configuration -> Configure MQTT, set up the parameters for mqtt on the sonoff:
- The Host is the IP of your Raspberry Pi running Mosquitto.
- Port is usually 1883 for unencrypted MQTT.
- Client ID should be a unique name identifying this Sonoff device.
- User and password are mosquitto user and passwords you created above. You can either create a unique one for each sonoff or have them share the same.
- The Topic should be a unique name identifying this Sonoff device
If all is well, you should see the mosquitto service start in syslog, and your clients connect to it:
pi@octopi:~ $ tail -f /var/log/syslog
Apr 28 15:58:09 octopi systemd: Starting LSB: mosquitto MQTT v3.1 message broker...
Apr 28 15:58:09 octopi mosquitto: Starting network daemon:: mosquitto.
Apr 28 15:58:09 octopi systemd: Started LSB: mosquitto MQTT v3.1 message broker.
Apr 28 15:58:09 octopi mosquitto: mosquitto version 1.4.11 (build date Mon, 20 Feb 2017 22:47:27 +0000) starting
Apr 28 15:58:09 octopi mosquitto: Config loaded from /etc/mosquitto/mosquitto.conf.
Apr 28 15:58:09 octopi mosquitto: Opening ipv4 listen socket on port 1883.
Apr 28 15:58:09 octopi mosquitto: Opening ipv6 listen socket on port 1883.
Apr 28 15:58:15 octopi mosquitto: New connection from 192.168.1.31 on port 1883.
Apr 28 15:58:15 octopi mosquitto: New client connected from 192.168.1.31 as SONOFF_PRUSA (c1, k15, u'sonoff').
Apr 28 15:58:16 octopi mosquitto: New connection from 192.168.1.32 on port 1883.
Apr 28 15:58:16 octopi mosquitto: New client connected from 192.168.1.32 as SONOFF_RND (c1, k15, u'sonoff').
The example below is how you subscribe to get the status of a power switch:
pi@octopi:~ $ mosquitto_sub -h localhost -p 1883 -u octoprint -P XXXX -t "stat/sonoff_prusa/POWER" -v
And this is how you control changing the state of the power switch:
pi@octopi:~ $ mosquitto_pub -h localhost -p 1883 -u octoprint -P XXXX -t "cmnd/sonoff_prusa/POWER" -m 0
pi@octopi:~ $ mosquitto_pub -h localhost -p 1883 -u octoprint -P XXXX -t "cmnd/sonoff_prusa/POWER" -m 1
To configure system commands in OctoPrint (the software that controls the 3D printer), install the System Command editor:
- Click Settings (the spanner icon).
- Click Plugin Manager
- Click Get More
- in "... from the Plugin Repository", type "System" into the Search field and press ENTER.
- Select "System Command Editor" and press Install.
- When prompted, click "Restart Now"
- When prompted, click "Reload Now"
Now we need to add the system commands to switch the 3D printer off and on. Click on Settings, and then System Command Editor. Right click in the green box and select "Create Command". Fill in the form as follows, replacing XXX with your mosquitto octoprint password:
- Name : Power ON 3D Printer
- Action: Power On 3D Printer via MQTT
- Command: mosquitto_pub -h localhost -p 1883 -u octoprint -P XXXX -t "cmnd/sonoff_prusa/POWER" -m 1
Press confirm, and create another System command:
- Name : Power OFF 3D Printer
- Action: Power Off 3D Printer via MQTT
- Command: mosquitto_pub -h localhost -p 1883 -u octoprint -P XXXX -t "cmnd/sonoff_prusa/POWER" -m 0
Finally, press confirm then save. The Octoprint menu should now have the following additional options:
When you select these items, the green light on the Sonoff should turn on and off, indicating it is switching.
Wiring the Sonoff
If you're happy with things so far, disconnect the Sonoff from the FTDI adapter. It is now time to wire it up to AC.
My 3D Printer uses a standard IEC power cable. I removed the sheathing from a spare cable, and cut the live and neutral wires, taking care not to damage the earth cable (check for connectivity on the earth line after you're done). I then connected this to the terminals on the Sonoff. The result is a cable that looks like this:
If you have something less expensive than a 3D Printer that powers up via IEC, I suggest testing with that first.
The Sonoff has mounting holes but they're a pain to get to, so I just used double sided tape and stuck it to the side of my IKEA Lack enclosure.
I might add a second Sonoff to link to the LED lights in the enclosure (or have the have the Sonoff power the 3D printer and lights together).
Other Stuff To Play With
Itead make many other types of Wifi-based AC controllers. There's the Slampher, which fits in-line with a E27 light bulb, and the S20 smart socket, a neater controller featuring an AC socket. All of these can be hacked with the Sonoff-Tasmota firmware to support MQTT. Once you have linked them to an MQTT broker, you can remotely control or automate their switching through any MQTT client or dashboard software.
MQTT can also be interfaced to Amazon's Alexa and Google's Google Home voice-activated assistants. I might look into interfacing more things in my house to MQTT and having them voice controlled.
Here's an arduino library to control most airconditioning units via infrared, that with some hacking can be controlled via MQTT and built with a ESP8266 module.