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:
#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:
#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!