I2C - Buffer Split

Hi

I would like to send multiple variables from slave arduino pro mini to master esp8266. I have a problem with 32 bytes buffer limitation. So i am looking for a way to split my data.

I am using union and currently it has float configs[18]; byte buf[216]; This wont work of course. I want to split this data into 3 blocks configs[6]; byte buf[24]; so that i am under the 32 byte buffer limitation.

How can i do that, i need help?

I have tested only single block configs[6]; byte buf[24]; and it works.

I am using Union but if there is better way to send multiple float variables (at least 20 - 40 of them) let me know.

Not sure how can i split this stuff(Here i need help). I mean should i duplicate unions, for statement, is there limitations how many wire.writes can it be triggered in Wire.onRequest?

I would appreciate any kind of an example for this split.

Master Code:

#include <SimpleTimer.h>
#include <Wire.h>
#include <ESP8266WiFi.h>

SimpleTimer timer;

int x = 2;

volatile float floatVar1;
volatile float floatVar2;
volatile float floatVar3;
volatile float floatVar4;
volatile float floatVar5;
volatile float floatVar6;
volatile float floatVar7;
volatile float floatVar8;
volatile float floatVar9;
volatile float floatVar10;
volatile float floatVar11;
volatile float floatVar12;
volatile float floatVar13;
volatile float floatVar14;
volatile float floatVar15;
volatile float floatVar16;
volatile float floatVar17;
volatile float floatVar18;

void setup() {
  Wire.begin(2,14);
  Serial.begin(115200);

  Serial.println("*** MASTER - START ***");

  timer.setTimer(5000, sendDataOne, 1);
  timer.setTimer(5000, receiveData, 1);
}

void loop() {
  timer.run();
}

void sendDataOne(){
  Wire.beginTransmission(8);
  Wire.write(x);            
  Wire.endTransmission();

  Serial.print("Send Data: ");
  Serial.println(x);
  Serial.println();
}

void receiveData(){

  switch(x){
    case 1:

      break;
    case 2:

      // I would like to split this code into 3 x blocks because of 32 byte buffer limit
      // float configs [6]; // instead of 18
      // byte buf [24];     // instead of 216

      union 
      {  
        float configs[18];
        byte buf[216];
      } configUnion;

      if (Wire.requestFrom(8, sizeof configUnion) != sizeof configUnion)
        return;  // oops!

      for (byte i = 0; i < sizeof configUnion; i++) {
        configUnion.buf[i] = Wire.read ();
      }

      floatVar1 = int(configUnion.configs[0]);
      floatVar2 = configUnion.configs[1];
      floatVar3 = configUnion.configs[2];
      floatVar4 = int(configUnion.configs[3]);
      floatVar5 = configUnion.configs[4];
      floatVar6 = configUnion.configs[5];
      floatVar7 = int(configUnion.configs[6]);
      floatVar8 = configUnion.configs[7];
      floatVar9 = configUnion.configs[8];
      floatVar10 = int(configUnion.configs[9]);
      floatVar11 = configUnion.configs[10];
      floatVar12 = configUnion.configs[11];
      floatVar13 = int(configUnion.configs[12]);
      floatVar14 = configUnion.configs[13];
      floatVar15 = configUnion.configs[14];
      floatVar16 = int(configUnion.configs[15]);
      floatVar17 = configUnion.configs[16];
      floatVar18 = configUnion.configs[17];

      Serial.println(floatVar1);
      Serial.println(floatVar2);
      Serial.println(floatVar3);
      Serial.println(floatVar4);
      Serial.println(floatVar5);
      Serial.println(floatVar6);
      Serial.println(floatVar7);
      Serial.println(floatVar8);
      Serial.println(floatVar9);
      Serial.println(floatVar10);
      Serial.println(floatVar11);
      Serial.println(floatVar12);
      Serial.println(floatVar13);
      Serial.println(floatVar14);
      Serial.println(floatVar15);
      Serial.println(floatVar16);
      Serial.println(floatVar17);
      Serial.println(floatVar18);
      Serial.println(""); 

      break;
    case 3:

      break;   
    default:
      Serial.println("Default");
      break;  
    }

}

Slave Code:

#include <Wire.h>

