So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By BmanDK
#79887 Hi.

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

Code: Select all// 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++;
  }
}
User avatar
By BmanDK
#80015
schufti wrote:are you on latest git?
I am not aware that "release" arduino esp core supports i2c slave mode.
https://arduino-esp8266.readthedocs.io/ ... re-library


Not sure. I just added the 8266 from the board-manager in the Arduino IDE as explained here https://arduino-esp8266.readthedocs.io/ ... ds-manager using the stable version.

So I think it would use the latest from GIT.
According to the folder-structure that was created when installed, I'm running version 2.5.0-beta1

The following is the header from my Wire.cpp

/*
TwoWire.cpp - TWI/I2C library for Arduino & Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support
*/


/Brian