# - OpenPLC Python SubModule (PSM) -
#
# PSM is the bridge connecting OpenPLC core to Python programs. PSM allows
# you to directly interface OpenPLC IO using Python and even write drivers
# for expansion boards using just regular Python.
#
# PSM API is quite simple and just has a few functions. When writing your
# own programs, avoid touching on the "__main__" function as this regulates
# how PSM works on the PLC cycle. You can write your own hardware initialization
# code on hardware_init(), and your IO handling code on update_inputs() and
# update_outputs()
#
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
# psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
# psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
#
# Below you will find a simple example that uses PSM to switch OpenPLC's
# first digital input (%IX0.0) every second. Also, if the first digital
# output (%QX0.0) is true, PSM will display "QX0.0 is true" on OpenPLC's
# dashboard. Feel free to reuse this skeleton to write whatever you want.
#import all your libraries here
import psm
import time
import glob
import os
import RPi.GPIO as GPIO
#global variables
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices'
DInputs = [17,27,22,5,6,13,19,26]
DOutputs = [18,23,24,25,12,16,20,21]
DevicePaths = []
counter = 0
connection = False
livetoggleIn = False
livetoggleOut = False
def hardware_init():
#Insert your hardware initialization code in here
global DInputs
global Outputs
global base_dir
global Devicepaths
# Getting file devices for all DS18B20 devices
Devicepaths = []
for path in glob.glob(f'{base_dir}/*/'):
if "/28-" in path:
DevicePaths.append(path + '/w1_slave')
GPIO.setmode(GPIO.BCM) #BCM Mode -> GPIO number not pin number
for x in DInputs: #setting up all inputs
GPIO.setup(x, GPIO.IN)
for x in DOutputs: #setting up all outputs
GPIO.setup(x, GPIO.OUT)
psm.start()
def general_conn():
global counter
global connection
global livetoggleIn
global livetoggleOut
livetoggleIn = psm.get_var("QX0.0") #built in togglebits to check connection between PSM and PLC
if (livetoggleOut == livetoggleIn):
counter = counter + 1
else:
counter = 0
connection = True
if (counter > 10): # if 10 cycles no change -> connection error
connection = False
livetoggleOut = livetoggleIn #setting toggle for PLC - one side is setting its output to input value, other side (PLC) is negated
psm.set_var("IX0.0", livetoggleOut)
def update_inputs():
#place here your code to update inputs
global DInputs
global DevicePaths
inp = "IX"
by = 0
bi = 1
for x in DInputs: #updating all input bits in the order like in DInputs[] and writing them to adresses from IX0.1 up
name = inp + str(by)+ "." + str(bi)
a = GPIO.input(x)
if (a and connection):
psm.set_var(name, True)
else:
psm.set_var(name, False)
if (bi == 7):
by = by + 1
bi = 0
else:
bi = bi + 1
by = 0
for p in DevicePaths: #reading all found DS18B20 sensors ind writing them to adresses from IW0 up
cel = int(read_temp(p)*100) #temperature value times 100 to get more accurate values to PLC - has to be divided by 100 in PLC
psm.set_var("IW"+str(by), cel)
by = by + 1
def update_outputs():
#place here your code to work on outputs
global DOutputs
out = "QX"
by = 0
bi = 1
for x in DOutputs: #reading all Output bits from QX0.1 up and writing them in the order like in DOutputs[] to Output Pins
name = out + str(by) + "." + str(bi)
a = psm.get_var(name)
if (a == True):
GPIO.output(x, GPIO.HIGH)
else:
GPIO.output(x, GPIO.LOW)
if (bi == 7):
by = by + 1
bi = 0
else:
bi = bi + 1
def disable_Outputs(): #writes false to all outputs
global DOutputs
out = "QX"
by = 0
bi = 1
for x in DOutputs:
name = out + str(by) + "." + str(bi)
GPIO.output(x, GPIO.LOW)
if (bi == 7):
by = by + 1
bi = 0
else:
bi = bi + 1
def read_temp(filepath): #read temperature values
lines = read_temp_raw(filepath)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c #, temp_f could be returned instead of temp_c to get fahrenheit
def read_temp_raw(filepath): #read raw temperature values
f = open(filepath, 'r')
lines = f.readlines()
f.close()
return lines
if __name__ == "__main__":
hardware_init()
while (not psm.should_quit()):
general_conn()
update_inputs()
update_outputs()
time.sleep(1) #You can adjust the psm cycle time here
disable_Outputs() #disable all GPIO Outputs
GPIO.cleanup() # cleanup all GPIO
psm.stop()