Timed gcode system commands

What is the problem?
We made a system gcode command where a led is blinking for 30 sec.:

#!/bin/bash

API_KEY=B9735C4DAD594499B5ACC7BFD2A96306

sleep 0.1

for i in {1..30}
do
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O2 S1" }" http://localhost/api/printer/command
sleep 0.5
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O2 S0" }" http://localhost/api/printer/command
sleep 0.5
done

exit 0

called it OCTO4
When we execute OCTO4 through terminal it works like a charm.
When we put OCTO4 in the pause gcode it does nothing for 30 seconds and then blinks 30x in a blink of an eye.
So while executing the script it accepts the commands coming from the script, but only executes the commands after the script has finished

Not exactly sure what's going on but also not sure if you really need to do this with a plugin. You should be able to just use G4.

https://www.reprap.org/wiki/G-code#G4:_Dwell

ENC O2 S1
G4 500
ENC O2 S0

OctoPrint GCode scripts also support Jinja syntax so you could do the loop there too.

Thanks for the answer.
We really want to stay away as much as possible of G4 as that is a very fickle command.

The loop in the jinja syntax is a nice one, but I would really need a wait or sleep or delay command as well.

Also other things should be happening, while the output is blinking for 30 sec.
What we used to do was execute a bash script, that executes 3 other bash scripts at the same time.(one of them the 30 sec blinking). This worked very well until we made some upgrades last week (one of them was gcode system commands). And now it waits until all 3 bash scripts are done and then executes all commands in those bash scripts at the same time.

Is the async: true not set in the updated gcode system commands plugin ?

It runs commands synchronously.

So that will be the hickup for us.
Is there any way to make it run asynchronous.
We tried adding & after the command, but that did not help.
Did the previous version run asynchronous?

Neither version ran async. The previous version was executing commands during the queue phase instead of at the time of sending which resulted in some out of order issues.

Either way I would expect issues with the way you are attempting to implement this.

What is ENC anyway?

In the setup for the Gcode System Command for OCTO4, you want it to have an ampersand at the end of the command as in:

/home/pi/scripts/blinkie.sh &

This should invoke your script as a background task and not block for the foreground thread.

We tried the ampersand, and it does not work inside of octoprint. (since the update ?)
The ENC command is for the enclosure plugin, to set an output like a gpio pin or a combination of pins.
Any thoughts on a good way to implement ?

You might then try an Inception approach:

  1. OCTO4 calls /home/pi/script/thing1
  2. thing1 then simply calls thing2 with an ampersand

@OutsourcedGuru : that is the way that we are doing it, but somehow octoprint still waits until all scripts are finished.
We (I) do have it in the home/pi/.octoprint/scripts folder. Does that matter?

If it's somewhere (usual) within the pi user's space then it would at least be findable and have the appropriate file ownership. You might want to include the content of everything you're discussing.

OCTO4 -> /home/pi/.octoprint/scripts/pause_print.sh &

pause_print.sh is:

#!/bin/bash

source /home/pi/.octoprint/scripts/pause_print_commands.sh &
source /home/pi/.octoprint/scripts/fume_on_30_commands.sh &
source /home/pi/.octoprint/scripts/blink_green_30_commands.sh &

exit 0

pause_print_commands.sh is:

#!/bin/bash

API_KEY=B9735C4DAD594499B5ACC7BFD2A96306

sleep 0.1

curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "M104 S160" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "G91" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "G1 Z5 F1200 " }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "G90" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "G60 S0" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "G1 X225 Y325 F12000" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O3 S1" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O1 S0" }" http://localhost/api/printer/command
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "M117 printing paused by user" }" http://localhost/api/printer/command

exit 0

blink_green_30_commands.sh is:

#!/bin/bash

API_KEY=B9735C4DAD594499B5ACC7BFD2A96306

sleep 0.1

for i in {1..30}
do
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O2 S1" }" http://localhost/api/printer/command
sleep 0.5
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O2 S0" }" http://localhost/api/printer/command
sleep 0.5
done

exit 0

fume_on_30_commands.sh is:

#!/bin/bash

API_KEY=B9735C4DAD594499B5ACC7BFD2A96306

sleep 0.1

curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O4 S1" }" http://localhost/api/printer/command
sleep 30
curl -H "Content-Type: application/json" -H "X-Api-Key:$API_KEY" -X POST -d "{ "command" : "ENC O4 S0" }" http://localhost/api/printer/command
sleep 0.5

exit 0

I am doing this through the action commands plugin now and that works as expected.
So instead of OCTO4 I am sending:

M118 // action:pause_print

and that executes : /home/pi/.octoprint/scripts/pause_print.sh &

Alright.

Just throwing this out there, usually when I find that I'm repeating the same text over and over, I tend to make another script to swallow all that.

For example, I might create: ~/scripts/send_command which then accepts a command line argument which is the string to send via curl.

Newer version of your script:

#!/bin/bash

API_KEY=B9735C4DAD594499B5ACC7BFD2A96306
sleep 0.1
pushd /home/pi/scripts
./send_command "\"{ \"command\" : \"ENC O4 S1\" }\""
sleep 30
./send_command "\"{ \"command\" : \"ENC O4 S0\" }\""
popd
sleep 0.5
exit 0