Kendziorra - ESP8266 I²C with Arduino Multi Masters
Last Arduino/ESP8266 project (click to open)

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

Article Index

Arduino Master/Slave #1:

#include <Wire.h>

// ESP has no slave function :(
const byte arduino_Me = 1; // Arduino Multi-Master/Slave #1, that's me!
const byte arduino_2 = 2; // Arduino Multi-Master/Slave #2, that is the other boss

char* msg = " "; // Master ESP message

void setup() {
  Serial.begin(115200);
  Wire.onRequest(requestEvent); // for all Masters
  Wire.onReceive(receiveEvent); // for all Masters
  Wire.begin(arduino_Me); // init as slave #1
  delay(1000);
}

void loop() {
  // i2c buffer max.32 chars !!
  msg = "Hi ESP, i am Arduino #1"; // set Master ESP message
  Wire.beginTransmission(arduino_2); // send message to Arduino Slave #2 
  Wire.write("Hi Slave #2, i am Arduino #1");
  Wire.endTransmission();
  delay(1000);
}

void requestEvent() { // sent message to Master ESP
  if (msg != "") Wire.write(msg); // max. 32 chars
  msg = ""; // avoid repetition
}

void receiveEvent(int howMany) { // receive messages from all Masters
  while (Wire.available()) {
    char c = Wire.read();
    Serial.print(c);
  }
  Serial.println();
}