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?

1
  • Put it into scheduler/cron, depending on your system? Commented Jul 1, 2017 at 16:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.