S To
S To

Reputation: 93

WeMos D1 Mini (ESP8266) I2C Slave Mode does not receive any incoming data

I have spent hours trying to figure out how I can get the "WeMos D1 Mini" to be in slave mode.

For some reason it does not acknowledge any incoming data. From all the forums I read, everyone needs to make their own "special interrupt based I2C" code in order to make the ESP8266 into slave mode since it is not supported officially.

So I have an Arduino Nano which is the master and it will be sending data to WeMos D1 Mini, but the example is not working at all.

Here is the code I'm using from "Arduino IDE 1.8.13" using the "ESP8266 core 2.7.4", it is the example code named "slave_receiver":

// Wire Slave Receiver
// by devyte
// based on the example by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// This example code is in the public domain.


#include <Wire.h>

#define SDA_PIN 4
#define SCL_PIN 5

const int16_t I2C_MASTER = 0x42;
const int16_t I2C_SLAVE = 0x08;

void setup() {
  Serial.begin(115200);           // start serial for output

  Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)
  Wire.onReceive(receiveEvent); // register event
}

void loop() {
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(size_t howMany) {

  (void) howMany;
  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
}

Upvotes: 2

Views: 1581

Answers (1)

ocrdu
ocrdu

Reputation: 2183

The ESP8266 won't work as a slave on I2C; this is a known problem that, as far as I know, has never been solved, and I don't know of any workarounds other than maybe programming everything from scratch.

See here and here for discussions of this.

You could try using an other way of exchanging data; I2C isn't all there is.

Upvotes: 2

Related Questions