volatile int x;
volatile float floatVar1 = 11;
volatile float floatVar2 = -22467.64;
volatile float floatVar3 = 33545.45;
volatile float floatVar4 = 22;
volatile float floatVar5 = -4267.64;
volatile float floatVar6 = 55545.45;
volatile float floatVar7 = 33;
volatile float floatVar8 = -7767.64;
volatile float floatVar9 = 33545.45;
volatile float floatVar10 = 44;
volatile float floatVar11 = -8467.64;
volatile float floatVar12 = 55545.45;
volatile float floatVar13 = 55;
volatile float floatVar14 = -27.64;
volatile float floatVar15 = 545.45;
volatile float floatVar16 = 66;
volatile float floatVar17 = -42467.64;
volatile float floatVar18 = 545.45;

void setup()
{
  Wire.begin(8);               
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);           
  Serial.println("/// Slave - Start ///");  
}

void loop()
{

}

void receiveEvent()
{
  while (Wire.available()) { 
    x = Wire.read();
  }
}

void requestEvent() {

  switch(x){
    case 1:

      break;
    case 2:

      // I would like to split this code into 3 x blocks because of 32 byte buffer limit
      // float configs [6]; // instead of 18
      // byte buf [24];     // instead of 216

      union 
      {  
        float configs[18];
        byte buf[216];
      } configUnion;

      configUnion.configs[0] = floatVar1;
      configUnion.configs[1] = floatVar2;
      configUnion.configs[2] = floatVar3;
      configUnion.configs[3] = floatVar4;
      configUnion.configs[4] = floatVar5;
      configUnion.configs[5] = floatVar6;
      configUnion.configs[6] = floatVar7;
      configUnion.configs[7] = floatVar8;
      configUnion.configs[8] = floatVar9;
      configUnion.configs[9] = floatVar10;
      configUnion.configs[10] = floatVar11;
      configUnion.configs[11] = floatVar12;
      configUnion.configs[12] = floatVar13;
      configUnion.configs[13] = floatVar14;
      configUnion.configs[14] = floatVar15;
      configUnion.configs[15] = floatVar16;
      configUnion.configs[16] = floatVar17;
      configUnion.configs[17] = floatVar18;

      //Serial.print(sizeof configUnion);
      Wire.write((byte *) &configUnion, sizeof configUnion); 

      break;
    case 3:
      Wire.write("hello");
      break;   
    default:
      Serial.println("Default");
      break;  
    }

}

See IMG: Results of the single block. “float configs [6]; byte buf [24];”

So i have tried with another code. This time I have created 2 Unions with float “configs[6]; byte buf[24];”. Check the code and image result. I get correct result only for first union but not for second one.

Not sure why I am getting the same result for second union?

Anyone has any clue what is wrong in my code and what i need to change that it will work?

Master Code

#include <SimpleTimer.h>
#include <Wire.h>
#include <ESP8266WiFi.h>

SimpleTimer timer;

int x = 1;

volatile float floatVar1;
volatile float floatVar2;
volatile float floatVar3;
volatile float floatVar4;
volatile float floatVar5;
volatile float floatVar6;
volatile float floatVar7;
volatile float floatVar8;
volatile float floatVar9;
volatile float floatVar10;
volatile float floatVar11;
volatile float floatVar12;
volatile float floatVar13;
volatile float floatVar14;
volatile float floatVar15;
volatile float floatVar16;
volatile float floatVar17;
volatile float floatVar18;

void setup() {
  Wire.begin(2,14);
  Serial.begin(115200);

  Serial.println("*** MASTER - START ***");
  
  timer.setTimer(5000, dataFun, 1);
}

void loop() {
  timer.run();
}

