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 #2:

#include <Wire.h>

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

char* msg = " "; // 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 #2
  delay(1000);
}

void loop() {
  // i2c buffer max.32 chars !!
  msg = "Hi ESP, i am Arduino #2"; // set ESP message
  Wire.beginTransmission(arduino_1); // send message to Arduino Slave #1
  Wire.write("Hi Slave #1, i am Arduino #2");
  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();
}