Cómo solucionar un mensaje ciclado en arduino uno con la tarjeta shield??

Hola :slight_smile:
Soy nueva en arduino y estoy elaborando un proyecto en el cual por medio de un mensaje de texto se le da una indicación al arduino y después de que el arduino lea la cadena que contiene el mensaje efectua cierta acción según sea el caso, ya logre hacer eso pero me surgió un problema cuando recibe el mensaje se cicla, es decir, le llega el mismo mensaje infinidad de veces... Y no puedo continuar con el siguiente módulo, el problema ocurre cada k pongo la condición con un if alguien podría decirme la causa del problema??

Muchas gracias :wink:

Si no pones el código, poco te vamos a poder ayudar

Disculpa :slight_smile:
Este es el código en donde tengo lo del mensaje

while(c=sms.read())

txtMsgs += c;
Serial.print(txtMsgs);
if(txtMsgs == "abrir"){
digitalWrite(pinLed1,HIGH);
digitalWrite(pinLed2,LOW);
// servoMain.write(180);
delay(pausa);
digitalWrite(pinLed1,LOW);
digitalWrite(pinLed2,HIGH);
//servoMain.write(0);
delay(2000);
digitalWrite(pinLed2,LOW);

char txtMsg[200] = "Llave cerrada";
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);

sms.beginSMS(senderNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");

Serial.println("\nEND OF MESSAGE");

// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
else
{
Serial.println("mensaje no valido");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}

txtMsgs = "";
}

delay(1000);

}

la variable txtMsgs la tenwo inicializada en otra parte del código, ésta variable la utilice para poder jugar con el contenido del mensaje y ya me lee la cadena pero ahora mi problema es que si mandas un mensaje le sigue llegando el mismo infinidad de veces... Que puedo hacer???

Te estaba por sugerir algo pero veo que ya lo implementas.
Todo me hace pensar que sms.flush no estan funcionando como piensas.
Investigo y respondo con mas criterio.

Esto encontré. Yo creo que debes revisar esta parte

