-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp3008_1.py
166 lines (132 loc) · 4.31 KB
/
mcp3008_1.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
#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO
import MySQLdb
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
DEBUG = 1
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25
# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
GPIO.setwarnings(False)
# 10k trim pot connected to adc #0
potentiometer_adc = 0;
led_adc = 1;
led_o_adc = 2;
#last_read = 0 # this keeps track of the last potentiometer value
#tolerance = 5 # to keep from being jittery we'll only change
# volume when the pot has moved more than 5 'counts'
def ConvertVolts(data,places):
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
def ConvertTemp(data,places):
# ADC Value
# (approx) Temp Volts
# 0 -50 0.00
# 78 -25 0.25
# 155 0 0.50
# 233 25 0.75
# 310 50 1.00
# 465 100 1.50
# 775 200 2.50
# 1023 280 3.30
temp = ((data * 330)/float(1023))-50
temp = round(temp,places)
return temp
elements_p = []
for i in range(0, 10):
led = readadc(led_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
#p = sensor.read_pressure()
#p = p / 100.00
#print "Adding %s to the list." % led
# append is a function that lists understand
# time.sleep(1)
# print p
elements_p.append(led)
elements_q = []
for i in range(0, 10):
led_o = readadc(led_o_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
#p = sensor.read_pressure()
#p = p / 100.00
#print "Adding %s to the list." % led
# append is a function that lists understand
# time.sleep(1)
# print p
elements_q.append(led_o)
#trim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
#led = readadc(led_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
#print trim_pot
elements_t = []
for i in range(0, 10):
trim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
temp_level = trim_pot
temp = ConvertTemp(temp_level,2)
#print "Adding %s to the list." % temp
# append is a function that lists understand
# time.sleep(1)
# print p
elements_t.append(temp)
temp_sql = round(sum(elements_t) / float(len(elements_t)),2)
led_sql = round(sum(elements_p) / float(len(elements_p)),0)
led_o_sql = round(sum(elements_q) / float(len(elements_q)),0)
#print temp_sql
#print led_sql
#temp_level = trim_pot
#temp = ConvertTemp(temp_level,2)
# Open database connection
db = MySQLdb.connect("localhost","weather","weather","mcp3008" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = "INSERT INTO data (temp, light, light_o) \
VALUES ('%s', '%s', '%s')" % \
(temp_sql,led_sql,led_o_sql)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()