Please, i am currently working on a Zigbee-based wireless sensor network system using the Xbee series 2 RF module. I am trying to display sensor readings using an LPC1768 mbed microcontroller but realized that most Xbee projects are arduino based. i got to a programming line where i need to discard certain bytes which i do not require. Can anyone help on how i achieve this with the mbed?
Please find below the program in arduino with some comments. i am more interested in ''for (int i = 0; i<18; i++) {
byte discard = Serial.read();''
make sure everything we need is in the buffer
if (Serial.available() >= 21) {
look for the start byte
if (Serial.read() == 0x7E) {
blink debug LED to indicate when data is received
digitalWrite(debugLED, HIGH);
delay(10);
digitalWrite(debugLED, LOW);
read the variables that we're not using out of the buffer
for (int i = 0; i<18; i++) {
byte discard = Serial.read();
}
int analogHigh = Serial.read();
int analogLow = Serial.read();
analogValue = analogLow + (analogHigh * 256);
}
}
We start by seeing if there’s potentially a full frame of information waiting in the buffer.
Because we set the sensor radio’s configuration ourselves, we already know how long
a frame will be: 22 bytes, or in this case 21 because we ignore the checksum. So we
begin by checking the Arduino’s serial buffer to see if there’s potentially a full frame of
data waiting for us:
make sure everything we need is in the buffer
if (Serial.available() >= 21) {
and then we read a byte in to see if it is a start byte of 0x7E. If it isn’t we’ll skip to the
next byte until we do find a 0x7E and know we’re most likely at the beginning of a data
frame:
look for the start byte
if (Serial.read() == 0x7E) {
When we know we’re at the start of a data frame, we can skip over most of the frame
contents. Length we already know. Same goes for frame type; we’re only sending I/O
sample frames so we can assume that whatever we receive will be the reply to one. The
source address will always be the paired sensor, so that information can be ignored as
well in this case. The receive options, number of samples, and all the channel mask
information will never change in this project so we merrily read in all those bytes and
throw them away:
read the variables that we're not using out of the buffer
for (int i = 0; i<18; i++) {
byte discard = Serial.read();
}
Finally we get to the bytes we do care about, the analog sensor readings from the remote
photocell. The two bytes (MSB and LSB) get read into two variables:
int analogHigh = Serial.read();
int analogLow = Serial.read();
...and then pasted together arithmetically to reconstitute the original 10-bit sensor
reading:
analogValue = analogLow + (analogHigh * 256);
Please, i am currently working on a Zigbee-based wireless sensor network system using the Xbee series 2 RF module. I am trying to display sensor readings using an LPC1768 mbed microcontroller but realized that most Xbee projects are arduino based. i got to a programming line where i need to discard certain bytes which i do not require. Can anyone help on how i achieve this with the mbed?
Please find below the program in arduino with some comments. i am more interested in ''for (int i = 0; i<18; i++) { byte discard = Serial.read();''
make sure everything we need is in the buffer if (Serial.available() >= 21) { look for the start byte if (Serial.read() == 0x7E) { blink debug LED to indicate when data is received digitalWrite(debugLED, HIGH); delay(10); digitalWrite(debugLED, LOW); read the variables that we're not using out of the buffer for (int i = 0; i<18; i++) { byte discard = Serial.read(); } int analogHigh = Serial.read(); int analogLow = Serial.read(); analogValue = analogLow + (analogHigh * 256); } } We start by seeing if there’s potentially a full frame of information waiting in the buffer. Because we set the sensor radio’s configuration ourselves, we already know how long a frame will be: 22 bytes, or in this case 21 because we ignore the checksum. So we begin by checking the Arduino’s serial buffer to see if there’s potentially a full frame of data waiting for us: make sure everything we need is in the buffer if (Serial.available() >= 21) { and then we read a byte in to see if it is a start byte of 0x7E. If it isn’t we’ll skip to the next byte until we do find a 0x7E and know we’re most likely at the beginning of a data frame: look for the start byte if (Serial.read() == 0x7E) { When we know we’re at the start of a data frame, we can skip over most of the frame contents. Length we already know. Same goes for frame type; we’re only sending I/O sample frames so we can assume that whatever we receive will be the reply to one. The source address will always be the paired sensor, so that information can be ignored as well in this case. The receive options, number of samples, and all the channel mask information will never change in this project so we merrily read in all those bytes and throw them away: read the variables that we're not using out of the buffer for (int i = 0; i<18; i++) { byte discard = Serial.read(); } Finally we get to the bytes we do care about, the analog sensor readings from the remote photocell. The two bytes (MSB and LSB) get read into two variables: int analogHigh = Serial.read(); int analogLow = Serial.read(); ...and then pasted together arithmetically to reconstitute the original 10-bit sensor reading: analogValue = analogLow + (analogHigh * 256);