Change Power Level Xbee (series1)

Hello guys I want to change the power level of my xbees through source code to my college project.
I have 2 XBee 1mW Chip Antenna - Series 1 (802.15.4), one XBee Explorer USB and one arduino duemilanove connected to a XBee Shield.

I already now how to set the power level using the X-CTU software, but I want to change that through source code because my project is about power consumption of a wirelles network. So I want to now if there is a library or a function in one of the standard libraries that do the power level change.
I had found some old libraries that don't have the function that I'm looking for.

Thanks.

You can send AT commands to the XBee, just like X-CTU does, to change settings.

But there's a way to send this AT command through source code?
Because I'm trying to do something autonomous, that don't need my interaction with serial communication.

But there's a way to send this AT command through source code?

Yes.

Because I'm trying to do something autonomous, that don't need my interaction with serial communication.

How is the XBee communicating with the Arduino? Semaphore flags waving around?

One of the Xbees is connected to the USB Xplorer that's connected to the computer. The second Xbee is connected to one Xbee Shield that's connected to one arduino duemilanove,being powered trough a external power supply (VIN/GND).
I want that the xbee that's connected to arduino/xbee shield could change your power level on an autonomous way to save power, decreasing the power level when signal is strong and increasing when signal is weak.

How is the XBee communicating with the Arduino?

You didn't answer this question. I'll tell you - the communication is via Serial.print(), Serial.write(), Serial.println(), Serial.read(), and Serial.available().

So, yes, you can send +++ to the XBee, to put it into command mode, and then send it AT commands, using Serial.print().

PaulS:

How is the XBee communicating with the Arduino?

You didn't answer this question. I'll tell you - the communication is via Serial.print(), Serial.write(), Serial.println(), Serial.read(), and Serial.available().

So, yes, you can send +++ to the XBee, to put it into command mode, and then send it AT commands, using Serial.print().

Thanks PaulS
I will try this:
Serial.print(+++);
Serial.print(ATPL);
Serial.print(x); //4,3,2,1

I will tell you if it works.

If that's

I will try this:
Serial.print(+++);
Serial.print(ATPL);
Serial.print(x); //4,3,2,1

It may be necessary to play with delay() times, to give the XBee time to enter command mode.

It definitely will be necessary to read what the XBee is responding with, to assure, for instance, that it entered command mode, before sending it an AT command.

I'll do a test within the code using the Serial.read(), to see if the command was received correctly, and I will put the delay too.
And I'll do it on Wednesday because tomorrow I have a lot of classes.

But thanks PaulS, you were very helpful, helped me a lot.

I'm using the following code just to do some tests:

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

void loop ()
{
  Serial.print("+++");
  delay(4000);
  Serial.println("ATPL");
  delay(1000);
  Serial.println("ATPL 2");
  delay(1000);
  Serial.println("ATPL");
  exit(1);
}

But that isn't working, because after I did the upload to arduino board and connected the xbee shield with the xbee and I open the X-CTU serial terminal to see if that's working.
The terminal shows me the code but don't return me the "OK" after the "+++", the ATPL command is typed autonomous by arduino but doesn't work.
Do you guys nows what's wrong with the code?
Thanks

"Sorry for me english, is not my native language"

Do you guys nows what's wrong with the code?

That code should be in setup(), or a function that gets called only as needed, not in loop().

The code talks to the XBee, but doesn't listen to the XBee. Why not?

PaulS:

Do you guys nows what's wrong with the code?

That code should be in setup(), or a function that gets called only as needed, not in loop().

The code talks to the XBee, but doesn't listen to the XBee. Why not?

The code isn't listening to xbee because this is only a prototype code, I'm using that just to learn how to use the AT commands in the source code. After I will do a complete code to my project
But thanks PaulS like always you helped me a lot.

The code isn't listening to xbee because this is only a prototype code, I'm using that just to learn how to use the AT commands in the source code.

In the production version, you might be able to get away with not reading the reply. In the prototype/development phase it is crucial to read the reply, to make sure that it is what you think are getting.

Ok that's still not working...
This is the code that I'm using now:

void setup ()
{
  Serial.begin(9600);
  
  Serial.print("+++");
  delay(3000);
  if(Serial.read()=='OK'){
  Serial.println("ATPL");
  delay(1000);
  if(Serial.read()>=3){
  Serial.println("ATPL 2");
  delay(1000);
  Serial.println("ATPL");
  }
  }
}

void loop ()
{
  
}

But I'm still note receiving the "OK" from the command "+++", and not receiving too the return of ATPL.
I listened to the returns too like you said. I really don't now what to do to this works.

Serial.read() returns one byte. That one byte is almost certainly not equal to the multibyte constant 'OK'.

Ok I will change that, but yet the command +++ is not returning the "OK", even if a only use that in void setup(), like it:

void setup()
{
Serial.begin(9600);
Serial.print("+++");
}

Checking it on T Term the command OK isn't returned.

I did the modiications on the code:

char str[5] = "OK";

void setup ()
{
  Serial.begin(9600);
  
  Serial.print("+++");
  delay(3000);
  if((int)Serial.read()==(int)str){
  Serial.println("ATPL");
  delay(1000);
  if(Serial.read()>=3){
  Serial.println("ATPL 2");
  delay(1000);
  Serial.println("ATPL");
  }
  }
}

void loop ()
{
 
  
}

Still... Not working =/

  if((int)Serial.read()==(int)str){

There are so many problems with this one statement...

str is a character array, containing "OK". How can you possibly cast that to an int?

Serial.read() reads ONE byte. That might be 'O'. It might be 'K'. Or, because you haven't made sure that there is actually anything to read, it might return -1. In any case, (-1, 'O', or 'K', or something other SINGLE character), it is impossible for that to equal "OK", no matter how you plead with the compiler to make it so.

This won't fix anything but it will make your code run faster. The guard time around the +++ is 1000ms by default. So I usually only delay for 1010ms (an extra 10 just to be safe) before moving on. This works fine.

Also, check some of my pretty old sample code for a kinda dumb but easy to read function for checking the OK in command mode: http://www.faludi.com/itp_coursework/meshnetworking/XBee/XBee_Send_Example.pde

Faludi I tried to do one function to see if the xbee returns "OK" based on your code (char returnedOK () ).
But i'm still don't receiving the ok, and when I connect the xbee to the Xbee Xplorer USB to check in X-CTU if something have changed, I didn't see any change of the previously configuration.
The code is that:

void setup ()
{
  Serial.begin(9600);
  
  Serial.print("X");
  delay(1100);
  
  Serial.print("+++");
  delay(1100);
   if (returnedOK() == 'T') {
  }
  else {
    setup(); 
  }
  
  Serial.println("ATPL3");

  Serial.println("ATPL");

  }

void loop ()
{
 
  
}


char returnedOK () {
  char incomingChar[3];
  char okString[] = "OK";
  char result = 'n';
  int startTime = millis();
  while (millis() - startTime < 2000 && result == 'n') {  
    if (Serial.available() > 1) {
   
      for (int i=0; i<3; i++) {
        incomingChar[i] = Serial.read();
      }
      if ( strstr(incomingChar, okString) != NULL ) { 

        result = 'T'; 
      }  
      else {
        result = 'F'; 
      }
    }
  }
  return result;
}

I'm really in doubt now if the xbee connected to an xbee shield could change your configurations without I have to use a Xbee Xplorer USB.