Python does not record sensor data in csv file

2

I'm doing a python tutorial with arduino, with an exercise that tries to record the data of two sensors: a motion sensor (digital input), and a potentiometer sensor (analog input).

The problem I have is that although when I run the program it creates the .csv file and the table headers (these being defined in the code), but it does not record the data collected by the sensors.

I show the code that I am using:

import csv
import pyfirmata
from time import sleep

port = '/dev/ttyACM0'
board = pyfirmata.Arduino(port)

it = pyfirmata.util.Iterator(board)
it.start()

pirPin = board.get_pin('d:11:i')
a0 = board.get_pin('a:1:i')

with open('SensorDataStore.csv', 'w') as f:

    w = csv.writer(f)
    w.writerow(["Number", "Potentiometer", "Motion sensor"])
    i = 1
    pirData = pirPin.read()
    potData = a0.read()
    while i < 25 :
        sleep(1)
        if pirData is not None:
            i += 1
            print (i)
            row = [i, potData, pirData]
            w.writerow(row)
    print "Done. CSV file is ready!"


board.exit()

The code performs the loop 25 times but does not capture any information from the sensors.

The operating system I use is a Ubuntu Studio 16.04. The file / dev / ttyACM0 has all the permissions.

I have not found anything on the web that can help me or my knowledge in programming are enough to find an alternative.

    
asked by Alphonsus 11.07.2017 в 10:18
source

1 answer

0

I understand that in the pirData, potData variables you have a couple of lists with all the information you need, I recommend you use Pandas to create the csv file

import pandas as pd

# Creas el dataframe 
data = pd.DataFrame(
{'Potentiometer': potData, 
 'Motion sensor': pirData
})

# Puedes borrar cualquier fila donde el valor sea nulo
data.dropna(inplace=True)

# Reinicias el index para que cuente en orden los valores no nulos
df.reset_index(drop=True)

# Si quieres le cambias el nombre
df.index.name = 'Number'
    
answered by 26.05.2018 в 19:45