something go wrong with 4th byte conversion

i get some problems when i write-read structure data to i2c 24LC64 eeprom

how EEPROM I2C Write Anything – Apex Logic seems not to or return nothing, i probe my code, but something go wrong with 4th byte conversion

please anybody give me a little help?
best regards, pescadito.

//  my probe code
//  Use the I2C bus with EEPROM 24LC64 
#include <Wire.h> //I2C library

struct edata_t {
     unsigned long i;           // 4 bytes from 0 to 4,294,967,295 (2^32 - 1). 
     unsigned long millitime;   // 4
     float temperature;         // 4 bytes 6-7 decimal digits of precision
     int32_t pressure;          // 4 bytes (32-bit signed type)
     float altitude;            // 4 bytes
     float altidudestd;         // 4 bytes
} edata;
byte ebuffer[] = " 0123456789 123456789 123"; // some temp buffer

void setup() 
{
    Wire.begin(); // initialise the connection

    Serial.begin(9600);
    Serial.println("Writing...");
    
    int ilines = 9;
    for(int i = 1; i <= ilines; i++) {
      edata.i = i;
      edata.millitime = millis();
      edata.temperature = 123.45;
      edata.pressure = 123456;
      edata.altitude = 321.09;
      edata.altidudestd = 333.33;

      ebuffer[0] = lowByte(edata.i);
      ebuffer[1] = lowByte(edata.i >> 8);
      ebuffer[2] = lowByte(edata.i >> 16);
      ebuffer[3] = lowByte(edata.i >> 24);

      ebuffer[4] = lowByte(edata.millitime);
      ebuffer[5] = lowByte(edata.millitime >> 8);
      ebuffer[6] = lowByte(edata.millitime >> 16);
      ebuffer[7] = lowByte(edata.millitime >> 24);

//      ebuffer[8] = etc....

      delay(10);
      i2c_eeprom_write_page(0x50, i*24, (byte *)ebuffer, sizeof(ebuffer)); // write to EEPROM 
      Serial.print("i "); Serial.print(i); 
      Serial.print(", ei "); Serial.print(edata.i); 
      Serial.print(", t "); Serial.print(edata.millitime); Serial.println(". "); 
    }
    Serial.println("Writed.");
    Serial.println(""); 
    delay(3000);
}

void loop() 
{
    Serial.println("Reading...");
    int ilines = 9;
    for(int i = 1; i <= ilines; i++) {
      i2c_eeprom_read_buffer(0x50, i*24, (byte *)ebuffer, sizeof(ebuffer));
      
      edata.i         = (ebuffer[3] << 24) | (ebuffer[2] << 16) | (ebuffer[1] << 8) | ebuffer[0];
      edata.millitime = (ebuffer[7] << 24) | (ebuffer[6] << 16) | (ebuffer[5] << 8) | ebuffer[4];
      
      Serial.print("i "); Serial.print(i); 
      Serial.print(", ei "); Serial.print(edata.i); 
      Serial.print(", t "); Serial.print(edata.millitime); Serial.println(". "); 
    }
    Serial.println("Readed...");
    Serial.println(" ");
    delay(10000);

}
  
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddresspage >> 8)); // MSB
    Wire.write((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.write(data[c]);
    Wire.endTransmission();
  }

void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available()) buffer[c] = Wire.read();
  }
//--------------------------------------------------------------------

// that returned this serial output:

Writing…
i 1, ei 1, t 0.
i 2, ei 2, t 13.
i 3, ei 3, t 26.
i 4, ei 4, t 39.
i 5, ei 5, t 53.
i 6, ei 6, t 66.
i 7, ei 7, t 79.
i 8, ei 8, t 93.
i 9, ei 9, t 106.
Writed.

Reading…
i 1, ei 1, t 0.
i 2, ei 2, t 13.
i 3, ei 3, t 26.
i 4, ei 14391, t 12849. // here some gabarish
i 5, ei 5, t 53.
i 6, ei 6, t 66.
i 7, ei 7, t 79.
i 8, ei 14391, t 12849. // here some gabarish
i 9, ei 9, t 106.
Readed…

strange but i get similar result when use a second probe code

