两个Arduino之间以api模式进行Xbee通信

问题描述 投票:2回答:1

我有两个Arduino Unos,两个LCD和Xbee s2。 我将两个xbee s2模块连接到每个arduino uno。 我仔细搜索了此实现并进行了尝试。 但是,它没有用。 我感谢您的帮助。

配置:AP = 2协调器API和路由器API

发件人:路由器

 #include <XBee.h>
       #include <SoftwareSerial.h

#include <LiquidCrystal.h>
#define LED 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

void setup(){
  //Serial.begin(9600);
  xSerial.begin(9600);
  xbee.setSerial(xSerial);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, Gang!");
  //Serial.println("xbee start");
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  delay(3000);
}

void loop(){
  ZBTxRequest zbTx;
  uint8_t payload[]={'H', 'E', 'Y', '\0'};
  XBeeAddress64 address=XBeeAddress64(0x13a200, 0x40b450f4);
  zbTx=ZBTxRequest(address, payload, sizeof(payload));
  xbee.send(zbTx);
   if (xbee.readPacket(500)) {              
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        lcd.setCursor(0,1);
        lcd.print("Success");
      } else {
        // the remote XBee did not receive our packet. is it powered on?
      }
    }
  } else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");  
    //nss.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
  }

  delay(1000);
}

接收方:协调员

#include <SoftwareSerial.h>
#include <XBee.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

SoftwareSerial xSerial(3,4);

XBee xbee=XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();

void setup(){
  xSerial.begin(9600);
  xbee.setSerial(xSerial);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, tang!");
  delay(3000);  
}

void loop(){
  xbee.readPacket();
  if (xbee.getResponse().isAvailable()) {
      // got something
      lcd.setCursor(0, 1);
      lcd.print( "MSG");
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        // now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
        lcd.setCursor(0, 1);
        lcd.print(rx.getData(1));

        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            lcd.setCursor(0,1);
            lcd.print( "ACK");

        } else {
            lcd.setCursor(0,1);
            lcd.print("ERROR");
        }
      } else {
        // not something we were expecting
        //flashLed(errorLed, 1, 25);   
       lcd.print("7 print"); 
      }
    } else if (xbee.getResponse().isError()) {
      lcd.print("error");
    }
    delay(3000);
}

啊,我将xbee s2连接到arduino上的xbee pro防护板。 xbee防护罩上没有dline / uart开关。 因此,我使用软件串行库...但是它没有用。

arduino arduino-uno xbee lcd
1个回答
0
投票

我已经用Arduino完成了两个XBee之间的通信,您可以看一下我的编码,然后尝试稍微更改一下编码。

发件人=

 #include <SoftwareSerial.h> #define Dout 2 #define Din 3 #define LED 9 SoftwareSerial XBeeSerial (Dout, Din); void setup() { XBeeSerial.begin(9600); pinMode(LED, OUTPUT); } void loop() { delay(500); XBeeSerial.write("+"); analogWrite(LED,13); delay(3000); XBeeSerial.write("-"); analogWrite(LED,0); delay(500); } 

接收者=

 #include<SoftwareSerial.h> #define LED 9 #define Dout 2 #define Din 3 SoftwareSerial XBeeSerial( Dout, Din); char XBee_message; void setup() { Serial.begin(9600); XBeeSerial.begin(9600); pinMode(LED, OUTPUT); } void loop() { while(XBeeSerial.available()) { XBee_message = XBeeSerial.read(); Serial.print(XBee_message); switch (XBee_message){ case '+': analogWrite(LED, 13); break; case '-': analogWrite(LED, 0); break; default: analogWrite(LED, 0); } } } 

© www.soinside.com 2019 - 2024. All rights reserved.