lundi 2 décembre 2013

Paramétrer XBee S1 pour échange de donnée issue de capteurs

Ci dessous, un extrait du livre de Rob Falludi indispensable pour paramétrer nos XBee avec un coordonateur et 1 end device.



14.4 Sending Sensor Data Between XBees

Problem
You want to send the status of digital and analog pins or control pins based on commands received from XBee.

Solution
Hook one of the XBees (the transmitting XBee) up to an analog sensor and configure it to read the sensor and transmit the value periodically. Connect the Arduino to an XBee (the receiving XBee) configured in API mode and read the value of the API frames that it receives from the other XBee.

Discussion
XBees have a built-in analog-to-digital converter (ADC) that can be polled on a regular basis. The XBee can be configured to transmit the values (between 0 and 1023) to other XBees in the network. The configuration and code differ quite a bit between Series 2 and Series 1 XBees.

Series 1 XBees
Using a terminal program, send the following configuration commands to the transmitting XBee:
ATRE
ATMY1234
ATDL5678
ATDH0
ATID0
ATD02
ATIR64
ATWR
Next, send the following configuration commands to the receiving XBee:
ATRE
ATMY5678
ATDL1234
ATDH0
ATID0
ATWR

Both XBees
ATRE resets the XBee to factory defaults. The ATMY command sets the identifier for an XBee. ATDL and ATDH set the low byte and the high byte of the destination XBee.
ATID sets the network ID (it needs to be the same for XBees to talk to one another).
ATWR saves the settings into the XBee so that it remembers the settings even if you power it down and back up.

Transmitting XBee
ATD02 configures pin 20 (analog or digital input 0) as an analog input; ATIR64 tells
the XBee to sample every 100 (64 hex) milliseconds and send the value to the XBee
specified by ATDL and ATDH.

Wire up the transmitting XBee to the sensor, as shown in Figure 14-8.


Figure 14-8. Series 1 XBee connected to an analog sensor

Next, load the following sketch onto the Arduino, and wire the transmitting XBee to the Arduino. If you need to reprogram the Arduino, disconnect it from the XBee first:


/*
XBeeAnalogReceiveSeries1
Read an analog value from an XBee API frame and set the brightness
of an LED accordingly.
*/
const int ledPin = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
configureRadio(); // check the return value if you need error handling
}
boolean configureRadio() {
// put the radio in command mode:
Serial.flush();
Serial.print("+++");
delay(100);
String ok_response = "OK\r"; // the response we expect.
// Read the text of the response into the response variable
String response = String("");
while (response.length() < ok_response.length()) {
if (Serial.available() > 0) {
response += (char) Serial.read();
}
}
// If we got the right response, configure the radio and return true.
if (response.equals(ok_response)) {
Serial.print("ATAP1\r"); // Enter API mode
delay(100);
Serial.print("ATCN\r"); // back to data mode
return true;
} else {
return false; // This indicates the response was incorrect.
}
}
void loop() {
if (Serial.available() >= 14) { // Wait until we have a mouthful of data
if (Serial.read() == 0x7E) { // Start delimiter of a frame
// Skip over the bytes in the API frame we don't care about
for (int i = 0; i < 10; i++) {
Serial.read();
}
// The next two bytes are the high and low bytes of the sensor reading
int analogHigh = Serial.read();
int analogLow = Serial.read();
int analogValue = analogLow + (analogHigh * 256);
// Scale the brightness to the Arduino PWM range
int brightness = map(analogValue, 0, 1023, 0, 255);
// Light the LED
analogWrite(ledPin, brightness);
}
}
}

1 commentaire:

  1. I hook up the 2 xbee series 1, 1 was programmed as transmitter, and 1 was programmed as receiver. The Arduino Sketch loaded without an errors. The response I get is +++ATAP1ATCN, LED9 on the receiver has never responded to the 10K wipe on the transmitter

    RépondreSupprimer