How can I get the right value (LAC,MNC,MCC,cellid)

Dear All,

I am trying to get my position with CellID.

I found that function, which seams to be strange

 int SIMCOM900::readCellData(int &mcc, int &mnc, long &lac, long &cellid)
{
  if (getStatus()==IDLE)
    return 0;
    
   //_tf.setTimeout(_GSM_DATA_TOUT_);
   //_cell.flush();
  SimpleWriteln(F("AT+QENG=1,0")); 
  SimpleWriteln(F("AT+QENG?")); 
  if(gsm.WaitResp(5000, 50, "+QENG")!=RX_FINISHED_STR_NOT_RECV)
    return 0;

  //mcc=_tf.getValue(); // The first one is 0
  mcc=_cell.read();
  //mcc=_tf.getValue();
  mcc=_cell.read();
  //mnc=_tf.getValue();
  mnc=_cell.read();
  //lac=_tf.getValue();
  lac=_cell.read();
  //cellid=_tf.getValue();
  cellid=_cell.read();
  
  gsm.WaitResp(5000, 50, "+OK");
  SimpleWriteln(F("AT+QENG=1,0")); 
  gsm.WaitResp(5000, 50, "+OK");
  return 1;
}

When I call that function

gsm.raedCellData(mmc,mnc,lac,cellid);

it return me

-1

for mmc,mnc,lac and cellid

What look starnge for me the following

  mcc=_cell.read();
  mcc=_cell.read();;
  mnc=_cell.read();
  lac=_cell.read();
  cellid=_cell.read();

_cell.read() will return the same value for all??
if Serial.print(_cell.read); display -1, it seams to be normal that the variable, mmc, mnc, lat, cellid return -1 as well.

Something seams to be wrong for you?
How would you correct it?
Do you have an other example?

Thank for all
Cheers
Pierre

_cell.read() will return the same value for all??

No, it won't. Would you say that if the function were called pop()?

In the Sim900 AT commands, I can't see "AT+QENG"
but "AT+CENG" instead.
try to change the AT command in these lines :

  SimpleWriteln(F("AT+QENG=1,0")); 
  SimpleWriteln(F("AT+QENG?")); 
  if(gsm.WaitResp(5000, 50, "+QENG")!=RX_FINISHED_STR_NOT_RECV)
    return 0;

Once again, I don't know the gsm library, but the AT+CENG command works for me

Hello

Thank fro your answer, but unfortunately it does not work.
As said alnath, I modified my function as the following:

 int SIMCOM900::readCellData(int &mcc, int &mnc, long &lac, long &cellid)
{
  if (getStatus()==IDLE)
    return 0;
    
   //_tf.setTimeout(_GSM_DATA_TOUT_);
   //_cell.flush();
  SimpleWriteln(F("AT+CENG=1,0")); 
  SimpleWriteln(F("AT+CENG?")); 
  if(gsm.WaitResp(5000, 50, "+CENG")!=RX_FINISHED_STR_NOT_RECV)
    return 0;

  //mcc=_tf.getValue(); // The first one is 0
  mcc=_cell.read();
  //mcc=_tf.getValue();
  mcc=_cell.read();
  //mnc=_tf.getValue();
  mnc=_cell.read();
  //lac=_tf.getValue();
  lac=_cell.read();
  //cellid=_tf.getValue();
  cellid=_cell.read();
  
  gsm.WaitResp(5000, 50, "+OK");
  SimpleWriteln(F("AT+CENG=1,0")); 
  gsm.WaitResp(5000, 50, "+OK");
  return 1;
}

and this code (that i upload in the Arduino Mini pro)

if(gsm.readCellData(mcc, mnc, lac, cellid)){
          Serial.println(F("\nCellID OK"));
        }else{
          Serial.println(F("\nCellID KO"));
        }
  
        #ifdef DEBUG
          Serial.print(F("MCC :\t")); Serial.println(mcc);
          Serial.print(F("MNC :\t")); Serial.println(mnc);
          Serial.print(F("LAC :\t")); Serial.println(lac);
          Serial.print(F("cellid :\t")); Serial.println(cellid);
        #endif

still return me

CellID OK
MCC : -1
MNC : -1
LAC : -1
cellid : -1

So the fact to modify QENG to CENG, did not change any thing :relaxed:

I am using that library (the second)
http://code.google.com/p/gsm-shield-arduino/downloads/list
and the raedCellData() is into the file SIM900.ccp

@alnath,

Could you provide an exemple of your code?

Cheers
Pierre

it is on another PC, I'll post an example which works asap.

Thank a lot, it's very nice from you.

ok, here is an example that works.
NB :

  • maSerie is a softwareserial instance
  • Writing "2" as first parameter in "AT+CENG=2,0" causes the SIM900 to send constantly the +CENG: sentence, no need to write a "AT+CENG?" , but that's also why I put it back to 0 after reading it.
  • It's just a test code, as you can see. I put it immediately after the SIM900 initialisation part, and I read the answer just for it. I still have to adapt it to insert it in my main program :wink:

