1

I'm working on a "WeatherLogger" app that will create a log file every hour/day. This is my code:

import Adafruit_DHT as dht
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
rainsensorpin = "4"
GPIO.setup(rainsensorpin, GPIO.IN)
def log():
        print "[!] Log action executed, processing it..."
        filename = time.strftime('%d%Y-%m-%d %H:%M:%S')
        humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
        temp = "{:0.1f}*C".format(temperature)
        hum = "{:0.1f}%".format(humidity)
        state = GPIO.input(rainsensorpin)
        data = "Temp: ", temp, " Hum: ", hum, " Rain: ", state, " Date+Time: ", filename
        os.chdir("logs")
        os.mknod(filename)
        print "[+] Successfully created file: ", filename

Is there any way to execute this code every hour, or every day?

2 Answers2

1

if you are on linux you could use cron with something like 5 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php, if you are on windows you use this link.

HTH

  • he have GPIO code, i bet it's either embeded linux or some other *nix embeded os. it will surprise me if it's windows. also it's not a good idea to paste a pure link. links expire. it might be gone years later. – Jason Hu Jul 01 '17 at 16:41
0

You can use apscheduler. There are 7 types of schedulers that you can learn about them here. Background scheduler is one type that I prefer which can be used by the following lines:

from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.add_job(log, 'cron', hours='0-24', id='my_job_id')
scheduler.start()
Amir H
  • 115
  • 12