I2C PROBLEM INTEGRATING WITH GSM

Hello Everyone,
I am new to arduino…I am using i2c communication to communicate 2 atmega2560 with each other.
basically my application is , a quectel gsm modem is connected to one arduino (slave) whatever the data is received when i fired some commands on gsm modem is stored in the variable. now with the help of i2c communication i have to send this stored data on another arduino(master). but i2c communication is not working. here is my code for slave

#include <Wire.h>
const byte SlaveDeviceId = 1;
char p;
static int i=0;
int length=0;
boolean stringComplete = false;
int8_t inflag=0,ans=0,l;
int8_t sendATcommand(char
ATcommand, char* expected_answer1, unsigned int timeout);
char response[100];
volatile char str_rec[50];
static uint8_t buffer[27]={1,2,3,4,5,6,7};
struct transmit
{
char cell_id_2G[5];
char lac_2G[7];
char cell_id_3G[5];
char lac_3G[7];
};

struct transmit tx;
void setup()
{
// Start I²C bus as a slave
Wire.begin(SlaveDeviceId);
// Set the callback to call when data is requested.
Wire.onRequest(requestCallback);
Serial.begin(9600);
Serial2.begin(9600);
ans=sendATcommand(“AT”, “OK”, 2000);
}

void loop()
{

ans=sendATcommand(“AT+QNWCFG=“NWSCANMODE”,0”, “OK”,2000);
if(ans==1)
{
Serial.println(“ENTER 2G”);
sendATcommand(“AT+CREG=2”, “OK”, 2000);
ans=sendATcommand(“AT+CREG?”, “+CREG:”, 2000);
if(p!=NULL)
{
for(l=0;l<4;l++)
{
tx.cell_id_2G[l]=p[12+l];
}
tx.cell_id_2G[l]=’\0’;
for(l=0;l<6;l++)
{
tx.lac_2G[l]=p[19+l];
}
tx.lac_2G[l]=’\0’;
Serial.println(tx.cell_id_2G);
Serial.println(tx.lac_2G);
}
else
{
tx.cell_id_2G[0]=’\0’;
tx.lac_2G[0]=’\0’;
}
}

ans=sendATcommand(“AT+QNWCFG=“NWSCANMODE”,2”, “OK”,2000);
if(ans==1)
{
Serial.println(“ENTER 3G”);
sendATcommand(“AT+CREG=2”, “OK”, 2000);
ans=sendATcommand(“AT+CREG?”, “+CREG:”, 2000);
if(p!=NULL)
{
for(l=0;l<4;l++)
{
tx.cell_id_3G[l]=p[12+l];
}
tx.cell_id_3G[l]=’\0’;
for(l=0;l<6;l++)
{
tx.lac_3G[l]=p[19+l];
}
tx.lac_3G[l]=’\0’;
Serial.println(“3G”);
Serial.println(tx.cell_id_3G);
Serial.println(tx.lac_3G);
}
else
{
tx.cell_id_3G[0]=’\0’;
tx.lac_3G[0]=’\0’;
}
}
}

void requestCallback()
{
for(l=0;l<5;l++)
{
buffer[l]=tx.cell_id_2G[l];
}
for(l=5;l<12;l++)
{
buffer[l]=tx.lac_2G[l];
}
Wire.write(buffer,27);
}
/* but if i change this block like this its working
void requestCallback()
{
for(l=0;l<27;l++)
{
buffer[l]=l;
Wire.write(buffer,27);
}
}*/

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){

uint8_t x=0, answer=0;

unsigned long previous;

memset(response, ‘\0’, 200); // Initialize the string

delay(100);

// while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial2.println(ATcommand); // Send the AT command

x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial2.available() != 0){
response = Serial2.read();
x++;
//Serial.println(response);
// check if the desired answer is in the response of the module

}
// Waits for the asnwer with time out
}
while(((millis() - previous) < timeout));
if ((p=strstr(response, expected_answer1)) != NULL)
{
answer = 1;
}
return answer;
}
-------------------------------------------/----------------------------/----------------------------/-------------------------------
this is the code for master

