GSM/GPRS SHIELD (Sim900)

Hi everyone! I have an UNO and a simcom sim900 gsm/gprs shield. I am sending sms’s with the shield: In my case I want to send out sms with a sensor value but have some issues…
I have two questions.

Question 1:

#include "SIM900.h"
#include <SoftwareSerial.h>
//#include "inetGSM.h"


#include "sms.h"
SMSGSM sms;


int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

void setup() 
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");
  
  if(started){
  if (sms.SendSMS("+4xxxxxxxxx", "Arduino is on!"))
    Serial.println("\nSMS sent OK");
  }

};

void loop() 
{
  if(started){
    //Read if there are messages on SIM card and print them.
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
    }
    delay(1000);
  }
};

I would really like the sensor value (with some additions from the DHT11 program) to be sent along with the first message within void start. The problem is that (t) for temperature (reading from the DHT11) doesnt go inside the sms text:
if (sms.SendSMS("+4xxxxxxxxx", “Arduino is on! TEMPERATURE SENSOR READING HERE!”))
Serial.println("\nSMS sent OK");

Have tried:
if (sms.SendSMS("+4xxxxxxxxx", “Arduino is on! (t)”))
Serial.println("\nSMS sent OK");

Or:
if (sms.SendSMS("+4xxxxxxxxx", “Arduino is on! " (t)))
Serial.println(”\nSMS sent OK");

Doesnt work… aybody have a clue??

(Question number 2:
Does anybody know how to request a text message with sensor value from the gsm shield? I send out a sms for example; TEMP and receive the sensor value?

any help would be appreciated!!

Doesnt work… aybody have a clue??

Yes. Your code is wrong. Look at sprintf().

Hi there! Thanks PaulS! I have put together a new code but I am still stuck with the temperature readings from the DHT22.

Here is some of the code:

void loop() 
{
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 int temp=(t); 
  
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    sprintf(string,"The temperature is:", temp);
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  if(started){

    if (sms.SendSMS("+4xxxxxxxx",string))
    Serial.println("\nSMS sent OK");
  }
} 
  delay(500000);

All I want is to get an sms with the temp reading... What am I doing wrong?

I solved it! :)

void loop() 
{
 float h = dht.readHumidity();
 float t = dht.readTemperature();

  int temp= (t); 
  int temp1=(t*100)-(temp*100);
  
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    sprintf(string,"Temperature is: %d.%d", temp, temp1 );
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
     if(started){

     if (sms.SendSMS("+4xxxxxxx",string))
    Serial.println("\nSMS sent OK");
   }
} 
  delay(1000000);

Here is the code that is working for me after several days of working… any improvements on the code? Please comment! :slight_smile:

#include "SIM900.h"
#include "sms.h"
#include "SoftwareSerial.h"
#include "sms.h"
#include "DHT.h"

#define DHTPIN A1     // DHT conected to this pin 

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)

SMSGSM sms;
DHT dht(DHTPIN, DHTTYPE);
 
boolean started=false;

char smsbuffer[160];
char n[20];
char string[160];
char string1[160];
char string2[160];

int fader=1;
int inc=10;
int numdata;
int val = 0;  
int inputPin = A0;               // PIR sensor
int pirState = LOW;             // we start, assuming no motion detected
int pir = 10;  

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  
  pinMode( 10, OUTPUT ); 
  digitalWrite( 10, HIGH ); 
  
  dht.begin();
  pinMode(inputPin, INPUT);     

  Serial.println("GSM Shield testing.");
  
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true; 
  }
  else Serial.println("\nstatus=IDLE");
  if(started){
    delsms();
  }
 
};
 
