SM5100b SMS receiving

hi all,

Here is what i want to do: sense the temperature, check if any SMS has arrived, turn on the leds as instructed by the SMS, repeat the loop.

void loop () {

while (cell.available() >0)
{
char a[10];
inchar = cell.read();
a[0] = inchar;
if ( a[0]== '#')
digitalWrite(11,HIGH);
}
temperature = analogRead(0);
temperature = (temperature/1024)*5*1000;
Serial.print(temperature);
}

in the code, i would like to make it works like a interrupt, when there is a SMS being sent to the shield, the Arduino can respond as instructed. But from what i have observed, the led fails to turn on. Is there anything i have done wrong? It should be..:astonished: Can anyone give me some advice?? Thanks!

Regards

Please post all your code - we can't see the declarations of inchar or temperature, for instance.

Sorry for the inconvenience caused. Here is the codes in full: :slight_smile:

#include <string.h>
#include <SoftwareSerial.h>
char incoming_char=0;
char a[20];
SoftwareSerial cell(2,3);
char mobilenumber[]="**********";
char textmessage[160];
char inchar;
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup() {
  Serial.begin(9600);
  cell.begin(9600);
  delay(30000);
  cell.println("AT+CMGF=1");
  delay(200);
  cell.println("AT+CNMI=3,3,0,0");
  delay(200);
  
  pinMode(led1,OUTPUT);  //relay decoy//
  pinMode(led2,OUTPUT);  //relay decoy//
  pinMode(led3,OUTPUT);  //relay decoy//
  pinMode(led4,OUTPUT);  //relay decoy//
  
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  
  pinMode(13,OUTPUT); //SMS status lamp
  pinMode(12,OUTPUT);
}


void SMSreceive() {
                    /*Receive setup*/

                    /*Loop Logic*/
                    
                    
                      
                        inchar=cell.read();
                        if (inchar=='#') // OK - the start of our command
                        {
                          delay(10);
                          inchar=cell.read();
                            if (inchar=='a')
                              {
                                delay(10);
                                inchar=cell.read();
                                  if (inchar=='0')
                                    {
                                      digitalWrite(led1, LOW);
                                    }

                                  else if (inchar=='1')
                                    {
                                      digitalWrite(led1, HIGH);
                                    }
                                      delay(10);
                                      inchar=cell.read();
                                        if (inchar=='b')
                                          {
                                            inchar=cell.read();
                                              if (inchar=='0')
                                                {
                                                  digitalWrite(led2, LOW);
                                                 }
                                              else if (inchar=='1')
                                                 {
                                                   digitalWrite(led2, HIGH);
                                                  }
                                                    delay(10);
                                                     inchar=cell.read();
                                                       if (inchar=='c')
                                                         {
                                                           inchar=cell.read();
                                                              if (inchar=='0')
                                                                 {
                                                                  digitalWrite(led3, LOW);
                                                                  }
                                                               else if (inchar=='1')
                                                                  {
                                                                   digitalWrite(led3, HIGH);
                                                                   }
                                                                    delay(10);
                                                                    inchar=cell.read();
                                                                      if (inchar=='d')
                                                                        {
                                                                         delay(10);
                                                                         inchar=cell.read();
                                                                            if (inchar=='0')
                                                                              {
                                                                               digitalWrite(led4, LOW);
                                                                               }
                                                                            else if (inchar=='1')
                                                                               {
                                                                                digitalWrite(led4, HIGH);
                                                                                }
                                                                                 delay(10);
                                                                          }

                                                              } // close c
                                                                  cell.println("AT+CMGD=1,4"); // delete all SMS

                                                           }//close b
                                                     }//close a
                                                  }//close #
                                                }//close available
                                              // close function


void startSMS() {

                  digitalWrite(13,HIGH);
                  cell.println("AT+CMGF=1");
                  cell.print("AT+CMGS=");
                  cell.write(34); 
                  cell.print(mobilenumber);
                  cell.write(34);
                  cell.println();
                  delay (500);
                }
