Hello,
pulling all my (grey) hairs out ....can't get this to work (and running out of grey hairs as well...
)
OK....what's up:
I want to send some data from a Wemos D1 mini 8266 to a Nano by I2C
At first I used a code that just sends and receives a byte (a counter from 1 to 100) and that gets received by the Nano so that tells me hardware is ok and they can communicate.
But now I want to send a structure.
I use this example https://forum.arduino.cc/t/use-i2c-for-communication-between-arduinos/653958/2
Whatever I do....can't get it to work.
If you have a look at my sending code for the 8266 in the send_data routine I print out valA.
That's working so it's telling me it enters this routine and there is a structure assigned and valA is present and is having the right value (for this test I did not change this value, first wanted it to work before doing that.
Now...
the receiver code.
You can see that in the loop there is a "Hello": that is working so the loop is processed however it seems that no data at all is being received because it never prints out "new data received" so it never enters this loop..even worse: also the text "we were here" never gets printed.
So....
It looks like the receiver never receives anything so I suspect nothing is being transmitted....
But again: when I just change the sketches for sending a byte in a loop: that's working fine.
So hardware, addresses etc is ok, I guess it's something with the structure not being send but I really have no clue.
What I also tried is making txData volatile (send sketch: volatile I2cTxStruct txData = {"xxx", 77, 0};)
and also made it volatile in the receive sketch but no luck as well.
What I also tried (after searching these forums) is the setClockstretch command however, even without this command the byte send/receive did already work.
Anyone a suggestion? Thanks!
Sending sketch for the 8266 master
/*
Nano: A4=SDA A5=SCL
Wemos: D2=SDA D1=SCL
Structure Sender program Wemos
*/
#define Slave_Addr 9 //I2C address receiver (nano)
#define loop_delay 1000
#include <Wire.h>
struct I2cTxStruct {
char textA[16]; // 16 bytes
int valA; // 2
unsigned long valB; // 4
byte padding[10]; // 10 to fill up to 32 bytes
//------
// 32
}; //end struct
I2cTxStruct txData = {"xxx", 77, 0}; //also tried volatile
void setup() {
Serial.begin (115200);
// Wire.setClockStretchLimit(40000); // in µs, no change
Wire.begin();
Serial.println (__FILE__);
} //end setup
void loop() {
send_data();
delay (loop_delay);
} //end loop
void send_data() {
Wire.beginTransmission(Slave_Addr);
Wire.write((byte*) &txData, sizeof(txData));
Wire.endTransmission();
Serial.println ((String)"valA = " + txData.valA);
} //end send_data
Nano receive as slave:
/*
Nano: A4=SDA A5=SCL
Wemos: D2=SDA D1=SCL
Receive structure Nano
*/
# define loop_delay 1000
# define Slave_Addr 9 //I2C adres van de Wemos
#include <Wire.h>
struct I2cRxStruct {
char textB[16]; // 16 bytes
int valC; // 2
unsigned long valD; // 4
byte padding[10]; // 10
//------
// 32
};
I2cRxStruct rxData;
bool newRxData = false;
void setup() {
Serial.begin(115200);
Wire.begin(Slave_Addr);
Wire.onReceive(receiveEvent);
} //end setup
void loop() {
//Serial.println("Hello");
if (newRxData == true) {
Serial.println("New data received");
showNewData();
newRxData = false;
} //end check nw data
} //end loop
void showNewData() {
Serial.print("This just in ");
Serial.print(rxData.textB);
Serial.print(' ');
Serial.print(rxData.valC);
Serial.print(' ');
Serial.println(rxData.valD);
}
void receiveEvent(int numBytesReceived) {
if (newRxData == false) {
// copy the data to rxData
Wire.readBytes( (byte*) &rxData, numBytesReceived);
Serial.println ("we were here");
newRxData = true;
}
else {
// dump the data
while(Wire.available() > 0) {
byte c = Wire.read();
}
}
} //end receiveEvent
