The code below gives me some problems.
When connected to the RasPi, I can see the i2c-slave on adr. #08, but when sending data to it, it fails (Have tested the sender with a Arduino Uno and that works, so the sender should be OK)
Wire.available() is always 0 and the requestEvent() is never fired.
I'm running the i2c at 10kHz and have enabled clock stretching and set it to 1500uS.
I have checked the wire.cpp and the functions I'm calling are not commented out, so I suppose they should work.
Any ideas?
/Brian
// ESP8266
#include <Wire.h>
unsigned long tid;
int num = 0;
void setup() {
Wire.pins(D5, D4);
Wire.begin(8); // join i2c bus with address #8
Wire.setClockStretchLimit(1500);
Wire.setClock(10000L);
Wire.onRequest(requestEvent); // register event
Serial.begin(115200); // start serial for output
Serial.println();
Serial.println("i2c adr #08 ready");
tid=millis();
}
void loop() {
delay(10);
Serial.println(Wire.available());
Serial.println(num);
Serial.println("---");
if (num>1000)
{
Serial.println();
Serial.print("Recv 1000 chars in ");
Serial.print(millis()-tid);
Serial.println(" mS");
num=0;
tid=millis();
}
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
while (0 < Wire.available()) { // loop through all
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
num++;
}
}