Hi, i just would like to know how to write a coding that would able me to store the result in a period of time to a text or excel file?i have attached the coding i been trying to do.thanks im now to python codes.
import spidev
import datetime
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
# Open up SPI bus
spi = spidev.SpiDev()
spi.open(0,1)
# Initialize Sensor
SoilSensor = 5
Pump = 22
sleepTime = 2
# Setup the pin that pump connected to
GPIO.setup(Pump, GPIO.OUT)
def getReading(channel):
# Get Raw Data from chip
rawData = spi.xfer([1, (8 + channel) << 4, 0])
# Process Data to Understandable
processedData = ((rawData[1]&3) <<8) + rawData[2]
return processedData
def convertVoltage(bitValue, decimalPlaces=2):
voltage = (bitValue * 3.3) / float(1023)
voltage = round(voltage, decimalPlaces)
return voltage
try:
while True:
delta = datetime.timedelta(minutes=30)
next_time = datetime.datetime.now()
dt = datetime.datetime.now()
SoilData = getReading(SoilSensor)
SoilVoltage = convertVoltage(SoilData)
#Print Values
print("Soil Moisture bitValue = {} ; Voltage = {} V".format(SoilData, SoilVoltage))
sleep(sleepTime)
#Save Result Every One Hour
if dt > next_time:
file = open("/home/pi/FYP/result/result.txt", "w")
file.write("{};Soil Moisture bitValue = {} ; Voltage = {} V\n".format(strftime("%Y-%m-%d %H:%M:%S"),SoilData, SoilVoltage))
file.close()
next_time = dt + delta
if SoilData < 40:
GPIO.output (Pump, True)
else:
GPIO.output (Pump, False)
finally:
# Reset the GPIO Pins to a safe state
GPIO.output (Pump, False)
GPIO.cleanup()