void dataFun(){
  Wire.beginTransmission(8);
  Wire.write(0);            
  Wire.endTransmission();

  union 
  {  
    float configs[6];
    byte buf[24];
  } dataUnion;

  if (Wire.requestFrom(8, sizeof dataUnion) != sizeof dataUnion)
    return;  // oops!
    
  while (Wire.available()) { 
    for (byte i = 0; i < sizeof dataUnion; i++) {
      dataUnion.buf[i] = Wire.read ();
    }
  
    floatVar1 = int(dataUnion.configs[0]);
    floatVar2 = dataUnion.configs[1];
    floatVar3 = dataUnion.configs[2];
    floatVar4 = int(dataUnion.configs[3]);
    floatVar5 = dataUnion.configs[4];
    floatVar6 = dataUnion.configs[5];
  }
  
  Serial.println(floatVar1);
  Serial.println(floatVar2);
  Serial.println(floatVar3);
  Serial.println(floatVar4);
  Serial.println(floatVar5);
  Serial.println(floatVar6);
  Serial.println("");

  delay(1000);
  
  Wire.beginTransmission(8);
  Wire.write(1);            
  Wire.endTransmission();

  union 
  {  
    float configs2[6];
    byte buf2[24];
  } dataUnion2;

  if (Wire.requestFrom(8, sizeof dataUnion2) != sizeof dataUnion2)
    return;  // oops!
    
  while (Wire.available()) { 
    for (byte i = 0; i < sizeof dataUnion2; i++) {
      dataUnion2.buf2[i] = Wire.read ();
    }
  
    floatVar8 = dataUnion2.configs2[7];
    floatVar9 = dataUnion2.configs2[8];
    floatVar10 = int(dataUnion2.configs2[9]);
    floatVar11 = dataUnion2.configs2[10];
    floatVar12 = dataUnion2.configs2[11];
    floatVar13 = int(dataUnion2.configs2[12]);
  }
  
  Serial.println(floatVar7);
  Serial.println(floatVar8);
  Serial.println(floatVar9);
  Serial.println(floatVar10);
  Serial.println(floatVar11);
  Serial.println(floatVar12);
  Serial.println("");
  
}

Slave Code

#include <Wire.h>

volatile int x;
volatile float floatVar1 = 11;
volatile float floatVar2 = -22467.64;
volatile float floatVar3 = 33545.45;
volatile float floatVar4 = 22;
volatile float floatVar5 = -4267.64;
volatile float floatVar6 = 55545.45;
volatile float floatVar7 = 33;
volatile float floatVar8 = -7767.64;
volatile float floatVar9 = 33545.45;
volatile float floatVar10 = 44;
volatile float floatVar11 = -8467.64;
volatile float floatVar12 = 55545.45;
volatile float floatVar13 = 55;
volatile float floatVar14 = -27.64;
volatile float floatVar15 = 545.45;
volatile float floatVar16 = 66;
volatile float floatVar17 = -42467.64;
volatile float floatVar18 = 545.45;

void setup()
{
  Wire.begin(8);               
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);           
  Serial.println("/// Slave - Start ///");  
}

void loop()
{

}

void receiveEvent()
{
  while (Wire.available()) { 
    x = Wire.read();
  }
}

void requestEvent() {

  switch(x){
    case 0:
        union 
        {  
          float configs[6];
          byte buf[24];
        } dataUnion;
        dataUnion.configs[0] = floatVar1;
        dataUnion.configs[1] = floatVar2;
        dataUnion.configs[2] = floatVar3;
        dataUnion.configs[3] = floatVar4;
        dataUnion.configs[4] = floatVar5;
        dataUnion.configs[5] = floatVar6;
        Wire.write((byte *) &dataUnion, sizeof dataUnion);

      break;
    case 1:
        union 
        {  
          float configs2[6];
          byte buf2[24];
        } dataUnion2;
        dataUnion2.configs2[6] = floatVar7;
        dataUnion2.configs2[7] = floatVar8;
        dataUnion2.configs2[8] = floatVar9;
        dataUnion2.configs2[9] = floatVar10;
        dataUnion2.configs2[10] = floatVar11;
        dataUnion2.configs2[11] = floatVar12;
        Wire.write((byte *) &dataUnion2, sizeof dataUnion2); 
      break;  
    case 3:
      Wire.write("hello");
      break;   
    default:
      Serial.println("Default");
      break;  
    }

}

Image: http://imgur.com/a/l2de2

OK, i have found my mistakes. Now it works.

Master Code

#include <SimpleTimer.h>
#include <Wire.h>
#include <ESP8266WiFi.h>

SimpleTimer timer;

int x = 1;

volatile float floatVar1;
volatile float floatVar2;
volatile float floatVar3;
volatile float floatVar4;
volatile float floatVar5;
volatile float floatVar6;
volatile float floatVar7;
volatile float floatVar8;
volatile float floatVar9;
volatile float floatVar10;
volatile float floatVar11;
volatile float floatVar12;
volatile float floatVar13;
volatile float floatVar14;
volatile float floatVar15;
volatile float floatVar16;
volatile float floatVar17;
volatile float floatVar18;

void setup() {
  Wire.begin(2,14);
  Serial.begin(115200);

  Serial.println("*** MASTER - START ***");
  
  timer.setTimer(5000, dataFun, 1);
}

void loop() {
  timer.run();
}

