Home > Enterprise >  Read input data to csv file
Read input data to csv file

Time:11-29

I am doing a BeagleBone project that messure temperature from a sensor. I want to save the data into a csv file. I am new to programming and Python and I am little lost. I have been google searching for a while now and found little that can help me. So I wanted to ask instead about this. How to I get new data written into the csv file every secound?

import Adafruit_BBIO.ADC as ADC
import time
import csv

sensor_pin = 'P9_40'

ADC.setup()

while True:
    reading = ADC.read(sensor_pin)
    millivolts = reading * 1800  # 1.8V reference = 1800 mV
    temp_c = (millivolts - 500) / 10
    temp_f = (temp_c * 9/5)   32
    print('mv=%d C=%d F=%d' % (millivolts, temp_c, temp_f))

    time.sleep(1)

    # field names 
    fields = ['Milivolts', 'Celsius'] 


    # data rows of csv file
    rows = [ [millivolts,"|", temp_c]]

    # name of csv file
    filename = "textfile.csv"

    # writing to csv file
    with open(filename, 'w') as csvfile:
        # creating a csv writer object
        csvwriter = csv.writer(csvfile)

        # writing the fields 
        csvwriter.writerow(fields)

        # writing the data rows
        csvwriter.writerows(rows)

CodePudding user response:

One fix to apply is to open the file in append mode so that the file content is not overwritten at every step; just change the 'w' to 'a' in this line:

with open(filename, 'a') as csvfile:

Please note that without any output and/or description of the problem you are encountering, it will be difficult to help you more.

CodePudding user response:

Honestly, i don't see much wrong with the code other than the order.

The way you wrote it, every single time you are iterating the loop, you are opening the file in write mode, which erases the file. So if im guessing right, you probably only have one row in the CSV.

The below is just a re-ordering and it should work. Note that i put the fields in before the loop because you only want it once.

Keep in mind that every time you run the program it will start a fresh csv. If you want it to keep a history regardless of interrupts/ restarts, just remove the fields, and use open(filename, 'a') instead.

Since this is data stuff, if you are going for a long time, you might want to include the time.time() as part of each row. That way you can see downtime ect.

import Adafruit_BBIO.ADC as ADC
import time
import csv

sensor_pin = 'P9_40'

ADC.setup()

# name of csv file
filename = "textfile.csv"
with open(filename, 'w') as csvfile:
    # creating a csv writer object
    csvwriter = csv.writer(csvfile)
    
    # writing the fields 
    csvwriter.writerow(['Milivolts', 'Celsius'])

    while True:
        reading = ADC.read(sensor_pin)
        millivolts = reading * 1800  # 1.8V reference = 1800 mV
        temp_c = (millivolts - 500) / 10
        temp_f = (temp_c * 9/5)   32

        # writing the data rows
        rows = [ [millivolts,"|", temp_c]]
        csvwriter.writerows(rows)
        
        time.sleep(1)
  • Related