#include <Wire.h>

const byte SlaveDeviceId = 1;
int available=0;
void setup()
{
// Start I²C bus as master
Wire.begin();

// For demonstration purposes.
Serial.begin(9600);
}

void loop()
{

// Request data from slave.
Wire.beginTransmission(SlaveDeviceId);
available = Wire.requestFrom(SlaveDeviceId, (uint8_t)27);
if(available > 0)
{
Serial.print("\nbytes received: ");
while(available > 0)
{
int receivedValue = Wire.read();
Serial.print((char)receivedValue);
available–;
}
}
int result = Wire.endTransmission();
if(result)
{
}
available=0;
delay(1000);

}

what can be the problem,y the buffer not getting update with the variable value … i tried changing storage class of variable with static and volatile but still its not working…plz help me with this…

what can be the problem

I KNOW that your code doesn't look like that. Go read the top two posts in this section of the forum that you were supposed to read BEFORE posting. When you get done, do it right. You'll know what that means when you get done reading.

I am posting my question again… sorry that i didnt follow the rules…but please help me with this problem
.I am using i2c communication to communicate 2 atmega2560 with each other.
basically my application is , a quectel gsm modem is connected to one arduino (slave) whatever the data is received when i fired some commands on gsm modem is stored in the variable. now with the help of i2c communication i have to send this stored data on another arduino(master). but i2c communication is not working. here is my code for slave

#include <Wire.h>
const byte SlaveDeviceId = 1;
char *p;
static int i=0;
int length=0;
boolean stringComplete = false;
int8_t inflag=0,ans=0,l;
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout);
char response[100];
volatile char str_rec[50];
static uint8_t buffer[27]={1,2,3,4,5,6,7};
struct transmit
{
 char cell_id_2G[5];
 char lac_2G[7];
 char cell_id_3G[5];
 char lac_3G[7];
};

 struct transmit tx;
void setup()
{
  // Start I²C bus as a slave
  Wire.begin(SlaveDeviceId);
  // Set the callback to call when data is requested.
  Wire.onRequest(requestCallback);
  Serial.begin(9600);
  Serial2.begin(9600);
  ans=sendATcommand("AT", "OK", 2000);
}

void loop()
{
 
   ans=sendATcommand("AT+QNWCFG=\"NWSCANMODE\",0", "OK",2000);
   if(ans==1)
   {
      Serial.println("ENTER 2G");
       sendATcommand("AT+CREG=2", "OK", 2000);
       ans=sendATcommand("AT+CREG?", "+CREG:", 2000);
       if(p!=NULL)
       {
          for(l=0;l<4;l++)
          {
            tx.cell_id_2G[l]=p[12+l];
          }
            tx.cell_id_2G[l]='\0';
            for(l=0;l<6;l++)
            {
             tx.lac_2G[l]=p[19+l];
            }
            tx.lac_2G[l]='\0';
             Serial.println(tx.cell_id_2G);
             Serial.println(tx.lac_2G);
        }
       else
       {
        tx.cell_id_2G[0]='\0';
        tx.lac_2G[0]='\0';   
       }
   }
 
   
   ans=sendATcommand("AT+QNWCFG=\"NWSCANMODE\",2", "OK",2000);
   if(ans==1)
   {
     Serial.println("ENTER 3G");
     sendATcommand("AT+CREG=2", "OK", 2000);
       ans=sendATcommand("AT+CREG?", "+CREG:", 2000);
     if(p!=NULL)
       {
          for(l=0;l<4;l++)
          {
            tx.cell_id_3G[l]=p[12+l];
          }
            tx.cell_id_3G[l]='\0';
           for(l=0;l<6;l++)
           {
             tx.lac_3G[l]=p[19+l];
           }
              tx.lac_3G[l]='\0';
               Serial.println("3G");
               Serial.println(tx.cell_id_3G);
               Serial.println(tx.lac_3G);
         }
       else
       {
          tx.cell_id_3G[0]='\0';
          tx.lac_3G[0]='\0';   
       }
   }
}


