//CONSTRUCTOR //////////////////////////////////////////////////////////////////////////// XBeePlus::XBeePlus() { XBee _xbee = XBee(); ZBRxResponse rx = ZBRxResponse(); XBeeAddress64 addr64 = XBeeAddress64(0x0, 0xFFFF); ZBTxStatusResponse txStatus = ZBTxStatusResponse(); }
/* Transmission Code */ void Transmit_Packet(void) { /* Create Xbee object */ XBee xbee = XBee(); /* Packet to be transmitted */ uint8_t payload[MAX_SIZE]; /* Obtain address of receiving end */ XBeeAddress64 addr64 = XBeeAddress64(0x0, 0x0); ZBTxStatusResponse txStatus = ZBTxStatusResponse(); /* Clear the payload */ memset(payload, '\0', sizeof(payload)); /* Debug Packet */ //payload[0] = '1'; //payload[1] = '5'; /* Transfer information into payload */ memcpy(payload, &G_packet, sizeof(payload)); for(int i = 0; i < MAX_SIZE; i++) { Serial.write(payload[i]);} ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload)); /* Transfer the payload */ xbee.send(zbTx); //!!Prints packet to serial monitor /* delay */ delay(1000); }
void gd_dev_xbee_write(uint8_t *data, int data_len) { XBeeAddress64 addr64 = XBeeAddress64(0, 0); ZBTxRequest zbtx = ZBTxRequest(addr64, data, data_len); xbee.send(zbtx); }
void SourceNode::broadcastMessage(const Message & mess) { Serial.println("About to broadcast message"); XBeeAddress64 addr = XBeeAddress64( CommonValues::Message::BROADCAST_PREFIX, CommonValues::Message::BROADCAST_SUFFIX ); sendMessage(addr, mess); Serial.println("\nDone broadcasting message"); }
/** Returns the own 64-bit Serial Address of the XBee connected **/ XBeeAddress64 LithneClass::getMyAddress64( bool forceCheck ) { /* If the address is unknown, or if we force a recheck. */ if( myAddress64.getLsb() == UNKNOWN_64B || forceCheck ) { uint32_t msb = sendATCommand( atSH, 1000 ); //We want to wait for an answer uint32_t lsb = sendATCommand( atSL, 1000 ); //We want to wait for an answer myAddress64 = XBeeAddress64( msb, lsb ); // Serial.print("myadd is now: "); // Serial.println(myAddress64.getLsb()); } return myAddress64; }
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; }
Radio::Radio(): xbee(){ _read_wait = 300; // millis _max_read_waits = 10; _WAITING_delay = 100; _max_connect_tries = 10; _show_debug = 1; _connected = 0; //RECIEVING _response = XBeeResponse(); // Packet to be recieved _rx = ZBRxResponse(); // TRANSMITTING // DH and DL of the recieving Xbee - default is coordinator _addr64 = XBeeAddress64(0x00000000, 0x00000000); _tx = ZBTxRequest(_addr64, payload,sizeof(payload)); _txStatus = ZBTxStatusResponse(); }
/** Transmit a DB request (RSSI, signal strength) by using the 16-bit address **/ void LithneClass::sendDBRequest16( uint16_t _addr16 ) { sendDBRequest( XBeeAddress64(0x0,0x0), _addr16 ); /* !!! Code has been changed, but not tested! RemoteAtCommandRequest db = RemoteAtCommandRequest( _addr16, atDB ); // Send your request xbee.send(db); if( nodeKnown( _addr16 ) ) { uint8_t remoteNode = getNodeId( _addr16 ); for (int i =0; i< numNodes; i++) { if (remoteNode == nodes[i]->getID()) { nodes[i]->openDBRequest(); } } } */ }
void SourceNode::unicastMessageToSink(const Message & mess) { char humanReadableSender[9]; sprintf(humanReadableSender, "%08lX", _firstGatewayToSink); if (_level > 0) { Serial.println("About to send unicast message to" + String(humanReadableSender)); XBeeAddress64 addr = XBeeAddress64( CommonValues::Message::MAC_PREFIX, _firstGatewayToSink ); sendMessage(addr, mess); Serial.println("Done sending message to gateway"); } else { Serial.println("Could not send unicast message. Level not set yet"); } }
/** Sends a string to the specified node **/ void LithneClass::println( String _stringArg ) { /* By default we send to the coordinator */ println( XBeeAddress64(0x00000000, 0x00000000), _stringArg); }
Rx64IoSampleResponse::Rx64IoSampleResponse() : RxIoSampleBaseResponse() { _remoteAddress = XBeeAddress64(); }
/** This function takes care of all processing when a message is received. It is called after Lithne.available(); This section is based on examples in the XBee library by Andrew Rapp**/ void LithneClass::readXbee() { /*DONT EDIT IF YOU DON'T KNOW WHAT YOU'RE DOING!!! */ /* Reads all available serial bytes until a packet is parsed, an error occurs, or the buffer is empty. */ xbee.readPacket(); if( newXBeePacket ) { newXBeePacket = false; } // Set this flag to false; only raise it if we receive a message if (xbee.getResponse().isAvailable()) //Returns a reference to the current response Note: once readPacket is called again this response will be overwritten! { newXBeePacket = true; int responseType = xbee.getResponse().getApiId(); if (responseType == ZB_RX_RESPONSE) { //Call with instance of ZBRxResponse class only if getApiId() == ZB_RX_RESPONSE to populate response. /* Indicate we have received a new message */ newMessage = true; // set flag /* Clear the old received message */ incomingMessage.clearArguments(); xbee.getResponse().getZBRxResponse(rx); /* Retrieve the sender from the packet and store it in the message */ XBeeAddress64 addr64 = rx.getRemoteAddress64(); uint16_t addr16 = rx.getRemoteAddress16(); /* if the 16 bit add shows this is the coordinator, we store the default coordinator address 0x0 0x0 instead of the hardware address */ if (addr16 == 0) { addr64 = XBeeAddress64(0x0, 0x0); } // Serial.println("-----------"); // for( uint16_t i = 0; i <rx.getDataLength(); i++ ) // { // Serial.print( rx.getData(i) ); // Serial.print( " " ); // } // Serial.println(""); // Serial.println("-----------"); incomingMessage.setSender( addr16, addr64 ); /* The scope of the message is stored in the first two bytes of the payload. Here we retrieve this and write it to the incoming message */ incomingMessage.setScope( (rx.getData(Message::SCOPE_MSB) << 8) + rx.getData(Message::SCOPE_LSB) ); /* The function identifier (1 byte) is stored in the third byte of the payload. Here we retrieve this and write it to the incoming message */ incomingMessage.setFunction((rx.getData(Message::FUNCTION_MSB) << 8) + rx.getData(Message::FUNCTION_LSB) ); /* The remainder of the payload contains our arguments. Here we retrieve the number of arguments, by subtracting the first three bytes (which contain the scope and the function ID) and dividing the number of bytes by two, since we send 16-bit values and not 8-bit values (2x 8-bits = 1x 16bits) */ uint16_t numOfBytes = (rx.getDataLength()-Message::MSG_HEADER_SIZE); /* Store the arguments in the incomingMessage */ for( uint16_t i = 0; i < numOfBytes; i++ ) { uint16_t pos = i + Message::MSG_HEADER_SIZE; incomingMessage.addByte( rx.getData(pos) ); } /* Here we always overwrite the 16-bit address. The received address is directly taken from the header information and thus correct. */ Node * senderNode = getNodeBy64( addr64 ); if (senderNode != NULL) { senderNode->setAddress16( addr16 ); } // delete senderNode; /* If the sender(!) of the message got an acknowledgement, this code is executed */ if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) { } /* If not, something strange has happened, because we got the message, but the sender did not receive an acknowledgement */ else { //we got it (obviously) but sender didn't get an ACK } } /* If the packet is indeed an AT_COMMAND_RESPONSE, we write it to the atResponse. */ else if (responseType == AT_COMMAND_RESPONSE) { AtCommandResponse atResponse = AtCommandResponse(); uint32_t atAnswer = 0; xbee.getResponse().getAtCommandResponse( atResponse ); /* If the atResponse is correct (does not return an error code), we can process it. */ if (atResponse.isOk()) { newATMessage = true; if (atResponse.getValueLength() > 0) { for (int i = 0; i < atResponse.getValueLength(); i++) { atAnswer = (atAnswer << 8) + atResponse.getValue()[i]; } } /* If we got a Serial High ('S','H') */ if( atResponse.getCommand()[0] == atSH[0] && atResponse.getCommand()[1] == atSH[1] ) { myAddress64.setMsb( atAnswer ); } /* If we got a Serial LOW ('S','L') */ if( atResponse.getCommand()[0] == atSL[0] && atResponse.getCommand()[1] == atSL[1] ) { myAddress64.setLsb( atAnswer ); } /* If we got my 16-bit address ('M','Y') */ if( atResponse.getCommand()[0] == atMY[0] && atResponse.getCommand()[1] == atMY[1] ) { myAddress16 = atAnswer & 0xFFFF; } /* If we got my 16-bit address ('M','Y') */ if( atResponse.getCommand()[0] == atID[0] && atResponse.getCommand()[1] == atID[1] ) { myPANid = atAnswer & 0xFFFF; } /* If we got my 16-bit address ('M','Y') */ if( atResponse.getCommand()[0] == atAI[0] && atResponse.getCommand()[1] == atAI[1] ) { myAssStat = atAnswer & 0xFFFF; } } else { } } /* NODE JOINING/LEAVING THE NETWORK */ else if( responseType == MODEM_STATUS_RESPONSE ) { xbee.getResponse().getModemStatusResponse(msr); if (msr.getStatus() == ASSOCIATED) { /* A new node has joined the network. Here we add the node if it is not yet known and store the 64-bit and 16-bit address */ // xbee.getResponse().; } else if (msr.getStatus() == DISASSOCIATED) { /* Node leaves the network */ } } /* DB MEASUREMENT RESPONSE */ else if( responseType == REMOTE_AT_COMMAND_RESPONSE ) //REMOTE_AT_COMMAND_RESPONSE { // Serial.println("Received Remote AT Command Response"); xbee.getResponse().getRemoteAtCommandResponse(rATcmd); if (rATcmd.isOk()) { if ( rATcmd.getCommand()[0] == atDB[0] && rATcmd.getCommand()[1] == atDB[1] ) { newRemoteATMessage = true; XBeeAddress64 rAddress64 = rATcmd.getRemoteAddress64(); uint16_t rAddress16 = rATcmd.getRemoteAddress16(); // Serial.print("AT DB from "); // Serial.print(rAddress64.getMsb(), HEX); // Serial.println(rAddress64.getLsb(), HEX); // This is now done at the end of this section //getNodeBy64( rAddress64 )->addDBMeasurement( rATcmd.getValue()[0] ); /* The line above replaces this section for (int i = 0; i < numNodes; i++) { if( nodes[i]->getID() == remoteId ) { nodes[i]->addDBMeasurement( rATcmd.getValue()[0] ); } } */ /* we use this function also to relate 16 bit addresses to 64 bit addresses */ if( !nodeKnown16( rAddress16 ) && nodeKnown64( rAddress64 ) ) { getNodeBy64( rAddress64 )->setAddress16( rAddress16 ); // Serial.println("We know the 64-bit address, but not the 16-bit - now setting"); } /* If we receive a message from the coordinator; it has 16-bit add 0. We related this to the node with 64 bit address 0x0 0x0; the coordinator. */ else if ( rAddress16 == 0 && !nodeKnown16( rAddress16 ) ) { Node * defaultCoordinator = getNodeBy64( XBeeAddress64(0x0,0x0) ); if (defaultCoordinator != NULL) { defaultCoordinator->setAddress16( rAddress16 ); } /*Serial.print("Set 16 bit add of coordinator to "); Serial.print( getNodeBy64( XBeeAddress64(0x0,0x0) )->getAddress16( ) ); Serial.print(" which is Node ID "); Serial.print( getNodeBy64( XBeeAddress64(0x0,0x0) )->getID( ) ); */ } /*Serial.print("Received Remote DB from nodeId "); Serial.print( getNodeBy16( rAddress16 )->getID() ); Serial.print(" (64b: "); Serial.print( rAddress64.getLsb(), HEX ); Serial.print(", 16b: "); Serial.print( rAddress16, DEC ); Serial.print( ") DB val: "); Serial.println( rATcmd.getValue()[0] ); */ Node * remoteNode = getNodeBy16( rAddress16 ); if ( remoteNode != NULL) { remoteNode->addDBMeasurement( rATcmd.getValue()[0] ); } } /* Here we can add other remote AT command responses else if( rATcmd.getCommand()[0] == atSH[0] && rATcmd.getCommand()[1] == atSH[1] ) { } */ } } /* Confirmation on transmitted package. This is received everytime a message is transmitted and has details whether the package is received or not. */ else if (responseType == ZB_TX_STATUS_RESPONSE) { // Serial.println("Received ZBTxStatusResponse"); xbee.getResponse().getZBTxStatusResponse(txStatus); if (txStatus.getDeliveryStatus() == SUCCESS) { /* Als we hier zijn aangekomen, hebben we een berichtje gestuurd, dat is goed aangekomen. Deze response geeft echter alleen een 16-bit address terug en geen 64-bit, dus die kunnen we niet aan een node koppelen. Nu willen we kijken of de 16-bit bekend is in onze node lijst. Als dat zo is, dan hoeven we niets te doen, we kennen deze node. Als dat NIET zo is, dan willen we van deze node ook het 64-bit address opvragen, zodat we 16-bit en 64-bit op kunnen slaan. */ uint16_t rAddress16 = txStatus.getRemoteAddress(); //Stores the 16-bit address setMessageDelivered( true ); if( !nodeKnown16( rAddress16 ) ) { /*Here we send something that requests data from the remote node We can link the two addresses in the returned data (remote at command response)*/ sendDBRequest16( rAddress16 ); } } else { /* the remote XBee did not receive our packet. If this is because the 16 bit address is outdated; we wish to reset that */ uint16_t rAddress16 = txStatus.getRemoteAddress(); //Get the 16-bit address if( nodeKnown16( rAddress16 ) ) // If we know that address, but the message was not received { Node * remoteNode = getNodeBy16( rAddress16 ); if ( remoteNode != NULL) { remoteNode->setAddress16( UNKNOWN_16B ); // We reset the 16 bit address so it will use the 64 bit again } } } } //Something occured that is unknown, or we do not care about else { } } }
void Radio::change_address(uint32_t msb, uint32_t lsb){ _addr64 = XBeeAddress64(msb,lsb); }
uint8_t atSL[] = {'S','L'}; uint8_t atMY[] = {'M','Y'}; uint8_t atID[] = {'I','D'}; uint8_t atAI[] = {'A','I'}; uint8_t atDB[] = {'D','B'}; /* uint16_t */ uint16_t scopes[MAX_SCOPES] = {0}; //Stores the HASH conversions of the groups the node belongs to uint16_t numNodes = 0; //Contains the number of nodes in storage uint16_t last16B = 0xFFFE; //Stores the last 16-bit address we send a message to. uint16_t myAddress16 = UNKNOWN_16B; //Stores own 16-bit address. uint16_t myPANid = UNKNOWN_PAN_ID; //Stores the PAN ID of the network uint16_t myAssStat = UNKNOWN_STATUS; //Stores the association status of the node /* XBeeAddresses */ XBeeAddress64 last64B = XBeeAddress64( 0x0, 0xFFFF); //Stores the last 64-bit address we send a message to. XBeeAddress64 myAddress64 = XBeeAddress64( 0x0, UNKNOWN_64B); /* Create a pointer to an array of nodes with length MAX_NODES */ Node * nodes[MAX_NODES] = {0}; /* allocate static variables */ Message LithneClass::incomingMessage; Message LithneClass::outgoingMessage; bool _outgoingMessageDelivered; XBee LithneClass::xbee; ZBRxResponse LithneClass::rx;
ZBRxResponse::ZBRxResponse() : RxDataResponse() { _remoteAddress64 = XBeeAddress64(); }
long XBee_Addr64_MS_tmp = 0; // long XBee_Addr64_LS_tmp = 0; // int XBee_Addr16; //16-разрядный адрес 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"); }
#include <XBee.h> XBee xbee = XBee(); unsigned char payload[] = { 0,0,0,0,0,0,0,0,0,0}; // SH + SL Address of receiving XBee XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x40bf8a68); ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload)); #define JX A0 #define JY A1 int SPIN = 13; int centerX = 0; int centerY = 0; int lastx = 0; int lasty = 0; int lasts = LOW; int x; int y; int s; void setup() { Serial.begin(9600); xbee.setSerial(Serial); centerX = analogRead(JX); centerY = analogRead(JY);
XbeeApiStream::XbeeApiStream() { xbee = XBee(); coordAddr64 = XBeeAddress64(0x00000000, 0x0000ffff); }
#include "Communication.h" #include "Frame.h" #include <drivers/XBeeConf.h> #include "controller/EventManager/EventManager.h" XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000FFFF); char EVENT_MESSAGE_RECEIVED[] = "XV"; void (*packetListener_)(Message_t*) = NULL; void COM_act(char* eventName, void* event) { comm.receiveAndReact(); } void Communication::setup() { eventManager.registerType(EVENT_MESSAGE_RECEIVED); }; void Communication::receiveAndReact() { reactOnMessage(receive()); }; Message_t* Communication::receive() { xbee.readPacket(); return processMessage_();