forked from CliveCarrington/RPi_Temp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.py
48 lines (35 loc) · 1.07 KB
/
adc.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import logging
from Adafruit_ADS1x15 import ADS1x15
# Setup logging
logging.basicConfig(filename='/home/pi/adc_error.log',
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# Function for storing readings into MySQL
def insertDB(level):
try:
con = mdb.connect('localhost',
'pi_insert',
'xxxxxxxxxx',
'measurements');
cursor = con.cursor()
sql = "INSERT INTO light(level) \
VALUES ('%s')" % \
(level)
cursor.execute(sql)
sql = []
con.commit()
con.close()
except mdb.Error, e:
logger.error(e)
# Get readings from sensor and store them in MySQL
ADS1015 = 0x00 # 12-bit ADC
gain = 4096 # +/- 4.096V
sps = 250 # 250 samples per second
# Initialise the ADC
adc = ADS1x15(ic=ADS1015)
# Read channel 0 in single-ended mode using the settings above
level = adc.readADCSingleEnded(0, gain, sps) / 1000
insertDB(level)