How to send commands to anoter device?

I'm working on a project that require me to send AT commands from arduino to a development board, before I start my project code, I wrote a small code to make sure AT commands are sent successfully, the follow code is used to shutdown the development board, but It doesn't work, anyone done this before? please help. Thanks

#include <NewSoftSerial.h>
#include <Wire.h>

int tx=2;
int rx=3;

NewSoftSerial transmit(rx,tx); // defines the rx/tx pins (rxpin,txpin)

void setup()
{
Serial.begin(9600);
transmit.begin(9600);
pinMode(tx,OUTPUT);
pinMode(rx,INPUT);
}

void loop()
{
digitalWrite(tx,HIGH);
delay(1000);
transmit.println("AT\r");
delay(1000);
transmit.println("AT+CPOWD=1\r");
delay(7000);
digitalWrite(tx,LOW);
}

I searched online, people say, we need to put \r after an AT command, that's why I put it on, but i tried both AT\r and AT, it didn't work. The link below is the development board I'm using.

http://www.ebay.com.au/itm/Arduino-GPRS-GSM-SIM300-Module-Development-Board-Ver2-/270728487837?pt=BI_Electrical_Equipment_Tools&hash=item3f08ace39d#ht_2688wt_905

I also tried to use arduino pin 0 and 1, 0 is the rx, and 1 is the tx, and connect Arduino tx to rx , rx to tx with the other board,but no difference.

I think I did send my text over to the board, because, when I just got the development board, there were 3 flashing LED on it, then i started to transmit my at command to it, the LEDs stopped flashing. But somehow the command doesn't work, anyone have any idea?

Once you define rx & tx for newsoft serial, you're likely interfering with serial operation by doing digitalWrite to the same pins.
You have I2C operations planned eventually too, with wire.h?

Don't thrash around doing both normal serial and soft serial. Decide on one and get it working. This should be enough:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("AT");
  delay(1000);
  Serial.println("AT+CPOWD=1");
  delay(7000);
}

If you look up Serial.println the documentation says it adds a carriage-return for you, so you don't need to put one of your own.

I also tried to use arduino pin 0 and 1, 0 is the rx, and 1 is the tx, and connect Arduino tx to rx , rx to tx with the other board,but no difference.

And you connected the ground wires as well?

I tried #include <Wire.h>, #include <VitualWire.h>, i searched some examples for both of them, and wrote small code for it, the code compiled well, no error, and i can upload it to arduino too, but nothing happened to my development board.

I connected to GND, also tried to connect Arduino 3V3, and 5V, and the board's own power supply on it.

From where I bought this board (ebay), i found this:
Two serial ports.
Three Mode
PC-GSM. [PC debug mode, DB9 Interface]
GSM-MCU. [TTL debug mode, P2 pin]
PC-MCU. [PC debug mode, for board with no serial port, especially Arduino development board]

I'm going to change to PC-MCU and see if it is working. Thanks for replying.

Or is there any other way to programming this kind of development board? Cause it is not a must to use arduino, but without arduino, I don't know how to upload codes to it.

Once you define rx & tx for newsoft serial, you're likely interfering with serial operation by doing digitalWrite to the same pins.

That should be definitely. Once you tell NewSoftSerial about those pins, you must forget that they even exist,

And, you shouldn't do NewSoftSerial on pins 0 and 1.

First, get rid of wire.h, that is for I2C commands using the SCL/SDA lines, you are not doing that. Same with virtual wire.

How are you powering it? Just the 5V from the Arduino? Or 12V via its own power connector?

"Two serial ports.
Three Mode
•PC-GSM. [PC debug mode, DB9 Interface]
•GSM-MCU. [TTL debug mode, P2 pin]
•PC-MCU. [PC debug mode, for board with no serial port, especially Arduino development board]"

How are you connecting? It is not clear to me how a mode is selected either.

