{"id":1794,"date":"2021-01-11T11:35:17","date_gmt":"2021-01-11T17:35:17","guid":{"rendered":"https:\/\/www.dalemorin.com\/?p=1794"},"modified":"2021-01-11T16:47:47","modified_gmt":"2021-01-11T22:47:47","slug":"raspberry-pi-project-swapping-sensors-part-2","status":"publish","type":"post","link":"https:\/\/www.dalemorin.com\/?p=1794","title":{"rendered":"Raspberry Pi project &#8211; swapping sensors, part 2"},"content":{"rendered":"<p>Now that I&#8217;ve laid out my reasons for trying a different type of sensor, let&#8217;s dig in.\u00a0 We need a couple of libraries that the DHT22 sensors didn&#8217;t need, so we should go ahead and get them installed.<\/p>\n<p>First, let&#8217;s install the smbus2 library. At a command prompt, type:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo pip install smbus2\r\n<\/pre>\n<p>Now, let&#8217;s install the library for the SME280 sensor. At a command prompt, type:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo pip install RPi.bme280\r\n<\/pre>\n<p>That takes care of the additional libraries we&#8217;ll need for the python script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1801\" src=\"https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_941.png\" alt=\"\" width=\"307\" height=\"379\" srcset=\"https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_941.png 307w, https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_941-243x300.png 243w\" sizes=\"auto, (max-width: 307px) 100vw, 307px\" \/><\/p>\n<p>Now, let&#8217;s connect the sensors to the Raspberry Pi. I&#8217;m using the model 4 rev B, so I have a 40-pin GPIO header. We&#8217;re going to use six pins to connect our two sensors. We&#8217;re going to connect each sensor to it&#8217;s own 3.3v source and it&#8217;s own ground. We&#8217;ll use RPi4 pins 1 and 6 for 3.3v and ground for the upstairs sensor, and pins 17 and 9 for 3.3v and ground for the downstairs sensor. We&#8217;ll connect the two clock lines together and they will connect to RPi4 pin 5; then we&#8217;ll connect the two data lines together and they will connect to RPi4 pin 3.\u00a0 Since the two sensors will have a unique address on the bus, we can connect them electrically but interrogate them separately.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1803\" src=\"https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_942.png\" alt=\"\" width=\"248\" height=\"319\" srcset=\"https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_942.png 248w, https:\/\/www.dalemorin.com\/wordpress\/wp-content\/uploads\/Selection_942-233x300.png 233w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/>We also need to make a small alteration to one of the sensors to change it&#8217;s address on the i2c bus.\u00a0 In the following image, the red mark indicates the we are removing the connection between the left and center pad, and adding a connection from the center pad to the right pad.\u00a0 We are only altering one of the two sensor boards in this manner.\u00a0 The unaltered sensor will have bus address 0x76, and the altered sensor will have address 0x77.\u00a0 The test script below is coded so that the upstairs sensor is the unaltered one, and the altered sensor is the downstairs sensor.<\/p>\n<p>This sensor test script interrogates the sensors and then displays the returned values separately, followed by a string containing the data. Note that python is sensitive to indentation. I&#8217;ve added a line of code to convert the celcius temperature into fahrenheit. This line is optional. The full code for our test script follows.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python3\r\n\r\nimport time\r\nimport smbus2\r\nimport bme280\r\n\r\nport = 1\r\nupstairs_addr = 0x76\r\ndownstairs_addr = 0x77\r\nbus = smbus2.SMBus(port)\r\n\r\ncalibration_params_upstairs = bme280.load_calibration_params(bus, upstairs_addr)\r\ncalibration_params_downstairs = bme280.load_calibration_params(bus, downstairs_addr)\r\n\r\n#exit()\r\n\r\nwhile True:\r\n    # interrogate the sensors and supply the calibration data we obtained earlier\r\n    upstairs_data = bme280.sample(bus, upstairs_addr, calibration_params_upstairs)\r\n    downstairs_data = bme280.sample(bus, downstairs_addr, calibration_params_downstairs)\r\n\r\n    # the compensated data\r\n    print(upstairs_data.id)\r\n    print(upstairs_data.timestamp)\r\n    print(upstairs_data.temperature)\r\n    ftemp_upstairs = upstairs_data.temperature * 9\/5.0 + 32\r\n    print(ftemp_upstairs)\r\n    print(upstairs_data.pressure)\r\n    print(upstairs_data.humidity)\r\n\r\n    # in string format\r\n    print(upstairs_data)\r\n\r\n    # the compensated data\r\n    print(downstairs_data.id)\r\n    print(downstairs_data.timestamp)\r\n    print(downstairs_data.temperature)\r\n    ftemp_downstairs = downstairs_data.temperature * 9\/5.0 + 32\r\n    print(ftemp_downstairs)\r\n    print(downstairs_data.pressure)\r\n    print(downstairs_data.humidity)\r\n\r\n    # in string format\r\n    print(downstairs_data)\r\n\r\n    time.sleep(15)\r\n<\/pre>\n<p>Once this is working and successfully retrieving and displaying the sensor data, we&#8217;ll move on to the changes needed to our data logger script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that I&#8217;ve laid out my reasons for trying a different type of sensor, let&#8217;s dig in.\u00a0 We need a couple of libraries that the DHT22 sensors didn&#8217;t need, so we should go ahead and get them installed. First, let&#8217;s install the smbus2 library. At a command prompt, type: sudo pip install smbus2 Now, let&#8217;s &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.dalemorin.com\/?p=1794\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Raspberry Pi project &#8211; swapping sensors, part 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-1794","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1794"}],"version-history":[{"count":12,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1794\/revisions"}],"predecessor-version":[{"id":1808,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=\/wp\/v2\/posts\/1794\/revisions\/1808"}],"wp:attachment":[{"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dalemorin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}