forked from Cliftonz/CSGTempuratureProbes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTempClient.py
93 lines (56 loc) · 2.68 KB
/
TempClient.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
# -*- coding: utf-8 -*-
"""
Created on Sat May 18 21:24:52 2019
@author: ZAC16
"""
import serial
import MySQLdb as db
import datetime
#all tempuratures will be in forinheit
average = 0.00 # This holds the sum of the data points taken.
arduinoData = serial.Serial('/dev/ttyACM0', 9600) #Creating our serial object named arduinoData
DataSent = True # We do not want to send blank data to the database
AverageList = [] # List to store the averages, particularly if the DB goes down.
cnt = 0 # This is the number of data points taken before computing average and sent to the database.
now = datetime.datetime.now() # Get current time
print("Starting countdown to 15 mark after the hour...")
while int(now.strftime("%M")) % 15 != 0:# Do nothing until the 15 minute mark is hit
now = datetime.datetime.now()
if(arduinoData.inWaiting()!=0):
arduinoData.readline()
while True: # While loop that loops forever
now = datetime.datetime.now() # Get current time
if int(now.strftime("%M")) % 15 == 0 and DataSent is False:
DataSent = True
AverageList.append((datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), float(average / cnt)) )
try:
conn = db.connect(host="192.168.1.249",port=3306,
user="pi#",
passwd="passwd",
db="Temps111A")
c = conn.cursor()
# For each item in list do: insert data or insert time & data
for item in AverageList:
c.execute('''
INSERT INTO Temps111A.P# (ts,tempurature)
VALUES
(%f,%.2f)
''' % (item[0],item[1]))
AverageList.clear() # remove item put into the database
conn.commit()
print("Successfully sent data")
conn.close()
except Exception as e:
print(e);
print("Could not insert data to DB, Storing date and time...")
finally:
average = 0.0
cnt = 0
elif(int(now.strftime("%M")) % 15 == 1 and DataSent is True):
DataSent = False
elif(arduinoData.inWaiting()==0):
pass# Do Nothing
else:
arduinobyte = arduinoData.readline() #read the line of text from the serial port
average += float(arduinobyte.decode('utf-8'))
cnt += 1