You need to read the AT Command Manual some more.
http://www.yourportablelab.com/downloads/schematics/SIM300AT_command.pdf

"Note: Only enter AT command through serial port after SIM300 is power on and Unsolicited Result Code “RDY” is received from serial port. And if unsolicited result code”SCKS: 0” returned it indicates SIM card isn’t present. If autobauding is enabled, the Unsolicited Result
Codes “RDY” and so on are not indicated when you start up the ME"

So the first thing your code should is look for is an unsolicited response from the device.

I would try some simple commands after that,
ATP, ATT, see if you can get an OK back to show that the interface is working.
ATE1, see if it will echo your commands back.

I'm using 12V via its own power connector, I don't actually know what SCL/SDA lines mean, but I will do some search about it tomorrow. One of my team mate used putty to send command to the board, AT and he got OK response from the device.

From the manual that come with the board, it shows us how to connect into PC-MCU, there are pins on the board, they are connected to each other at the beginning, by changing the connection, we can switch it from manual to automatic, or even update the board. ( that's what it says on the SIM300 manual book, I don't know how the detail about it either, so I can't explain it to you)

And I read the SIM300 AT_command file before, but briefly, it's kind of hard to understand, maybe I should read it from page 1 again, another 200 pages to read, LOL, the purpose of my little code is to get a response from device, well, I'm just trying to shut it down instead of getting RDY.

ATP,ATT,ATE1, and I2C commands, I will try these after class, thanks a lot CrossRoads, this is great help for me, at least I won't be looking at the screen, and don't know what to do next.

Not I2C, that's not a serial interface you can use with this board.

If you can go Arduino D0/D1 (Rx-Tx at TTL (0-5V) levels) direct to pins on the board, do that. I think that would be MCU interfacing.
Otherwise, you need a TTL-RS232 adapter for the Arduino, and then you can connect to the serial port.

http://www.nkcelectronics.com/rs232-to-ttl-3v--55v-convert232356.html

I have a question about sending text from arduino to other device. for example:

when we use #include <VirtualWire.h>, to send a message, we use code

vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone

when we use #include <Wire.h>, we use

Wire.send("hello ");

both of them are using special function to send out a text, Wire.send, or vw_send(),these functions are included in their library, it must be some setting or something is required, that's why the designer made a function for them.

So I'm wondering, maybe I shouldn't use println("") in my project, because, println is more like to display some message on the monitor, but not sending message out, correct me if I'm wrong, started with arduino not long ago, so not very sure about this.

So I'm wondering, maybe I shouldn't use println("") in my project, because, println is more like to display some message on the monitor, but not sending message out, correct me if I'm wrong, started with arduino not long ago, so not very sure about this.

Each of the methods you mention - VirtualWire, Wire, and Serial - use different pins and protocols to send data. You need to use the class/methods that go with the pins that you are using to send the data.

@4design,
wire.h uses a built-in clock and set of shift register to clock data and clock it back in. The address of the device being accessed is part of the message stream.

spi.h uses a built-in clock and a set of shift registers to clock data out and back in. slave select is used to tell the other device that it is to shift in data using the clock, or to shift out data using the same clock.

serial.begin usesa built in UART to send data out and back in, the 2 devices have to have pre-agreed on the baud rate and sample the data line multiple times to create the high-lows that are read. Data can be sent from the slave indepedently of the master doing anything.

wire.h & spi.h both require the master to initiate the transfer in either direction as the master controls the clock.

The board you are talking to has a UART built in and is coded to receive data from your serial.print or serial.println commands.
You can't mix things up.

int rx=0,tx=1;
int reply;

void setup()
{
Serial.begin(9600);
delay(2000);
Serial.println("AT\r");
delay(3000);
Serial.println("AT+CPOWD=1\r");
delay(6000);
reply=analogRead(rx);
Serial.println(reply);

}

void loop()
{
}

I uploaded this to my arduino, and I got a response "501", anyone knows what does it mean?

You don't need this, Serial.begin takes these pins over:
int rx=0,tx=1;

Next:
reply=analogRead(rx);
This will end up taking an analog reading of pin A0. 501 is about 2.5v, an analog input pin not connected to anything.

What you need to do is use things like:

if (Serial.available >0){ // any serial data received?
incomingDatabyte = Serial.read(); // yes, read it from the incoming buffer
Serial.print (incomingDatabyte); // and print it
}

The problem is you only have 1 serial port - so anything you try and serialprint goes to the development board too, which may lead to more confused responses coming back.
What you really need to do is have seperate serial ports; the standard serial for debug comm's with the PC, and a 2nd one for comms with the development board. You could use newsoftserial for that.
http://arduiniana.org/libraries/NewSoftSerial/

Thus if you had Serial and Serial1 for example:

Serial1.println("AT\r"); //send a command
  delay(3000);
 if (Serial1.available>0){  // any response received?
  reply=Serial1.read();
  Serial.println(reply); // send it to PC
// and there ways to expand this to read everything that comes  back & pass it out
}
 Serial1.println("AT+CPOWD=1\r");
  delay(6000);
if (Serial1.available>0){
  reply=Serial1.read();
  Serial.println(reply);
}

You can see how Serial & Serial1 are used to send to development, read from develpment, and send to PC.
You could also use this method to receive a command from PC keyboard via IDE Serial Monitor, pass to development, receive from development and send to PC,
give yourself an easy way to try out commands & check out the responses.
I would do this without the delays tho.

1 Like

Sorry, was busy for mid semester exams. I tried what you told me

I connect arduino pin 0, 1 to TXD and RXD.
Use NewSoftSerial, make pin 2 and 3 as tx, and rx, then connect them to the DBGTX, and DBGRX on my development board.
and ground both.

trying to upload the picture here, but don't know how to do it. So I uploaded as attachment

#include <NewSoftSerial.h>

NewSoftSerial debugSerial(2,3); // define the rx/tx pins (rx,tx)

void setup()
{
Serial.begin(9600);
Serial.println("AT\r");
Serial.println(debugSerial.read());

if(Serial.available()>0)
{
Serial.println(debugSerial.read());
}

if(Serial.available()<0)
{
Serial.println("Connection failed");
}
}

When i upload this code, the line i marked as red, returned a "-1" in the serial monitor, but both of the if() statement didn't work, not returning anything back to me, I think the way i connect the two boards is wrong.

pin.png

The AT command is sent followed by a \r, a \n, and a \r. I think that's a few too many characters...

In any case, you waited only a few nanoseconds for the device to get ready, before trying to read its response. Hence, the -1.

Then, you are testing to see if you have sent any data from the serial monitor. If so, then you print one character read from the device. You most likely have not sent anything from the serial monitor (at least not in the time between opening the serial monitor (which reset the Arduino) and the Arduino being ready to read serial data), so Serial.available() returns 0, which is why nothing else was read from the device.

The Serial.available() call returns the number of bytes available to be read from the buffer. It is impossible for there to be less then 0 bytes, so the test for a negative value will never succeed.

Then, setup ends.

You don't show what loop looks like.

Now I got "connection failed" from the device, but i think it is from arduino, not the development board.

before I sent this code, I also connected the development board to the computer, and use putty, do some setting on it by using AT commands.

AT+IFC=1,1
AT+IPR=19200
AT&W

after this, I sent the following code over, and i got "connection failed".

void setup()
{
Serial.begin(19200);
delay(2000);
}

void loop()
{
Serial.println("AT\r");
delay(2000);
if(Serial.available())
{
Serial.println(debugSerial.read());
delay(1000);
}

if(!Serial.available())
{
Serial.println("Connection failed");
delay(1000);
}
}

and on putty, i get some random %$$#$& data showing. i think the way I connect two boards are wrong, I use wires directly connect to the RX and TX pins, then ground..