{"id":998,"date":"2021-08-28T18:06:55","date_gmt":"2021-08-28T10:06:55","guid":{"rendered":"https:\/\/104.es\/?p=998"},"modified":"2021-08-28T18:06:55","modified_gmt":"2021-08-28T10:06:55","slug":"raspi-opc-ua-server","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2021\/08\/28\/raspi-opc-ua-server\/","title":{"rendered":"\u4f7f\u7528\u6a39\u8393\u6d3e\u5efa\u7acbOPC UA Server"},"content":{"rendered":"\n<p>\u6a39\u8393\u6d3e\u56e0\u70ba\u53ef\u4ee5\u9023\u63a5\u611f\u6e2c\u5668\u53d6\u5f97\u6240\u9700\u8981\u7684\u74b0\u5883\u8cc7\u8a0a\uff0c\u4e14\u53c8\u5177\u5099Linux\u4f5c\u696d\u7cfb\u7d71\u53ef\u4ee5\u5b89\u88dd\u8a31\u591a\u8edf\u9ad4\u53ca\u4f3a\u670d\u5668\uff0c\u56e0\u6b64\u5f88\u9069\u5408\u4f5c\u70ba\u7269\u806f\u7db2\u7cfb\u7d71\u4e2d\u7684\u908a\u7de3\u904b\u7b97\u88dd\u7f6e\uff08Edge Device\uff09\u3002\u5728\u5be6\u969b\u5de5\u5ee0\u751f\u7522\u7dda\u7684\u61c9\u7528\u4e0a\uff0cOPC UA\u662f\u73fe\u5728\u975e\u5e38\u6d41\u884c\u7684\u5354\u5b9a\u4e4b\u4e00\uff0c\u672c\u6587\u7684\u76ee\u7684\u5373\u5728\u65bc\u793a\u7bc4\u5982\u4f55\u5728\u6a39\u8393\u6d3e\u4e0a\u5b89\u88ddOPC UA Server\uff0c\u8b93\u5b83\u53ef\u4ee5\u88ab\u5176\u5b83\u88dd\u7f6e\u4ee5OPC UA\u7684\u5354\u5b9a\u8b80\u53d6\u5b83\u6240\u611f\u6e2c\u5230\u7684\u8cc7\u6599\u3002<\/p>\n\n\n\n<p>\u5728\u672c\u6587\u4e2d\u7684\u6a39\u8393\u6d3e\u6240\u9023\u63a5\u7684\u611f\u6e2c\u5668\u5ef6\u7e8c\u524d\u9762\u6587\u7ae0\u4e2d\u7684\u6eab\u6fd5\u5ea6\u4ee5\u53ca\u8072\u97f3\u548c\u4eae\u5ea6\u611f\u6e2c\u5668\uff0c\u6211\u5011\u7684\u76ee\u7684\u662f\u85c9\u7531OPC UA\u7684\u5354\u5b9a\uff0c\u5728\u4e0d\u540c\u88dd\u7f6e\u9593\u50b3\u9001\u9019\u4e9b\u611f\u6e2c\u5668\u7684\u8cc7\u6599\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u5728\u5176\u4e2d\u4e00\u53f0\u6a39\u8393\u6d3e\u4e0a\u57f7\u884cOPCUA Server\uff0c\u5728\u53e6\u5916\u4e00\u53f0\u6a5f\u5668\u4e0a\uff08\u53ef\u4ee5\u662f\u6a39\u8393\u6d3e\uff0c\u4e5f\u53ef\u4ee5\u662fPC\uff09\u57f7\u884c\u5ba2\u6236\u7aef\u7a0b\u5f0f\u4ee5\u53d6\u5f97\u4f3a\u670d\u5668\u4e0a\u7684\u8cc7\u6599\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u662f\u4f3a\u670d\u5668\u7684\u90e8\u4efd\uff0c\u5728\u6a39\u8393\u6d3e\u7684\u4f5c\u696d\u7cfb\u7d71\u4e0b\uff0c\u8981\u5148\u57f7\u884c\u4ee5\u4e0b\u7684\u5b89\u88dd\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update\nsudo apt-get -y upgrade\nsudo apt-get install libxml2-dev libxmlsec1-dev libffi-dev<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u6307\u4ee4\u4e3b\u8981\u662f\u5b89\u88dd\u5728OPC UA Server\u6a21\u7d44\u4e2d\u53ef\u80fd\u6703\u4f7f\u7528\u5230\u7684\u958b\u767c\u7a0b\u5f0f\u5eab\u3002\u63a5\u8457\u8981\u5b89\u88ddPython\u6240\u9700\u8981\u7684\u5957\u4ef6\uff0c\u4e00\u822c\u800c\u8a00\uff0c\u5efa\u8b70\u6240\u6709\u57f7\u884c\u7684\u5c08\u6848\u90fd\u70ba\u5176\u5efa\u7acb\u4e00\u500bPython\u7684\u865b\u64ec\u74b0\u5883\uff0c\u5728\u865b\u64ec\u74b0\u5883\u4e2d\u5b89\u88dd\u500b\u5225Python\u5c08\u6848\u6240\u9700\u8981\u7684\u6a21\u7d44\u53ca\u5957\u4ef6\u6703\u6bd4\u8f03\u55ae\u7d14\u4e14\u4e0d\u6613\u51fa\u932f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install cryptograpy\npip install freeopcua<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u6a21\u7d44\u9806\u5229\u5b89\u88dd\u5b8c\u7562\u4e4b\u5f8c\uff0c\u5148\u4f86\u770b\u4f3a\u670d\u5668\u7aef\u7684\u7a0b\u5f0f\u78bc\uff08opcua_server.py\uff09\uff0c\u6211\u5011\u628a\u76ee\u524d\u4f3a\u670d\u5668\u7684ip\u653e\u5728ip.txt\u6a94\u6848\u4e2d\uff0c\u5176\u4e2dip.txt\u7684\u5167\u5bb9\u770b\u8d77\u4f86\u6703\u662f\u50cf\u4e0b\u9762\u9019\u500b\u6a23\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>192.168.73.204<\/code><\/pre>\n\n\n\n<p>ip.txt\u4e2d\u7684\u5167\u5bb9\uff0c\u5c31\u662f\u76ee\u524d\u4f60\u7684\u6a39\u8393\u6d3e\u672c\u8eab\u7684IP\u4f4d\u7f6e\uff0c\u9019\u500b\u503c\u8981\u78ba\u5b9a\u662f\u80fd\u5920\u88ab\u9023\u7dda\u7684\uff0c\u4e4b\u5f8c\u5ba2\u6236\u7aef\u7a0b\u5f0f\u4f7f\u7528\u540c\u6a23\u7684IP\u624d\u80fd\u5920\u9806\u5229\u9023\u7dda\u3002\u63a5\u8457\u624d\u662f\u6211\u5011\u7684\u4e3b\u7a0b\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from opcua import Server\nfrom random import randint\nimport datetime\nfrom gpiozero import MCP3008\nimport time\nimport Adafruit_DHT\n\nGPIO_PIN = 4\n\nhumdity, temperature, light, sound = 0, 0, 0, 0\nlight_adc = MCP3008(0)\nsound_adc = MCP3008(1)\n\nwith open(\"ip.txt\", \"r\") as f:\n    ip = f.read().strip()\nserver =Server()\nurl =\"opc.tcp:\/\/{}:4840\".format(ip)\nprint(\"Path:\", url)\nserver.set_endpoint(url)\n\nname =\"OPCUA_SIMULATION_SERVER\"\naddspace =server.register_namespace(name)\n\nnode=server.get_objects_node()\n\nParam=node.add_object(addspace,\"Parameters\")\n\nTemperature = Param.add_variable(addspace,\"Temperature\",0)\nHumidity = Param.add_variable(addspace,\"Humidity\",0)\nSound =Param.add_variable(addspace,\"Sound\",0)\nLight = Param.add_variable(addspace,\"Light\",0)\n\nTemperature.set_writable()\nHumidity.set_writable()\nSound.set_writable()\nLight.set_writable()\n\nserver.start()\n\nwhile(True):\n    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, GPIO_PIN)\n    sound = sound_adc.value\n    light = light_adc.value\n\n    #print(temperature, humidity, sound, light)\n\n    Temperature.set_value(temperature)\n    Humidity.set_value(humidity)\n    Light.set_value(light)\n    Sound.set_value(sound)\n    # time.sleep(2)\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u5229\u7528\u524d\u9762\u6559\u5b78\u6587\u7ae0\u4e2d\u4ecb\u7d39\u7684\u5167\u5bb9\u53d6\u5f97\u4e86\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\u3001\u8072\u97f3\u611f\u6e2c\u5668\u3001\u4ee5\u53ca\u4eae\u5ea6\u611f\u6e2c\u5668\u7684\u5167\u5bb9\uff0c\u5206\u5225\u653e\u5728temperature, humidity, sound, light\u9019\u5e7e\u500b\u8b8a\u6578\u4e2d\uff0c\u800c\u5728\u4e00\u958b\u59cb\u5b9a\u7fa9OPC UA\u4f3a\u670d\u5668\u53ef\u4ee5\u88ab\u5b58\u53d6\u7684\u8cc7\u6599\u4e2d\uff0c\u5206\u5225\u662fTemperature\u3001Humidity\u3001Sound\u3001Light\u7b494\u500b\u7269\u4ef6\uff0c\u628a\u5b83\u5011\u8a2d\u5b9a\u6210OPC UA\u4f3a\u670d\u5668\u4e2d\u53ef\u4ee5\u63d0\u4f9b\u5ba2\u6236\u7aef\u8b80\u53d6\u7684\u53c3\u6578\uff0c\u518d\u5229\u7528\u8ff4\u5708\u4e0d\u65b7\u5730\u5beb\u5165\u65b0\u7684\u8cc7\u6599\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5247\u662fOPC UA\u5ba2\u6236\u7aef\u7684\u7a0b\u5f0f\u78bc\uff08opcua_client.py\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from opcua import Client\nimport time \n\nwith open(\"ip.txt\", \"r\") as f:\n    ip = f.read().strip()\nurl=\"opc.tcp:\/\/{}:4840\".format(ip)\nprint(\"Client URL:\", url)\n\nclient=Client(url)\n\nclient.connect()\nprint(\"Client Connected\")\nwhile (True):\n    Temperature=client.get_node(\"ns=2;i=2\")\n    temperature=Temperature.get_value()\n    Humidity=client.get_node(\"ns=2;i=3\")\n    humidity=Humidity.get_value()\n    print(temperature, humidity)\n    \n    Sound=client.get_node(\"ns=2;i=4\")\n    sound=Sound.get_value()\n    Light=client.get_node(\"ns=2;i=5\")\n    light=Light.get_value()\n    print(sound, light)\n    time.sleep(1)<\/code><\/pre>\n\n\n\n<p>\u5728\u5ba2\u6236\u7aef\u4e2d\u6211\u5011\u8b80\u53d6\u540c\u4e00\u500b\u5167\u5bb9\u7684ip.txt\uff0c\u5229\u7528\u8a72\u6a94\u6848\u7684\u5167\u5bb9\u4f5c\u70ba\u4f3a\u670d\u5668\u7684IP\u4f4d\u5740\u9032\u884c\u9023\u7dda\u3002\u9023\u7dda\u5b8c\u6210\u4e4b\u5f8c\uff0c\u518d\u5229\u7528\u4e00\u500b\u7121\u7aae\u8ff4\u5708\u53d6\u51faOPC UA\u6240\u63d0\u4f9b\u7684\u8cc7\u6599\u3002<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u90fd\u5efa\u7acb\u5b8c\u6210\u4e4b\u5f8c\uff0c\u5148\u57f7\u884copcua_server.py\uff0c\u904e\u4e00\u6bb5\u6642\u9593\u7b49\u51fa\u73fe\u7b49\u5f85\u8a0a\u606f\u4e4b\u5f8c\uff0c\u5982\u679c\u662f\u540c\u4e00\u53f0\u6a5f\u5668\u9032\u884c\u6e2c\u8a66\uff0c\u5247\u53ef\u518d\u958b\u53e6\u5916\u4e00\u500b\u7d42\u7aef\u6a5f\u57f7\u884copcua_client.py\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a39\u8393\u6d3e\u56e0\u70ba\u53ef\u4ee5\u9023\u63a5\u611f\u6e2c\u5668\u53d6\u5f97\u6240\u9700\u8981\u7684\u74b0\u5883\u8cc7\u8a0a\uff0c\u4e14\u53c8\u5177\u5099Linux\u4f5c\u696d\u7cfb\u7d71\u53ef\u4ee5\u5b89\u88dd\u8a31\u591a\u8edf\u9ad4\u53ca\u4f3a\u670d\u5668\uff0c\u56e0\u6b64\u5f88\u9069\u5408\u4f5c\u70ba\u7269\u806f\u7db2\u7cfb\u7d71\u4e2d\u7684\u908a\u7de3\u904b\u7b97\u88dd\u7f6e\uff08Edge Device\uff09\u3002\u5728\u5be6\u969b\u5de5\u5ee0\u751f\u7522\u7dda\u7684\u61c9\u7528\u4e0a\uff0cOPC UA\u662f\u73fe\u5728\u975e\u5e38\u6d41\u884c\u7684\u5354\u5b9a\u4e4b\u4e00\uff0c\u672c\u6587\u7684\u76ee\u7684\u5373\u5728\u65bc\u793a\u7bc4\u5982\u4f55\u5728\u6a39\u8393\u6d3e\u4e0a\u5b89\u88ddOPC UA Server\uff0c\u8b93\u5b83\u53ef\u4ee5\u88ab\u5176\u5b83\u88dd\u7f6e\u4ee5OPC UA\u7684\u5354\u5b9a\u8b80\u53d6\u5b83\u6240\u611f\u6e2c\u5230\u7684\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-998","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}