SIM900 + Servo. String poblem

Hello! Good day! I am making a device that lets the user control any device that has a knob on it. So far my codes are working when the Servo.h library has not yet been implemented but when I did, my string could not get the right input.

To be more specific. I am using a gizduino v4 (arduino uno clone), gsm shield, arduino 1.0.5, and the sms library from
https://code.google.com/p/gsm-shield-arduino/downloads/detail?name=SIM908IDE100.zip&can=4&q=.

here is my code:

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

#include "sms.h"
SMSGSM sms;
//Servo myservo;
/****************************************************************************************************/
boolean GSMactive = false;
char smsbuffer[160];
char n[20];
char remoteNumber[20] = "09XXXXXXXXX";

String intString = "";
String charString = "";

int minute = 0;

unsigned long int timeMillis = 0;
unsigned long int activeRead = 0;
unsigned long int startRead = 0;

unsigned long int timeLeft = 0;
unsigned long int runTime = 0;

boolean timerActive = false;
boolean finalCount = false;
/****************************************************************************************************/
void setup(){
  Serial.begin(9600);
  //myservo.attach(9);
  //myservo.write(0);
  //myservo.detach();
  Serial.println("DEVICE LOADING...");
  if(gsm.begin(9600)){
    Serial.println("STATUS = DEVICE READY");
    GSMactive = true;
  }
  else Serial.println("STATUS = DEVICE IDLE");
  if(GSMactive){
    if (sms.SendSMS("09XXXXXXXXX", "on 02"))
      Serial.println("\nSMS sent OK");
  }
}
/****************************************************************************************************/
void loop(){
  intString = String("");
  charString = String("");
  minute = 0;
  activeRead = millis();
  
  if(timerActive){
    runTime = activeRead - startRead;
    timeLeft = timeMillis - runTime;
    Serial.println("TOTAL RUN TIME:");
    Serial.println((float)runTime/60000);
    Serial.println("TIME REMAINING:");
    Serial.println((float)timeLeft/60000);
    delay(900);
  }
  
  if(timeLeft < 60000 && timerActive){
    //if(GSMactive)
    Serial.println("LESS THAN A MINUTE LEFT");
    finalCount = true;
    GSMactive = false;
  }
  
  if(finalCount && timeLeft < 1000){
    Serial.println("TIME UP!");
    timerActive = false;
    finalCount = false;
    GSMactive = true;
    //switchOff()
    //sms.SendSMS(remoteNumber, "TIMER OUT. DEVICE DEACTIVATED!");
  }
  
  if(GSMactive){
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
      
      for(int index = 0; smsbuffer[index] != '\0'; index++){
        int thisChar = smsbuffer[index];
        if(isDigit(thisChar)){
          intString += (char)thisChar;
        }
        if(isAlpha(thisChar)){
          charString += (char)thisChar;
        }
        if(isPunct(thisChar)){
          charString += (char)thisChar;
        }
      }
      charString.toUpperCase();
      minute = intString.toInt();
      timeMillis = 0;
      for(int x = 0; x < minute; x++){
        for(int y = 0; y < 60; y++){
          timeMillis = timeMillis + 1000;
        }
      }
      Serial.println("COMMAND GRABBED:");
      Serial.println(charString);
      Serial.println("NUMBER OF MINUTE(S):");
      Serial.println(minute);
      Serial.println("IN MILLISECONDS:");
      Serial.println(timeMillis);
    }
    if(charString == "ON"){
      Serial.println("TURNING DEVICE ON...");
      if(minute < 15 && minute > 0){
        startRead = millis();
        timerActive = true;
        //switchOn();
        //sms.SendSMS(remoteNumber, "DEVICE ACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
    if(charString == "OFF"){
      Serial.println("TURNING DEVICE OFF...");
      if(timerActive){
        timerActive = false;
        //switchOff();
        //sms.SendSMS(remoteNumber, "DEVICE DEACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
  }
   
  Serial.println("LOOP ENDED...");
}
/*
switchOn(){
  myservo.attach(9)
  for(int pos = 0; pos < 180; pos++)
  myservo.detach();
  return;
}
switchOff(){
  myservo.attach(9)
  for(int pos = 190; pos >= 1; pos--)
  myservo.detach();
  return;
}
*/

sketch_jul22a.ino (3.65 KB)

this is how it looks like in the serial monitor when i ran the code without removing the comments (see attachedwithoutComment file).
this what happened when I removed the comment lines (see attached withComment file).
here is the code for this:

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

#include "sms.h"
SMSGSM sms;
//Servo myservo;
/****************************************************************************************************/
boolean GSMactive = false;
char smsbuffer[160];
char n[20];
char remoteNumber[20] = "09XXXXXXXXX";

String intString = "";
String charString = "";

int minute = 0;

unsigned long int timeMillis = 0;
unsigned long int activeRead = 0;
unsigned long int startRead = 0;

unsigned long int timeLeft = 0;
unsigned long int runTime = 0;

boolean timerActive = false;
boolean finalCount = false;
/****************************************************************************************************/
void setup(){
  Serial.begin(9600);
  //myservo.attach(9);
  //myservo.write(0);
  //myservo.detach();
  Serial.println("DEVICE LOADING...");
  if(gsm.begin(9600)){
    Serial.println("STATUS = DEVICE READY");
    GSMactive = true;
  }
  else Serial.println("STATUS = DEVICE IDLE");
  if(GSMactive){
    if (sms.SendSMS("09XXXXXXXXX", "on 02"))
      Serial.println("\nSMS sent OK");
  }
}
/****************************************************************************************************/
void loop(){
  intString = String("");
  charString = String("");
  minute = 0;
  activeRead = millis();
  
  if(timerActive){
    runTime = activeRead - startRead;
    timeLeft = timeMillis - runTime;
    Serial.println("TOTAL RUN TIME:");
    Serial.println((float)runTime/60000);
    Serial.println("TIME REMAINING:");
    Serial.println((float)timeLeft/60000);
    delay(900);
  }
  
  if(timeLeft < 60000 && timerActive){
    //if(GSMactive)
    Serial.println("LESS THAN A MINUTE LEFT");
    finalCount = true;
    GSMactive = false;
  }
  
  if(finalCount && timeLeft < 1000){
    Serial.println("TIME UP!");
    timerActive = false;
    finalCount = false;
    GSMactive = true;
    //switchOff()
    sms.SendSMS(remoteNumber, "TIMER OUT. DEVICE DEACTIVATED!");
  }
  
  if(GSMactive){
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
      
      for(int index = 0; smsbuffer[index] != '\0'; index++){
        int thisChar = smsbuffer[index];
        if(isDigit(thisChar)){
          intString += (char)thisChar;
        }
        if(isAlpha(thisChar)){
          charString += (char)thisChar;
        }
        if(isPunct(thisChar)){
          charString += (char)thisChar;
        }
      }
      charString.toUpperCase();
      minute = intString.toInt();
      timeMillis = 0;
      for(int x = 0; x < minute; x++){
        for(int y = 0; y < 60; y++){
          timeMillis = timeMillis + 1000;
        }
      }
      Serial.println("COMMAND GRABBED:");
      Serial.println(charString);
      Serial.println("NUMBER OF MINUTE(S):");
      Serial.println(minute);
      Serial.println("IN MILLISECONDS:");
      Serial.println(timeMillis);
    }
    if(charString == "ON"){
      Serial.println("TURNING DEVICE ON...");
      if(minute < 15 && minute > 0){
        startRead = millis();
        timerActive = true;
        //switchOn();
        sms.SendSMS(remoteNumber, "DEVICE ACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
    if(charString == "OFF"){
      Serial.println("TURNING DEVICE OFF...");
      if(timerActive){
        timerActive = false;
        //switchOff();
        sms.SendSMS(remoteNumber, "DEVICE DEACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
  }
   
  Serial.println("LOOP ENDED...");
}
/*
switchOn(){
  myservo.attach(9)
  for(int pos = 0; pos < 180; pos++)
  myservo.detach();
}
switchOff(){
  myservo.attach(9)
  for(int pos = 190; pos >= 1; pos--)
  myservo.detach();
}
*/

My intString and charString could npt get the right input anymore after removing the comment lines. Even without the Servo.h, once i have implemented the sms.SendSMS(remoteNumber, “DEVICE DEACTIVATED!”), my strings suddenly could not get the expected inputs anymore

Why is that?

The SoftwareSerial library and the Servo library interfere with each other.

I wrote an alternative to software serial here which does work with the servo library but it doesn't have all the bells and whistles.

There is also a library called ServoTimer2 which may work with SoftwareSerial.

...R

Thank you for your suggestion. i'll try using the servotimer2 library after work. Really appreciated your help. :)

Robin2:
The SoftwareSerial library and the Servo library interfere with each other.

I wrote an alternative to software serial here which does work with the servo library but it doesn’t have all the bells and whistles.

There is also a library called ServoTimer2 which may work with SoftwareSerial.

…R

i tried using the ServoTimer2 library. and tried the Servo library from the arduino ide. there is no problem with both libraries interfering with the SoftwareSerial and my codes are running well. My concern is with my string handling. everytime i try to run my codes removing all comment lines, my Strings (intString and charString) can only take the first character of the smsbuffer. DO you have any solution for that?

#include "SIM900.h"
#include <SoftwareSerial.h>
#include<Servo.h>

#include "sms.h"
SMSGSM sms;
Servo myservo;
/****************************************************************************************************/
boolean GSMactive = false;
char smsbuffer[160];
char n[20];
char remoteNumber[20] = "09369607964";

String intString = "";
String charString = "";

int minute = 0;

unsigned long int timeMillis = 0;
unsigned long int activeRead = 0;
unsigned long int startRead = 0;

unsigned long int timeLeft = 0;
unsigned long int runTime = 0;

boolean timerActive = false;
boolean finalCount = false;
/****************************************************************************************************/
void setup(){
  Serial.begin(9600);
  myservo.attach(9);
  //myservo.write(0);
  switchOn();
  switchOff();
  myservo.detach();
  Serial.println("DEVICE LOADING...");
  if(gsm.begin(9600)){
    Serial.println("STATUS = DEVICE READY");
    GSMactive = true;
  }
  else Serial.println("STATUS = DEVICE IDLE");
  if(GSMactive){
    if (sms.SendSMS("09369607964", "on 02"))
      Serial.println("\nSMS sent OK");
  }
}
/****************************************************************************************************/
void loop(){
  intString = String("");
  charString = String("");
  minute = 0;
  activeRead = millis();
  
  if(timerActive){
    runTime = activeRead - startRead;
    timeLeft = timeMillis - runTime;
    Serial.println("TOTAL RUN TIME:");
    Serial.println((float)runTime/60000);
    Serial.println("TIME REMAINING:");
    Serial.println((float)timeLeft/60000);
    delay(900);
  }
  
  if(timeLeft < 60000 && timerActive){
    //if(GSMactive)
    Serial.println("LESS THAN A MINUTE LEFT");
    finalCount = true;
    GSMactive = false;
  }
  
  if(finalCount && timeLeft < 1000){
    Serial.println("TIME UP!");
    timerActive = false;
    finalCount = false;
    GSMactive = true;
    //switchOff()
    //sms.SendSMS(remoteNumber, "TIMER OUT. DEVICE DEACTIVATED!");
  }
  
  if(GSMactive){
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.println(n);
      Serial.println(smsbuffer);
      
      for(int index = 0; smsbuffer[index] != '\0'; index++){
        int thisChar = smsbuffer[index];
        if(isDigit(thisChar)){
          intString += (char)thisChar;
        }
        if(isAlpha(thisChar)){
          charString += (char)thisChar;
        }
        if(isPunct(thisChar)){
          charString += (char)thisChar;
        }
      }
      charString.toUpperCase();
      minute = intString.toInt();
      timeMillis = 0;
      for(int x = 0; x < minute; x++){
        for(int y = 0; y < 60; y++){
          timeMillis = timeMillis + 1000;
        }
      }
      Serial.println("COMMAND GRABBED:");
      Serial.println(charString);
      Serial.println("NUMBER OF MINUTE(S):");
      Serial.println(minute);
      Serial.println("IN MILLISECONDS:");
      Serial.println(timeMillis);
    }
    if(charString == "ON"){
      Serial.println("TURNING DEVICE ON...");
      if(minute <= 15 && minute > 0){
        startRead = millis();
        timerActive = true;
        switchOn();
        sms.SendSMS(remoteNumber, "DEVICE ACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
    if(charString == "OFF"){
      Serial.println("TURNING DEVICE OFF...");
      if(timerActive){
        timerActive = false;
        switchOff();
        sms.SendSMS(remoteNumber, "DEVICE DEACTIVATED!");
      }
      else Serial.println("ERROR!");
    }
  }
   
  Serial.println("LOOP ENDED...");
}
/****************************************************************************************************/

void switchOn(){
  myservo.attach(9);
  for(int pos = 0; pos < 180; pos++){
    myservo.write(pos);
    delay(15);
  }
  myservo.detach();
  return;
}
void switchOff(){
  myservo.attach(9);
  for(int pos = 180; pos >= 1; pos--){
    myservo.write(pos);
    delay(15);
  }
  myservo.detach();
  return;
}

I don't know the GSM library. What is this line supposed to do?

gsm.readSMS(smsbuffer, 160, n, 20)

The eqivalent Serial.read() only reads a single character - which seems to be what your code does.

...R

try using a other servo pin 8) this wil help sometimes

Robin2: I don't know the GSM library. What is this line supposed to do?

gsm.readSMS(smsbuffer, 160, n, 20)

The eqivalent Serial.read() only reads a single character - which seems to be what your code does.

...R

the line gsm.readSMS(smsbuffer, 160, n, 20) grabs the sms from the gsm shield. the smsbuffer is where the sms text is stored while the number of the sender is stored in the n.

GoldSheep: the line gsm.readSMS(smsbuffer, 160, n, 20) grabs the sms from the gsm shield. the smsbuffer is where the sms text is stored while the number of the sender is stored in the n.

What are the 160 and the 20 for?

If this was my problem I would write the shortest sketch possible that just reads SMS messages and displays them - and nothing else. When that was working properly I would try adding other stuff one small piece at a time - making sure that it worked at every stage before moving on to the next. And keeping a copy of each incremental version of the working code before trying any additions.

...R

the 120 and 20 are the number of characters that can be stored within those array of characters which are smsbuffer and n.

thanks for the suggestion. i've already solve the problem. i changed the intString  = String(""); to intString = ""; same goes for the charString.