<p>A solution I use involves a 3D print server. I have defined shell scripts that address the GPIO ports of the Raspberry Pi that runs OctoPrint. OctoPrint is a 3D print server that can be accessed over your home network.</p>
<p>From the <a href="https://octoprint.org/" rel="nofollow noreferrer">OctoPrint homepage</a>:</p>
<blockquote>
<p>OctoPrint is the snappy web interface for your 3D printer that allows
you to control and monitor all aspects of your printer and print jobs,
right from your browser.</p>
</blockquote>
<p>This print server application allows for specification of custom G-code commands (linked to system commands/scripts for instance; note this is a plug-in called &quot;GCODE Systems Commands&quot;, see image below).</p>
<p>As an example, e.g. <code>OCTO100</code> is scheduled to run <code>fan_on.sh</code> shell script. This script in its turn drives a relay to switch the annoying extruder cooling fan that is always on when the printer is powered. These codes can then be used throughout your sliced file to do stuff you want (e.g. by using the TweakAtZ plugin of Cura). E.g. my extruder fan will stay on several minutes before it is scheduled off after a successful print through <code>OCTO110</code> which in itself runs the script <code>fan_off.sh</code>.</p>
<p><a href="https://i.sstatic.net/1CaMa.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1CaMa.png" alt="enter image description here" /></a></p>
<p>The scripts from the figure could be setup to schedule to do something, e.g. <code>fan_on.sh</code> controls a relay using port 22 of the GPIO of the Raspberry PI:</p>
<pre><code>#!/bin/bash
gpio export 22 out
gpio -g write 22 0
</code></pre>
<p>So to disable the fan you would need <code>fan_off.sh</code> to be:</p>
<pre><code>#!/bin/bash
gpio export 22 out
gpio -g write 22 1
</code></pre>

CC BY-SA 3.0
2018-04-13T14:48:11.470Z
nice example of octoPi interation
profesor79
CC BY-SA 3.0
2018-04-13T14:54:28.450Z
@professor79 thanks, this is just a sample, I also schedule the LED lights and can also switch the printer on and off. You can also add custom menu items, so all the scripts can also be activated through the GUI (menu) of OctoPrint
0scar
CC BY-SA 4.0
2018-04-13T11:01:19.103Z
2023-12-07T18:36:40.497Z
2023-12-07T18:36:40.497Z
0scar
<p>One of the solutions could be adding a layer change script (simplifi3d has that out of the  box) and then using marlin firmware you could set a value to digital pin that could triger external actions.</p>

<blockquote>
  <p><a href="https://forum.simplify3d.com/viewtopic.php?t=1959" rel="nofollow noreferrer">Layer Change G-Code</a>: I personally haven't had to use this, but I'm
  sure that there are some excellent reasons/ideas to use for this. If
  you'd like for a G-Code script to be inserted in-between each layer,
  than you can simply place it in this tab. One interesting use of this,
  is for the FlashForge Dreamer, to have the lights blink in between
  each layer, however that can be a bit too much at times! </p>
</blockquote>

<p>The syntax for the M42 command is: M42 S(value to be written to pin) P (pin number) e.g. To set digital pin 30 high, you would use M42 S1 P30</p>

<blockquote>
  <p><a href="http://forums.reprap.org/read.php?146,168237,173098" rel="nofollow noreferrer">The MARLIN firmware will</a> not enable you to change the status / write
  values to any of the pins in use for things such as the heaters,
  thermistors, end stops etc. The command will let you send values other
  than 0 and 1 to any pins which can output analogue values. (0-255)</p>
</blockquote>

CC BY-SA 3.0
2018-04-13T08:34:45.583Z
2018-04-13T08:34:45.583Z
profesor79
<p>I saw this 3 years ago, this is a <a href="https://www.youtube.com/watch?time_continue=33&amp;v=L-_5KsAOVko" rel="nofollow noreferrer">closed loop control</a>, I think is better that taking a photo then analyze it with a kind of optical recognition software which needs some of possible failures.</p>

<p>The video has also the link for the <a href="https://www.youmagine.com/designs/dc-motor-closed-loop-control-software" rel="nofollow noreferrer">additional resources</a> the 3d printer will need according his author.</p>

CC BY-SA 3.0
2018-04-14T00:24:53.370Z
2018-04-14T00:24:53.370Z
Fernando Baltazar
<p>I would like to be able to add custom commands/script to be executed during a print.</p>

