I'm sending a command e.g. "wifion" with I2C arduino(master) to RPI(slave) That works perfect but I need to return a confirmation value.

How can I send back/reply from the RPI to the Ardunio a value?

#!/usr/bin/env python
# bsc_arduino.py
# 2016-10-31
# Public Domain

import time
import pigpio
import RPi.GPIO as GPIO
import subprocess
import os



print "Start"
def i2c(id, tick):
   global pi
   s, b, d = pi.bsc_i2c(I2C_ADDR)
   #GPIO.output(17, GPIO.HIGH)
   if b:
      if d=="wifion":
        print "An"
        os.system ('rfkill unblock wifi')

      if d=="wifioff":
        print "Aus"
        os.system ('rfkill block wifi')

      print d

pi = pigpio.pi()
if not pi.connected:
# 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
pi.bsc_i2c(0) # Disable BSC peripheral

Inside your i2c function add the data that you want to transmit as a reply:

pi.bsc_i2c(I2C_ADDR, data=["your data here"])
  • Tried it but get this error : ValueError: string must be of size 1 – user3435167 Apr 4 '19 at 20:41
  • @user3435167, what exactly did you try to pass as data? – David Apr 4 '19 at 21:50
  • I added ', data=["your data here"]' as you suggested to my code to see if it replys with a word/number – user3435167 Apr 7 '19 at 18:23
  • @user3435167, try just sending a character data="A" – David Apr 7 '19 at 20:29
  • There is also no reaction. i guess there's a problem with my master's code then.. – user3435167 Apr 11 '19 at 19:48

With the I²C bus, each module can become master or slave at any moment. Check the smbus library in python, it's by default on the Raspberry.

In the python shell: help(smbus.SMBus)

You have functions to read and write data with I²C bus, juste give him the address of the device you want to communicate with (your Arduino).

  • Thanks, but I'm not using the regular I2C pins but SDA=18 SCL=19 instead. – user3435167 Apr 4 '19 at 10:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.