Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Averaging sensor results
I have written the following code in python to monitor three sensor values per minute
My goal is to measure every second and take the average per minute and stores this in the csv file 'data.csv'
What is the best way to achieve this goal?

kind regards,


#importeert alle bibliotheken
import csv
import time
import datetime as dt
import board
from board import SCL, SDA
import busio
from adafruit_seesaw.seesaw import Seesaw
import adafruit_tsl2591
import RPi.GPIO as GPIO
from time import sleep
GPIO.setup(18, GPIO.OUT)

i2c = board.I2C()
i2c_bus = busio.I2C(SCL, SDA)
sensor = adafruit_tsl2591.TSL2591(i2c)
ss = Seesaw(i2c_bus, addr=0x36)

#Initialisatie nummers
date  = float
temp  = float
moist = float
lux   = float
i     = 1

#maakt een csv bestand straks aan met deze kolommen
fieldnames = ["date","temp","moist","lux"]

#opent het csv bestand
with open('/home/pi/Documents/Plant_monitor/data.csv', 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

#zorgt ervoor dat er een maximaal hoeveelheid data gegenereerd kan worden (1dag)
while i < 1440*7:

    with open('/home/pi/Documents/Plant_monitor/data.csv', 'a') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

        info = {
            "date": date,
            "temp": temp,
            "moist": moist,
            "lux": lux
        #leest de sensoren uit en rond waarden af
        lux   = round(sensor.lux,1)
        moist = round(ss.moisture_read(),0)
        temp  = round(ss.get_temp(),1)
        date  = ('%H:%M'))

        #laat de led aangaan als de grond te droog is 
        if moist < 400:
            GPIO.output(18, GPIO.HIGH)
            GPIO.output(18, GPIO.LOW)

        #schrijft de waarden naar de juiste kolommen
        #print de waarden uit om te controleren of het script werkt
        print(date, temp, moist, lux)
    #verhoogt de waarde voor i 
    #tussen elke meting zit 60s (1min)
buran write Jun-17-2021, 02:53 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
How about a loop inside your while loop to sum the sensor values received for each of 60 seconds, then divide all by 60 and write to file?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 632 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 1,027 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 1,171 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 1,150 Jun-24-2018, 08:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020