Raspberry Pi Asked by user3435167 on December 12, 2021
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
SDA=18
SCL=19
I2C_ADDR=28
print "Start"
def i2c(id, tick):
global pi
s, b, d = pi.bsc_i2c(I2C_ADDR)
#GPIO.output(17, GPIO.HIGH)
if b:
#print(d[:-1])
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:
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()
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).
Answered by Marc Aurèle on December 12, 2021
Inside your i2c
function add the data that you want to transmit as a reply:
pi.bsc_i2c(I2C_ADDR, data=["your data here"])
Answered by David on December 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP