domingo, 13 de marzo de 2011

Arduino programación wireless con Xbee oficial. Cuarta parte.

El código del sketch que debe correr en el Arduino Master para hacer la programación remota con el shield de Xbee es:


//MAKE AN ARDUINO WIRELESS PROGRAMMING WITH XBEE SHIELD LIBELIUM
//RemoteXbee v1.0
//RELYNXANDO Marz-11
//This example code is in the public domain.
#include <xbeefg.h>
#include <newsoftserial.h>
// SH + SL of your remote radio Slave Arduino XBeeAddress64(SH,SL)
// in my case SH=0x0013a200 and SL=0x4049CC62
XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x4049CC62);
// Define NewSoftSerial TX/RX pins
// Connect Arduino pin 6 to TX of usb-serial device
uint8_t ssRX = 6;
// Connect Arduino pin 7 to RX of usb-serial device
uint8_t ssTX = 7;
xBeeFG XBeeNSS = xBeeFG( ssRX, ssTX );
// Software UART to communicate with the Xbee module
NewSoftSerial nss(ssRX, ssTX);

// Configure remote Xbee to API Mode (AP=2)
uint8_t APIModeCmd[] = {  'A', 'P' };
uint8_t APIModeValue[] = {  0x2 };
uint8_t TransModeValue[] = {  0x0 };

AtCommandRequest atRequest = AtCommandRequest(APIModeCmd, APIModeValue, sizeof(APIModeValue));
AtCommandResponse atResponse = AtCommandResponse();

// Set DIO7 (pin 12) to OUT HIGH
uint8_t d7Cmd[] = {  'D', '7' };
uint8_t d7ONValue[] = {  0x5 };
uint8_t d7OFFValue[] = {  0x4 };

// Set DIO5 (pin 15) to OUT HIGH
uint8_t d5Cmd[] = {  'D', '5' };
uint8_t d5ONValue[] = {  0x5 };
uint8_t d5OFFValue[] = {  0x4 };
uint8_t ATModeRemote[] = {  '+', '+', '+'};
uint8_t APIModeRemote[] = {  'A', 'T', 'A', 'P', '2'};

char payload[20];   

// unless you have set DH/DL for 64 bit this will be received as a RX16 packet
Tx64Request tx64 = Tx64Request(remoteAddress, (uint8_t*)payload, sizeof(payload)); 

// This will contain the status responses of that the XBee receives
TxStatusResponse txStatus = TxStatusResponse();                            

// Create a remote AT request with the IR command
RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(remoteAddress, APIModeCmd, APIModeValue, sizeof(APIModeValue));

// Create a Remote AT response object
RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();

void setup()
{
  Serial.begin(19200);
  Serial.println("REMOTE PROGRAMMING"); 
  delay(10);
  XBeeNSS.begin( 19200, ssRX, ssTX );
  delay(100);
  nss.begin(19200);
  delay(100);
}

void loop()
{
  //CONFIGURE XBEE1 AND XBEE2 AS MODE API
  //Configure local XBEE1 in API mode again with AT AP 2 command
  delay(150); 
  nss.print("+++");
  delay(1500);
  nss.println("ATAP2");
  delay(3000);
  //XBEE2 in AT mode 
  tx64 = Tx64Request( remoteAddress, ATModeRemote, sizeof( ATModeRemote ) );
  XBeeNSS.send( tx64 );
  delay(3000);
  //Xbee2 in API mode
  remoteAtRequest.setCommand(APIModeCmd);  
  remoteAtRequest.setCommandValue(APIModeValue);
  remoteAtRequest.setCommandValueLength(sizeof(APIModeValue));
  XBeeNSS.send(remoteAtRequest);
  delay(5000);
  //END CONFIGURATION XBEE1 AND XBEE2 AS MODE API
  //Reset remote XBEE2
  remoteAtRequest.setCommand(d7Cmd);  
  remoteAtRequest.setCommandValue(d7ONValue);
  remoteAtRequest.setCommandValueLength(sizeof(d7ONValue));
  XBeeNSS.send(remoteAtRequest);
  delay(2000);
  //Enable remote XBEE2 enter bootloader
  remoteAtRequest.setCommand(d7Cmd);  
  remoteAtRequest.setCommandValue(d7OFFValue);
  remoteAtRequest.setCommandValueLength(sizeof(d7OFFValue));
  XBeeNSS.send(remoteAtRequest);
  //Configure remote XBEE2 TO TRANSPARENT MODE to send file .hex in a transparent way
  remoteAtRequest.setCommand(APIModeCmd);  
  remoteAtRequest.setCommandValue(TransModeValue);
  remoteAtRequest.setCommandValueLength(sizeof(TransModeValue));
  XBeeNSS.send(remoteAtRequest);
  //Configure local XBEE1 TO TRANSPARENT MODE to send file .hex in a transparent way
  atRequest.setCommand(APIModeCmd);
  atRequest.setCommandValue(TransModeValue);
  atRequest.setCommandValueLength(sizeof(TransModeValue));  
  XBeeNSS.send(atRequest);
  delay(30);
  Serial.flush();
while(1){
      if (nss.available()) {
          Serial.print((char)nss.read());  
      }
      if (Serial.available()) {
          nss.print((char)Serial.read());  
      }
 }
  //end send Hex file
}

No hay comentarios:

Publicar un comentario