示例#1
0
bool XBeeUtil::getRadioAddress(XBee* xbee, int32_t& msb, int32_t& lsb, Stream* debug_stream){

	bool toRet = true;
	XBeeUtil::debug_stream = debug_stream;

	AtCommandRequest atRequest = AtCommandRequest();
	AtCommandResponse atResponse = AtCommandResponse();

	uint8_t SH_cmd[] = { 'S', 'H' };
	uint8_t SL_cmd[] = { 'S', 'L' };


	atRequest.setCommand(SH_cmd);
	if (XBeeUtil::atCommand(xbee, atRequest, atResponse) && atResponse.getValueLength() == 4
			&& XBeeUtil::readAtResponseInt(xbee, atResponse, msb)) {
	} else {
		toRet = false;
	}

	atRequest.setCommand(SL_cmd);
	if (XBeeUtil::atCommand(xbee, atRequest, atResponse) && atResponse.getValueLength() == 4
			&& XBeeUtil::readAtResponseInt(xbee, atResponse, lsb)) {
	} else {
		toRet = false;
	}


	return toRet;
}
/**	EXPERT FUNCTION: Sends an ATCommand and returns the response as a 32-bit integer.
	If you specify a value of waitForResponse, the program will wait
	for the amount of ms specified by waitForResponse and immediately
	process the AT response. If you don't specify a value or specify 0
	the program will call the general readXbee() and process the response
	there and write it to the corresponding variables.
	This function will then return 0.	**/
uint32_t LithneClass::sendATCommand( uint8_t * cmd, uint16_t waitForResponse )
{
	uint32_t	atAnswer	=	0;
	
	/*	Create a new AtComand Request and Response	*/
	AtCommandRequest atRequest   	= AtCommandRequest();

	/*	Set the specific command you want to send to the XBee in the atRequest	*/
	atRequest.setCommand( cmd );
	
	/*	Send this request to the xbee	*/
	xbee.send( atRequest );
  
	/*	The processing of the AT response is handled in the readXBee().
		We thus call this function. If we manually tell this function to
		wait for a response, it will skip this and attempt to read the
		information at this point. */
	if( waitForResponse == 0 )
	{
		readXbee();
	}
	else
	{
		if ( xbee.readPacket(waitForResponse) )
		{
			AtCommandResponse atResponse 	= AtCommandResponse();
			/*	If the packet is indeed an AT_COMMAND_RESPONSE,	we write it to the atResponse.	*/
			if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) 
			{
				xbee.getResponse().getAtCommandResponse(atResponse);
				/*	If the atResponse is correct (does not return an error code), we can process it.	*/
				if (atResponse.isOk()) 
				{
					if (atResponse.getValueLength() > 0) 
					{  
						for (int i = 0; i < atResponse.getValueLength(); i++) 
						{
							atAnswer	=	(atAnswer << 8) + atResponse.getValue()[i];
						}
        			}
				}
			}
  		}
		else 
  		{
	    	// at command failed
	    	if (xbee.getResponse().isError()) 
		    {
	//      	Serial.print("Error reading packet.  Error code: ");  
	//  	    Serial.println(xbee.getResponse().getErrorCode());
		    } 
	    	else 
		    {
	//      	Serial.print("No response from radio");  
	    	}
	    }
	}
  return atAnswer;
}
示例#3
0
bool XBeeUtil::associate(XBee* xbee)
{
	uint8_t AI_cmd[] = { 'A', 'I' };
	AtCommandRequest atRequest = AtCommandRequest();
	AtCommandResponse atResponse = AtCommandResponse();
	atRequest.setCommand(AI_cmd);
	if (XBeeUtil::atCommand(xbee, atRequest, atResponse) && atResponse.getValueLength() == 1) {
		return true;
	} else {
		return false;;
	}
}
示例#4
0
namespace FireNest {

    struct Channel {
      int button_pin;
      int led_pin;
      int state;
      int time;
    };



    Channel channels[] = { 
      {2,8,0,0}, 
      {3,9,0,0},
      {4,A3,0,0},
      {5,A0,0,0},
      {6,A1,0,0},
      {7,A2,0,0}
    };


    //prototypes
    void discover();
    boolean checkNDResponse();
    void flashAll(int ms);
    void clearLeds();
    void nr(uint8_t nr);
    uint32_t pack(uint32_t c1, uint32_t c2, uint32_t c3, uint32_t c4);

    //NewSoftSerial mySerial(12, 13);

    //used when using first button to fire all channels
    uint8_t channel_count = 0;
    uint8_t fired_channels = 0;

    XBee xbee = XBee();

    uint8_t payload[] = { 0 };

    // SH + SL Address of receiving XBee
    uint32_t sh = 0;
    uint32_t sl = 0;

    //XBeeAddress64 addr64; //XBeeAddress64(0x0013a200, 0x403141DA);
    XBeeAddress64 addr64; //XBeeAddress64(0x0013a200, 0x403141DA);
    ZBTxRequest zbTx;
    ZBTxStatusResponse txStatus = ZBTxStatusResponse();

    //for device discovery
    uint8_t atCmd[] = {'N','D'};
    AtCommandRequest atRequest = AtCommandRequest(atCmd);
    AtCommandResponse atResponse = AtCommandResponse();


    void setupold() {
      
      //mySerial.begin(4800);
      //mySerial.println("Hello world");
      
      for (int i= 0; i<CHANNELS; i++) {
        pinMode(channels[i].button_pin,INPUT);
        digitalWrite(channels[i].button_pin,HIGH); //enable internal 20K pullup
        
        pinMode(channels[i].led_pin,OUTPUT);
        //blink leds a bit
        digitalWrite(channels[i].led_pin,HIGH);
        delay(200);
        digitalWrite(channels[i].led_pin,LOW);
      }
      
      //debug led
      //pinMode(13,OUTPUT);
      //digitalWrite(13,HIGH);
      //delay(500);
      //digitalWrite(13,LOW);
      
      xbee.begin(XBEE_BAUD);

      //discover the other XBEE's address
      discover();
      zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
      
      //send a no-op packet so that the xbees can do their magic and find each other
      payload[0] = 254;
      xbee.send(zbTx);
      
      //Flash all leds once so the user knows
      flashAll(500);

      //mySerial.println("Discovered address");
      //mySerial.print("MSB: ");
      //mySerial.println(addr64.getMsb());
      //mySerial.println(addr64.getMsb()==0x0013a200?"Yes!":"NO");
      //mySerial.print("LSB: ");
      //mySerial.println(addr64.getLsb());
      //mySerial.println(addr64.getLsb()==0x403141DA?"Yes!":"NO");
    }



    //State 0 == not pressed, waiting for press

    //State 1 == pressed, debouncing time not up
    //Fire on press

    //State 2 == pressed, waiting for release 

    //State 3 == release, debouncing time not up

    void loopold() {
      int val;
      int m;
      
      for (uint8_t i= 0; i<CHANNELS; i++) {
        m = millis();
        
        if (channels[i].state == 0 || channels[i].state == 2) {
          val = digitalRead(channels[i].button_pin);
          
          if (channels[i].state == 0 && val == LOW) {
              //a press!, fire!
              uint8_t cc = i;
              
              //special case, we can fire all channels by firing the first button repeatably
              if (i == 0) {
                cc = channel_count;
                channel_count = (channel_count + 1) % CHANNELS;
              } 
              
              //fire!
              payload[0] = cc;
              xbee.send(zbTx);
              
              //set as fired 
              fired_channels |= (1 << cc);  
              digitalWrite(channels[cc].led_pin,HIGH);  
              
              //check if all is fired
              if (fired_channels == B00111111) {
                //wait a bit
                delay(500);
                
                //reset all
                channel_count = 0;
                fired_channels = 0;
                for (int j = 0; j<CHANNELS; j++) {
                  channels[j].state = 0;
                  digitalWrite(channels[j].led_pin,LOW);
                  delay(300);
                }
                break;
              }
          }
          
          if ((channels[i].state == 0 && val == LOW) || (channels[i].state == 2 && val == HIGH)) {
            channels[i].state = (channels[i].state + 1) % 4; //change state 
            channels[i].time = m;
          }
                
        } else if (m - channels[i].time >  THRESHHOLD) {
          channels[i].state = (channels[i].state + 1) % 4; //change state   
        }
      } 
    }


    //discover target node
    void discover() {
      //mySerial.println("discover");
      //if we don't get a address we can't fire
      while (true) {
        //send node discovery
        xbee.send(atRequest);
        
        //default value is that responding XBEE can wait up to six seconds before answering
        //so spamming it with node discoverys might be a bad thing, but waiting so long is booring so
        //we we'll try it and see if it works...
        
        //knight rider on the diodes let's the users know we're looking
        for (int i=0; i<CHANNELS; i++) {
          clearLeds();
          digitalWrite(channels[i % CHANNELS].led_pin,HIGH);

          if (checkNDResponse()) {
            return;
          }      
        }

        for (int i=CHANNELS-1; i>=0; i--) {
          clearLeds();
          digitalWrite(channels[i % CHANNELS].led_pin,HIGH);

          if (checkNDResponse()) {
            return;
          }      
        }
      }
    }

