Introducción
En esta breve entrada del blog, veremos cómo instalar y utilizar la librería Python para los PLCs Raspberry de Industrial Shields. La librería se puede encontrar here.
Installation
Github link
Para instalar la librería, primero necesitas clonar este repositorio a tu PLC usando el siguiente comando:
# For RPi PLC v3
$ git clone -b v1.0.0 https://github.com/Industrial-Shields/python3-librpiplc/
# For RPi PLC v4
$ git clone -b v2.0.0 https://github.com/Industrial-Shields/python3-librpiplc/
Después, ejecuta el siguiente comando para instalar la librería:
$python3 setup.py install
#dtoverlay=mcp23017,noints,mcp23008,addr=0x20
#dtoverlay=mcp23017,noints,mcp23008,addr=0x21
#dtoverlay=i2c-pwm-pca9685a,addr=0x40
#dtoverlay=i2c-pwm-pca9685a,addr=0x41
#dtoverlay=ads1015,addr=0x48
#dtparam=cha_enable=true,cha_gain=1
#dtparam=chb_enable=true,chb_gain=1
#dtparam=chc_enable=true,chc_gain=1
#dtparam=chd_enable=true,chd_gain=
#dtoverlay=ads1015,addr=0x49
#dtparam=cha_enable=true,cha_gain=1
#dtparam=chb_enable=true,chb_gain=1
#dtparam=chc_enable=true,chc_gain=1
#dtparam=chd_enable=true,chd_gain=1
#dtoverlay=ads1015,addr=0x4a
#dtparam=cha_enable=true,cha_gain=1
#dtparam=chb_enable=true,chb_gain=1
#dtparam=chc_enable=true,chc_gain=1
#dtparam=chd_enable=true,chd_gain=1
#dtoverlay=ads1015,addr=0x4b
#dtparam=cha_enable=true,cha_gain=1
#dtparam=chb_enable=true,chb_gain=1
#dtparam=chc_enable=true,chc_gain=1
#dtparam=chd_enable=true,chd_gain=1
API
Inicialización PLC
rpiplc.init("RPIPLC_57R")
Puedes elegir de la siguiente lista de modelos para rellenar el parámetro.
Lista de Modelos PLC
- RPIPLC_19R
- RPIPLC_21
- RPIPLC_38AR
- RPIPLC_38R
- RPIPLC_42
- RPIPLC_50RRA
- RPIPLC_53ARR
- RPIPLC_54ARA
- RPIPLC_57AAR
- RPIPLC_57R
- RPIPLC_58
Inicialización Pin: Es una buena práctica inicializar los pines como entradas o salidas. Para ello, utiliza el siguiente código:
pin_mode(pin_name,mode) #mode can be rpiplc.OUTPUT; rpiplc.INPUT or 1;0
Funciones Pin
analog_read(): rpiplc.analog_read("A0.0")
analog_write(): rpiplc.analog_write("A0.0",value) #Here the value is in the range of 0 to 4095. 0 being 0V and 4095 is 10v
digital_read(): rpiplc.digital_read("Q0.0")
digital_write(): rpiplc.digital_write("Q0.0",rpiplc.HIGH)
relay(): rpiplc.digital_write("R0.1",rpiplc.HIGH)
Código de Ejemplo
from rpiplc_lib import rpiplc
def digitalreadwrite():
rpiplc.pin_mode("Q0.0",1)
rpiplc.digital_write("Q0.0",rpiplc.HIGH)
rpiplc.delay(1000)
rpiplc.digital_write("Q0.0",rpiplc.LOW)
rpiplc.delay(1000)
def analogreadwrite():
rpiplc.pin_mode("A1.0",rpiplc.INPUT)
read_value=rpiplc.analog_read("A1.0")
print("The A1.0 is reading : ",read_value)
rpiplc.pin_mode("A1.1",rpiplc.OUTPUT)
rpiplc.analog_write("A1.1",1024) # 2.5v Output
rpiplc.delay(2000)
rpiplc.analog_write("A1.1",4095) # 10v Output
rpiplc.delay(2000)
rpiplc.analog_write("A1.1",0)
def relaytest():
rpiplc.pin_mode("R0.1",rpiplc.OUTPUT)
rpiplc.digital_write("R0.1",rpiplc.HIGH)
rpiplc.delay(1000)
rpiplc.digital_write("R0.1",rpiplc.LOW)
rpiplc.delay(1000)
def main():
rpiplc.init("RPIPLC_57R")
while True:
digitalreadwrite()
analogreadwrite()
relaytest()
if __name__ == "__main__":
main()
Otros enlaces relevantes
Raspberry PLC
InfluxDB y Raspberry PLC
Librería Python para Raspberry PLC