We are trying to communicate between multiple Arduino's (in this example a Nano and a Mega) and a Raspberry Pi. The Raspberry pi is defined as master and the Arduino's as slaves. We would also like that the Arduino's communicate between each other. For example: The Mega requests from the Nano and at some point, the Mega also communicates with the Raspberry Pi. This is the short version of what we are trying to accomplish but we ran into the problem that the i2c communication failed between the Nano and Mega upon the request from the Mega to the Nano.
Problem
After some time the i2c bus 'fills' up...
Code Nano
(We send RFID data to Mega)
#include <Wire.h>
#include "SPI.h"
#include "MFRC522.h"
#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Wire.begin(0x10);
Wire.onRequest(requestEvent);
}
void loop() {
}
void requestEvent() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial())
return;
// Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
// Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
String strID = "";
for (byte i = 0; i < 4; i++) {
strID +=
(rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
String(rfid.uid.uidByte[i], HEX) +
(i != 3 ? ":" : "");
}
strID.toUpperCase();
//81:AE:1D:29
Serial.print("Send to Mega: ");
Serial.println(strID);
Wire.write(strID.c_str());
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
Code Mega
#include <Wire.h>
void setup() {
Wire.begin(0x03);
}
void loop() {
Wire.requestFrom(0x10, 11);
String uid = "";
while (Wire.available()) {
char c = "";
c = Wire.read();
uid += c;
}
if (uid.indexOf(':') != 2) {
return;
}
Serial.println(uid);
delay(500);
}
We would like to communicate between the Arduino's when the Raspberry Pi is master.