Pine64 & OctoPrint (Part 3) - Now With mFi Control

I've been reliably using OctoPrint on a Pine64 since I wrote the original two parts (Part 1, Part 2) of this series back in January. I was a bit behind on OctoPrint updates (running 1.3.2), and took the opportunity this weekend to upgrade to the latest stable version (1.3.4), and add in the custom action I've been meaning to create for remotely powering up my 3D printer.

I've been using a Ubiquiti Networks mPower-Mini to control power to my 3D printer since day one. In addition to providing remote on/off capabilities, these devices also allow you to track and trend power consumption and other metrics. Ubiquiti has unfortunately ended development of the mFi controller, but these devices are still manufactured for users willing to script their own interface to them. I've been working my way off of my existing mFi controller for this reason, and figured it was time to connect my mFi switch to an action inside of OctoPrint. This allows OctoPrint to issue the on/off commands directly to the outlet, without needing to login to the mFi controller, portal, or mobile app.

Note that while Ubiquiti is no longer formally supporting these devices, they are readily available with a wide array of community support. That said, in theory, you could likely adapt this script to work with any number of Wifi enabled outlets, such as the TP-Link HS100.

Assuming you followed my guide to installing OctoPrint in Part 2, your OctoPrint scripts are probably located in /usr/local/etc/OctoPrint/scripts. This script is adapted from the work Scott Emery has shared on the Ubiquiti Community forums.

Start by creating the file to contain our script by running nano /usr/local/etc/OctoPrint/scripts/mfictrl. Scott's script contains a lot of additional features that we won't use (yet), but I've included the entire script here for completeness. One adaptation I made was to remove the dimmer mode from the on/off functions, ensuring that the switch behaves as a relay and not as a dimmer.

Paste the following script into your mfictrl file, being sure to update the username and password as needed.

Hit Ctrl + X to exit, being sure to save the script, then ensure the script has execution rights by running sudo chmod +x /usr/local/etc/OctoPrint/scripts/mfictrl.

You should now be able to remotely turn on your outlet by running /usr/local/etc/OctoPrint/scripts/mfictrl -l {outlet-ip} ON. Likewise, /usr/local/etc/OctoPrint/scripts/mfictrl -l {outlet-ip} OFF should turn off your outlet.

The last step is to configure custom actions inside of OctoPrint so that the outlet can be turned on/off from the power menu inside OctoPrint. To do this run nano ~/.octoprint/config.yaml, find the system:actions section, and paste the following. Note that spacing on each line here is critical.

Hit Ctrl + X to exit, being sure to save the script, then reboot the system by running sudo reboot. When OctoPrint comes back up, you should now have buttons to enable and disable the printer's power inside the power menu at the top right.