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
#include
#include
#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(1);
packet.append(CO2); //Achtung auf Variablenzuweisung
packet.append(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("
")
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