{"id":264,"date":"2019-10-12T00:54:05","date_gmt":"2019-10-11T22:54:05","guid":{"rendered":"https:\/\/petr.maxbox.cz\/?p=264"},"modified":"2019-10-16T21:56:20","modified_gmt":"2019-10-16T19:56:20","slug":"temperature-and-humidity-transmitter-sht20-sensor-modbus-rs485-python","status":"publish","type":"post","link":"https:\/\/petr.maxbox.cz\/index.php\/2019\/10\/12\/temperature-and-humidity-transmitter-sht20-sensor-modbus-rs485-python\/","title":{"rendered":"Python MinimalModbus + SHT20 Modbus RTU RS485 Temperature and humidity sensor"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<figure class=\"wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1000\" src=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1.png\" alt=\"\" data-id=\"270\" data-link=\"https:\/\/petr.maxbox.cz\/index.php\/2019\/10\/12\/temperature-and-humidity-transmitter-sht20-sensor-modbus-rs485-python\/rs485-high-precision-sht20-temperature-humidity-sensor-2\/\" class=\"wp-image-270\" srcset=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1.png 1000w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1-150x150.png 150w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1-300x300.png 300w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1-768x768.png 768w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-1-100x100.png 100w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1000\" src=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB.png\" alt=\"\" data-id=\"269\" data-link=\"https:\/\/petr.maxbox.cz\/index.php\/2019\/10\/12\/temperature-and-humidity-transmitter-sht20-sensor-modbus-rs485-python\/rs485-high-precision-sht20-temperature-humidity-sensor-pcb\/\" class=\"wp-image-269\" srcset=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB.png 1000w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB-150x150.png 150w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB-300x300.png 300w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB-768x768.png 768w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/RS485-High-Precision-SHT20-Temperature-Humidity-Sensor-PCB-100x100.png 100w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"308\" src=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/Screenshot-from-2019-10-12-03-55-17.png\" alt=\"\" data-id=\"302\" data-full-url=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/Screenshot-from-2019-10-12-03-55-17.png\" data-link=\"https:\/\/petr.maxbox.cz\/index.php\/2019\/10\/12\/temperature-and-humidity-transmitter-sht20-sensor-modbus-rs485-python\/screenshot-from-2019-10-12-03-55-17\/\" class=\"wp-image-302\" srcset=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/Screenshot-from-2019-10-12-03-55-17.png 348w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/10\/Screenshot-from-2019-10-12-03-55-17-300x266.png 300w\" sizes=\"(max-width: 348px) 100vw, 348px\" \/><\/figure><\/li><\/ul><\/figure>\n<\/div><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#! \/usr\/bin\/python\nimport minimalmodbus\nimport time\n# Set the value minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True \n# immediately after import minimalmodbus\nminimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True\n\n# default slave address = 1\ninstr = minimalmodbus.Instrument(port='\/dev\/ttyUSB0', slaveaddress=1)\ninstr.serial.baudrate = 9600\ninstr.serial.parity  = \"N\"\ninstr.serial.timeout  = 0.8\n#instr.debug = True\nprint(instr)\n\n\"\"\"\nread slave address, baud rate, temp. correction, hum. correction\n\"\"\"\nprint(instr.read_registers(257, 4,functioncode=3))\n\n\"\"\"\nchange of the slave address \nthe change will take effect after reboot only! \nit is possible to continue using the old address till reboot\n\"\"\"\nslaveNewAddress = 5\n#instr.write_register(257, slaveNewAddress, functioncode=6)\n\n\"\"\"\nchange of the speed - take effect imediatelly\nthe change will take effect after reboot only! \nit is possible to continue using the old address till reboot\n\"\"\"\nslaveNewBaudrate = 9600\n#instr.write_register(258, slaveNewBaudrate, functioncode=6) \n\n# read slave address, baud rate, temp. correction, hum. correction\nprint(instr.read_registers(257, 4,functioncode=3)) \n\ntime.sleep(1)\n\nwhile 1:\n    try:\n        print instr.read_registers(1,2, functioncode=4) # read temp and hum (list)\n        print instr.read_register(1,1, functioncode=4) # read temp \n        print instr.read_register(2,1, functioncode=4) # read hum \n    except minimalmodbus.NoResponseError:\n        print(\"No Response Error\")\n    time.sleep(1)\n<\/pre>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@danfoss:\/home\/pi\/danfoss# sudo pip install -U minimalmodbus\nCollecting minimalmodbus\n  Downloading https:\/\/files.pythonhosted.org\/packages\/6c\/d5\/77d42e8a0b73da2b5f97acd91900ac50e303b4cb959f76350cfbb38e05a0\/minimalmodbus-1.0.2-py2.py3-none-any.whl\nRequirement already up-to-date: pyserial>=3.0 in \/usr\/local\/lib\/python2.7\/dist-packages (from minimalmodbus)\nInstalling collected packages: minimalmodbus\n  Found existing installation: MinimalModbus 0.7\n    Uninstalling MinimalModbus-0.7:\n      Successfully uninstalled MinimalModbus-0.7\nSuccessfully installed minimalmodbus-1.0.2\nroot@danfoss:\/home\/pi\/danfoss# \n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Description of the module <a href=\"https:\/\/www.maxbox.cz\/nextcloud\/index.php\/s\/rmMKR62jMFeiXPJ\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description of the module here<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,5,6,3],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-nezarazene","tag-modbus","tag-python","tag-rs485","tag-sht20"],"_links":{"self":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/comments?post=264"}],"version-history":[{"count":24,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/264\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}