void loop()
{
float h = dht.readHumidity();
 float t = dht.readTemperature();
  int pos=0;
  
  int temp= (t); 
  int temp1=(t*100)-(temp*100);
  int hum= (h); 
  int hum1=(h*100)-(hum*100);
  
  sprintf(string,"Temperature: %d.%d", temp, temp1 );
  sprintf(string1,"Humidity: %d.%d", hum, hum1);
  sprintf(string2,"Motion detected! temperature is: %d.%d", temp, temp1 );

  if(started){
    pos=sms.IsSMSPresent(SMS_ALL);
    if(pos){
      Serial.println("Receiving SMS");
      Serial.println(pos);
      sms.GetSMS(pos,n,smsbuffer,100);
        Serial.println(n);
        Serial.println(smsbuffer);
          
          if(!strcmp(smsbuffer,"temp")){
         sms.SendSMS(n,string);
          Serial.println("\nSMS sent OK");
      }
    
         if(!strcmp(smsbuffer,"hum")){
         sms.SendSMS(n,string1);
          Serial.println("\nSMS sent OK");
      }
          if(!strcmp(smsbuffer,"PIRON")){
          digitalWrite(pir, HIGH);
        }   
         if(!strcmp(smsbuffer,"PIROFF")){
          digitalWrite(pir, LOW);
        }   
  
   delsms(); 
    
    }
     
  }
  
  
  
  val = digitalRead(inputPin); 
  
  
  if (val == HIGH) {            
  
    delay(150);

    
    if (pirState == LOW) {
     
      Serial.println("Motion detected!");
    
      pirState = HIGH;
   if(started){
  
    if (sms.SendSMS("+4xxxxxxxx", string2)) // insert cell number
    Serial.println("\nSMS sent OK");
  }
  }
  } else {
    
      delay(300);    
      
      if (pirState == HIGH){
      Serial.println("Motion ended!");
     
      pirState = LOW;
    }
  }
};
 
 
void delsms(){
  Serial.println("Checking SIM card for new messages...");
  for (int i=0; i<10; i++){ 
      int pos=sms.IsSMSPresent(SMS_ALL);
      if (pos!=0){
        Serial.print("\nFound SMS at the position ");
        Serial.println(pos);
        if (sms.DeleteSMS(pos)==1){   
          Serial.print("\nDeleted SMS at the position ");
          Serial.println(pos);     
        }
        else
        {
          Serial.print("\nCant delete SMS at the position ");
          Serial.println(pos);        
        }
      }
    }
 
}
  sprintf(string,"Temperature: %d.%d", temp, temp1 );
  sprintf(string1,"Humidity: %d.%d", hum, hum1);
  sprintf(string2,"Motion detected! temperature is: %d.%d", temp, temp1 );

Do string, string1, and string2 need to be 160 characters?
Do they need to have such meaningless names?
Do you really need to populate all three of them when you will only be sending one of them? You could free up (more than) 320 bytes of SRAM by only creating the string when you need to.

void loop()
{
float h = dht.readHumidity();
 float t = dht.readTemperature();
  int pos=0;

Why
does
your
code
wander
all
over
the
place? Tools + Auto Format would stop that drunken waltz.

Thanks for the feedback! Sorry about the messy code! :drooling_face: I am having a problem with the strings cuz I want to combine the temperature and the humidity in on sms but I can't.. I am stuck at this point! ANy recommendations?

I am having a problem with the strings cuz I want to combine the temperature and the humidity in on sms but I can't.

Sure you can. You have our permission.

ANy recommendations?

Gee, let me think about that.

OK. Here's one. Explain WHY you can't. What happens when you try?

That is the string I am using..

sprintf(string,"Temperature: %d.%d", temp, temp1 );
sprintf(string1,"Humidity: %d.%d", hum, hum1);
sprintf(string2,"Motion detected! temperature is: %d.%d", temp, temp1);

but want to combine temperature and humidity in one string:

sprintf(string,"Temperature: %d.%d", temp, temp1, "Humidity: %d.%d", hum, hum1 );
sprintf(string2,"Motion detected! temperature is: %d.%d", temp, temp1);

It only reads the temperature..

but want to combine temperature and humidity in one string:
sprintf(string,"Temperature: %d.%d", temp, temp1, "Humidity: %d.%d", hum, hum1 );

The first argument to sprintf is where to store the results. The second defines how to format the results, the 3rd and subsequent arguments get substituted in place of the format specifiers in the 2nd argument. So, temp gets substituted in place of the the 1st %d, and temp1 gets substituted in place of the second %d. There are no more format specifiers in the 2nd argument, so the remaining input to sprintf is ignored.

Now, your turn. Explain what this does:

sprintf(string,"Temperature: %d.%d and humidity: %d.%d", temp, temp1, hum, hum1);

There are now 4 format specifiers in the 2nd argument, so how many more values need to follow that argument?

@PaulS: You are a genius!! It worked! THANK YOU!!! :sweat_smile:

sprintf(string,"Temperature: %d.%d and humidity: %d.%d", temp, temp1, hum, hum1);

Good day everyone! In the same script (above) I am trying to implement a simple gps code with my skylab SKM 53 gps device. I am not getting any readings… I suspect it is the softserial issue… cuz when I remove gsm.begin(2400); it works. What can I do here…?

#include "SIM900.h"
#include "sms.h"
#include "SoftwareSerial.h"
#include "DHT.h"
#include "TinyGPS.h"

TinyGPS gps;
SoftwareSerial ss(A4, A5);

#define DHTPIN A1     // DHT conected to this pin 
#define DHTTYPE DHT22   // DHT 22  (AM2302)

SMSGSM sms;
DHT dht(DHTPIN, DHTTYPE);

boolean started=false;
long previousMillis = 0;  
long interval = 120000;           // interval at which to blink (milliseconds)


char smsbuffer[160];
char n[20];
char string[160];
char string2[160];

int fader=1;
int inc=10;
int numdata;
int val = 0;  
int inputPin = A0;               // PIR sensor
int pirState = LOW;             // we start, assuming no motion detected
int pir = 10;        

void setup()
{
  //Serial connection.
  Serial.begin(9600);
  ss.begin(9600);

  pinMode( 10, OUTPUT ); 
  digitalWrite( 10, HIGH ); 

  dht.begin();
  pinMode(inputPin, INPUT);     

  Serial.println("GSM Shield testing.");

  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true; 
  }
  else Serial.println("\nstatus=IDLE");
  if(started){
    delsms();
  }

}

