Hi,
I am trying to transfer array of 134 bytes from Mega ADK to Node MCU via I2C. I am able to transfer single byte and string without problem.
Master - Node MCU
void setup() {
Serial.begin(115200); //start serial for debug
while(!Serial){
//Wait for Serial to start
}
delay(1000);
Serial.println("Starting NodeMCU");
Wire.begin(D1, D2); //join i2c bus with SDA=D1 and SCL=D2 of NodeMCU
}
void loop() {
if (Serial.available() >= 2) { //2 is needed to not loop for new line character
// get incoming byte:
read_command = Serial.read();
switch (read_command){
case 'X':
Serial.println("Array Transfer Selected");
tTransfer();
break;
default:
Serial.println("\nUNKNOWN COMMAND: Serial");
}
}
}
void tTransfer(){
Serial.println("Sending Transfer Command to Mega ADK");
Wire.beginTransmission(8); //begin with device address 8
Wire.write("X"); //sends hello string
Wire.endTransmission(); // stop transmitting
//Receive Array Size from Mega ADK
Wire.requestFrom(8, 134); // request & read data of size 134 bytes from slave
while(Wire.available()){
int c = Wire.read();
if (toggle == 1){
temp = c << 8;
toggle = 0;
}else{
temp = temp | c;
Serial.println(temp,DEC); /Data is 16 bit, hence combining 2 bytes
temp = 0;
toggle = 1;
}
}
}
Slave Mega ADK
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(115200); /* start serial for debug */
while(!Serial){
//Wait for Serial to start
}
delay(1000);
Serial.println("Starting MegaADK");
Serial.print(F("uint16_t rawData[RAW_DATA_LEN]={\n\t"));
for (int i=0;i < arrayLength; i++){
Serial.print((rawArray[i]),DEC);
Serial.print(F(", "));
if( (i % 10)==0) Serial.print(F("\n\t"));
}
}
void loop() {
delay(100);
}
void requestEvent() {
int count = 0;
Serial.println("Transfer Mode Selected");
for(int i;i<arrayLength;i++){//Spiting 16bit array to byte array
rawArray_8[count] = rawArray[i] >> 8;
rawArray_8[count+1] = rawArray[i] & 0xff;
count = count + 2;
}
for(count;count<134;count+2){
tempArray[0] = rawArray_8[count];
tempArray[1] = rawArray_8[count+1];
Wire.write(tempArray,2);
}
//Wire.write(rawArray_8,32);
}
If I use Wire.write(rawArray_8,32);, the data received by master is proper, if I increase this number to 134 i.e. //Wire.write(rawArray_8,134); or any number greater than 32 then the data received by master is 65535.
Serial output with 32
4492
4536
528
1716
528
1712
532
1712
528
596
528
592
528
592
532
592
Serail output with 134
Array Transfer Selected
Sending Transfer Command to Mega ADK
255
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
65535
and so on.... till 134 bytes
Does this mean only 32 bytes can be transferred at a time? and for large array I need to request again from master