anyway, it gives me useful informations, my location was given in a 5km precision 8)
I think I'll get more accurate answers if I give more than one cell : send the same command, without a 2nd parameter or with, "1", and "2" etc... as a 2nd parameter.... I'll try that later.

....
....
maSerie.println("AT+CENG=2,0"); 
delay(200);  // will try without this 
//----------------------------------
  while(maSerie.available())
  {
     char InByte;
     InByte = (unsigned char)maSerie.read();  
       //  <CR>   --> on sort
     if(InByte == 13)
     {
            Serial.println(cellid);
            maSerie.println("AT+CENG=0,0"); 
            break;  
      }
      if(InByte == 10 ){ }
      else 
      {
           // Stocke le caractère dans la mémoire tampon 
            cellid += String(InByte);
       } 
  }   
  maSerie.println("AT+CENG=0,0"); 

// do something with cellid string 
....
.....

and cellid, how did you declare it? as y byte?

no, it's a String object.
it has to be cleared once it has been used
but once again, it is just a try...I just wanted to see if I could get accurate information.
I won't read the serial datas in a while loop in my program :wink:

the "interesting" part is the serial.write command, it shows you how to send an AT commands to your shield. But as you use the GSM libary, you won't use it. I don't use GSM library just because it takes a lot of RAM, and I don't really need it (I don't do complicated stuff with my shield :wink: ) , but maybe for these things, it is easier with the library

BTW, I tried to replace the first parameter with a "1" (as in your example), but "AT+CENG?" doesn't answer anything !
using "2" is tricky and IMO, shouldn't be done in a final sketch.

Still have to investigate about this :grin:

Unfortunatey, I still can not make it working. Even with your answer :o((

Some one ask another idea?

I am a bit frustred because my library has a such function but it return -1 for lac, mnc, mcc, and cellid. It does not work as expected.

Some has an idea or another example?

Cheers

I am a bit frustred because my library has a such function but it return -1 for lac, mnc, mcc, and cellid. It does not work as expected.

Then, perhaps it is your expectations that are wrong. Your particular phone/sim card may not provide the information that the function is trying to provide.

You have the source code. Look at the function implementation. Add Serial.print() statements to track what is happening.

Dear Paul,

But it's what I did

int SIMCOM900::readCellData(int &mcc, int &mnc, long &lac, long &cellid)
 
  
  if (getStatus()==IDLE)
    return 0;
    
  
  
   //_tf.setTimeout(_GSM_DATA_TOUT_);
   //_cell.flush();
  SimpleWriteln(F("AT+QENG=1,0")); 
  SimpleWriteln(F("AT+QENG?")); 
  if(gsm.WaitResp(5000, 50, "+QENG")!=RX_FINISHED_STR_NOT_RECV)
    return 0;

  //mcc=_tf.getValue(); // The first one is 0
  mcc=_cell.read();
  //mcc=_tf.getValue();
  mcc=_cell.read();
  //mnc=_tf.getValue();
  mnc=_cell.read();
  //lac=_tf.getValue();
  lac=_cell.read();
  //cellid=_tf.getValue();
  cellid=_cell.read();
  
// THE BELOW SERIAL.PRINTLN DISPLAY -1
  Serial.println(mcc);
  Serial.println(mnc);
  Serial.println(lac);
  Serial.println(cellid);
  
  gsm.WaitResp(5000, 50, "+OK");
  SimpleWriteln(F("AT+QENG=1,0")); 
  gsm.WaitResp(5000, 50, "+OK");
  
  
  return 1;
}

But it's what I did

All you've proven is that the modem did not respond with enough data or did not respond in a timely fashion. That suggests to me that your modem does not understand the QENG? or does not implement it (fully).

as I said before, "AT+QENG" doesn't appear in the "sim900 at command" documents I've found, and when I replace the AT+CENG command with AT+QENG , the answer I get is "ERROR" .
All your calls return "-1" , a return value which often means "error"

does the gsm library have a function that prints the error messages ?
there is an AT command that enables detailed error messages, maybe you could try to "serial.print" all the messages the gsm sends ?

All your calls return "-1" , a return value which often means "error"

It's a return code that means no data. That is not quite the same thing.

PaulS:

All your calls return "-1" , a return value which often means "error"

It's a return code that means no data. That is not quite the same thing.

always ? :roll_eyes:
it means what the programmer who wrote the library wanted it to mean :wink:

anyway, it doesn't matter, and I'm pretty sure that SIM900 doesn't understand QENG , it would be interesting to get the answer it sends to this command.

I've searched again today in this doc Redirect Notice , no QENG .

what are the results with CENG ? still "-1" ?

BTW, the "AT+CMEE=2" command enable the verbose mode for error messages