void endSMS()   {
                  cell.write(26);
                  cell.println();
                  delay(15000); 
                  digitalWrite(13, LOW);
                }

void GSMTest()
{
if(cell.available() >0)
  {
  incoming_char=cell.read();
  Serial.print(incoming_char);
  }
if(Serial.available() >0)
  {
  incoming_char=Serial.read();
  cell.print(incoming_char);
  }
}


void dateFunction ()
{
time_t t = now();
Serial.print("System Date:  ");
Serial.print(day(t));
Serial.print(" : ");
Serial.print(month(t));
Serial.print(" : ");
Serial.println(year(t));
}
void timeFunction ()
{
time_t t = now();
Serial.print("System Time:  ");
Serial.print(hour(t));
Serial.print(" : ");
Serial.print(minute(t));
Serial.print(" : ");
Serial.println(second(t));
}

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}


float LM3500()
{
  float r;
  float temperature;
  r=readVcc()/1000.0;
  temperature = analogRead(0);
  temperature = (((r*temperature)/1024))/0.01;
  Serial.print("Measured voltage: ");
  Serial.println( r, 2 );
  Serial.print("Sensor_2 temperature:");
  Serial.println(temperature,2);
  return temperature;
}

void voltmeter03()
{
  float r;
  float measuredV;
  float correctfactor = 0.023;
  float correctedV;
  
  r=readVcc()/1000.0;
  measuredV = analogRead(3);
  measuredV = measuredV*r/1024.0;
  correctedV = measuredV - measuredV*correctfactor;
  Serial.print("Vcc Supply voltage");
  Serial.println( r, 2 );
  Serial.print("Measured voltage at voltage divider:");
  Serial.println(measuredV,2);
  Serial.print("Corrected voltage at voltage divider:");
  Serial.println(correctedV);
}

void loop() {
  
  /*dateFunction();
  timeFunction();*/
  
  /****************************Initialization for GSM Shield**********/
  /*Serial.println("Starting SM5100B Communication...");
  for(int i =1;i<=4800;i++) //cannot be used with the SMS receive function
  {
  GSMTest();
  delay(25);
  }*/
  /****************************End of Initalization*******************/
  
  
  //Transitional State//
  Serial.println("GSM Shield Initalization Completed!");
  Serial.println("Now proceed to the measuring stage...");
  
  
  
 /******************************Sending SMS***************************/ 
   startSMS();
  cell.print("Arduino Initalization completed!");
  endSMS();
 /******************************End of SMS****************************/
  
  delay(100);
  
  for(;;)  
  {  
    while(cell.available() >0) 
   { 
    inchar=cell.read();       
    a[0]=inchar;
    delay(10);
    inchar=cell.read();  
    a[1]=inchar; 
    delay(10);
    inchar=cell.read();     
    a[2]=inchar;   
   }
   LM3500();
   
    if(a[0] == '#')    
    digitalWrite(13,HIGH);
  }
  
  
  
}
while (cell.available() >0)
{
char a[10];
inchar = cell.read();
a[0] = inchar;
if ( a[0]== '#')
digitalWrite(11,HIGH);
}

This is your whole while loop. In the loop, you create an array of 10 elements, and only write to the first one. The array goes out of scope as soon as there is no more serial data to read. Therefore, it has served no useful purpose.

Why is it there?

in the code, i would like to make it works like a interrupt, when there is a SMS being sent to the shield, the Arduino can respond as instructed. But from what i have observed, the led fails to turn on.

Your full code is full of commented out code, code that is never called, etc. Delete all the dead code and try again.

    while(cell.available() >0) 
   { 
    inchar=cell.read();       
    a[0]=inchar;
    delay(10);
    inchar=cell.read();  
    a[1]=inchar; 
    delay(10);
    inchar=cell.read();     
    a[2]=inchar;   
   }

If there is at least one byte to read, hope like hell that two more arrive before you try to read them. Fail!

Re: PaulS,

Thank you very much for your help!! Now i am having a clearer picture, and after some modifications, it seems to work!! I really appreciated your help. :smiley:

Regards,