-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheatingOneShot.py
136 lines (104 loc) · 3.09 KB
/
heatingOneShot.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import datetime
import MySQLdb as mdb
import logging
import smbus
#### Import dB Details from access.py
from access import hostname, username, password, dB
# DS1621 commands
START = 0xEE
STOP = 0x22
READ_TEMP = 0xAA
READ_COUNTER = 0xA8
READ_SLOPE = 0xA9
ACCESS_CONFIG = 0xAC
ACCESS_TH = 0xA1
ACCESS_TL = 0xA2
# read-only status bits
DONE = 0x80
TH_BIT = 0x40
TL_BIT = 0x20
NVB = 0x10 # Non-Volatile memory Busy
# r/w status bits (bit masks)
POL_HI = 0x02
POL_LO = 0xFD
ONE_SHOT = 0x01
CONT_MODE = 0xFE
CLR_TL_TH = 0x9F
logging.basicConfig(filename='/home/pi/DS1621_error.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# Load the modules (not required if they are loaded at boot)
# os.system('modprobe w1-gpio')
# os.system('modprobe w1-therm')
# Basic numerical assist functions
def twos_comp(byte):
'''input byte in two's complement is returned as signed integer. '''
if len(bin(byte)[2:]) > 8:
# shouldn't ever get here
print ('\nWarning: input ' + str(hex(byte)) + \
' truncated to least significant byte: ' + \
str(hex(0xFF & byte)))
byte = 0xFF & byte
return ~(255 - byte) if byte > 127 else byte
# Function for storing readings into MySQL
def insertDB(IDs, temperature):
try:
con = mdb.connect(hostname,
username,
password,
dB);
cursor = con.cursor()
for i in range(0,len(temperature)):
sql = "INSERT INTO temperature(temperature, sensor_id) \
VALUES ('%s', '%s')" % \
( temperature[i], IDs[i])
# print sql
cursor.execute(sql)
sql = []
con.commit()
con.close()
except mdb.Error:
e = sys.exc_info()[0]
logger.error(e)
# routines for accessing bus data
# General read function, also updates a register
def wake_up(bus, sensor):
''' Device always starts in Idle mode, first reading is not usable.'''
read_degreesC_byte(bus, sensor)
time.sleep(0.6)
def read_degreesC_byte(bus, sensor):
'''returns temperature in degrees Celsius as integer '''
bus.read_byte_data(sensor, START)
degreesC_byte = twos_comp(bus.read_byte_data(sensor, READ_TEMP))
return degreesC_byte
def write(value, address):
bus.write_byte_data(address,0,value)
return -1
def read(address):
reading = bus.read_byte(address)
return reading
################################################
#
# Start of "Main"
#
################################################
# Set up variables
address = [0x48, 0x49]
temperature = []
IDs = ["HWT_Top", "HWT_Bottom"]
# Get readings from sensors and store them in MySQL
bus = smbus.SMBus(1)
# sensorname at bus address.
for dev_addr in address:
wake_up(bus,dev_addr)
temp = read_degreesC_byte(bus, dev_addr)
# print (dev_addr, temp)
temperature.append(temp)
currentDT = datetime.datetime.now()
print ("OneShot: ", str(currentDT), IDs, temperature)
insertDB(IDs, temperature)