如何用pythoni2ctca9548a从多路复用器读取数据

2024-10-01 22:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

基于以下链接: https://forums.adafruit.com/viewtopic.php?f=19&t=83918&p=541562&hilit=TCA9548A#p541562https://github.com/kizniche/Mycodo/issues/43#issuecomment-156718451

我创建了以下版本:

在多路复用器.py在

#!/usr/bin/python

# Change channel of TCA9548A
# Example: sudo ./multiplexer_channel.py 0

import smbus
import time
import sys

I2C_address = 0x77
I2C_bus_number = 1
I2C_ch_0 = 0b00000001
I2C_ch_1 = 0b00000010
I2C_ch_2 = 0b00000100
I2C_ch_3 = 0b00001000
I2C_ch_4 = 0b00010000
I2C_ch_5 = 0b00100000
I2C_ch_6 = 0b01000000
I2C_ch_7 = 0b10000000

def I2C_setup(i2c_channel_setup):
    bus = smbus.SMBus(I2C_bus_number)
    bus.write_byte(I2C_address,i2c_channel_setup)
    time.sleep(0.1)
    print "TCA9548A I2C channel status:", bin(bus.read_byte(I2C_address))

I2C_setup(int(sys.argv[1]))

以及 索引2.py

^{pr2}$

如果我用参数0到7(多路复用器端口)执行第一个文件,总是会出现连接超时。在

请注意,我使用的是树莓皮3和一切都是连接。在

如果我执行第二个文件,我会得到读数,但它们总是误导性的(固定读数),顺便说一句,如果我有很多传感器,第二个文件将无法工作(BMP180)

编辑1: 1当我们尝试i2cdetect y时,我们什么也得不到。 2我们找到了另一个Pythong程序来处理多路复用器

# coding=utf-8

import argparse
import smbus
import time


class TCA9548A(object):
    def __init__(self, bus, address=0x70, ):
        self.i2c_address = address
        self.i2c_bus = bus
        self.bus = smbus.SMBus(self.i2c_bus)

    def setup(self, channel):
        try:
            self.bus.write_byte(self.i2c_address, channel)
            return 1, "Success"
        except Exception as msg:
            return 0, "Fail: {}".format(msg)

    def read(self):
        time.sleep(0.1)
        return self.bus.read_byte(self.i2c_address)


def menu():
    parser = argparse.ArgumentParser(description='Select I2C address and channel of TCA9548A I2C multiplexer')
    parser.add_argument('-a', '--address', metavar='ADDRESS', type=int,
                        help='I2C address of the multiplexer, only last two digits, (ex. enter "70" if 0x70)',
                        required=True)
    parser.add_argument('-b', '--bus', metavar='BUS', type=int,
                        help='I2C bus of the multiplexer',
                        required=True)
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-c', '--channel', metavar='CHANNEL', type=int,
                       help='Channel to be activated with the multiplexer')
    group.add_argument('-r', '--read', action='store_true',
                       help='Only read multiplexer and return channel number')

    args = parser.parse_args()

    i2c_address = int(str(args.address), 16)
    multiplexer = TCA9548A(args.bus, i2c_address)
    if args.channel:
        multiplexer.setup(args.channel)
    read_response = multiplexer.read()
    print("TCA9548A I2C channel status: {} (channel {})".format(bin(read_response), read_response))


if __name__ == "__main__":
    menu()

当我们试图这样执行时:

sudo python index3py -a 70 -b 1

我们还有一个连接超时

我开始相信我们之间有一些错误的联系。在

我们用了adafruit布线样品

编辑2:有照片吗

enter image description here

enter image description here

enter image description here

编辑3: 我重新启动了PI,现在我可以看到地址为77的i2c。在

然而,我得到了误导性的结果,我尝试了所有7个频道,同一个程序(index2.py)更改为70到77。我的体温总是不合理。在

pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 0
TCA9548A I2C channel status: 0b11111000 (channel 248)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.599998474
pressure: 40287
altitude: -31.4230690002

temperature: -52.5
pressure: 40183
altitude: -31.5887317657

temperature: -52.5
pressure: 40281
altitude: -37.2313270569

temperature: -52.5
pressure: 40284
altitude: -34.078414917

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
keyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 1
TCA9548A I2C channel status: 0b1 (channel 1)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40166
altitude: -31.7548980713

temperature: -52.4000015259
pressure: 40163
altitude: -34.5762786865

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 2
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28375
altitude: 1204.44226074

temperature: -52.4000015259
pressure: 28303
altitude: 1222.52453613

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 3
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28304
altitude: 1205.19128418

temperature: -52.5
pressure: 28301
altitude: 1222.90002441

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 4
TCA9548A I2C channel status: 0b100 (channel 4)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40136
altitude: -46.9332351685

temperature: -52.5
pressure: 40116
altitude: -47.5958938599

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 5
TCA9548A I2C channel status: 0b101 (channel 5)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40185
altitude: -46.9332351685

temperature: -52.5
pressure: 40136
altitude: -47.0986480713

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 6
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28365
altitude: 1216.15002441

temperature: -52.5
pressure: 28297
altitude: 1215.96289062

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 7
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.p
python: can't open file 'index2.p': [Errno 2] No such file or directory
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28368
altitude: 1219.89953613

temperature: -52.5
pressure: 28283
altitude: 1219.71179199

temperature: -52.5
pressure: 28353
altitude: 1219.71179199

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ ^C

Tags: pychannelpisudoi2cdocumentstemperaturebus

热门问题