Send at+csq command in an sms..

HI, i'd like to know how can i send the csq at command inside in an sms?

i use this code to know the signal:

SIM900.println("AT+CSQ");

and the response is:

AT+CSQ

+CSQ: 11,0

OK

how can i send that in an sms?

how can i send that in an sms?

Is "that" supposed to be the result of the AT command, or the AT command itself?

and the response is

For all we know, all you did was print the response. It you had saved the response in a NULL-terminated char array, you could send the response just like any other text.

Hi PaulS thanks for your answer..fist of all i have to admit i'm not an expert in arduino, not even close to...now that i said that, i feel better jaja..

i want to send the result of the at command which i didn't save it anywhere yet..i'd like to do so, save it:

"in a NULL-terminated char"...i'm afraid i won't be able to do it..

can anyone give an example of a NULL-terminated char array..where i can save an AT command to send it by sms?

can anyone give an example of a NULL-terminated char array

char response[80];

Declare this at global scope, and it will be an empty, NULL terminated, string.

Define another variable to keep track of where to write in the array:

byte index = 0;

Should be global or static.

Now, where you read the response:

   response[index++] = someInstance.read();
   response[index] = '\0'; // Keep the string NULL-terminated

Then, elsewhere, you can include response as part, or all, of the text message.

Thanks PaulS, i give it a try...

sory, i can't make it work.. this is my code to read te signal:

