Skip to content

ESP8266 to ESP8266 i2c #3046

@reubenrodrigues24

Description

@reubenrodrigues24

With added support for i2c slave from here by @bjoham, after modifying all files.

Master (ESP8266)

#include <Wire.h>

int x = 3;
void setup() {
  
  Wire.pins(4,5);
  Wire.begin();               // join i2c bus (address optional for master)
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
}

void loop() {

} 

Slave (ESP8266)

#include <Wire.h>

int x = 3;


void setup() {
  pinMode(D0,OUTPUT);
  Wire.pins(4,5);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  //Serial.begin(9600);           // start serial for output
//  Serial.println("Got something?");
  digitalWrite(D0,LOW);
}

void loop() {
  delay(100);
  if (x == 3) {
    digitalWrite(D0, HIGH);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 0) {
    digitalWrite(D0, LOW);
  }
}              

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
    x = Wire.read();    // read one character from the I2C'
}

I have connected pins D1,D2,GND on both the boards(NodeMCU).

This doesn't seem to work. Any help will be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions