0

I'm trying to use i2c to exchange messages between 3 raspberry pi, all of them must receive a message do something and then send a response.

Raspberry as an I2C SLAVE From here I came up with a solution. First I tested with two raspberry pi and it worked by connecting the 2/3 pin from one raspberry pi to the 18/19 pin from the other and vice versa. This worked flawlessly.

When I tried to connect the third raspberry pi this is where things start to get weird. From two raspberry pi I can see all the other two and my slave address, but from the third, I can only see my address and another raspberry pi. In this case, I connected all the 2/3 and 18/19 together.

This is the code I'm running to make them slave:

import time
import pigpio

#SDA11=2
SDA=18
#SCL11=3
SCL=19

I2C_ADDR = 9


def i2c(id, tick):
    global pi

    s, b, d = pi.bsc_i2c(I2C_ADDR)
    if b:
        print("d", d)
    s1, b1, d1 = pi.bsc_i2c(I2C_ADDR)
    if b1:
        print("d1", d1)
    received = d + d1
    print(received)

pi = pigpio.pi()

if not pi.connected:
    exit()

# Add pull-ups in case external pull-ups haven't been added

pi.set_pull_up_down(SDA, pigpio.PUD_UP)
pi.set_pull_up_down(SCL, pigpio.PUD_UP)

# Respond to BSC slave activity

e = pi.event_callback(pigpio.EVENT_BSC, i2c)


pi.bsc_i2c(I2C_ADDR) # Configure BSC as I2C slave

time.sleep(600)

e.cancel()

pi.bsc_i2c(0) # Disable BSC peripheral

pi.stop()

In order to check the address I use the i2cdetect -y 1 command. It is important to say that I change the I2C address from each script. Can anyone tell me what I am doing wrong?

  • Raspberries can't run as I2C slaves. Use TCP/IP over WiFi or TCP/IP over ethernet as that's two orders of magnitude easier, has better bandwidth than I2C and is known to work. – Dougie Apr 25 '19 at 19:44
  • Actually they can act as I2C slaves., I posted a link in the question showing how, just don't know how to make it work with this multi master architecture The goal of using i2c is to limit the bandwidth, it is emulating a acoustic channel – Nuno Moreira Apr 25 '19 at 21:52
  • Use plain old networking. It's a many-to-many comms protocol. It works. It's got more than 50 years of history behind it. And it meets the en.wikipedia.org/wiki/KISS_principle – Dougie Apr 25 '19 at 22:01
  • well, I don't know what I've done but now all of them recognize each other. So it can be done and I proved it. – Nuno Moreira Apr 27 '19 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.