为什么我看不到使用Arduino串行监视器通过XBees发送的数据?

时间:2014-06-13 22:26:28

标签: c arduino xbee

我使用3个Xbees。 2被配置为路由器AT而另一个被配置为协调器AT。路由器上的代码用于读取LM和3个OneWire DS18B20传感器的温度:

#include <SoftwareSerial.h>

float temp;
int tempPin = 0;

SoftwareSerial xbee(4, 3);

void setup()
{
Serial.begin(9600);
xbee.begin(9600);
}

void loop()
{
delay(1000);
temp = analogRead(tempPin);
temp = temp * 0.48828125;
//Serial.print("TEMPRATURE = ");
//Serial.print(temp);
// Serial.print("*C");
//Serial.println();
//delay(1000);
xbee.print("Temp D: "); xbee.print(temp);
delay(1000);
}

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 8

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress camera1Temp = { 0x28, 0xF6, 0x7C, 0xA0, 0x05, 0x00, 0x00, 0x0E };
DeviceAddress camera2Temp = { 0x28, 0x2A, 0x61, 0xDD, 0x03, 0x00, 0x00, 0xC3 };
DeviceAddress camera3Temp = { 0x28, 0x52, 0x8D, 0x30, 0x05, 0x00, 0x00, 0x04 };

SoftwareSerial xbee(4, 3); // RX, TX

void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(camera1Temp, 10);
sensors.setResolution(camera2Temp, 10);
sensors.setResolution(camera3Temp, 10);

xbee.begin(9600);

}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Eroare citire temperaturi");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
delay(2000);
//Serial.print("Citire temperaturi...\n\r");
sensors.requestTemperatures();

/*delay(1000);

Serial.print("Temperatura in camera 1: ");
printTemperature(camera1Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 2: ");
printTemperature(camera2Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 3: ");
printTemperature(camera3Temp);
*/

float temp1 = sensors.getTempC(camera1Temp);
float temp2 = sensors.getTempC(camera2Temp);
float temp3 = sensors.getTempC(camera3Temp);
/*Serial.print(int(temp1*100));
delay(1000);
Serial.println(int(temp2*100));
delay(1000);
Serial.println(int(temp3*100));
*/

//Serial.print("\n\r\n\r");

xbee.print("Temp A: "); xbee.print(temp1);
delay(100);
xbee.print("Temp B: "); xbee.print(temp2);
delay(100);
xbee.print("Temp C: "); xbee.print(temp3);
delay(100);

}

在XCTU中连接Xbee协调器时,我可以看到接收文本和温度。但是,当将xbee协调器连接到arduino并使用以下代码时,我看不到任何内容:

void setup()

{

Serial.begin(9600);

}

void loop()

{

if (Serial.available()>0)

{

Serial.write(Serial.read());

}

}

在此之后,我想要从4个传感器中获取传入的文本....但是我无法在arduino上看到任何内容。 你能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

在你的Arduino草图中;我认为这样做是不对的:
Serial.write(Serial.read());

从Arduino论坛看到这一点:Help with Serial.Read() getting string.
这个草图可以帮助你:

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
   while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
       if(index < 19) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
   // Now do something with the string (but not using ==)
    for(int i = 0; i < index ; i++){
        Serial.print(inData[i]);
    }
}


在将数据写入串行接口之前,必须先缓冲数据。