2015/02/02

How To Display OctoPrint Progress on a PiGlow

I've been using OctoPrint/OctoPi and a Raspberry Pi to drive my PrintrBot 1405 and it's been working well.  There are times when being able to glance over and see the progress of the print without using a web browser would be nice, so I added a Pimoroni PiGlow.

PrintrBot with PiGlow

Getting the PiGlow Running

The first step was enabling i2c on the Pi.  I followed the Pimoroni supplied instructions for enabling support for the PiGlow.  Running 'sudo python piglow-example.py' resulted in the following error message:

Traceback (most recent call last):
  File "piglow-example.py", line 52, in
    piglow = PiGlow(1)
  File "piglow-example.py", line 21, in __init__
    self.bus = SMBus(i2c_bus)
IOError: [Errno 2] No such file or directory

Hmmm... that's not quite right... after much fumbling about, I read the source and found out that my Version 1 Model B required the i2c port to change from 1 to 0:

piglow = PiGlow(1)

became:

piglow = PiGlow(0)

And blinking LED goodness ensued:

PiGlow Running Test Program

Awesome!  At this point, a test print using OctoPi insured I hadn't messed up anything during my fumbling about.  So far, so good.

Reporting Progress

Reading through the OctoPrint documentation on EventHooks I decided to start with just tracking the ZChange and using the progress variable to show the percentage complete.  This allows for a very simple python script that takes the percentage complete as a command line argument and updates the PiGlow accordingly.

Jason Barnett's excellent PiGlow library abstracts away a lot of the details of working with the PiGlow as well as dealing with the version of the Raspberry Pi itself.  Jason's library and my script will reside in ~/octoglow/progress.py:

cd ~
mkdir octoglow
cd octoglow
wget https://raw.github.com/Boeeerb/PiGlow/master/piglow.py
nano progress.py

The following script will light up the spiral from the inside to the outside as the print progresses:

from piglow import PiGlow
import sys

piglow = PiGlow()

def updateProgress(progress):
    piglow.white(10)
    if progress > 20:
        piglow.blue(10)
    if progress > 40:
        piglow.green(10)
    if progress > 60:
        piglow.yellow(10)
    if progress > 80:
        piglow.orange(10)
    if progress >= 100:
        piglow.red(10)

piglow.all(0);
if len(sys.argv) > 1:
    updateProgress(float(sys.argv[1]))

You can then test the script from the command line:

sudo python ~/octoglow/progress.py 100

PiGlow Showing OctoPrint Progress

Adding the Event Hooks to OctoPrint

Next up is editing '.octoprint/config.yaml' and adding the following snippet:

events:
  enabled: True
  subscriptions:
  - event: PrintStarted
    command: sudo python ~/octoglow/progress.py {__progress}
    type: system
  - event: ZChange
    command: sudo python ~/octoglow/progress.py {__progress}
    type: system
  - event: PrintDone
    command: sudo python ~/octoglow/progress.py {__progress}
    type: system

Then, via the web interface, I restarted OctoPrint.  Things looked good initially; but, unfortunately, the progress never got beyond zero.  After some debugging, I traced it back to a mismatch in hash keys that looks like it has been fixed in the development branch courtesy of a pull request by Salandora. A few small updates to the local copy of events.py, another restart of OctoPrint and the progress bar is up and running!


No comments:

Post a Comment