#!/usr/bin/python
#SwitchDoc Labs May 2016
#
# reads all four channels from the Grove4Ch16BitADC Board in single ended mode
# also reads raw values
#
import time, signal, sys
from MADS1x15 import ADS1x15
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
#print 'Press Ctrl+C to exit'
ADS1115 = 0x01 # 16-bit ADC
# Select the gain
gain = 6144 # +/- 6.144V
#gain = 4096 # +/- 4.096V
# gain = 2048 # +/- 2.048V
# gain = 1024 # +/- 1.024V
# gain = 512 # +/- 0.512V
# gain = 256 # +/- 0.256V
# Select the sample rate
# sps = 8 # 8 samples per second
# sps = 16 # 16 samples per second
# sps = 32 # 32 samples per second
# sps = 64 # 64 samples per second
# sps = 128 # 128 samples per second
sps = 250 # 250 samples per second
# sps = 475 # 475 samples per second
# sps = 860 # 860 samples per second
# Initialise the ADC using the default mode (use default I2C address)
adc = ADS1x15(ic=ADS1115)
while (1):
# Read channels in single-ended mode using the settings above
print"--------------------"
voltsCh0 = adc.readADCSingleEnded(0, gain, sps) / 1000
rawCh0 = adc.readRAW_ADCSingleEnded(0, gain, sps)
print "Channel 0 =%.6fV raw=%d raw=0x%4X" % (voltsCh0, rawCh0, rawCh0)
voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000
rawCh1 = adc.readRAW_ADCSingleEnded(1, gain, sps)
# O2 Sensor
sensorVoltage = voltsCh1 *(5.0/6.144)
AMP = 121
K_O2 = 7.43
sensorVoltage = sensorVoltage/AMP*10000.0
Value_O2 = sensorVoltage/K_O2 - 1.05
print "Channel 1 =%.6fV raw=0x%4X O2 Percent=%6.2f" % (voltsCh1, rawCh1, Value_O2)
voltsCh2 = adc.readADCSingleEnded(2, gain, sps) / 1000
rawCh2 = adc.readRAW_ADCSingleEnded(2, gain, sps)
print "Channel 2 =%.6fV raw=0x%4X" % (voltsCh2, rawCh2)
voltsCh3 = adc.readADCSingleEnded(3, gain, sps) / 1000
rawCh3 = adc.readRAW_ADCSingleEnded(3, gain, sps)
print "Channel 3 =%.6fV raw=0x%4X" % (voltsCh3, rawCh3)
print"--------------------"
time.sleep(0.5)--------------------
Channel 0 =0.000000V
Channel 1 =0.000000V
Channel 2 =0.000000V
Channel 3 =0.000000V
--------------------
from MADS1x15 import ADS1x15
import time
import syslog
import weewx
import weewx.manager
from datetime import datetime
from weewx.wxengine import StdService
from weeutil.weeutil import to_bool
VERSION = "0.1"
if weewx.__version__ < "3.2":
raise weewx.UnsupportedFeature("weewx 3 is required, found %s" %
weewx.__version__)
schema = [('dateTime', 'INTEGER NOT NULL PRIMARY KEY'),
('usUnits', 'INTEGER NOT NULL'),
('ch0_voltage', 'REAL'),
('ch1_voltage', 'REAL'),
('ch2_voltage', 'REAL'),
('ch3_voltage', 'REAL'),]
def get_default_binding_dict():
return {'database': 'voltage_sqlite',
'manager': 'weewx.manager.Manager',
'table_name': 'archive',
'schema': 'user.ads1115.schema'}
def logmsg(level, msg):
syslog.syslog(level, 'ads1115: %s' % msg)
def logdbg(msg):
logmsg(syslog.LOG_DEBUG, msg)
def loginf(msg):
logmsg(syslog.LOG_INFO, msg)
def logerr(msg):
logmsg(syslog.LOG_ERR, msg)
class ADS1115(StdService):
def __init__(self, engine, config_dict):
super(ADS1115, self).__init__(engine, config_dict)
loginf("service version is %s" % VERSION)
svc_dict = config_dict.get('ADS1115', {})
self.data_binding = svc_dict.get('data_binding', None)
loginf("data_binding=%s" % self.data_binding)
pkt_binding = svc_dict.get('binding', 'archive')
loginf("binding=%s" % pkt_binding)
self.data = []
# if a binding was specified, then use it to save voltage readings to database
if self.data_binding is not None:
# configure the voltage database
dbm_dict = weewx.manager.get_manager_dict(
config_dict['DataBindings'], config_dict['Databases'],
self.data_binding, default_binding_dict=get_default_binding_dict())
with weewx.manager.open_manager(dbm_dict, initialize=True) as dbm:
# ensure schema on disk matches schema in memory
dbcol = dbm.connection.columnsOf(dbm.table_name)
memcol = [x[0] for x in dbm_dict['schema']]
if dbcol != memcol:
raise Exception('ads1115: schema mismatch: %s != %s' %
(dbcol, memcol))