I'm trying to build a simple remote keyboard that's gonna relay simple messages sent directly from my Node-Red dashboard as aa learning project. However I cannot seem to get past the I2C transmission part. Here's the code I'm using for the Master:
#include<Wire.h>
char messageToRelay[] = "I am your master";
//NodeMCU: SDA is D2, SCL is D1
//ESP01: SDA is 0, SCL is 2
//Arduino Pro Micro: SDA is 2, SCL is 3
//Arduino Uno: SDA is A4, SCL is A5
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(8); //don't use number lower than 8
Wire.write(messageToRelay);
Wire.endTransmission();
Serial.println(messageToRelay);
delay(1000);
Wire.requestFrom(8,1);
while(Wire.available()){
byte a = Wire.read();
Serial.println(a);
}
}
And here's the code I'm using for the slave:
#include<Wire.h>
//NodeMCU: SDA is D2, SCL is D1
//ESP01: SDA is 0, SCL is 2
//Arduino Pro Micro: SDA is 2, SCL is 3
//Arduino Uno: SDA is A4, SCL is A5
void setup() {
// put your setup code here, to run once:
delay(1000);
Wire.begin(8); //same addr of the master
Wire.onReceive(reader);
Wire.onRequest(request);
Serial.begin(9600);
Serial.println("Ready to receive");
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// digitalWrite(5, HIGH);
// delay(100);
// digitalWrite(5, LOW);
delay(100);
}
void reader(int howMany) {
while(0 < Wire.available()){
char c = Wire.read();
Serial.println(c);
}
Serial.println(".");
}
void request(){
Wire.write(1);
}
This code will work as is with an Uno as Master and Pro Micro as Slave, Pro Micro as Master and Uno as Slave and NodeMCU as Master and Uno as slave, but not with NodeMCU as Master and Pro Micro as Slave. I searched the net for a solution but couldn't find anything specific to this issue. I realize there are already ready solutions for what I'm trying to achieve such as Wifi Duck, but I'd like to understand where it's going wrong. It cannot be a wiring mistake since the Pro Micro does work with I2C in tandem with the UNO. I have tried wiring it to the NodeMCU both with and without a logic converter.