vitormhenrique / OctoPrint-Enclosure

OctoPrint Enclosure Plugin
GNU General Public License v3.0
396 stars 202 forks source link

I2C Mux Support with BME280 Temperature / Humidity Sensor #391

Open wickedbeernut opened 3 years ago

wickedbeernut commented 3 years ago

Would you please consider supporting an TCA9548A I2C 8-channel multiplexer (e.g., SparkFun Qwiic Mux Breakout BOB-16784) in order to enable more than two BME280 temperature / humidity sensors (addresses 0x76 and 0x77)? In addition to an enclosure temperature / humidity sensor, I would like to enable a temperature / humidity sensor corresponding to up to five filament dryers. The mux may (should?) support other I2C temperature / humidity sensors such as SI7021, TMP102 and MCP9808.

The Python syntax is straightforward,

import qwiic_tca9548a
mux = qwiic_tca9548a.QwiicTCA9548A()
mux.enable_channels(CHANNEL)
# read BME280 temperature and humidity here
mux.disable_channels(CHANNEL)
wickedbeernut commented 3 years ago

Here's a screen shot of the Dashboard plugin with four BME280 sensors ("Enclosure", "Dryer 1", "Dryer 2", and "Dryer 3" ... all four with address 0x77) and two TMP102 sensors ("Dryer 4" and "Dryer 5" ... both with address 0x48). For this proof of concept, I entered the mux channel number (0x00 - 0x07) as the "high byte" in the sensor address field (e.g., 0x0477). Here's a summary of the changes to BME280.py. Equivalent changes were made to tmp102.py.

import smbus
import sys
import time
import qwiic_tca9548a ###
from ctypes import c_short
from ctypes import c_byte
from ctypes import c_ubyte

if len(sys.argv) == 2:
    CHANNEL = int(sys.argv[1],16) >> 8 ###
    DEVICE = int(sys.argv[1],16) & 0xFF ###
else:
    print('-1 | -1')
    sys.exit(1)

bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1
                     # Rev 1 Pi uses bus 0

mux = qwiic_tca9548a.QwiicTCA9548A() ###

# ...

def main():
  try:
    mux.enable_channels(CHANNEL) ###
    temperature,pressure,humidity = readBME280All()
    mux.disable_channels(CHANNEL) ###
    print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity))
  except:
     print('-1 | -1')

if __name__=="__main__":
   main()

bcd

cde