2

I connected SIM300cz module's GSM board with Arduino Mega. The GSM board has the pins VCC, GND, RXD, TXD, GND, RI, DTR, STATUS, PWKEY and GND. I connected the GNDs with Arduino's GND, VCC to Arduinos 5V (took power from Arduino), RXD to pin 5, TXD to pin 6 (because of using software serial) and PWKEY to pin 53. My code is as follows:

#include<SoftwareSerial.h>
SoftwareSerial GPRS(5, 6);

int inputPinTempSensor = 0;
float tempCelsius = 0 ;
float thresholdTemp = 45;

void setup()
{
  GPRS.begin(9600);               // the GPRS baud rate
  Serial.begin(9600);  // the Serial port of Arduino baud rate.
  pinMode(53, OUTPUT);
  digitalWrite(53, HIGH);
  delay(100);
}

void loop() {
  GPRS.print("AT+CREG/r");
  Serial.print(GPRS.read());
  delay(2000);
}

The AT command response should be something like "ok" but it's returning -1 for every loop waiting 2 seconds. Why and what could be the solution?

Edit:

Sorry for the mistakes. I was trying different ways with carriage return and new line characters to make it work. Now the code is:

#include<SoftwareSerial.h>
SoftwareSerial GPRS(5, 6);

void setup()
{
  GPRS.begin(9600);               // the GPRS baud rate
  Serial.begin(9600);  // the Serial port of Arduino baud rate.
  pinMode(53, OUTPUT);
  digitalWrite(53, HIGH);
  delay(2000);
  GPRS.println("AT+CREG");
}

void loop() {
  while(GPRS.available()){
    Serial.print(GPRS.read());
  }
}

Now it gives no responses at all. Actually after buying the module I was trying with these examples and they were not working either. So, I thought to test if the AT commands were working properly. I could be a problem with my wiring too.

3
  • Did you wire DTR to GND? – jfpoilpret Dec 9 '14 at 20:59
  • Yes, I tried with both keeping it opened and by adding to GND. – Nafis Dec 10 '14 at 10:40
  • BTW, pin 53 is to pull up PWKEY. It's connected to PWKEY. – Nafis Dec 10 '14 at 11:19
1

I think there are 2 mistakes in your code:

First, you should ensure that the 3AT3 commands are termintaed by a new line character:

GPRS.print("AT+CREG/r");

should be changed into:

GPRS.println("AT+CREG/r");

or:

GPRS.print("AT+CREG/r\n");

Nite that I am not sure why you added /r at the end of the AT command, is that supposed to be part of the command? If your intention was to force a new line, then you meant \r not /r, which is not really a new line character but a carriage return (that might be interpreted the same by yur GSM module, but not necessarily).

The the second mistake in your code, is directly calling GPRS.read() without first waiting for a character to be available; in this situation, GPRS.read() will always return -1 as a specific value meaning "no available character to read".

So you should change your code to something like:

// 1st wait for a character to be available
while (!GPRS.available()) {
}
// Then read all available characters and log them to Serial
while (GPRS.available()) {
    Serial.print(GPRS.read());
}
2
  • Thanks a lot for your answer. I edited my question. I still have problems. – Nafis Dec 9 '14 at 8:18
  • Note that GPRS.read() will return -1 if the input buffer is empty. – Wirewrap Oct 6 '16 at 7:50
1

The correct AT command is AT+CREG?\r. A delay of at least 500ms is also advised after every command, though a timeout loop would be better. Since you didn't send any other commands, the modem will have no further responses for you and the Arduino will return -1 to any Serial.read()s, indicating there's nothing to be read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.