-->
Page 1 of 1

I2C slave

PostPosted: Sat Jun 20, 2015 12:11 pm
by Sorunome
Hey,
I wondered how I can make the ESP8266 act as an I2C slave, as it needs different ports.
I tried Wire.pins(0,2); Wire.begin(2); but it doesn't seem to work.
I know that Wire.pins is deprecated but when specifying the pins in Wire.begin it seems to be impossible to go into slave mode.

ESP8266 code:
Code: Select all#include <Wire.h>
#include "ESP8266WiFi.h"
#define GAMEBUINOPORT 2
#define debug


#define CMD_SCAN 1
byte command;
bool ready = false;
void setup() {
  Wire.pins(0,2);
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
#ifdef debug
  Serial.begin(115200);
  while(!Serial);
#endif
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
#ifdef debug
  Serial.println("Setup done");
#endif
}

void loop() {
  // put your main code here, to run repeatedly:

}
void write(char s){
  Wire.beginTransmission(GAMEBUINOPORT);
  Wire.write(s);
  Wire.endTransmission();
}

void receiveEvent(int num){
  Serial.println(num);
  while(Wire.available()){
    command = Wire.read();
    Serial.print(command);
    ready = true;
  }
  Serial.print("\n");
}

void requestEvent(){
#ifdef debug
  Serial.println("Got request");
#endif
  ready = true;
  command = CMD_SCAN;
  if(ready){
    switch(command){
      case CMD_SCAN:
        int n = WiFi.scanNetworks();
        Wire.write(n);
        break;
    }
  }else{ // our reply isn't ready yet
    Wire.write(0);
  }
}



Arduino code:
Code: Select all#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(115200);
  while(!Serial);
  Serial.println("Setup done");
}

void loop() {
  Serial.println("attempting request...");
  Wire.requestFrom(2,2);
  while(Wire.available()){
    int c = Wire.read();
    Serial.print(c);
  }

  delay(500);
}



Thanks for any answers!

Re: I2C slave

PostPosted: Sat Jun 20, 2015 2:19 pm
by tytower
It says in the Github ESP8266/Arduino readme
I2C (Wire library)

Wire library currently supports master mode up to approximately 450KHz. Before using I2C, pins for SDA and SCL need to be set by calling Wire.begin(int sda, int scl), i.e. Wire.begin(0, 2); on ESP-01, else they default to pins 4(SDA) and 5(SCL).

So put in Wire.begin(int sda, int scl);
Unfortunately his english is lacking the nuances of an english speaker ( with respect of course) so it is difficult to say then that it does not support slave mode but I come away from the above with that impression. There is also a boot mode called SDIO which is entered by having GPIO15 high which they say they don't know anything about yet so there is a lot more to come with this chip