I am modifying a code snippet from a udemy course, to fit my goal. My goal - > being able to store the timestamps in a sqlite3 database corresponding to the actual time the data was retrieved. The original code excerpt to update the database from the RaspberryPiFullStack_Raspbian github repository is given below;
def log_values(sensor_id, temp, hum):
conn=sqlite3.connect('/var/www/lab_app/lab_app.db') #It is important to provide an
#absolute path to the database
#file, otherwise Cron won't be
#able to find it!
curs=conn.cursor()
curs.execute("""INSERT INTO temperatures values(datetime(CURRENT_TIMESTAMP, 'localtime'),
(?), (?))""", (sensor_id,temp))
curs.execute("""INSERT INTO humidities values(datetime(CURRENT_TIMESTAMP, 'localtime'),
(?), (?))""", (sensor_id,hum))
conn.commit()
conn.close()
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
temperature = temperature * 9/5.0 + 32
As you can see, when the log_values function is called, only the instantaneous temperature and humidity value from the sensor, with the sqlite datetime function to timestamp when the reading was taken.
However, I have to process the values before i input them into the database. i have a function called "getAEEG(raw_data)", that accepts a list of raw data and returns a list of processed data. This function works fine independently. I have created an empty list for the temperature, humidity, and time that runs approximately for 10 seconds to store data onto the list. At the end of 10 seconds, i wish to store the values onto the database, with a for loop that iterates throughout the list to put them into the database. I have tried multiple iterations with no success, This is my first attempt coding in python and attempting to do projects with servers and databases, so i am reaching out to seek help diagnosing what's wrong with my code below.
t_data = []
ch1_data = []
ch2_data = []
ch1_aeeg = []
ch2_aeeg = []
def log_values(time_now,sensor_id, temp, hum):
conn=sqlite3.connect('/var/www/lab_app/lab_app.db') #It is important to provide an
#absolute path to the database
#file, otherwise Cron won't be
#able to find it!
# For the time-related code (record timestamps and time-date calculations) to work
# correctly, it is important to ensure that your Raspberry Pi is set to UTC.
# This is done by default!
# In general, servers are assumed to be in UTC.
curs=conn.cursor()
curs.execute("""INSERT INTO temperatures values((?),
(?), (?))""", (time_now,sensor_id,temp))
curs.execute("""INSERT INTO humidities values((?),
(?), (?))""", (time_now,sensor_id,hum))
conn.commit()
conn.close()
def getAEEG(raw_eeg):
fs = 240.0 #typical sampling frequency for EEG
f0 = 60.0 #freq to remove by notch
Q = 30.0 #quality factor
#notch filter to remove 60 Hz
b_n, a_n = signal.iirnotch(f0/(fs/2), Q)
notch_eeg = signal.lfilter(b_n, a_n, raw_eeg)
#rectify eeg signal and convert V to uV
rectify_eeg = abs(notch_eeg)
#envelope filtered signal with low-pass 5th order Butterworth filter
b,a = signal.butter(5, 0.021)
envelope_eeg = signal.filtfilt(b,a, rectify_eeg)
#aeeg output with added gain of 1.631
aeeg_output = 1.631*(envelope_eeg)
return aeeg_output
# setting up the 10 second condition for the while loop
time_now_counter = datetime.datetime.now()
tdelta = timedelta(seconds = +10)
time_stop = time_now_counter + tdelta
while time_now_counter < time_stop:
time_now = datetime.datetime.now()
t_data.append(time_now.strftime('%Y-%m-%d %H:%M:%S'))
ch1_data.append(chan.voltage)
ch2_data.append(random.randint(1,100))
time.sleep(0.5)
time_now_counter = datetime.datetime.now()
ch1_aeeg = getAEEG(ch1_data)
ch2_aeeg = getAEEG(ch2_data)
for x in range(len(ch1_aeeg)):
time_now = t_data[x]
temperature = ch1_aeeg[x]
humidity = ch2_aeeg[x]
log_values(time_now,"1", temperature, humidity)
P.S. i also have scheduled a cron job, that is supposed to run the script every 15 seconds, since i thought that between two consecutive scheduled runs, the script will have to run for 10 seconds to get enough data with the remaing 5 seconds being buffer time.
is my rationale valid?