Home/Support/Support Forum/Xbee S2C: Read data partly not correct
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Xbee S2C: Read data partly not correct

0 votes
Hello,
an Arduino reads data from a CO2-sensor. The data are sent by an XBee S2C (Router) connected to the Arduino by sparkfun shield to another XBee S2C connected to the laptop by USB-explorer. The data are read by Python.

Arduino code:
´#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>
#include "binary.h"

#include "RunningAverage.h"
#include "histogram.h"


// Definition Moving Average

RunningAverage myRA(5);
int samples = 0;
int meas_CO2;
int meas_CO2_old;
int difference = 0;

// Definitionen Xbee
XBeeWithCallbacks xbee;
AltSoftSerial SoftSerial;

#define DebugSerial Serial
#define XBeeSerial SoftSerial
unsigned long last_tx_time = 0;

void setup() {
meas_CO2 = 0;
meas_CO2_old = 0;

// Setup debug serial output
DebugSerial.begin(115200);
DebugSerial.println(F("Starting..."));

// Setup XBee serial communication
XBeeSerial.begin(9600);
xbee.begin(XBeeSerial);
delay(1);

// Let all responses be printed
xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&DebugSerial);
xbee.onResponse(printResponseCb, (uintptr_t)(Print*)&DebugSerial);

// Send a "VR" command to retrieve firmware version
AtCommandRequest req((uint8_t*)"VR");
xbee.send(req);

// setup co2
// Set the default voltage of the reference voltage
analogReference(DEFAULT);
}

void loop() {
// Check the serial port to see if there is a new packet available
xbee.loop();
// Send a packet every 10 seconds
if (millis() - last_tx_time > 10000) {
last_tx_time = millis();

// Sensor read
int meas_CO2 = readCO2();
Serial.println("meas_CO2");

//MAV-Filter
myRA.addValue(meas_CO2);
samples++;
Serial.print("new value: ");
Serial.println(meas_CO2);
Serial.print("Running Average: ");
Serial.println(myRA.getAverage(), 3);

Serial.print("Difference: ");
difference = meas_CO2 - meas_CO2_old;
Serial.println(difference);
Serial.print ("Element 0: ");
Serial.println (meas_CO2);
Serial.print ("Element 1: ");
Serial.println (meas_CO2_old);

meas_CO2_old = meas_CO2;

sendPacket(meas_CO2, difference); }
}


void sendPacket(float CO2, float Filter) {
// Prepare the Zigbee Transmit Request API packet
ZBTxRequest txRequest;
txRequest.setAddress64(0x0000000000000000);

AllocBuffer<9> packet;
packet.append<uint8_t>(1);
packet.append<int16_t>(CO2); //Achtung auf Variablenzuweisung
packet.append<int16_t>(Filter);
txRequest.setPayload(packet.head, packet.len());
xbee.send(txRequest);
}



int readCO2(){

const int sensorPin = A0;
float concentration;

//Read voltage
delay(30000);
int sensorValue = analogRead(sensorPin);
Serial.print(sensorValue);

// The analog signal is converted to a voltage
float voltage = sensorValue*(5000/1024.0);
if(voltage == 0)
{
Serial.println("Fault");
}
else if(voltage < 400)
{
Serial.println("preheating");
}
else
{
int voltage_diference=voltage-400;
concentration=voltage_diference*50.0/16.0;
Serial.println(concentration);
}

return sensorValue;
}
´
Python Code:

´import serial
import time
import types


# port = '/dev/ttyUSB0' #linux
port = 'COM4'
baudrate = 9600

print 'open ' + port
xbee = serial.Serial(port, baudrate)

print 'waiting incoming message...'
while True:
try:
d = open("read_CO2_test_hex1.txt", 'a')
out = ''
while xbee.inWaiting() > 0:
out += xbee.read(1)

if out != '' and len(out)==23:
time.sleep(1)


print out
print out.encode('hex')
outhex = out.encode('hex')


print len(out)

outhex2 = ' '.join(map(lambda x: x.encode('hex'), out))
print outhex2

lt = time.localtime()
messzeit = time.strftime("%d.%m.%Y %H:%M:%S", lt)
print messzeit
d.write(messzeit)
d.write(";")
d.write(out[19].encode('hex'))
d.write(out[18].encode('hex'))
d.write(";")
d.write(out[21].encode('hex'))
d.write(out[20].encode('hex'))
d.write(";")
d.write(outhex)
d.write("\n")
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
xbee.close()
raise

´
I have done a measurement for one night and I partly get not the complete XBee-data:
´13.05.2017 23:06:28;7e007d3190007d33a200415490170c960101a700fbff39;7e 00 7d 31 90 00 7d 33 a2 00 41 54 90 17 0c 96 01 01 a7 00 fb ff 39
13.05.2017 23:06:57;7e007d3190007d33a200415490;7e 00 7d 31 90 00 7d 33 a2 00 41 54 90
13.05.2017 23:06:59;170c960101a600ffff36;17 0c 96 01 01 a6 00 ff ff 36
13.05.2017 23:07:27;7e007d3190007d33a200415490170c960101a700010032;7e 00 7d 31 90 00 7d 33 a2 00 41 54 90 17 0c 96 01 01 a7 00 01 00 32 ´

I have tried to change the delay-times, but the incomplete data still occurs.
Therefore I would be happy to get a hint what is going wrong, because I'm a beginner with XBee
Thank you very much for the support.
Regards Daniel
asked May 17, 2017 in Python by bergiel New to the Community (2 points)

Please log in or register to answer this question.

2 Answers

0 votes
Try using CTS flow control and hold the XBee module off till you are read to read the serial port.
answered May 17, 2017 by mvut Veteran of the Digi Community (15,323 points)
0 votes
Thank you very much for the answer. I would be happy to know how to do this in Python 2.7.
I my Arduino code it was necessary to send the data in an other format:

AllocBuffer<15> packet;
packet.append<uint8_t>(1);
packet.append<int16_t>(CO2);
packet.append<float>(CO2_filtered);
packet.append<float>(CO2_difference_MAV);
txRequest.setPayload(packet.head, packet.len());
// send packet
xbee.send(txRequest);

With my Python Code I get the data with different packet lengths and the packets with the additional byte (e.g. 10:55:23 and 10:55:53) have wrong results from technical point of view.

29.05.2017 10:54:22;7e001790007d33a200415490170c9601017c000000fe4200cdcc3e47
29.05.2017 10:54:53;7e001790007d33a200415490170c9601019400666600430033b33f12
29.05.2017 10:55:23;7e001790007d33a200415490170c9601017d5d00cdcc004300cecc3ea9
29.05.2017 10:55:53;7e001790007d33a200415490170c9601017d5d003333014300cccc3edd

Do you have any idea what is going wrong?
Thank you very much for your support.
Regards Daniel
answered Jun 5, 2017 by bergiel New to the Community (2 points)
...