Opensource OPC UA für den Raspberry Pi

mimuel
Posts: 7
Joined: 22 Nov 2016, 17:22
Answers: 0

Opensource OPC UA für den Raspberry Pi

Post by mimuel »

Hallo,

übrigens gibt es ein freies OPC UA für den Rasp:
Download hier: https://github.com/FreeOpcUa
Doku hier: http://python-opcua.readthedocs.io/en/latest/

In Python sehr einfach zu programmieren - funktionierte auf Anhieb!

Gruß Michael
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Opensource OPC UA für den Rasp

Post by volker »

Hallo Michael,
super! Vielen Dank für diese Links. Bin gespannt, ob hier jemand das mal ausprobiert und darüber berichtet...
Unser RevPi Motto: Don't just claim it - make it!
scrolllkock
Posts: 16
Joined: 03 Nov 2016, 17:14
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by scrolllkock »

mimuel wrote:Hallo,

übrigens gibt es ein freies OPC UA für den Rasp:
Download hier: https://github.com/FreeOpcUa
Doku hier: http://python-opcua.readthedocs.io/en/latest/

In Python sehr einfach zu programmieren - funktionierte auf Anhieb!

Gruß Michael
Hallo Michael,

kannst du mir bei OPC UA behilflich sein?
Ich habe das Projekt installiert und bekomme bei Ausführung des scripts client-minimal.py auch eine Verbindung zum OPC UA Server auf meiner B&R Steuerung...
Aber weiter...?

Hier das leicht gewandelte Script:

Code: Select all

import sys
sys.path.insert(0, "..")


from opcua import Client


if __name__ == "__main__":

    client = Client("opc.tcp://192.168.100.250:4840/server/")
    # client = Client("opc.tcp://localhost:4840/freeopcua/server/")
    # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
    try:
        client.connect()

        # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
        root = client.get_root_node()
        print("Objects node is: ", root)

        # Node objects have methods to read and write node attributes as well as browse or populate address space
        print("Children of root are: ", root.get_children())

        # get a specific node knowing its node id
        #var = client.get_node(ua.NodeId(1002, 2))
        #var = client.get_node("ns=3;i=2002")
        var = client.get_node("ns=3;i=84")
        print(var)
        #var.get_data_value() # get value of node as a DataValue object
        #var.get_value() # get value of node as a python builtin
        #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
        #var.set_value(3.9) # set node value using implicit data type

        # Now getting a variable node using its browse path
        myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
        obj = root.get_child(["0:Objects", "2:MyObject"])
        print("myvar is: ", myvar)

    finally:
        client.disconnect()
und hier die Ausgabe:

Code: Select all

pi@RevPi ~/python-opcua/examples $ python3 client-min*
Objects node is:  Node(TwoByteNodeId(i=84))
Children of root are:  [Node(TwoByteNodeId(i=85)), Node(TwoByteNodeId(i=86)), Node(TwoByteNodeId(i=87))]
Node(NumericNodeId(ns=3;i=84))
Traceback (most recent call last):
  File "client-minimal.py", line 34, in <module>
    myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
  File "../opcua/common/node.py", line 435, in get_child
    result.StatusCode.check()
  File "../opcua/ua/uatypes.py", line 201, in check
    raise UaStatusCodeError(self.value)
opcua.ua.uaerrors._auto.BadNoMatch: The requested operation has no match to return.(BadNoMatch)

Was wollen mir diese beiden Zeilen sagen?
Objects node is: Node(TwoByteNodeId(i=84))
Children of root are: [Node(TwoByteNodeId(i=85)), Node(TwoByteNodeId(i=86)), Node(TwoByteNodeId(i=87))]
mimuel
Posts: 7
Joined: 22 Nov 2016, 17:22
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by mimuel »

Hallo OhneNamen,

hier findest Du einen freien Client, damit kannst du auf die Objekte des Informationsmodells schauen - dann wird dir die Struktur im Programm klarer:

https://www.unified-automation.com/prod ... xpert.html

Am Wochenende werde ich mir deinen Code anschauen.

Gruß Michael
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Opensource OPC UA für den Rasp

Post by volker »

Hallo Michael,
ICH FINDE DAS SUPER!!! Danke für Deine Postings. Das ist gelebte Community. Weiter so!
Unser RevPi Motto: Don't just claim it - make it!
scrolllkock
Posts: 16
Joined: 03 Nov 2016, 17:14
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by scrolllkock »

