{"id":118,"date":"2018-02-27T14:27:16","date_gmt":"2018-02-27T11:27:16","guid":{"rendered":"http:\/\/itbru.ru\/?p=118"},"modified":"2018-02-27T15:12:28","modified_gmt":"2018-02-27T12:12:28","slug":"monitoring-raspberrypi-python-mqtt","status":"publish","type":"post","link":"https:\/\/itbru.ru\/index.php\/2018\/02\/27\/monitoring-raspberrypi-python-mqtt\/","title":{"rendered":"\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 RaspberryPi \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Python \u0438 MQTT."},"content":{"rendered":"<p>\u0417\u0430\u0432\u0435\u043b \u0441\u0435\u0431\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0434\u043e\u043c\u0430\u0448\u043d\u044e\u044e RaspberryPi. \u0418 \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0438\u043c\u0435\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433, \u0434\u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433, \u0430 \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043f\u0430\u0434\u0430\u043b\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 IoBroker. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 IoBroker \u044f \u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c MQTT. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b Raspberry \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0433\u043e \u0436\u0435.<\/p>\n<p><!--more--><\/p>\n<p>\u0415\u0441\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f Python \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u043e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 MQTT. \u041d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430\u00a0\u00a0<a href=\"https:\/\/pypi.python.org\/pypi\/paho-mqtt\">Paho-MQTT<\/a> , \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 PIP. \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e Python \u0438 Paho-MQTT \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0435\/\u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u0438 \u043e\u0431\u044a\u0435\u043c\u0435 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u043d\u0430 \u0434\u0438\u0441\u043a\u0435:<\/p>\n<pre class=\"lang:python decode:true  \">import paho.mqtt.publish as publish\r\nimport os\r\nimport time\r\n\r\n# \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u043e\u043f\u0438\u043a\u0430 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.\r\ndef pub (topic, val):\r\n        publish.single(\"raspberry\/hardware\/\"+topic, val, client_id=\"RasPI\", hostname=\"%IP%\", auth={'username':\"%Username%\",'password':\"%Password%\"})\r\n\r\n# \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430\r\ndef getCPUtemperature():\r\n    res = os.popen('vcgencmd measure_temp').readline()\r\n    return(res.replace(\"temp=\",\"\").replace(\"'C\\n\",\"\"))\r\n\r\n# \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430\r\ndef getCPUuse():\r\n    return(str(os.popen(\"top -b -n1 | awk '\/Cpu\\(s\\):\/ {print $2}'\").readline().strip(\\\r\n)))\r\n\r\n# \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0434\u0438\u0441\u043a\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\r\n# Index 0: \u041e\u0431\u044a\u0435\u043c \u0434\u0438\u0441\u043a\u0430\r\n# Index 1: \u0417\u0430\u043d\u044f\u0442\u043e \u043d\u0430 \u0434\u0438\u0441\u043a\u0435\r\n# Index 2: \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e\r\n# Index 3: \u0417\u0430\u043d\u044f\u0442\u043e \u043d\u0430 \u0434\u0438\u0441\u043a\u0435 \u0432 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u0430\u0445 \r\ndef getDiskSpace():\r\n    p = os.popen(\"df -h \/\")\r\n    i = 0\r\n    while 1:\r\n        i = i +1\r\n        line = p.readline()\r\n        if i==2:\r\n            return(line.split()[1:5])\r\n\r\n# \u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0437 \u0432 5 \u043c\u0438\u043d\u0443\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \r\n# \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f MQTT \u0431\u0440\u043e\u043a\u0435\u0440\u0443\r\nwhile True:\r\n        disk = getDiskSpace()\r\n        pub(\"CPU_temp\",getCPUtemperature())\r\n        pub(\"CPU_load\",getCPUuse())\r\n        pub(\"Disk_used\",disk[3])\r\n        time.sleep(300)\r\n<\/pre>\n<p>\u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c IP, login \u0438 password \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a MQTT \u0431\u0440\u043e\u043a\u0435\u0440\u0443. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b 4 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0432\u044b\u0432\u043e\u0434\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0434\u0438\u0441\u043a\u0435. \u0412 \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432\u044b\u0432\u043e\u0434 \u0432 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u0430\u0445.<\/p>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432 \u0444\u043e\u043d\u0435 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e supervisord.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0417\u0430\u0432\u0435\u043b \u0441\u0435\u0431\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0434\u043e\u043c\u0430\u0448\u043d\u044e\u044e RaspberryPi. \u0418 \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0438\u043c\u0435\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433, \u0434\u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433, \u0430 \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043f\u0430\u0434\u0430\u043b\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 IoBroker. \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,13],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-iobroker","category-smart-home"],"_links":{"self":[{"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":2,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/posts\/118\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itbru.ru\/index.php\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}