1

I am using a two Arduino setup with two XBee radios. One radio is sending data and the other is receiving data. I am sending three sensor readings following a protocol which uses three bytes for each sensor reading. Byte one is sensor information, byte two is data, and byte three is for error checking. When I run the set up for about 5 minutes I get approximately 47% errors in transmission. When I use the same size output and paste it all into the serial monitor I get 5 errors and all five come with the reading of the last 5 blocks of three bytes. Here is the code to send the data:

        delay(2000);

        // Calcuate weather parameters for serial and web
        calcWeather(); // Go calc all the various sensors

        // send weather out to serial port
        if (prevtemplm34_1 == templm34_1) // 2 in a row must be the same to send
        {
            Serial.write(0);
            Serial.write(templm34_1);
            Serial.write(templm34_1 ^ 0);
        }

        // send weather out to serial port
        if(prevtemplm34_2 == templm34_2) // 2 in a row must be the same to send
        { 
            Serial.write(1);
            Serial.write(templm34_2);
            Serial.write(templm34_2 ^ 1);
        }

        // send weather out to serial port
        if(prevtemplm34_3 == templm34_3) // 2 in a row must be the same to send
        {
            Serial.write(2);
            Serial.write(templm34_3);
            Serial.write(templm34_3 ^ 2);
        }
        delay(2000);   

The code for parsing received data is:

int readData ()
{ 
// There must be at least three available bytes to satisfy the protocol
// Otherwise, wait until three bytes have arrived to read anything
if (Serial.available() >= 3) {

      // Read the next three byte in the serial buffer
      incomingByteOne = Serial.read();
      Serial.write("Byte One: ");
      Serial.write(incomingByteOne);
      Serial.println();
      incomingByteTwo = Serial.read();
      Serial.write("Byte Two: ");
      Serial.write(incomingByteTwo);
      Serial.println();
      incomingByteThree = Serial.read();
      Serial.write("Byte Three: ");
      Serial.write(incomingByteThree);
      Serial.println();

      // XOR the first and second incoming bytes so that the result can be checked against the third error checking byte
      byte xorValue = incomingByteOne ^ incomingByteTwo;
      Serial.write("XOR VALUE: ");
      Serial.write(xorValue);
      Serial.println("Number of Errors: ");
      Serial.println(numberOfErrors);
      Serial.println();
      Serial.println();
      if(xorValue != incomingByteThree) {
        numberOfErrors++; // Add one to the total count of number of errors in transmission
      } 
      else {
        // Mask for retrieving the five rightmost bits from incomingByteOne (00011111)
        byte maskForSensorNumber = 0x1f;

        // Shift incomingByteOne right 5 times in order to obtain the three bits representing the unit number
        int unitNumber = incomingByteOne >> 5;

        // AND incomingByteOne with the mask in order to retrieve only the five righmost bits representing the sensor number
        int sensorNumber = incomingByteOne & maskForSensorNumber;

        // Store the data for the correct sensor on the correct unit
        data[unitNumber][sensorNumber] = incomingByteTwo*1;
     }
}
}

It seems like the error is at least partially with my code. What could I do to reduce errors?

  • You seem to be using the same Serial instance / UART for both the xbee and the output of the sketch, that may be problematic - how exactly do you have things wired?. Additionally you do not seem to have any mechanism to restore synchronization to the transmitter's 3 byte units if that is ever lost - or even not well established to being with. Systems like this often have a header that cannot occur in the data, and look not just for a number of bytes available, but a sequence starting with the required header. – Chris Stratton Oct 18 '16 at 0:37
  • What is the serial speed you are using? – Visual Micro Oct 18 '16 at 1:00
  • @VisualMicro 9600 baud – tpm900 Oct 18 '16 at 1:20
  • 1
    @ChrisStratton I am just using the Xbee on a radio shield. I was thinking my problem was what you suggested, an avalanche affect of one transmission being wrong. – tpm900 Oct 18 '16 at 1:23
  • How many messages are being sent per minute? When connected to the pc are errors in the last packets or at ransom times? Are both xbee at same speed? Broadcast or coordinator setup? Which Arduino IDE version? – Visual Micro Oct 19 '16 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.