    boolean checkNDResponse() { 
      //mySerial.println("checkNDResponse");
      // wait a small bit so the animation looks good
      if (xbee.readPacket(ND_WAIT_TIME / 6)) {
        // got a response!

        // should be an AT command response
        if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
          xbee.getResponse().getAtCommandResponse(atResponse);

          if (atResponse.isOk()) {
            if (atResponse.getCommand()[0] == atCmd[0] && atResponse.getCommand()[1] == atCmd[1] && atResponse.getValueLength() > 3) {

              //mySerial.println(pack(atResponse.getValue()[2],atResponse.getValue()[3],atResponse.getValue()[4],atResponse.getValue()[5]));          
              //mySerial.println(pack(atResponse.getValue()[6],atResponse.getValue()[7],atResponse.getValue()[8],atResponse.getValue()[9]));
              
              addr64 = XBeeAddress64( pack(atResponse.getValue()[2],atResponse.getValue()[3],atResponse.getValue()[4],atResponse.getValue()[5]),pack(atResponse.getValue()[6],atResponse.getValue()[7],atResponse.getValue()[8],atResponse.getValue()[9]) );
              
              
              return true;
            }
          } 
          else {
            //nss.print("Command return error code: ");
            //nss.println(atResponse.getStatus(), HEX);
            nr(1);
          }
        } else {
          //nss.print("Expected AT response but got ");
          //nss.print(xbee.getResponse().getApiId(), HEX);
          nr(2);
        }   
      } else {
        // at command failed
        if (xbee.getResponse().isError()) {
          //nss.print("Error reading packet.  Error code: ");  
          //nss.println(xbee.getResponse().getErrorCode());
          nr(3);
        } 
        else {
          //nss.print("No response from radio");  
          nr(4);
        }
      }
      return false;
    }

    //flash leds once, variable time
    void flashAll(int ms) {
      for (int i=0;i<CHANNELS; i++) {
        digitalWrite(channels[i].led_pin,HIGH);
      }
      
      delay(ms);
      clearLeds();  
    }

    //clear all leds
    void clearLeds() {
      for (int i=0;i<CHANNELS; i++) {
        digitalWrite(channels[i].led_pin,LOW);
      }
    }

    //light up a nr, binary code
    void nr(uint8_t nr) {
      //TODO: smarter code...
      if (nr & B00000001) {
        digitalWrite(8,HIGH);
      }
      if (nr & B00000010) {
        digitalWrite(9,HIGH);
      }
      if (nr & B00000100) {
        digitalWrite(A3,HIGH);
      }
      if (nr & B00001000) {
        digitalWrite(A0,HIGH);
      }
      if (nr & B00010000) {
        digitalWrite(A1,HIGH);
      }
      if (nr & B00100000) {
        digitalWrite(A2,HIGH);
      }
    }
     
    uint32_t pack(uint32_t c1, uint32_t c2, uint32_t c3, uint32_t c4) {
        return (c1 << 24) | (c2 << 16) | (c3 << 8) | (c4);
    }
}
示例#5
0
int Len_XBee = 0;
unsigned char info_XBee_data[96];
unsigned char info_XBee_data1[96];
char* simbol_ascii[2];
char   cmd;

XBeeResponse response = XBeeResponse();               //Это создает экземпляр объекта "response" "ответ" обрабатывать пакеты Xbee
ZBRxResponse rx = ZBRxResponse();                     //Это создает экземпляр объекта "rx" на процесс Xbee Series 2 API пакеты
ModemStatusResponse msr = ModemStatusResponse();      //Это создает экземпляр объекта "msr" процесс associate/disassociate packets (PAN membership)
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();


XBeeAddress64 addr64 = XBeeAddress64(XBee_Addr64_MS, XBee_Addr64_LS);                                     // SH + SL Address of receiving XBee
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));                                         // Формирует пакет  zbTx с адресом отправителя и данными
ZBTxStatusResponse txStatus = ZBTxStatusResponse();                                                       // Это создает экземпляр объекта "txStatus" процесс благодарности прислал Xbee Series 2 API пакеты
AtCommandRequest atRequest = AtCommandRequest(shCmd);                                                     // XBeeAddress64 remoteAddress = XBeeAddress64(XBee_Addr64_MS, XBee_Addr64_LS);
AtCommandRequest arRequestMod = AtCommandRequest(command, commandValue, commandValueLength);
AtCommandResponse atResponse = AtCommandResponse();
RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(addr64, irCmd, irValue, sizeof(irValue)); // Create a remote AT request with the IR command
RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();                                     // Create a Remote AT response object

XBeeActive RealXBeeMenu;

void XBeeActive::setup()
{
	xbee.begin(Serial1);
	DEBAG.println("\nXBee setup");
}
void XBeeActive::sendAtCommand()
{
	int i10;
#define XBEE_INIT_WAITING_TIME  3000 // XBee initialization waiting time

XBeeMACLayer::XBeeMACLayer(){
}

// serial high
uint8_t shCmd[] = {'S','H'};
// serial low
uint8_t slCmd[] = {'S','L'};
// cca failures
uint8_t ecCmd[] = {'E','C'};

uint8_t mac_position;

AtCommandRequest atRequest = AtCommandRequest(shCmd);
AtCommandResponse atResponse = AtCommandResponse();

#define AT_RESPONSE_MAX_ATTEMPTS 5
#define AT_REQUEST_MAX_ATTEMPTS 20

bool XBeeMACLayer::getResponseMAC(){
          if (atResponse.getValueLength() == 4) {            
            for (int i = 0; i < atResponse.getValueLength(); i++) {
              my_mac.addr[mac_position++] = atResponse.getValue()[i];
            }
            return true;
          }
          return false;
}