I am trying to write data coming in from 2 sensor nodes to a CSV file using Python. Communication is done via Xbee Series 1 in AT mode, with the Xbee End Devices at the 2 sensor nodes passing data to an XBee coordinator connected to my computer. These data would then have to be written to a CSV file. The sensors are connected to an Arduino.
The current problem I am facing, is that sensor data coming in from the 2 nodes are written in this way in the CSV file:
However, I would like the data format to be: i.e. have data from 2 sensor nodes being written into a single row in an instance.
My Python code for writing the CSV files below is:
import serial
import time
import csv
# Arduino
arduino = serial.Serial('COM11', 9600, timeout=1)
time.sleep(3) # wait for Arduino to initialize
while True:
for datastring in arduino:
datastring = datastring.strip() # removes whitespaces and newline char
if datastring:
datasplit = datastring.split(',') # Splits line of txt -> array of strings composed of EA individual sensor data
field1 = datasplit[0]
field2 = datasplit[1]
field3 = datasplit[2]
with open('testing1.csv', 'ab') as csvfile: # 'ab' to remove newline char after each print
field1 = field1.replace('\n',"")
sensor_fields = [field1, field2, field3, time.strftime("%H%M%S")]
writer = csv.writer(csvfile)
writer.writerow(sensor_fields)
May I ask where did I go wrong in my code? Thank you all so much! :)


datastringis still one event from arduino. And it appears to be three columns, but you claim to have more columns than that. Where are these other columns supposed to come from?