Go Down

Topic: reading sms (Read 8 times) previous topic - next topic

PaulS

Quote
i am still trying to figure out how the Strcmp string works and how i would use it

Code: [Select]
if(strcmp(data, "on") == 0)
{
   // Turn the pin on
}
if(strcmp(data, "off") == 0)
{
   // Turn the pin off
}

kevin-se

do i need any new variables on the top,sorry,little knowledge of c++

PaulS

Quote
do i need any new variables on the top,sorry,little knowledge of c++

Not for that code.


kevin-se

ok,thanks,i will try out the new code now,

kevin-se

OK,final sketch,please let me know if it will work,
Code: [Select]
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
    digitalWrite(onModulePin,HIGH);
    delay(2000);
    digitalWrite(onModulePin,LOW);
}

void setup(){

    Serial.begin(19200);                // UART baud rate
    delay(2000);
    pinMode(led, OUTPUT);
    pinMode(onModulePin, OUTPUT);
    switchModule();                    // switches the module ON

    for (int i=0;i < 5;i++){
        delay(5000);
    }

    Serial.println("AT+CMGF=1");         // sets the SMS mode to text
   
    delay(1000);
}

void loop(){
   
   
        Serial.println("AT+CMGR");    //Reads the first SMS
       
         
        Serial.flush();
        for (x=0;x < 255;x++){           
            data[x]='\0';                       
        }
        x=0;
        do{
            while(Serial.available()==0);
            data[x]=Serial.read(); 
            x++;           
            if(data[x-1]==0x0D&&data[x-2]=='"'){
                x=0;
            }
        }while(!(data[x-1]=='K'&&data[x-2]=='O'));

        data[x-3]='\0';        //finish the string before the OK

        Serial.println(data);    //shows the message
       

        delay(5000);
     
    if(strcmp(data, "on") == 0)
{
  digitalWrite(led,HIGH);
}
if(strcmp(data, "off") == 0)
{
   digitalWrite(led,LOW); // Turn the pin off
   
    Serial.println("AT+CMGD=1,4");
}   
}     
   
       




Go Up