//  another example
#include "MyTypes.h"
edata_t edata;
//......
void setup() 
{
    Wire.begin(); // initialise the connection
    Serial.begin(9600);
    Serial.println("Writing...");
    //...    
    for(int i = 1; i <= ilines; i++) {
      //...     
      i2c_eeprom_write_edata(0x50, i*24, &edata); // write to EEPROM 
      //...
    }
}

void loop() 
{
    Serial.println("Reading...");
    //...
    for(int i = 1; i <= ilines; i++) {
        edata = i2c_eeprom_read_edata(0x50, i*24); //, edata); // write to EEPROM 
        Serial.print("i "); Serial.print(i); 
         Serial.print(", ei "); Serial.print(edata.i); 
         Serial.print(", t "); Serial.print(edata.millitime); Serial.println(". "); 
    }
   //...
}
//
void i2c_eeprom_write_edata( int deviceaddress, unsigned int eeaddresspage, edata_t* ed ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddresspage >> 8)); // MSB
    Wire.write((int)(eeaddresspage & 0xFF)); // LSB
    //
    unsigned long uBufSize = sizeof(edata_t);
    byte pBuffer[uBufSize];
    memcpy(pBuffer, &ed, uBufSize);
    //
    Wire.endTransmission();
}

edata_t i2c_eeprom_read_edata( int deviceaddress, unsigned int eeaddress) {
    edata_t ed;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();

    unsigned long uBufSize = sizeof(edata_t);
    byte pBuffer[uBufSize];
    
    Wire.requestFrom(deviceaddress,uBufSize);
    for (int i = 0; i < uBufSize; i++ )
       if (Wire.available()) pBuffer[i] = Wire.read();

    memcpy(&ed, pBuffer, uBufSize);

    return ed;
  }

// that return a similar gabarish:
Writing…
i 1, ei 1, t 0.
i 2, ei 2, t 10.
i 3, ei 3, t 21.
i 4, ei 4, t 31.
i 5, ei 5, t 43.
i 6, ei 6, t 53.
i 7, ei 7, t 64.
i 8, ei 8, t 83.
i 9, ei 9, t 103.
Writed.

Reading…
i 1, ei 1, t 0.
i 2, ei 2, t 13.
i 3, ei 3, t 26.
i 4, ei 540620855, t 875770417. //here some gabarisn
i 5, ei 5, t 53.
i 6, ei 6, t 66.
i 7, ei 7, t 79.
i 8, ei 540620855, t 875770417. //here some gabarisn
i 9, ei 9, t 106.
Readed.

      ebuffer[0] = lowByte(edata.i);
      ebuffer[1] = lowByte(edata.i >> 8);
      ebuffer[2] = lowByte(edata.i >> 16);
      ebuffer[3] = lowByte(edata.i >> 24);

This makes no sense. Taking the same byte and shifting it 4 different amounts will not reproduce the 4 bytes of edata.i in the array.

please consider that (edata.i) is a unsigned long integer (4 bytes), and i try to convert the unsigned long integer putting it into a array of bytes (ebuffer)

please consider that (edata.i) is a unsigned long integer (4 bytes), and i try to convert the unsigned long integer putting it into a array of bytes (ebuffer)

I considered that. You are doing it wrong, though. Nothing in the code reads anything but the low order byte from i. That one byte is then shifted 4 different amounts, and each shifted value is written to the array.

There are EEPROM_writeAnything and EEPROM_readAnything functions that do the conversion for you, and do it properly. Why not just use them?

I know its “late” to write to a topic that’s 2 year old, but…may be it could help to somebody.
The proposal of using EEPROM_writeAnything isn’t the idea here, because the memory he/she is trying to access is at a I2C bus.

I think the problem is with the buffer of the memory to receive “so many” bytes all together.

Try this to see if the problem is a buffer issue:

void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    byte c;
    for ( c = 0; c < length; c++) {
       Wire.beginTransmission(deviceaddress);
       Wire.write((int)((eeaddresspage + c) >> 8)); // MSB
       Wire.write((int)((eeaddresspage + c) & 0xFF)); // LSB
       Wire.write(data[c]);
       Wire.endTransmission();
       delay(50); //<--------------------This delay is to give the memory the time to finish de write cycle (inefficient but useful to see what’s happening here) 
    }
  }

(this is a message in a bottle, hahaha)
Pato