<p>For example I would like to write some software to take a picture check the print hasn't moved off the bead between layers. </p>

<p>Does anyone know if any of the available software/firmware allows custom scripts or calling back to the computer before continuing printing?</p>

<p>I am happy to build/buy a new printer if anyone knows a control board that has this sort of feature. </p>

CC BY-SA 3.0
2018-04-17T05:38:36.197Z
you can trigger by moving the head to a reserved place that activates a sensor. you can abort by intercepting a sensor like hot end temp that will abort the print, using a relay or mosfet or whatever
dandavis
<g-code>
Run custom code between G-code commands?
user802599
# Run custom code between G-code commands?

(Asked by: username_0 on 2018-04-13)

I would like to be able to add custom commands/script to be executed during a print.

For example I would like to write some software to take a picture check the print hasn't moved off the bead between layers.

Does anyone know if any of the available software/firmware allows custom scripts or calling back to the computer before continuing printing?

I am happy to build/buy a new printer if anyone knows a control board that has this sort of feature.

**Tags:** g-code
> **Comments:**
> **username_1**: you can trigger by moving the head to a reserved place that activates a sensor. you can abort by intercepting a sensor like hot end temp that will abort the print, using a relay or mosfet or whatever  

---

## 3 answers

### Answer by username_2 on 2018-04-13. Score: 4

A solution I use involves a 3D print server. I have defined shell scripts that address the GPIO ports of the Raspberry Pi that runs OctoPrint. OctoPrint is a 3D print server that can be accessed over your home network.
From the OctoPrint homepage:

OctoPrint is the snappy web interface for your 3D printer that allows
you to control and monitor all aspects of your printer and print jobs,
right from your browser.

This print server application allows for specification of custom G-code commands (linked to system commands/scripts for instance; note this is a plug-in called "GCODE Systems Commands", see image below).
As an example, e.g. OCTO100 is scheduled to run fan_on.sh shell script. This script in its turn drives a relay to switch the annoying extruder cooling fan that is always on when the printer is powered. These codes can then be used throughout your sliced file to do stuff you want (e.g. by using the TweakAtZ plugin of Cura). E.g. my extruder fan will stay on several minutes before it is scheduled off after a successful print through OCTO110 which in itself runs the script fan_off.sh.

The scripts from the figure could be setup to schedule to do something, e.g. fan_on.sh controls a relay using port 22 of the GPIO of the Raspberry PI:
#!/bin/bash
gpio export 22 out
gpio -g write 22 0

So to disable the fan you would need fan_off.sh to be:
#!/bin/bash
gpio export 22 out
gpio -g write 22 1

> **Comments:**
> **username_3**: nice example of octoPi interation  
> **username_2**: @professor79 thanks, this is just a sample, I also schedule the LED lights and can also switch the printer on and off. You can also add custom menu items, so all the scripts can also be activated through the GUI (menu) of OctoPrint  

---

### Answer by username_3 on 2018-04-13. Score: 2

One of the solutions could be adding a layer change script (simplifi3d has that out of the box) and then using marlin firmware you could set a value to digital pin that could triger external actions.

> [Layer Change G-Code](https://forum.simplify3d.com/viewtopic.php?t=1959): I personally haven't had to use this, but I'm
> sure that there are some excellent reasons/ideas to use for this. If
> you'd like for a G-Code script to be inserted in-between each layer,
> than you can simply place it in this tab. One interesting use of this,
> is for the FlashForge Dreamer, to have the lights blink in between
> each layer, however that can be a bit too much at times!

The syntax for the M42 command is: M42 S(value to be written to pin) P (pin number) e.g. To set digital pin 30 high, you would use M42 S1 P30

> [The MARLIN firmware will](http://forums.reprap.org/read.php?146,168237,173098) not enable you to change the status / write
> values to any of the pins in use for things such as the heaters,
> thermistors, end stops etc. The command will let you send values other
> than 0 and 1 to any pins which can output analogue values. (0-255)


---

### Answer by username_4 on 2018-04-14. Score: 1

I saw this 3 years ago, this is a [closed loop control](https://www.youtube.com/watch?time_continue=33&v=L-_5KsAOVko), I think is better that taking a photo then analyze it with a kind of optical recognition software which needs some of possible failures.

The video has also the link for the [additional resources](https://www.youmagine.com/designs/dc-motor-closed-loop-control-software) the 3d printer will need according his author.


---

