Problème communication entre Raspberry pi 3 et arduino
Posté : sam. 22 août 2020 20:06
Bonjour je connecte un pi 3 a un arduino avec les 3 fils de l'I2C. Je pense que c'est ok car la fonction i2cdetect -y 1 trouve bien mon arduino à l'adresse definie. Si je débranche un des fils ça ne marche plus. Et pourtant pas moyen de transmettre une info !!! Si quelqu'un a une idée ?
Mode code RP pi 3 :
import smbus
import time
bus = smbus.SMBus(1) # indicates /dev/ic2-1
addr = 0x75
numb = 1
ledstate=0
print ("Enter 1 for ON or 0 for OFF")
while numb == 1:
ledstate = input(">>>> ")
if ledstate == "1":bus.write_byte_data(addr, 0x00, 0x01)
if ledstate == "0":bus.write_byte(0x75, 0x0) # switch it on
if ledstate == "q":numb = 0
Mode code Arduino :
#include <Wire.h>
#define SLAVE_ADDRESS 0x75
byte data_to_echo = 0;
void setup()
{
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Serial.begin(9600);
Serial.print("init");
}
void loop() {
}
void receiveData(int bytecount)
{
Serial.print("reception");
for (int i = 0; i < bytecount+1; i++) {
data_to_echo = Wire.read();
Serial.print("reception");
Serial.println(data_to_echo);
}
}
void sendData()
{
Serial.begin(9600);
Serial.print("envoi");
Serial.print("envoi");
Serial.println(data_to_echo);
}
Mode code RP pi 3 :
import smbus
import time
bus = smbus.SMBus(1) # indicates /dev/ic2-1
addr = 0x75
numb = 1
ledstate=0
print ("Enter 1 for ON or 0 for OFF")
while numb == 1:
ledstate = input(">>>> ")
if ledstate == "1":bus.write_byte_data(addr, 0x00, 0x01)
if ledstate == "0":bus.write_byte(0x75, 0x0) # switch it on
if ledstate == "q":numb = 0
Mode code Arduino :
#include <Wire.h>
#define SLAVE_ADDRESS 0x75
byte data_to_echo = 0;
void setup()
{
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Serial.begin(9600);
Serial.print("init");
}
void loop() {
}
void receiveData(int bytecount)
{
Serial.print("reception");
for (int i = 0; i < bytecount+1; i++) {
data_to_echo = Wire.read();
Serial.print("reception");
Serial.println(data_to_echo);
}
}
void sendData()
{
Serial.begin(9600);
Serial.print("envoi");
Serial.print("envoi");
Serial.println(data_to_echo);
}