Go Down

Topic: SerialRelay for SIM908 - Needs Leonardo. (Read 2437 times) previous topic - next topic

dannable

Feb 10, 2015, 10:07 pm Last Edit: Feb 10, 2015, 10:18 pm by dannable
Below is a version of SerialRelay for use with the SIM908. It works the same as the regular version, you send commands to the device through Serial Monitor, and any reply is echoed back on screen, thus allowing you to see how the device is responding and how long each command takes.
 
The down side is that it is intended to run on a Leonardo as the SIM908 shields I have come across use D0 & D1 to communicate with the device so you cannot use those pins for serial communication.
 
I have added three commands - a lower case 'p' with toggle the shield power on and off,  a lower case 'x' will toggle between gps and gsm modes and a carat '^' will be translated as ctrl+z. Obviously if these symbols conflict with data you wish to send they can be changed. As the default setting for the SIM908 is auto-bauding then AT commands should be sent in upper case.
 
The sketch as listed is for use with the DfRobot shield. If you wish to use it with a different shield you may need to modify the pin numbers to suit your device.

Code: [Select]
//Serial Relay using Leo
// dannable 11/02/2015

unsigned char buffer[64];  // buffer array for data receive over serial port
int count=0;               // counter for buffer array
char inChar;               // holding incoming char from Serial Monitor
byte gsmDriverPin[3] = {3,4,5};// The default digital driver pins for the GSM and GPS mode for DfRobot.  Amend if necessary.

boolean GSM_mode = false;

void setup()
{
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }
  Serial1.begin(9600);
  while (!Serial1)
  {
    ;
  }

  for (int i=3;i<6;i++)
  {
    pinMode(i,OUTPUT);
  } 
  digitalWrite(5,HIGH);
  delay(1500);
  digitalWrite(5,LOW);
  toggle_mode();
}

void loop()
{
  if (Serial1.available())
  {
    while(Serial1.available())
    {
      buffer[count++]=Serial1.read();
      if(count == 64)break;
    }
    Serial.write(buffer,count);
    clearBufferArray();
    count = 0;
  }
  if (Serial.available())
  {
    inChar=Serial.read();
    if (inChar=='p')
    {
      sim908_power();
    }
    else
      if (inChar=='x')
    {
      toggle_mode();
    }
    else
      if (inChar=='^')
    {
      Serial1.write((char)26);
    }
    else
    {
      Serial1.write(inChar);
    }
  }
}

void clearBufferArray()
{
  for (int i=0; i<count;i++)
  {
    buffer[i]=NULL;
  }
}

void sim908_power()
{
  digitalWrite(5,HIGH);
  delay(1500);
  digitalWrite(5,LOW);
}

void toggle_mode()
{
  if (GSM_mode)
  {
    digitalWrite(4,LOW);  //Enable GPS mode
    digitalWrite(3,HIGH); //Disable GSM mode
    GSM_mode=false;
    Serial.println("GPS mode selected.");
  }
  else
  {
    digitalWrite(3,LOW);  //Enable GSM mode
    digitalWrite(4,HIGH); //Disable GPS mode
    GSM_mode=true;
    Serial.println("GSM mode selected.");
  }
}
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Go Up