void loop()
{
  char c;

  // If there are any SMSs available()  
  if (sms.available())
  {
    Serial.println("Message received from:");

    // Get remote number
    sms.remoteNumber(remoteNumber, 20);
    Serial.println(remoteNumber);

    // This is just an example of message disposal    
    // Messages starting with # should be discarded
    if(sms.peek()=='#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }

    // Read message bytes and print them
    while(c=sms.read())
      Serial.print(c);

    Serial.println("\nEND OF MESSAGE");

    // delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}

Te estaba por sugerir algo pero veo que ya lo implementas.
Todo me hace pensar que sms.flush no estan funcionando como piensas.
Investigo y respondo con mas criterio.

Muchas gracias amigo :slight_smile: Pero esa parte de código ya la tengo implementada... Sólo que ya no la pegue jeje me estoy guiando con el ejemplo de GSM recibe mensajes que se encuentra en el IDE de arduino primero tenia problemas porque deseaba hacer comparaciones con el texto del mensaje pero no podía comparar la cadena, realice una agregación para poder guardar el valor que recibe C en un String llamado txtMsgs y ahora que estoy realizando pruebas me doy cuenta que cuando mandas cualquier cadena si te muestra en consola que tu mensaje no es valido pero cuando mandas la cadena correcta que en este caso es "abrir" hace lo que debe de hacer pero le sigue llegando el mensaje infinidad de veces.... Te mando el código completo :slight_smile: Gracias

#include <GSM.h>
//#include <Servo.h>
// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char senderNumber[20];
int pinLed1 = 10;
int pinLed2 = 9;
int pausa = 30000;

String txtMsgs = "";
//Servo servoMain;
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
pinMode(pinLed1, OUTPUT);
pinMode(pinLed2, OUTPUT);
//servoMain.attach(13);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println("SMS Messages Receiver");

// connection state
boolean notConnected = true;

// Start GSM connection
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}

Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}

void loop()
{
char c;

// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");

// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);

// An example of message disposal
// Any messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}

// Read message bytes and print them
while(c=sms.read())

txtMsgs += c;
Serial.print(txtMsgs);
if(txtMsgs == "abrir"){
digitalWrite(pinLed1,HIGH);
digitalWrite(pinLed2,LOW);
// servoMain.write(180);
delay(pausa);
digitalWrite(pinLed1,LOW);
digitalWrite(pinLed2,HIGH);
//servoMain.write(0);
delay(2000);
digitalWrite(pinLed2,LOW);

char txtMsg[200] = "Llave cerrada";
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);

sms.beginSMS(senderNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");

Serial.println("\nEND OF MESSAGE");

// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
else
{
Serial.println("mensaje no valido");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}

txtMsgs = "";
}

delay(1000);

}

Los codigos van usando este icono
y si es muy largo lo subes con Attachments and other options (Abajo)
Lo veo y te respondo.

Viste el ejemplo que te expiqué que también has usado.
Porque no te concentras en leer un SMS y verificar que no lo vuelva a leer.
YO me apartaría de todo el programa y me concentraría en esa parte del mismo.

No tengo el módulo sms así que no puedo reproducir tu falla.
Algo que si puedo decirte y no veo en tu programa de lo que recuerdo, luego lo busco y te digo mas con precisión.

Ajusta los pines para que se correspondan con tu hardware
No estas comprobando respuesta del modulo SMS. Cuando te responde a tus comandos no esperas la respuesta, solo le das DELAY. Eso no me gusta.
No esta mal porque claro es algo sugerido pero si hay algo mal no lo sabes.

ATE1 --> Set echo ON
AT+CMGF=1 --> Establishes text mode in modem
// hasta aca seguramente los usaste y los que siguien

AT+CMGL="REC UNREAD",1 --> Checks if exists unread SMS. Return OK if not exist any SMS, in other case returns a list with them (the first number of each of them are the index of each of them).
AT+CMGR=[sms_index],0 --> Read SMS with index sms_index from modem memory (ej: AT+CMGR=1,0).
AT+CMGD=[sms_index] --> Delete SMS with index sms_index from modem memory (ej: AT+CMGD=1).

PROGRAMA SMS READ para asegurarnos que funciona bien
Ejecuta este programa y me dices que resultados da: si lo hace bien o mal.
Esto tiene que funcionar bien y lo veo parecido al tuyo.

 /
* GSM shield 

 created 25 Feb 2012
 by Javier Zorzano / TD

 This example is in the public domain.
*/

// libraries
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

char remoteNumber[20];  // Holds the emitting number

void setup() 
{
  // initialize serial communications
  Serial.begin(9600); 

  Serial.println("SMS Messages Receiver");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() 
{
  char c;

  // If there are any SMSs available()  
  if (sms.available())
  {
    Serial.println("Message received from:");

    // Get remote number
    sms.remoteNumber(remoteNumber, 20);
    Serial.println(remoteNumber);

    // This is just an example of message disposal    
    // Messages starting with # should be discarded
    if(sms.peek()=='#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }

    // Read message bytes and print them
    while(c=sms.read())
      Serial.print(c);

    Serial.println("\nEND OF MESSAGE");

    // delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}

ammm muchas gracias :slight_smile: :slight_smile: :slight_smile: Lo intentare aplicar y ver el funcionamiento... Si soluciona mi problema lo comparto aqui mismo... Ammm y gracias por el consejo igual soy nueva en el foro.. :slight_smile:

Ok. Lo importante es que puedas resolver tu problema, por eso insisto en que te aisles de TODO el programa y solo te concentres en mensaje recibido y bien leido. Y luego borrado.
Hay un comando que borra todos los msges. Tal vez debas hacer eso. Borrarlos todos y empezar a probar.
Todo tiene una secuencia y puede ser que incurras una y otra vez en una error sistemático.
Por eso RESET a la lista de SMS. Todos borrados y comienza de cero con este programa sugerido por el propio Blog de ARduino.