Hallo Michael,

danke für den Denkanstoß mit der Windows Software!
Ich hatte das Problem, das im Beispiel nur ein Integer Identifier genutzt wurde, ich aber ein String benötige!

Hier die Schritte zum einrichten:



Download von github

Code: Select all

wget https://github.com/FreeOpcUa/python-opcua/archive/master.zip
 
entpacke zip

Code: Select all

unzip master.zip
 
Python Setuptools installieren

Code: Select all

sudo apt-get install python-setuptools 
 
Python pip installieren

Code: Select all

sudo apt-get install python-pip
 
Folgende Packete installieren gemäß freeopcUa Webseite

Code: Select all

sudo pip install enum34
sudo pip install trollius
sudo pip install futures


sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo pip install cryptography
sudo pip install dateutil
 
FreeOpcUa installieren, dazu ins Verzeichnis von FreeOpcUa wechseln

Code: Select all

python setup.py build
sudo python setup.py install
 
Aufräumen

Code: Select all

rm master.zip
 
Und hier das Python Script um auf eine B&R Steuerung zuzugreifen:

Code: Select all

import time
sys.path.insert(0, "..")


from opcua import Client


if __name__ == "__main__":

    ip = "192.168.100.250"
    port = "4840"
    variable = ["Variable_Bool","Variable_INT","Variable_Real","Variable_String"]
    
    while 1:
        if os.system("fping -t 250 "+ip) == 0:
            print "B&R ist erreichbar"
            break
        else:
            print "B&R nicht erreichbar!"
            
    client = Client("opc.tcp://"+ip+":"+port+"/server/")
    try:
        client.connect()

        # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
        root = client.get_root_node()
    
        while 1:
            anzahlVar = len(variable)
            i = 0
            while i < anzahlVar:
                # get a specific node knowing its node id
                var = client.get_node("ns=6;s=::AsGlobalPV:"+variable[i])
                varvalue = var.get_value()
                #print varvalue
                # Wert ablegen in virtueller Datei
                fobj_out = open("/home/pi/tmp_br/"+variable[i],"w")
                fobj_out.write(str(varvalue))
                fobj_out.close()
                time.sleep(1) # delays for 1 seconds
                i = i + 1
        

    finally:
        client.disconnect()

Danke für die Hilfe,

Bernhard
Last edited by scrolllkock on 14 Dec 2016, 16:35, edited 1 time in total.
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Opensource OPC UA für den Rasp

Post by volker »

Bernhard, auch Dir ein fettes Lob für diesen ausführlichen Beitrag! Das wird sicher vielen helfen...
Unser RevPi Motto: Don't just claim it - make it!
scrolllkock
Posts: 16
Joined: 03 Nov 2016, 17:14
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by scrolllkock »

Habe das python script nochmal angepasst.

Das Script wird beim Booten im Hintergrund gestartet und wartet, bis die SPS erreichbar ist.
Erst dann versucht der Client einen Verbindungsaufbau.
Die zu lesenden Variablen werden oben in ein Array eingetragen.
Im Script werden dann die vorbelegten Variablen ausgelesen und der Inhalt der Variable in eine Datei mit dem Namen der Variable eingetragen.
Das Verzeichnis habe ich vorher ins Ram gelegt, damit ich nicht so viele Schreibzyklen auf dem Speichermedium habe.

Damit können andere Programme, egal ob bash, python oder c, auch auf die Daten zugreifen.
mimuel
Posts: 7
Joined: 22 Nov 2016, 17:22
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by mimuel »

Hallo Bernhardt,

dann bist Du ja selbst zum Ziel gekommen.
Mit dem vorgeschlagenen Client/Browser kannst Du die Veränderungen im Informationsmodell sehr plastisch anschauen.
Auf GitHub gibt es auch einen Browser geschrieben mir der Python Library.
schau mal hier : https://github.com/FreeOpcUa

Gruß Michael
mimuel
Posts: 7
Joined: 22 Nov 2016, 17:22
Answers: 0

Re: Opensource OPC UA für den Rasp

Post by mimuel »

Sorry Bernhard,

den Link kanntest Du schon - ich meinte diesen:
https://github.com/FreeOpcUa/opcua-client-gui

Ich danke Dir für den B&R-Client - der kann mir auch noch nützlich sein,
obwohl ja OPC UA ja kein proprietäres Protokoll ist.

Gruß Michael
Post Reply