Sim900 GPRS and keyPad

Hi there!

I have tried this simple thing for some time now and can't get it to work. I will use my matrix keyPad to dial the phone number I want to call. I started to feel that I have tried everything. I use some Norwegian words and shortcuts so just try to understand me :slight_smile: , but here is my code:

////Libs//////////////////////////////////
#include <Key.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
//////////////////////////////////////////

///SoftwareSerial/////////////////////////
SoftwareSerial sim900(2, 3);
//////////////////////////////////////////

////char//////////////////////////////////
char telNR[] = {"00000000"};
char key;
//////////////////////////////////////////

////INTes/////////////////////////////////
int antT = 0;
//////////////////////////////////////////

////Tast opsett - keyPad//////////////////
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
/////////////////////////////////////////

////Pin oppsett///////////////////////////
byte rowPins[ROWS] = {3, 4, 5, 8};
byte colPins[COLS] = {9, 10, 11, 12};
//////////////////////////////////////////

////Oppsett keyPad////////////////////////
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//////////////////////////////////////////

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sim900.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  key = keypad.getKey();

  if (key) {
    if (key >= '0' && key <= '9' && antT < 8) {
      telNR[antT] = key;
      antT++;
    }
  }
  if (antT >= 8) {
      sim900.print("ATD");
      sim900.print(telNR);
      sim900.println(";");

      Serial.println("Ringer....");
      Serial.print(telNR);
      reset();
    }
}

void reset() {
  telNR[0] = 0;
  telNR[1] = 0;
  telNR[2] = 0;
  telNR[3] = 0;
  telNR[4] = 0;
  telNR[5] = 0;
  telNR[6] = 0;
  telNR[7] = 0;

  antT = 0;
}

It serial print "Ringer.....(telNR)" "Ringer" is Norwegian for calling btw :slight_smile: , but does not serial print the AT command to the sim900.

But when I but

sim900.print("ATD");
      sim900.print(telNR);
      sim900.println(";");

in void setup() and change "char telNR" to my phone number it works.

So why does it work under void setup() and not under "if (antT >= 8 )", and under there it does serial print to the serial monitor but not to the sim900, and in the serial monitor it prints the right number to call, so that's should mean that the sim900 is getting the right number.

Please help me, I'm thoughtless.

Thanks! :slight_smile:

That’s because your telNR is not a well formed c-string, you are missing the ‘\0’ at the end and probably some garbage in memory is also sent until it find a NULL byte (and possibly those bytes are invisible in ascii so you don’t see them in the monitor)

Declare

const byte nbDigitInPhoneNb = 8;
char telNR[nbDigitInPhoneNb+1]; // +1 for the trailing null char ‘\0’

And then

if (antT >= nbDigitInPhoneNb) {
      telNR[nbDigitInPhoneNb] = ‘\0’; // terminate the c-String correctly
      sim900.print("ATD");
      sim900.print(telNR);
      sim900.println(";");
...

Your setup() could prepare the buffer (overkill for this case because it’s a global variable so already set to 0)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sim900.begin(9600);
  reset(); // prépare the buffer
}

Note that you could rewrite your void reset() this way:

void reset() {
   antT = 0;
   memset(telNR, ’\0’, nbDigitInPhoneNb+1); // http://www.cplusplus.com/reference/cstring/memset/
}

(the memset() call is not even needed as antT is really what you use to trigger the call and you don’t need to empty the buffer as it will get overwritten when you type new stuff in, so the reset function could just be antT = 0;)

Thanks a lot for the help, I'm going to try it out now :slight_smile:

I could still not get it to work, here is my updated codes:

////Libs//////////////////////////////////
#include <Key.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
//////////////////////////////////////////

///SoftwareSerial/////////////////////////
SoftwareSerial sim900(2, 3);
//////////////////////////////////////////

////Bytes/////////////////////////////////
const byte nbDigitInPhoneNb  = 8;
/////////////////////////////////////////

////char//////////////////////////////////
char telNR[nbDigitInPhoneNb+1];
char key;
//////////////////////////////////////////

////INTes/////////////////////////////////
int antT = 0;
//////////////////////////////////////////

////Tast opsett - keyPad//////////////////
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
/////////////////////////////////////////

////Pin oppsett///////////////////////////
byte rowPins[ROWS] = {3, 4, 5, 8};
byte colPins[COLS] = {9, 10, 11, 12};
//////////////////////////////////////////

////Oppsett keyPad////////////////////////
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//////////////////////////////////////////

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  sim900.begin(9600);
  reset();
}

void loop() {
  // put your main code here, to run repeatedly:
  key = keypad.getKey();

  if (key) {
    if (key >= '0' && key <= '9' && antT < 8) {
      telNR[antT] = key;
      antT++;
    }
  }
  if (antT >= nbDigitInPhoneNb) {
    telNR[nbDigitInPhoneNb] = '\0';
    sim900.print("ATD");
    sim900.print(telNR);
    sim900.println(";");

    Serial.println("Ringer....");
    Serial.print(telNR);
    reset();
  }
}

void reset() {
  antT = 0;
  //memset(telNR, '\0', nbDigitInPhoneNb+1);
}

Is this right?

Thanks again :slight_smile:

You have a hardware conflict

 SoftwareSerial sim900(2, [color=red]3[/color]);
...
byte rowPins[ROWS] = {[color=red]3[/color], 4, 5, 8};

how are things wired?

OMG!!! You are not blind :stuck_out_tongue_closed_eyes: , that was the solution thank you very much. I just switch the pin 3 to the keyPad to pin 0 :slight_smile: , and will that mean that my original code could work to? But I can understand that my new code is the best solution. :slight_smile:

Again thanks!!! :slight_smile:

HansDia:
OMG!!! You are not blind :stuck_out_tongue_closed_eyes: , that was the solution thank you very much. I just switch the pin 3 to the keyPad to pin 0 :slight_smile: , and will that mean that my original code could work to? But I can understand that my new code is the best solution. :slight_smile:

Again thanks!!! :slight_smile:

Not pin 0.... this is used by Serial... pick something else...

No your original code had a wrongly terminated c-string , so behavior would have been arbitrary

I know that pin 0 i used to Serial, but I'm not receiving anything through serial, so that's why I used pin 0. But I agree with you that pin 0 is a bad pin to use :slight_smile:

Can you explain me little short the big reason with '\0' I could not quite understand it in your first topic, or send me a link where I can read more about it, I tried a quick Google search by my self but could not find anything that explain it good enough. :slight_smile:

Thanks!! :slight_smile: