4

I am trying to get my ESP8266's connect and send messages over an i2c bus. I am using a NodeMcu Development Board. Pins D1,D2 and GND are connected to each other.

The code on my master is :

#include <Wire.h>

void setup() {
  Wire.begin(D1,D2); // join i2c bus (address optional for master)
  Serial.begin(115200); 
}

byte x = 0;

void loop() {

 Wire.beginTransmission(8);
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    Serial.println("Transmitted");
  x++;
  delay(500);
}  

And the code on my slave ESP is:

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  Serial.println("Received..");
  /*
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  */
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

Running this gives no output on the receiver chip.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
user3599285
  • 91
  • 1
  • 7
  • 1
    I2C Slave mode is currently not possible on the Arduino-ESP8266 or the NodeMCU firmware. See https://github.com/nodemcu/nodemcu-firmware/issues/1687 and https://github.com/esp8266/Arduino/issues/1330 . Also see http://bbs.espressif.com/viewtopic.php?t=2092 . – Maximilian Gerhardt Feb 25 '17 at 13:24
  • you might try plain old serial communication. – dandavis Feb 27 '17 at 19:18
  • See [link](https://github.com/esp8266/Arduino/compare/master...bjoham:i2c_slave) . But I still can't figure it out. even after the cahnges – user3599285 Mar 13 '17 at 11:10
  • I'm using two esp07. Is it possible to make them communicate via i2c (GPIO4,GPIO5 => they are SDA,SCL)? – Oki Erie Rinaldi May 31 '17 at 02:47

3 Answers3

1

As mentioned in the comments it doesn't look like I2C is supported, but you could use PJON

You just need to connect a single wire to enable communication between the two devices

Esp8266 to Esp8266 diagram

witnessmenow
  • 308
  • 3
  • 8
0

I'm not sure but I would expect the Wire library from Arduino to use the hardware I2C controller for ATMega. The I2C driver in the firmware from Espressif seems to be doing I2C over GPIO, that would hint there is no hw controller on ESP (what are the odds they would be the same anyway). So you need to use something else than Wire.h, thus I would suggest - try downloading something that fakes I2C over GPIO for your Arduino IDE. Like this .. maybe, I haven't tried that out. I know not a complete solution, but maybe at least this helps.. good luck!

Pyjong
  • 3,095
  • 4
  • 32
  • 50
0

ESP8266(I2C Master) to ESP8266(I2C Slave) works from version 2.5.0. Check out my comments on the ESP8266 GitHub

Gotfredsen
  • 56
  • 4