r/pythonhelp • • Jan 01 '19

schedule module

I'm a beginner. Thanks in advance. This doesn't work. There doesn't seem to be a way to pass arguments to the def logData using schedule.every

It gives the error.

main()

File "/home/pi/Sensors_Database/logDHT11schedule.py", line 45, in main

schedule.every(6).seconds.do(logData, temp, hum)

NameError: name 'temp' is not defined

>>>

I could find no examples of how to execute this, I'm not sure the schedule.every should go into def main():

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#

# logDHT11.py

# edited for DHT11, tk

# Developed by Marcelo Rovai, MJRoBot.org @ 10Jan18

#

# Capture data from a DHT11 sensor and save it on a database

import time

import schedule

import sqlite3

import Adafruit_DHT

dbname='sensorsData.db'

# sampleFreq = 60 # time in seconds

# get data from DHT sensor

def getDHTdata():

DHT11Sensor = Adafruit_DHT.DHT11

DHTpin = 16

hum, temp = Adafruit_DHT.read_retry(DHT11Sensor, DHTpin)

if hum is not None and temp is not None:

hum = round(hum)

temp = round(temp, 1)

return temp, hum

# log sensor data on database

def logData (temp, hum):

conn=sqlite3.connect(dbname)

curs=conn.cursor()

curs.execute("INSERT INTO DHT_data values(datetime('now'), (?), (?))", (temp, hum))

conn.commit()

conn.close()

return

# main function

def main():

schedule.every(5).seconds.do(getDHTdata)

schedule.every(6).seconds.do(logData, temp, hum)

print("I'm working...")

while True:

schedule.run_pending()

time.sleep(1)

# ------------ Execute program

main()

3 Upvotes

1 comment sorted by

1

u/ryanrocket Jan 04 '19

make sure to define the input from your DHT11 sensor as temp and humidity first. it seems as though you have not defined it. Look up an Adafruit tutorial on Getting & Setting your DHT11 inputs and how to provide a delay between grabs (like 5 mins between readings).