Dec-01-2022, 05:13 AM
(This post was last modified: Dec-01-2022, 06:14 AM by Yoriz.
Edit Reason: Added code tags
)
I have most of the code written. I want to add the first two columns of the csv file to be date and time. And, have DATE and TIME in the headers. When I comment out the date and time, it exports everything uniformly, just under the wrong columns.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 2 23:17:11 2022
@author: eh5713
"""
#script to communicate with arduino and load serial monitor data
#Your sketch/code should be already loaded/flashed to the board prior to running this script
#However communication port should not be occupied
#I usually close Arduino software after flashing the code to be on the safe side
#we need serial module to communicate with the arduino
#csv module is needed because we will be logging sensor data to a csv file
import serial
import csv
from datetime import date,datetime
arduino_port="COM3" #serial port of arduino
#Port that arduino uses - for my mac it is usbmodem.., on windows it will be COM3-4
baud = 9600
#defining the file we will use for data loading
#creates a file named by the user's input
filename = input("Input the Filename: ")
f_extns = filename.split(".csv")
print ("The extension of the file is : " + repr(f_extns[-1]))
#filename = "data_loaded.csv"
# open serial port
ser = serial.Serial(arduino_port, baud)
#print out the active port
print("Connected to Arduino port:" + arduino_port)
#we create 'data_loaded.csv file'
file = open(filename,'w')
print("File created!")
#creating empty lists for data to be taken
#this is a loop that performs 20 consecutive readings
samples = 20
print_labels = False
line = 0
sensor_data = []
#dates = []
#instance = []
while line <= samples:
getData=ser.readline()
dataString = getData.decode('utf-8')
data = dataString[0:][:-2]
print(data)
# datetime object containing current date and time
#data_taken = datetime.now()
#data_taken_date=data_taken.strftime('%m/%d/%Y')
#dates.append(data_taken_date)
#data_taken_time=data_taken.strftime('%H:%M:%S')
#instance.append(data_taken_time)
readings = data.split(",")
print(readings)
sensor_data.append(readings)
print(sensor_data)
#overall=list(zip(dates,instance,sensor_data))
line = line + 1
with open(filename, 'w',encoding = 'UTF8', newline= '') as f:
writer = csv.writer(f)
writer.writerows(date,sensor_data)
#closing the port
file.close()
ser.close()
Attached Files
