User manual

Scripting
8
Function definition Signature Description
bus_list = tml.get_busses() nil -> table Returns a list of available Modbus
server connections as table of
strings. Those strings are used
as bus parameter in all tml.*
commands.
Table4.1.Extensions to the LUA language on gpio.ipCore
4.2. Python
In this example, a digital input and a relay of the gpio.relay card is connected to
the internal database. These can then be used via the REST interface on URL “http://
ipcore.local/shared/button” and “http://ipcore.local/shared/relay”. The actions standing
behind these URLs can, of course, turn out to be far more complex than in this example.
#!/usr/bin/env python2.7
import redis
from pymodbus.client.sync import ModbusTcpClient
r = redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0)
mb = ModbusTcpClient("127.0.0.1", 12346)
oldButtonState = False
oldRelayState = False
while True:
buttonState = mb.read_discrete_inputs(unit = 8, address = 2).bits[0]
if oldButtonState != buttonState:
oldButtonState = buttonState
if buttonState == True:
print "Button pressed"
r.incr('button')
relayState = r.get('relay')
if oldRelayState != relayState:
oldRelayState = relayState
if relayState == "1":
print "Turning relay on"
mb.write_coil(unit = 8, address = 4, value = 1)
else:
print "Turning relay off"
mb.write_coil(unit = 8, address = 4, value = 0)
4.3. Automated starting of scripts
The two previous scripts both contain in their first line a construct in the form of “#!
command parameter”. This helps the system to start the appropriate interpreter for the
script. If the data has been generated as described in the last paragraph, it can be easily
started and stopped by the system automatically. The following steps are required to have
“myscript.py” managed by the system:
mkdir /service/myscript
cd /service/myscript
ln s /var/ipcore/myscript/myscript.py run