-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompressorAuto.py
170 lines (143 loc) · 5.18 KB
/
CompressorAuto.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer
import sys
import os
#from datetime import datetime
import datetime
import glob
import MySQLdb
from time import strftime
import time # time used for delays
import httplib, urllib # http and url libs used for HTTP $
import socket
import json
import ctypes
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(36,gpio.OUT)
gpio.output(36,0)
#############################################################################################
server = "data.sparkfun.com" # base URL of your feed
publicKey = "9b1g78xoMjs8787z3OwZ" #"GEGLXdO3dnHDKar5x2xW"
privateKey = "xv9R17XkDntnYnYr259q" #"Nnlxo7DK7Vij456RVJVD"
fields = ["aux_temp","bat_temp","comp_curr","hp","lp","milk_temp"] # Your feed's data fields
# from pymodbus.client.sync import ModbusSerialClient as ModbusClient
client=ModbusClient(method='rtu',port='/dev/ttyUSB0',baudrate=9600,timeout=1,parity='N')
client.connect()
db = MySQLdb.connect(host="localhost", user="root",passwd="root", db="temp_database")
cur = db.cursor()
response=client.read_holding_registers(0004,6,unit=1) #(starting addr, no of registers to be read, slave addr)
responseFault=client.read_holding_registers(0022,14,unit=1)
with open('/var/www/html/AutomationProject/file2.json')as f:
data = json.loads(f.read())
print data['serial']
serialNum = data['serial']
######################### Read MOD-BUS registers ############################
bat_x = response.registers[0]
bat_temp = (ctypes.c_int16(bat_x).value)/10.0
print bat_temp
milk_y = response.registers[1]
milk_temp = (ctypes.c_int16(milk_y).value)/10.0
print milk_temp
aux_temp = response.registers[2]/10.0
comp_curr = response.registers[5]/10.0
HP=0
LP=0
fault = responseFault.registers[12]
print "Fault register reading: ",fault
if fault == 4:
HP = 1
elif fault == 5:
LP = 1
else:
HP = 0
LP = 0
#############################################################################
client.close()
while True:
Milk = milk_temp;
Ambient = aux_temp;
Battery = bat_temp;
Comp = comp_curr;
print "Milk Temp: ",Milk
print "Aux Temp: ",Ambient
print "Battery Temp: ",Battery
print "Comp curr: ",Comp
print "HP in while : ",HP
print "LP in while : ",LP
datetimeWrite = (time.strftime("%Y-%m-%d "))
time_ = (time.strftime("%H:%M:%S"))
#time_ = "%s-%s-%s" % (datetime.now().hour,datetime.now().minute, datetime.now().second)
#datestamp = "%s-%s-%s" % (datetime.now().year,datetime.now().month, datetime.now().day)
if 24.8 <= Milk <=25.2:
print "In milk"
gpio.output(36,1)
time.sleep(3)
gpio.output(36,0)
elif Battery == -10.0:
print "In battery"
gpio.output(36,1)
time.sleep(6)
gpio.output(36,0)
else:
print "Out of buzzer condition"
print datetimeWrite
print time_
if Milk<=29.0 or Battery==-10.0:
sql = ("""INSERT INTO compData(date,time,SerialNo, Bat_temp, Milk_temp, Aux_temp, Comp_curr,HP,LP) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",(datetimeWrite,time_,serialNum,Battery,Milk,Ambient,Comp,HP,LP))
try:
print "Writing to database..."
# Execute the SQL command
cur.execute(*sql)
# Commit your changes in the database
db.commit()
print "Write Complete"
data = {} # Create empty set, then fill in with our three fields:
# Field 0, light, gets the local time:
data[fields[0]] = aux_temp
# Field 1, switch, gets the switch status:
data[fields[1]] = bat_temp
data[fields[2]] = comp_curr
data[fields[3]] ="0"# hp
data[fields[4]] ="0"# lp
data[fields[5]] = milk_temp
params = urllib.urlencode(data)
headers = {} # start with an empty set
# These are static, should be there every time:
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Connection"] = "close"
headers["Content-Length"] = len(params) # length of data
headers["Phant-Private-Key"] = privateKey # private key he
#c = httplib.HTTPConnection(server)
#c.request("POST", "/input/" + publicKey + ".txt", params, headers)
#r = c.getresponse() # Get the server's response and print it
#print r.status, r.reason
################################# Condition check for Buzzer ######################
if 24.8 <= Milk <=25.2:
print "In milk"
gpio.output(36,1)
time.sleep(3)
gpio.output(36,0)
elif -9.8 <= Battery <= -10.0:
print "In battery"
gpio.output(36,1)
time.sleep(6)
gpio.output(36,0)
####################################################################################
except:
# Rollback in case there is any error
db.rollback()
print "Failed writing to database"
cur.close()
db.close()
break
#print response.registers[0:]
#print "Battery Temp:",bat_temp
#print "Milk_temp:",milk_temp
#print "Aux. Temp:",aux_temp
#print "Comp curr:",comp_curr