我正在开发一个使用XBee S1作为发送器/接收器的项目。这些是我正在使用的硬件:
项目的目的很简单,即打开/关闭LED。这是代码(控制器):
public static void main(String args[]) throws XBeeException,
InterruptedException {
XBee xbee = new XBee();
boolean running = true;
try {
// OPEN SERIAL PORT
xbee.open("COM10", 9600); // String arg is
// Unique to YOUR
// MACHINE!
Scanner input = new Scanner(System.in);
XBeeAddress16 address = new XBeeAddress16(0x22, 0x22);
int[] payload;
payload = new int[1];
System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
payload[0] = (int) input.nextByte();
TxRequest64 request = new TxRequest64(address, payload);
System.out.println("\nXBee request is: " + request.getXBeePacket());
while (running) {
try {
TxStatusResponse response = (TxStatusResponse) xbee
.sendSynchronous(request, 100);
request.setFrameId(xbee.getNextFrameId());
System.out.println("\nXBee request is: " + request.getXBeePacket());
System.out.println("Response received" + response);
if (response.getApiId() != ApiId.TX_STATUS_RESPONSE)
{
System.out.println("Response error");
}
else
{
System.out.println("Response success");
}
}
catch (XBeeTimeoutException e) {
System.out.println("Me ! Unable to send");
}
System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
payload[0] = (int) input.nextByte();
request.setPayload(payload);
if(payload[0] == 2)
running = false;
}
}
finally {
xbee.close();
}
}
这是Arduino(终端设备)的代码:
#include <LiquidCrystal.h>
int LED = 12; //Turn this LED on or off, depending on packet rx'd
int debugLED = 13; //Flash light to indicate rx
int packet; //Packet will be two bytes in length
int analogValue;
int debugValue = 0;
int discard;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(LED, OUTPUT);
pinMode(debugLED, OUTPUT);
Serial.begin(9600);
packet = 0x01;
lcd.begin(16,2);
}
void loop() {
lcd.setCursor(0, 1);
if(Serial.available() >= 16){ //<--Changing the value from 21 to 16 fixed the problem!
debugValue = 0;
debugValue = Serial.read();
if(debugValue == 0x7e){ //Look for starting byte
lcd.clear();
lcd.print(debugValue);
delay(500);
digitalWrite(debugLED, HIGH); //Flash Arduino LED (not the one on digital pin 11)
delay(1000); //to indicate rx'ing data
digitalWrite(debugLED, LOW);
//lcd.print(debugValue);
for(int i = 2; i != 10; i++){ //Discard unused data (see XBee API protocol for more info)
discard = Serial.read();
if (i != 8)
packet = discard;
// print the number of seconds since reset:
if(i == 5)
lcd.setCursor(0,1);
lcd.print(packet);
delay(500);
}
lcd.print(discard);
}
}
if(packet == 0x7e)
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
}
问题是LCD显示屏的输出始终显示相同的输出。怎么会这样?
这些是我从LCD获得的“奇怪”数字:
126 0 6 129 125 49 125 49 125 125 29 29
这些值保持不变,很难将输入值从0更改为1 !!
这是我的Java终端(控制台)的示例输出:
1
XBee request is: 0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x75,error=false,frameId=0x01,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on
0
XBee request is: 0x7e,0x00,0x06,0x01,0x03,0x22,0x22,0x00,0x00,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x74,error=false,frameId=0x02,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on
我确定这是有效载荷值(标有^^^^):
0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
^^^^
但是如何用Arduino获得这个价值?
无论如何,我参考了教程 XBee API Project One 。
答案 0 :(得分:2)
好吧,经过一些搜索后,通过Arduino获取XBee的有效负载值的最佳方法是使用XBee API for Arduino,因为我读了这个网站:wirelessly transmit analog signals using the Arduino XBee API。以下是获取“有效负载”值的代码:
#include<Xbee.h>
int DEBUG = 13;
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
void setup() {
pinMode(DEBUG, OUTPUT);
Serial.begin(9600);
DEBUG = 0;
}
void loop() {
xbee.readPacket();
if(xbee.getResponse().isAvailable()){
lcd.print("Data received");
if(xbee.getResponse().getApiId() == RX_16_RESPONSE){
xbee.getResponse().getRx16Response(rx16);
uint8_t payload = rx16.getData(0);//this is the payload value, easy to get it :D
}
}
}
那是!! :d