简体   繁体   中英

Python- writing sensor data to a file

I'm trying to write a code where I can take in data from an analog sensor and want to write the data to a .txt file. I did some research and wrote this code-

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)
datafile = file.open("temperature.txt", "w")

def ReadChannel(channel):
    adc = spi.xfer2([1, 8+channel <<4, 0])
    data = ((adc[1]& 3) << 8) + adc[2]
    return data

def ConvertVolts(data, places):
    volts = (data*3.3)/float(1023)
    volts = round(volts, places)
    return volts

def ConvertTemp(data, places):
    temp = ((data*200)/float(1023))-50
    temp = round(temp, places)
    return temp

temp_channel = 0
delay = 5

while True:
    temp_level = ReadChannel(temp_channel)
    temp_volts = ConvertVolts(temp_level, 2)
    temp = ConvertTemp(temp_level, 2)

    print"Temperature (deg F): ", temp
    datafile.write(str(temp)+"\n")
    time.sleep(delay)

datafile.close()

But when I run this code, it forms a file "temperature.txt" with no text in it. Could anyone please point out my mistake? If it helps, I have taken some inspiration from the following websites- https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/step10.py http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/ Thanks in advance

I think your file output stream never gets flushed to disk, as you just write to the file but then end the loop with control-c, right?

Maybe try to let the loop run 10 times and close the file then, as a try. Do you get the correct print Temperature outputs?

Why not use the builting logging function?? Then it could look something like this!

import logging

logging.basicConfig(filename=PATHTOFILE, loglevel=logging.INFO)

then you could just:

logging.info("TEXTTOWRITETOTHEFILEHERE")

instead of datafile.write(......) It will append to the file if exists and adding date/time to it. it will also handle the file eg. close() ect. Shouldnt be needing the delay!

change line:

datafile = file.open("temperature.txt", "w")

to this:

datafile = open("temperature.txt", "w")

as it is working for me now. If still not writing to file, check obtaining of sensor data. If temp variable is printed properly, read following please.

For your purpose is better use the with keyword, becouse it includes .close() and even try/finally block (very suitable in looping formula). You will probably want use "a" mode to add data, then "w" mode to rewrite them:

while True:
    #read temp here
    with open("temperature", "a") as datafile:
        datafile.write(temp)

instead of less consistent:

datafile = open("temperature.txt", "w")
while True:
    #temp here
    datafile.write(temp)
datafile.close()

which actually never closes the file...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM