Save AT Command response (Modulo´s IMEI) on variable - SIM900/SIM800

Hello All,

I'm trying to send an AT Command to get the IMEI number of the module and save it on a string to use it later on my program... everything lines of code I try just fail.

Below, it's the only one that worked (kind of)... because it only works if it's inside of void setup, and I don't know why it keeps repeating as it was on void loop. And I only want the IMEI number, and with the IMEI it saves the OK response too.

gsm.println("AT+CGSN");

if(gsm.available() > 0)
  {
       imei = gsm.readStringUntil('\n');

   }

Serial.print (imei);

The imei variable is String type, and I'm just printing on serial because I want to see if the variable is saved right.

What should I do?

The imei variable is String type, and I'm just printing on serial because I want to see if the variable is saved right.

What should I do?

Serial.print("imai: [");
Serial.print(imai);
Serial.println("]");

The art of giving bad answers lies in misreading the questions.