NewSoftSerial + phone: sends OK but not receives

Hello!
I want to connect my phone to Arduino with serial interface.
I’m using NewSoftwareSerial.
My phone communicates with computer without problems (with empty setup and loop functions) - when it’s on hardware pins 0 and 1.
Now I want to use NewSoftSerial for communication.
This is the testing code:

#include <NewSoftSerial.h>
NewSoftSerial phoneSerial =  NewSoftSerial(6, 7);


void setup() 
{ 
  Serial.begin(9600);
  phoneSerial.begin(9600);
   
  // prints title with ending line break 
  Serial.println("Phone test"); 
    Serial.print("\n\rread: ");

}

void loop(){
  
    phoneSerial.println("AT");
  delay(1500);

 
 char buffer[40]; 
 
int i = 0;
for(i; i<40; i++){
int c;
      c = phoneSerial.read();
Serial.print(c);
            }
}

The problem is I’m not receiving anything useful.
After the “AT” command I’m getting bunch of ‘0’ and then ‘-1’.

Phone is connected right, because if I send “ATDmynumber” - phone calls.

What can be the problem?

Leonti

Leonti –

If you ever see phoneSerial.read() return -1, that means you called it before any characters had arrived. Only call read() when available() tells you a character is ready by returning a number greater than 0.

int i = 0;
for (int i=0; i<40; i++)
{
  if (phoneSerial.available() > 0)
  {
    int c;
    c = phoneSerial.read();
    Serial.print(c);
  }
}

Sounds like a cool app. What phone is it?

Mikal

Now I changed my code to look like this:

void loop(){
  
  phoneSerial.println("AT");
  delay(3500);
while (true)
{
  if (phoneSerial.available())
    Serial.print(phoneSerial.read());
}
}

It eliminated "-1" but still all I get are bunch of "0". If I change "AT" to something else - phone response changes too - number of zeroes seems to depend on a command. When it's supposed to be "OK" I get only a couple of zeroes, if it's "ERROR" I get more of them.

I'm planning to make sms-controlled device. It's working already on computer - I send a message - I receive one in response :) But it's not working (yet :) ) on arduino. I used NewSoftSerial before for my GPS chip and it worked perfectly so I'm suspecting I'm just doing something wrong.

As for phone it's half-dead Sony - J5 :)

Leonti

Oh yes, of course. It should (probably) be:

Serial.print(phoneSerial.read(), BYTE);

Mikal

This way instead of zeroes I get just "squared" symbols :(

Leonti

The most strange thing just happened here. I have RX pin assigned to pin 6. Just now I tried connecting pin 0 (hardware rx) to pin 6 (software rx) in hope that I would get data from the phone directly to the arduino IDE. Instead - it helped getting data from software RX! I get my "OK" from phone on pin 6 when it's connected to pin 0!

This is just a wild guess but maybe I'm not getting enough "juice" on pin 6 to correctly drive interrupts, and connecting it to 0 increased power on pin 6?

If it matters I'm using NewSoftwareSerial from 18-02-09 can't compile version 9 because of the errors:

/tmp/ccba0zGi.s: Assembler messages:

/tmp/ccba0zGi.s:229: Error: register r24, r26, r28 or r30 required

/tmp/ccba0zGi.s:282: Error: register r24, r26, r28 or r30 required

Leonti

What Arduino board are you using? What version of the IDE?

Unfortunately, your other problem seems wiring-related, making it difficult to diagnose electronically. :) I think the code is good now.

Mikal

I solved my problem by using pull-up resistor! Now I'm getting my desired "OK" :)

As for compiling problem - I found this topic: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1233298805/111 So I installed cross-avr-gcc43 and now it compiles and works perfectly.

Mikal, thanks for great library and your help!

Leonti

Excellent! I'm delighted to hear!

Mikal

Hi Leonti,

Can you pls show how you connect the pull-up resistor? I've the same problem!

Thanks.