void requestCallback()
{
  for(l=0;l<5;l++)
  {
    buffer[l]=tx.cell_id_2G[l];
  }
  for(l=5;l<12;l++)
  {
    buffer[l]=tx.lac_2G[l];
  }
  Wire.write(buffer,27);
}   
 /* but if i change this block like this its working
void requestCallback()
{
for(l=0;l<27;l++)
  {
    buffer[l]=l;
Wire.write(buffer,27);
  }
}*/

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){

    uint8_t x=0,  answer=0;
   
    unsigned long previous;

    memset(response, '\0', 200);    // Initialize the string

    delay(100);

   // while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial2.println(ATcommand);    // Send the AT command


        x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(Serial2.available() != 0){   
            response

    x= Serial2.read();

            x++;
           //Serial.println(response);
            // check if the desired answer is in the response of the module
           
        }
        // Waits for the asnwer with time out
    }
    while(((millis() - previous) < timeout));   
     if ((p=strstr(response, expected_answer1)) != NULL)   
            {
                answer = 1;
            }
    return answer;
}

this is the code for master

#include <Wire.h>

const byte SlaveDeviceId = 1;
int available=0;
void setup()
{
  // Start I²C bus as master
  Wire.begin();
 
  // For demonstration purposes.
  Serial.begin(9600);
}

void loop()
{
 
  // Request data from slave.
  Wire.beginTransmission(SlaveDeviceId);
  available = Wire.requestFrom(SlaveDeviceId, (uint8_t)27);
  if(available > 0)
  {
     Serial.print("\nbytes received: ");
     while(available > 0)
     {
      int receivedValue = Wire.read();
      Serial.print((char)receivedValue);
      available--;
     }
  }
   int result = Wire.endTransmission();
  if(result)
  {
  }
  available=0;
 delay(1000);
 
}

what can be the problem,y the buffer not getting update with the variable value … i tried changing storage class of variable with static and volatile but still its not working…plz help me with this…

  for(l=0;l<5;l++)
  {
    buffer[l]=tx.cell_id_2G[l];
  }

There’s a damned good reason that i is the usual loop index. i can not be mistaken for 1. That is not the case with the letter l.
You are populating 12 characters in buffer, but NOT NULL terminating it. Why not?

static uint8_t buffer[27]={1,2,3,4,5,6,7};

Your global buffer is declared static. Why? You provide 7 silly initializers for a 27 element array. Why?

  Wire.write(buffer,27);

After assigning 12 values to the array, you send 27. Why?

 /* but if i change this block like this its working
void requestCallback()
{
for(l=0;l<27;l++)
  {
    buffer[l]=l;
Wire.write(buffer,27);
  }
}*/

It doesn’t make sense to send 27 characters 27 times.

    while(((millis() - previous) < timeout));

Congratulations on reimplementing delay() in a less obvious way. Why did you?

what can be the problem,y the buffer not getting update with the variable value

Something is in the buffer you send. What is? How do you know if it is right or wrong?
Something is in the buffer you receive. What?

     while(available > 0)
     {
      int receivedValue = Wire.read();
      Serial.print((char)receivedValue);
      available--

A for loop would make more sense. Why should one have to study this code to discover that it is an inside out for loop?

sorry i misprint my code…it is like this

void requestCallback()
{
for(l=0;l<27;l++)
 buffer[l]=l;
Wire.write(buffer,27);

}*/

while(((millis() - previous) < timeout)); 
i dont have any problem with this section its working properly…

uint8_t buffer[27]=;
Wire.write(buffer,12);

i tried this then also its not working

what i am not understanding is y this section of code works and when i update the array outside it wont work

void requestCallback()
{
for(l=0;l<27;l++)
 buffer[l]=l;
Wire.write(buffer,27);

}

i tried this then also its not working

It doesn't even compile, so any talk about working is useless.

what i am not understanding is y

you can't define what "is not working" means. The code DOES something.