void loop()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int pos=0;

  int temp= (t); 
  int temp1=(t*10)-(temp*10);
  int hum= (h); 
  int hum1=(h*10)-(hum*10);

  sprintf(string,"Temp: %d.%dc | Hum: %d.%d", temp, temp1, hum, hum1 );
  sprintf(string2,"Motion detected! Temp: %d.%d | Hum: %d.%d", temp, temp1, hum, hum1 );


  while (ss.available())
  {
    char c = ss.read();
    if (gps.encode(c)) // Did a new valid sentence come in?
    {
      float flat, flon;
      unsigned long age;
      gps.f_get_position(&flat, &flon, &age);
      Serial.print("LAT,LON =");
      Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
      Serial.print(",");
      Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
      Serial.print(" SAT=");
      Serial.println(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
      delay(1000);
    }
  }


  if(started){
    pos=sms.IsSMSPresent(SMS_ALL);
    if(pos){
      Serial.println("Receiving SMS");
      Serial.println(pos);
      sms.GetSMS(pos,n,smsbuffer,100);
      Serial.println(n);
      Serial.println(smsbuffer);

      if(!strcmp(smsbuffer,"temp")){
        sms.SendSMS(n,string);
        Serial.println("\nSMS sent OK");
      }

      if(!strcmp(smsbuffer,"PIRON")){
        digitalWrite(pir, HIGH);
        Serial.println("PIR on");
      }   
      if(!strcmp(smsbuffer,"PIROFF")){
        digitalWrite(pir, LOW);
        Serial.println("PIR off");
      }   

      delsms(); 

    }

  }



  val = digitalRead(inputPin); 
  unsigned long currentMillis = millis();

  if (val == HIGH) {            

    delay(150);

    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;   
      if (pirState == LOW) {

        Serial.println("Motion detected!");

        pirState = HIGH;
        if(started){

          if (sms.SendSMS("+4748292096", string2))
            Serial.println("\nSMS sent OK");
        } 
      }
    }
  } 
  else {

    delay(300);    

    if (pirState == HIGH){
      Serial.println("Motion ended!");

      pirState = LOW;
    }
  }
}


void delsms(){
  Serial.println("Checking SIM card for messages...");
  for (int i=0; i<10; i++){ 
    int pos=sms.IsSMSPresent(SMS_ALL);
    if (pos!=0){
      Serial.print("\nFound SMS at the position ");
      Serial.println(pos);
      if (sms.DeleteSMS(pos)==1){   
        Serial.print("\nDeleted SMS at the position ");
        Serial.println(pos);     
      }
      else
      {
        Serial.print("\nCant delete SMS at the position ");
        Serial.println(pos);        
      }
    }
  }

}

When I run the gps cpde all by itself it is working just fine…!

