Starting with version 0.91.0 you can put lua scripts into /var/lib/Repetier-Server/lua folder and they get called approx. 100 times per second. Fast enough to react on buttons. These scripts have direct access to the server data structures and can also send messages or commands. You can use them to to monitor the printers and add automatic actions on some conditions or you can write or read pins on the computer (only for linux).
The minimum plugin would look like this:
function setup() end function loop() end
The setup function is called once at server startup. The loop function is later called 100 times per second. It MUST return so other plugins get their loop function also called. If loop is too cpu intensive the frequency will drop down.
For interaction with the server internals you can find available objects here: https://www.repetier-server.com/manuals/programming/API/modules.html
What you can not use are the registration methods below “Integrating into the server” section. These are only available in modules that are integrated into the normal flow of the server, while this monitoring function is for independent code.
To test a newly uploaded or modified file you need to restart the server:
> sudo service RepetierServer restart
Especially if you are new to lua, you might add some syntax errors. To check if your script compiles correctly, check the last 100 lines of the server.log:
> tail -100 /var/lib/Repetier-Server/logs/server.log
If everything is ok, you will see something like this:
2019-01-30 15:45:46: LUA initalization finished. 2019-01-30 15:45:46: Including lua file /var/lib/Repetier-Server/lua/blink.lua 2019-01-30 15:45:46: lua setup called 2019-01-30 15:45:46: Starting web server ...
If your script contains errors, you will see the error message after the “Including lua file…” message. In that case the server will ignore the plugin until next restart.
If the server runs under linux you can access GPIO pins. For this we include this library: https://github.com/vsergeev/lua-periphery/blob/master/docs/gpio.md
The only difference is that you do not require the library. It is available via the periphery variable.
local GPIO = periphery.GPIO local loopCounter = 0 local blink = false local gpioBlink = nil function setup() gpioBlink = GPIO(3, "out") end function loop() loopCounter = loopCounter + 1 if loopCounter >= 100 then loopCounter = 0 blink = not blink gpioBlink:write(blink) end end
Here pin 3 is defined as output. This will be used to light a led. Connect a low amp led to pin 3, connect the other side to a 330 ohm resistor and to GND. If you need more power google for a FET or transistor schematic.
In setup the mode of operation gets defined. In loop we count loopCounter up to 100 and then invert blink variable and write value to pin 3. This makes it blink with approximately 1 second on and 1 second off.
local GPIO = periphery.GPIO local lastButton = true local gpioButton = nil function setup() gpioButton = GPIO(2, "in") end function loop() if lastButton ~= gpioButton:read() then lastButton = not lastButton if lastButton then os.execute("sudo /sbin/shutdown -h now") end end end
For health of sd card it is important to always shut down the pi so you get no defects on sd card. In advanced setup in tutorial we already have shown how to add a command for this. Here we want to do the same with an extra button, so we can shutdown the printer safely even if we have no display.
For this example a simple push button that is normally not connected is connected to pin 2 and GND. Default is pull-up resistor so when not pressed, the pin returns true. In setup we define it as input on pin 2. In loop we compare current state with last state. If it goes false and then back to true it executes the shell command “sudo /sbin/shutdown -h now” with the user repetierserver, so you must ensure /sbin/shutdown is in list of sudoers for user repetierserver. If you have created the sample extcommands.xml with shutdown you have done that already. If not read in the Repetier-Server manual how to do it.