void dataFun(){
  Wire.beginTransmission(8);
  Wire.write(0);            
  Wire.endTransmission();

  union 
  {  
    float configs[6];
    byte buf[24];
  } dataUnion;

  if (Wire.requestFrom(8, sizeof dataUnion) != sizeof dataUnion)
    return;  // oops!
    
  while (Wire.available()) { 
    for (byte i = 0; i < sizeof dataUnion; i++) {
      dataUnion.buf[i] = Wire.read ();
    }
  
    floatVar1 = int(dataUnion.configs[0]);
    floatVar2 = dataUnion.configs[1];
    floatVar3 = dataUnion.configs[2];
    floatVar4 = int(dataUnion.configs[3]);
    floatVar5 = dataUnion.configs[4];
    floatVar6 = dataUnion.configs[5];
  }
  
  Serial.println(floatVar1);
  Serial.println(floatVar2);
  Serial.println(floatVar3);
  Serial.println(floatVar4);
  Serial.println(floatVar5);
  Serial.println(floatVar6);
  Serial.println("");

  delay(1000);
  
  Wire.beginTransmission(8);
  Wire.write(1);            
  Wire.endTransmission();

  union 
  {  
    float configs2[6];
    byte buf2[24];
  } dataUnion2;

  if (Wire.requestFrom(8, sizeof dataUnion2) != sizeof dataUnion2)
    return;  // oops!
    
  while (Wire.available()) { 
    for (byte i = 0; i < sizeof dataUnion2; i++) {
      dataUnion2.buf2[i] = Wire.read ();
    }
  
    floatVar7 = dataUnion2.configs2[0];
    floatVar8 = dataUnion2.configs2[1];
    floatVar9 = int(dataUnion2.configs2[2]);
    floatVar10 = dataUnion2.configs2[3];
    floatVar11 = dataUnion2.configs2[4];
    floatVar12 = int(dataUnion2.configs2[5]);
  }
  
  Serial.println(floatVar7);
  Serial.println(floatVar8);
  Serial.println(floatVar9);
  Serial.println(floatVar10);
  Serial.println(floatVar11);
  Serial.println(floatVar12);
  Serial.println("");
  
}

Slave Code

#include <Wire.h>

volatile int x;
volatile float floatVar1 = 11;
volatile float floatVar2 = -22467.64;
volatile float floatVar3 = 33545.45;
volatile float floatVar4 = 22;
volatile float floatVar5 = -4267.64;
volatile float floatVar6 = 55545.45;
volatile float floatVar7 = 33;
volatile float floatVar8 = -7767.64;
volatile float floatVar9 = 33545.45;
volatile float floatVar10 = 44;
volatile float floatVar11 = -8467.64;
volatile float floatVar12 = 55545.45;
volatile float floatVar13 = 55;
volatile float floatVar14 = -27.64;
volatile float floatVar15 = 545.45;
volatile float floatVar16 = 66;
volatile float floatVar17 = -42467.64;
volatile float floatVar18 = 545.45;

void setup()
{
  Wire.begin(8);               
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);           
  Serial.println("/// Slave - Start ///");  
}

void loop()
{

}

void receiveEvent()
{
  while (Wire.available()) { 
    x = Wire.read();
  }
}

void requestEvent() {

  switch(x){
    case 0:
        union 
        {  
          float configs[6];
          byte buf[24];
        } dataUnion;
        dataUnion.configs[0] = floatVar1;
        dataUnion.configs[1] = floatVar2;
        dataUnion.configs[2] = floatVar3;
        dataUnion.configs[3] = floatVar4;
        dataUnion.configs[4] = floatVar5;
        dataUnion.configs[5] = floatVar6;
        Wire.write((byte *) &dataUnion, sizeof dataUnion);

      break;
    case 1:
        union 
        {  
          float configs2[6];
          byte buf2[24];
        } dataUnion2;
        dataUnion2.configs2[0] = floatVar7;
        dataUnion2.configs2[1] = floatVar8;
        dataUnion2.configs2[2] = floatVar9;
        dataUnion2.configs2[3] = floatVar10;
        dataUnion2.configs2[4] = floatVar11;
        dataUnion2.configs2[5] = floatVar12;
        Wire.write((byte *) &dataUnion2, sizeof dataUnion2); 
      break;  
    case 3:
      Wire.write("hello");
      break;   
    default:
      Serial.println("Default");
      break;  
    }

}

okok.JPG