void Signal(){
  SIM900.println("AT+CSQ");
  if (message.indexOf("OK")){
  SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
  Clean_SMS();

this is what the monitor shows:

AT+CSQ

+CSQ: 14,0

OK

works great, but now i have to send that via sms with your examples:

char response[80];
byte index = 0;
response[index++] = someInstance.read();
response[index] = '\0'; // Keep the string NULL-terminated

but i swear i don't know how to do it, put all these together and send the sms with the signal as content...

any idea how can i do this? this was my closest try:

  SIM900.print("AT+CMGF=1\r");
  delay(1000);
  SIM900.println("AT+CSQ");
  char signal = SIM900.println("AT+CSQ");
  Serial.println(signal);
  SIM900.print("AT+CMGF=1\r");
  delay(1000);
  SIM900.println("AT+CMGS=\"" + Sender_SMS + "\"");
  SIM900.print(signal);
  delay(1000);
  SIM900.println((char)26);

unfortunately i received an empty sms...

Hi it's me again, what should i do with this:

response[index++] = someInstance.read();

error:

someInstance' was not declared in this scope

where should i declare that instance?

Anyone who can give me an idea of how can i solve this issue?

landom:
Anyone who can give me an idea of how can i solve this issue?

Bumping your post 4 times in 15 hours isn't showing patience, grasshopper.

this is what the monitor shows:

The snippet of code you posted does NOT generate that output.

but i swear i don't know how to do it, put all these together and send the sms with the signal as content...

You have a thousand piece puzzle, and you've showed us the back side of one piece and the front side of another, and you want us to write detailed instructions for you to complete the puzzle. I'm sure that you can recognize that that is NOT going to happen.

When you post ALL of your code, THEN we can make some progress helping you.

absolutely right, i'm impatient since i was born.. but it's a little bit difficult for me to change now..i'm above forty :frowning:

no problem to share the full code, here you are..but please focus on my exposed issue, because i know you'll find a lo of things maybe unnecessary or can be improved/fixed etc..

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // Configura el puerto serial para el SIM900
#include <EEPROM.h>

struct {
  char Admin[15];
} parametros;

//---------------------------------------------------------------//

// Esta función cargará los parámetros guardados en la eeprom a
//nuestra variable parametros
void cargaParams() {
  byte *p = (byte*)  &parametros;
  for (int x = 0; x < sizeof(parametros); x++) {
    *p++ = EEPROM.read(x);
  }
}

//---------------------------------------------------------------------//

// Esta función guardará nuestros parámetros en la eeprom
void salvaAdmin() {
  byte *p = (byte*) &parametros.Admin;
  for (int x = 0; x < sizeof(parametros); x++) {
    EEPROM.write(x, *p++);
  }
}

//---------------------------------------------------------------------------------//

char incoming_char = 0; //Variable que guarda los caracteres que envia el SIM900
String mensaje = "";
String Remitente, Inicial;
int LED = 5 ;
int LED2 = 6;


int enviarAT(char* ATcommand, char* resp_correcta, unsigned int tiempo)
{
  int x = 0;
  bool correcto = 0;
  char respuesta[100];
  unsigned long anterior;

  memset(respuesta, '\0', 100); // Inicializa el string
  delay(100);
  while ( SIM900.available() > 0) SIM900.read(); // Limpia el buffer de entrada
  SIM900.println(ATcommand); // Envia el comando AT
  x = 0;
  anterior = millis();
  // Espera una respuesta
  do {
    // si hay datos el buffer de entrada del UART lee y comprueba la respuesta
    if (SIM900.available() != 0)
    {
      //Comprueba que no haya desbordamiento en la capacidad del buffer
      if (x < 99) {
        respuesta[x] = SIM900.read();
        x++;
      }
      else Serial.println("Desbordamiento!");
      // Comprueba si la respuesta del modulo es la 1
      if (strstr(respuesta, resp_correcta) != NULL)
      {
        correcto = 1;
      }
    }
  }
  // Espera hasta tener una respuesta
  while ((correcto == 0) && ((millis() - anterior) < tiempo));
  Serial.println(respuesta);

  return correcto;
}

void setup()
{
  pinMode( LED, OUTPUT) ; // LED como salida
  pinMode( LED2, OUTPUT) ; // LED2 como salida
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  SIM900.begin(9600); //Configura velocidad del puerto serie para el SIM900
  Serial.begin(9600); //Configura velocidad del puerto serie del Arduino
  delay (1000);
  SIM900.print("AT+CPIN=\"1234\""); //Comando AT para introducir el PIN de la tarjeta
  delay(25000); //Tiempo para que encuentre una RED
  SIM900.print("AT+CMGF=1\r"); //Configura el modo texto para enviar o recibir mensajes
  delay(1000);
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); // Saca el contenido del SMS por el puerto serie del GPRS / asi activas los mensajes del Monitor Serial
  delay(5000);
  while ( enviarAT("AT+CREG?", "+CREG: 0,1", 1000) == 0)
  {
  }
  delay(1000);
  cargaParams();
  delay(1000);
  Inicial = (parametros.Admin);
  //SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
  delay(1000);
  SIM900.println("AT+CSQ"); // Activa la identificación de señal
  delay(1000);
  SIM900.print("AT+CLIP=1\r"); // Activa la identificación de llamada
  delay(100);
  SIM900.println("AT+CMGF=1\r");
  delay(1000);
  SIM900.println("AT+CMGS=\"" + Inicial + "\"");
  delay(1000);
  SIM900.print("Sistema iniciado. Aguardando comandos....");
  delay(100);
  SIM900.println((char)26); //Comando de finalización ^Z
  delay(100);
  SIM900.println();
  delay(5000);  // Esperamos un tiempo para que envíe el SMS*/
}

void loop()
{
  if (SIM900.available() > 0)
  {
    incoming_char = SIM900.read(); //Guardamos el carácter del GPRS
    Serial.print(incoming_char); //Mostramos el carácter en el monitor serie
    mensaje = mensaje + incoming_char ; // Añadimos el carácter leído al mensaje
  }
  mensaje.toUpperCase();
  identifica_SMS();
}

//  ---------------------------------------------------------  //

void(* resetFunc) (void) = 0; // Function to reset Arduino

//  ---------------------------------------------------------  //

void Remitente_SMS() {
  if (mensaje == "") {
  }
  else {
    String char13 = mensaje;
    uint8_t startPos, endPos;
    startPos = char13.indexOf("\"") + 5;
    endPos = char13.indexOf("\"", startPos);
    Remitente = char13.substring(startPos, endPos);
  }
}

void Limpia_SMS() {
  while ( SIM900.available() > 0) SIM900.read(); // Limpia el buffer de entrada
  SIM900.println();
  mensaje = "";
  Remitente = "";
}


void identifica_SMS()
{
  int rele_on = mensaje.indexOf("ER1");
  int rele_off = mensaje.indexOf("AR1");
  int Admin_tel = mensaje.indexOf("790M41LX21AD");
  int Admin_sg = mensaje.indexOf("#N");

  if (rele_on >= 0)
  {
      Remitente_SMS();
      if (Remitente == parametros.Admin) {
      digitalWrite( LED, LOW) ;
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + Remitente + "\"");
      delay(1000);
      SIM900.print("\nRele 1 encendido.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
      Limpia_SMS();
    }
    else{
      SIM900.println();
      Limpia_SMS();
    }
  }

  else  if (rele_off >= 0)
  {
      Remitente_SMS();
      if (Remitente == parametros.Admin) {
      digitalWrite( LED, HIGH);
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + Remitente + "\"");
      delay(1000);
      SIM900.print("\nRele 1 apagado.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
      Limpia_SMS();
    }
    else{
      SIM900.println();
      Limpia_SMS();
    }
  }

else  if (Admin_tel >= 0)
  {
      Remitente_SMS();
      if (Remitente == parametros.Admin) {
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + Remitente + "\"");
      delay(1000);
      SIM900.print("Ud. YA es el Admin.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
      Limpia_SMS();
    }
    else {
      String char13 = mensaje;
      uint8_t startPos, endPos;
      String NvoTelAdmin;
      Serial.println(char13);
      startPos = char13.indexOf("\"") + 5;
      endPos = char13.indexOf("\"", startPos);
      NvoTelAdmin = char13.substring(startPos, endPos);
      NvoTelAdmin.toCharArray(parametros.Admin, 15);
      salvaAdmin();
      //delay(1000);
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + NvoTelAdmin + "\"");
      delay(1000);
      SIM900.print("Ud. es el nuevo Admin.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
      Limpia_SMS();
    }
  }

   else  if (Admin_sg >= 0)
  {
    Senial();
  }
//
}

  void Senial(){
  SIM900.println("AT+CSQ");
  if (mensaje.indexOf("OK")){
    //here i'd like to send the result of the AT Command via SMS
  Limpia_SMS();
 }
}
  SIM900.println("AT+CSQ"); // Activa la identificación de señal
  delay(1000);

Here, you send an AT command to the modem. It generates a response, asynchronously.

void loop()
{
  if (SIM900.available() > 0)
  {
    incoming_char = SIM900.read(); //Guardamos el carácter del GPRS
    Serial.print(incoming_char); //Mostramos el carácter en el monitor serie
    mensaje = mensaje + incoming_char ; // Añadimos el carácter leído al mensaje
  }

Here, you read any response(s) from the modem.

The problem is that you have no idea what command(s) the response(s) is/are in response, and you have no idea whether this data is a complete response to one command, complete responses to multiple commands, a partial response to one command, or partial responses to two or more commands.

You need a far more robust mechanism for reading replies, you need to call mechanism after EVERY AT command sent, so you know that mensaje contains the complete response to a single command, and which command it is a response. THEN, you can use the response data for another purpose (the content of a text message, for instance.

Can you give me an example i can use in my code of rhat mechanism you're talking about? Not a generic answer..an specific one..so i could be able to aply that directly into my code

landom:
Can you give me an example i can use in my code of rhat mechanism you're talking about? Not a generic answer..an specific one..so i could be able to aply that directly into my code

No. You are sending AT commands. YOU have to determine what constitutes a complete packet, and nothing more than a simple packet. Then, you write the code to read a complete packet, and when you recognize that you have a complete packet, you stop reading.

It is unfortunate that there is no end of packet marker specifically sent in response to an AT command, or any indication up front exactly how large the response packet is going to be. That is inexcusable in my opinion.