#include "SoftwareSerial.h"
#include "TinyGPS.h"

TinyGPS gps;
SoftwareSerial ss(A4, A5);

void setup()
{
  Serial.begin(9600);
  ss.begin(9600);
}

void loop()
{
  while (ss.available())
  {
    char c = ss.read();
    if (gps.encode(c)) // Did a new valid sentence come in?
    {
      float flat, flon;
      unsigned long age;
      gps.f_get_position(&flat, &flon, &age);
      Serial.print("LAT,LON =");
      Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
      Serial.print(",");
      Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
      Serial.print(" SAT=");
      Serial.println(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
      delay(1000);
    }
  }
}

What can I do here…?

Quit acting like you are writing code on a PC with 4 terabytes of memory. The Arduino has 1K, 2K, 8K or 96K, depending on which (unspecified) model you have.

char smsbuffer[160];
char n[20];
char string[160];
char string2[160];

Surely some of these could be smaller/reused.

  sprintf(string,"Temp: %d.%dc | Hum: %d.%d", temp, temp1, hum, hum1 );
  sprintf(string2,"Motion detected! Temp: %d.%d | Hum: %d.%d", temp, temp1, hum, hum1 );

Just for the hell of it, on every pass through loop, just in case it might (it probably won’t!) you populate both strings that differ only in whether or not the message "Motion detected! " is present, or not.

Stop that.

I’m trying, and failing, to guess what the purpose of a device with a motion detector, GPS, temperature and humidity sensor is. I’m struggling because if someone broke into my home, and I got a text message, I really wouldn’t give a shit what the temperature and humidity levels were. And, I know where my home is. So, what IS the purpose of this device you are making?

I'm trying, and failing, to guess what the purpose of a device with a motion detector, GPS, temperature and humidity sensor is. I'm struggling because if someone broke into my home, and I got a text message, I really wouldn't give a shit what the temperature and humidity levels were. And, I know where my home is. So, what IS the purpose of this device you are making?

I want to use it on my boat... with solarpanel. Want to check the conditions...

Okay, I removed string2.. It is unnecessary. I am still in a development mode so any help is much appreciated!

@PaulS: Sorry to make your eyes roll on this!! :P

I use this to check SRAM: http://jeelabs.org/2011/05/22/atmega-memory-use/

Back to my question... how come I dont get any readings from the GPS? is it because of the softwareserial?

Have you checked the amount of free memory you have?

Hello

I have exactly the same issue, once i do gsm.begin(2400), i have losen the data received from softwareserial.sh. In my case i am using a apc220 transceiver to communicate with another arduinos. I have mapped rx and tx pins. The comumunication between arduinos are working fine, but as i said, once the gsm.begin is done the communication is down.

Reading your post i have checked ram and the mentioned function show 1076 in com monitor, so i think that is not ram related issue.
If someone can help me i appreciate that. I want that some arduinos send information from several shields/sensor by sms separated for several meters. To got that i will set up an arduino with gsm shield from elecfreaks.

Thanks in advance!!

MY CODE


#include “SIM900.h”
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include “inetGSM.h”

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include “sms.h”
#include “call.h”
SMSGSM sms;
CallGSM call;
//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
SoftwareSerial Apc220Serial(11, 12);
int intSerialVal = 0;
void setup()
{
//Serial connection.
Serial.begin(9600);
Apc220Serial.begin(9600);
Serial.println(“GSM Shield testing.”);
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)){
Serial.println("\nstatus=READY");
started=true;
}
else Serial.println("\nstatus=IDLE");

// delay(10000);

};
void loop()
{

//delay(1000);
intSerialVal = Apc220Serial.read();
if (intSerialVal != -1)
Serial.println (intSerialVal);

switch (intSerialVal) {
case ‘0’:
if(started){
//Enable this two lines if you want to send an SMS.
if (sms.SendSMS("+3333333333", “Arduino SMS”))
Serial.println("\nSMS sent OK");
}

break;
case ‘1’:
if(started){
call.Call("+333333333333");
Serial.println("\nCall done OK");}
break;
}

};

Links to the devices that you are using are going to be essential. If the communication between the Arduino and any attached device uses SPI, then you can't use the SPI pins (10, 11, 12, and 13 on the 328-based Arduinos) for ANY other purpose.