-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (127 loc) · 5.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# main.py modified for counting/timing purposes to main_count.py using I2CSensor_count.py
# CREATION DATE: 11/28/2018
# PROGRAM AUTHOR: CJ ([email protected])
# CPU: RASPBERRY PI 3E
# PROGRAM DESCRIPTION:
# THE PROGRAM TAKES READINGS FROM AN IR TEMPERATURE SENSOR
# USING A CUSTOM CLASS CALLED SENSOR INSIDE THE I2CSensor
# PYTHON FILE.
#
# PROGRAM VARIABLES BELOW: SAMPLE_RATE - time the program will
# wait between readings
# DEVICE_ADDRESS - ADDRESS OF I2C DEVICE (hex)
# READING_COUNT - NUMBER OF READINGS THE PROGRAM
# WILL TAKE
#
# on_data: This is a callback function for the Sensor
# class. The input variable is a dictionary
# with the data inside. If there is no callback
# supplied it will print the data inside of the
# class method. The user can take the data outside
# of the callback method for futher processing
#
# device data registers: The program reads data register 0x07 (full word)
# 0x07 Which takes in bytes at address 0x07 and 0x08
#
# loop_forever: This class method does the same as the take_readings
# method, but the reading_count is infinite
#
#
# TO RUN THE FILE: python3 main.py or in Rpi Pixel open programming,
# click on Programming>>python (2 or 3) and select
# Recent Files or search folders. Rerun file: F5
#
#
# to add line numbers: Add extension IDLEX, see: idlex.sourceforge.net
#
# to format print: see: https//www.python-course.eu/python3_formatted_output.php
#
#
# import the class file. This file needs to be in the
# same directory as this main_count.py file
import i2c_sensor
import db
import time
# program variables adjustable
# sample rate in seconds
SAMPLE_RATE = 0.00001
#SAMPLE_RATE = 0.25
# device address on i2c bus
DEVICE_ADDRESS = 0x5a
#Known addresses in MLX90614 sensor memory are:
#observed object temperature data address is 0x07
#Ambient temperature data address is 0x06
#Ambient temperature data can be process same as observed object temperature
#Emissivity value (1.0 or less, use format %$1.3F) address is 0x04
#Emissivity data can be read the same way but needs to be processed as a float.
#the read temperature command is written on line 74 of I2CSensor module
# number of readings you want
# to take
READING_COUNT = 100
# on_data - callback method for the class
# the output can be used for further processing
# here.
def on_data(data_reading):
# this is the callback for the on_data method
# of the class object. The method takes in a
# model.reading class object.
print(data_reading)
# end on_data
if __name__ == '__main__':
# create the class that will interact with the database
db_interface = db.Interface("mysql_config.json")
# instantiates the I2CSensor class. When the data is read
# from the sensor, the on_data method will be called.
sensor = i2c_sensor.Sensor(address=DEVICE_ADDRESS, bus=None,
on_data=on_data, db_interface=db_interface)
# loop forever - blocking call
# use this method if you want constant readings.
# the input to the method is a sample rate in
# seconds.
# ---COMMENT THIS OUT IF YOU WANT A SET NUMBER OF
# READINGS.---
#sensor.loop_forever(SAMPLE_RATE)
# discrete method for a set number of readings
# at a given sample rate
# ---COMMENT THIS OUT IF YOU WANT TO CONTINUOUSLY
# READ DATA.---
print("----------------------------------------------------")
print(" Starting to read data...")
print("----------------------------------------------------")
sensor.take_readings(reading_count=READING_COUNT, sample_rate=SAMPLE_RATE)
#comment out the above line and uncomment the one below
#if you just want the total time printed
print("----------------------------------------------------")
print(" Total ttr %s" % round(sensor.get_total_ttr(), 4))
print("----------------------------------------------------")
print(" Max TTR %s" % sensor.get_max_ttr().ttr)
#print(" Max TTR READING %s" % sensor.get_max_ttr())
print("----------------------------------------------------")
print(" Max Object Temperature: %s C" % sensor.get_max_object_temp().get_data().get_celcius())
#print(" Max Object Temperature READING %s" % sensor.get_max_object_temp())
print("----------------------------------------------------")
# loop and rest while the database
# class is processing readings.
try:
print("waiting for db thread to complete...")
buffer_size1 = db_interface.data_queue.__len__()
print("sql records to be logged: %s" % buffer_size1)
print("logging...")
while db_interface.thread.is_alive():
#print("db thread running..buffer length=%s" % db_interface.data_queue.__len__())
buffer_size2 = db_interface.data_queue.__len__()
if buffer_size2 < buffer_size1:
#print("sql records to be logged: %s" % buffer_size2)
buffer_size1 = buffer_size2
# end if
time.sleep(0.1)
# end while
print("db thread complete.")
except KeyboardInterrupt:
print("terminating db thread...could take up to 2 minutes for sql connection timeout. Output will appear regarding thread termination.")
while db_interface.thread.is_alive():
db_interface.kill()
time.sleep(0.1)
# end while
# end try
print("main thread complete.")