Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By Reuben Rodrigues
#63286 I've been trying to get this to work by using the NodeMCU ESP8266 Development board using one ESP as the master and the other as the slave. I tried making the changes as in https://github.com/esp8266/Arduino/compare/master...bjoham:i2c_slave to no avail.
Code as follows :

Master:
Code: Select all#include <Wire.h>

void setup() {
  Wire.pins(5,4);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  Serial.println("Received..");
  /*
  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
}                                                                               

Slave:
Code: Select all#include <Wire.h>

void setup() {
  Wire.pins(5,4);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
}

void loop() {
  delay(100);
}

void receiveEvent(int howMany) {
  Serial.println("Received..");
  /*
  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
}                                                                               


Help from anyone who has pulled this off would be great.
User avatar
By Reuben Rodrigues
#63696 I made a few changes to my code :
Master:

Code: Select all#include <Wire.h>

int x = 3;
void setup() {
 
  Wire.pins(4,5);
  Wire.begin();               // join i2c bus (address optional for master)
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting
}

void loop() {

}


Slave:

Code: Select all#include <Wire.h>

int x = 0;


void setup() {
  pinMode(D0,OUTPUT);
  Wire.pins(4,5);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  //Serial.begin(9600);           // start serial for output
//  Serial.println("Got something?");
  digitalWrite(D0,LOW);
}

void loop() {
  delay(100);
  if (x == 3) {
    digitalWrite(D0, HIGH);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 0) {
    digitalWrite(D0, LOW);
  }
}             

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
    x = Wire.read();    // read one character from the I2C'
}


If I reset master it should send 3. and D0 should turn HIGH. Can't understand. Sorry for messy example. Tried debugging here a lot.
User avatar
By bjoham
#63750 You need to set the clock to a "low" value. From my experience, this is below 15kHz. You can't use the Wire.setClock() call directly, however. You must also set the prescaler bits.

I don't have access to my code at the moment so I can't give you the exact details on how to do it. I hope you can find it out yourself in the meantime...

My plan is to hook up a bunch of modules and try this out once and for all. The idea is to figure out why others seem to have problems where I am still amazed by how great it has been working. I have purchased one Adafruit HUZZAH, one ESP8266-01, and one NodeMCU. As a master, I only have access to Adafruit Trinket Pro (both 5V and 3V, though). What would be good is a set of other "normal" Arduino masters.

Anyways, I just need the time to put my head around it...