Enable Javascript


Last Arduino/ESP8266 project (click to open)

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

Article Index

ESP8266:

#include <Wire.h>

const byte arduino_1 = 1;  // Arduino as Slave #1
const byte arduino_2 = 2;  // Arduino as Slave #2

void setup() {
  Serial.begin(115200);  
  Wire.begin();  // init as master
  delay(1000);
}

void loop() {
  // WRITE Arduino Slave #1
  Wire.beginTransmission(arduino_1);
  Wire.write("Hi Arduino #1, i am ESP");
  Wire.endTransmission();   
  //-----------------------
  // WRITE Arduino Slave #2
  Wire.beginTransmission(arduino_2);
  Wire.write("Hi Arduino #2, i am ESP");
  Wire.endTransmission();   
  //-----------------------
  // READ Arduino Slave #1
  String inBuf1 = "";
  Wire.requestFrom(arduino_1, 32, 1); // max 32 bytes
  while (Wire.available()) { 
    int c = Wire.read(); 
    if (c >= 32 && c <= 126) inBuf1 += (char)c;  
  }
  if (inBuf1 != "" ) Serial.println(inBuf1);  
  //-----------------------
  // READ Arduino Slave #2
  String inBuf2 = "";
  Wire.requestFrom(arduino_2, 32, 1); // max 32 bytes
  while (Wire.available()) { 
    int c = Wire.read(); 
    if (c >= 32 && c <= 126) inBuf2 += (char)c;  
  }
  if (inBuf2 != "" ) Serial.println(inBuf2);
  //-----------------------
  delay(1000);
}