本文整理汇总了Python中ABE_ADCPi.ADCPi.read_voltage方法的典型用法代码示例。如果您正苦于以下问题:Python ADCPi.read_voltage方法的具体用法?Python ADCPi.read_voltage怎么用?Python ADCPi.read_voltage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ABE_ADCPi.ADCPi
的用法示例。
在下文中一共展示了ADCPi.read_voltage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
class dld_solar_window:
_channel = 0
_report_period = 5
_thread = None
_stop_requested = False
_srv_ip_addr = "1.2.2.201"
_srv_port = 8200
def __init__(self, channel=0, report_period=the_report_period):
self._channel = channel
self._report_period = report_period
self._i2c_helper = ABEHelpers()
self._bus = self._i2c_helper.get_smbus()
self._adc = ADCPi(self._bus, 0x68, 0x69, 12)
def Start(self):
self._stop_requested = False
self._thread = threading.Thread(target=self.SamplingThread)
self._thread.start()
print("sampling thread initialized")
def SamplingThread(self):
print("sampling thread started")
mc_socket = None
time_last_report = time.time()
connected = False
while self._stop_requested != True:
if connected == False:
try:
# time.sleep(5)
mc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mc_socket.connect((self._srv_ip_addr, self._srv_port))
print("connected")
time.sleep(1)
except Exception as e:
connected = False
continue
time_now = time.time()
voltage = self._adc.read_voltage(self._channel)
try:
mc_socket.send("Vout:%02f\n" % (voltage))
print("Vout:%02f\n" % (voltage))
except Exception as e:
connected = False
time.sleep(5.0)
def Stop(self):
if self._thread == None:
return
self._stop_requested = True
if self._thread.isAlive():
self._thread.join()
self._thread = None
示例2: main
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
def main():
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 18)
if not os.path.isfile(db_name):
createdb()
while (True):
temp = 0
light = 0
moisture = 0
for x in range(0, 60):
# read from adc channels and write to the log file
temp = temp + (adc.read_voltage(1) - 0.5) * 100
light = light + adc.read_voltage(2)
moisture = moisture + adc.read_voltage(3)
time.sleep(0.4)
# Average temp
temp = temp / 60
# Correct temp
temp = temp - 1.2
# Average light
light = light / 60
# Average moisture
moisture = moisture / 60
print("temp,light,moisture")
print(temp)
print(light)
print(moisture)
writetodb("%02f" % temp, "%02f" % light, "%02f" % moisture)
示例3: __init__
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
class line_test:
def __init__(self):
""" Standard Constructor """
logging.info("Three Point Turn constructor")
# set up ADC
self.i2c_helper = ABEHelpers()
self.bus = self.i2c_helper.get_smbus()
self.adc = ADCPi(self.bus, 0x6a, 0x6b, 12)
self.killed = False
def run(self, line_sensor=None):
logging.info("Started Looking")
while not self.killed:
# If we have a line sensor, check it here. Bail if necesary
#if line_sensor and (self.adc.read_voltage(line_sensor) > self.red_min):
# logging.info("Line Detected")
if line_sensor:
logging.info( str(self.adc.read_voltage(line_sensor)) )
time.sleep(0.05)
示例4: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
Sample rate can be 12,14, 16 or 18
"""
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 18)
def writetofile(texttowrtite):
f = open("adclog.txt", "a")
f.write(str(datetime.datetime.now()) + " " + texttowrtite)
f.closed
while True:
# read from adc channels and write to the log file
writetofile("Channel 1: %02f\n" % adc.read_voltage(1))
writetofile("Channel 2: %02f\n" % adc.read_voltage(2))
writetofile("Channel 3: %02f\n" % adc.read_voltage(3))
writetofile("Channel 4: %02f\n" % adc.read_voltage(4))
writetofile("Channel 5: %02f\n" % adc.read_voltage(5))
writetofile("Channel 6: %02f\n" % adc.read_voltage(6))
writetofile("Channel 7: %02f\n" % adc.read_voltage(7))
writetofile("Channel 8: %02f\n" % adc.read_voltage(8))
# wait 1 second before reading the pins again
time.sleep(1)
示例5: wind_speed_meter
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
#.........这里部分代码省略.........
time.sleep(0.2)
continue
if(len(self._samples) == 0):
continue
if(lines_reported >= file_length):
#open new file if current is full
fl.close()
self.files_to_compress.append(fl_name)
fl_name = "{}wind_{}.csv".format(file_location, time.strftime("%Y%m%d_%H%M%S"))
fl = open(fl_name, "w")
lines_reported = 0
self._samples_lock.acquire()
smpl = self._samples.pop(0)
self._samples_lock.release()
if(len(self._dirs_volts)>0):
for i in range(0, len(self._dirs_volts)):
if self._dirs_volts[i].is_in(smpl.direction_voltage):
smpl.direction_code = self._dirs_names[i]
break
self.log_at_display("REPORTED: speed={}m/s Vspeed_avg={}V ({} reads) Vdir={}V DIr={}".format(smpl.speed_m_per_sec, smpl.sample_average_voltage, smpl.voltage_reads, smpl.direction_voltage, smpl.direction_code))
fl.write("{},{}\n".format(smpl.direction_code, smpl.speed_m_per_sec))
lines_reported+=1
self._dir_calibration(smpl)
time_last_report = time_now
def _thread_compression(self):
while(self._stop_requested != True):
if(len(self.files_to_compress) == 0):
continue
f_name = self.files_to_compress.pop(0)
f_in = open(f_name, 'rb')
f_out = gzip.open(f_name + '.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
os.remove(f_name)
def _thread_sampling(self):
self.log_at_display("sampling thread started")
speed_reads_counter = 0
last_sample_timestamp = 0
time_now = time.time()
time_last_sampling = time_now
speed_voltage_sum = 0.0
while(self._stop_requested != True):
time_now = time.time()
# output voltage is proportional to the wind speed at the voltage read moment
#read voltage from speed sensor
speed_voltage_sum += self._adc.read_voltage(self._a2d_chan_speed)
speed_reads_counter += 1
if(time_now - time_last_sampling < self._file_report_period) or (time_now < time_last_sampling):
time.sleep(0.01)
continue
smpl = sample()
smpl.sample_time_stamp = time_now
smpl.sample_time_span = time_now - time_last_sampling
smpl.voltage_reads = speed_reads_counter
smpl.sample_average_voltage = round(speed_voltage_sum/smpl.voltage_reads,2)
#Vout may vary from 0 to 5V which linearry relates to 0.5 to 50M/s
# Thus each 1V relates to 10M/s or 1 mV -> 0.01 M/s: 1mV means 1cm/s
# smpl.sample_average_voltage is in volts
# smpl.sample_average_voltage*1000 gives the value in milli-volts
# from here smpl.speed_m_per_sec = (smpl.sample_average_voltage*1000)mV*0.01 (Ms/ per mV)
# or smpl.speed_m_per_sec = smpl.sample_average_voltage*10
smpl.speed_m_per_sec = round(smpl.sample_average_voltage*10, 2)
if(self._a2d_chan_vcc != -1):
vcc = self._adc.read_voltage(self._a2d_chan_vcc)
if(a2d_chan_direction != -1):
smpl.direction_voltage = round(self._adc.read_voltage(self._a2d_chan_direction),1)
smpl.direction_code = ""
self._samples_lock.acquire()
self._samples.append(smpl)
self.log_at_display("CAPTURED: speed={}m/s Vspeed_avg={}V ({} reads) Vdir={}V List_len={}".format(smpl.speed_m_per_sec, smpl.sample_average_voltage, smpl.voltage_reads, smpl.direction_voltage, len(self._samples)))
self._samples_lock.release()
speed_reads_counter = 0
speed_voltage_sum = 0.0
time_last_sampling = time.time()
def Stop(self):
self._stop_requested = True
if(self._thread_sampling_obj.isAlive()):
self._thread_sampling_obj.join();
if(self._thread_reporting_obj.isAlive()):
self._thread_reporting_obj.join();
if(self._thread_compression_obj.isAlive()):
self._thread_compressions_obj.join();
示例6: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 12)
# Configure the GPIO pins
BUTTON_PIN = 24
EXIT_BUTTON = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(EXIT_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
button_press = GPIO.input(BUTTON_PIN)
# voltage reading from channel 1 (v_supply) / 2, used for calccurrent()
v_i = (adc.read_voltage(1)) /2
# voltage reading from channel 1 (v_supply), used for calccurrent()
v1 = (adc.read_voltage(1))
# calculates current from channel labelled 'v_i'
# i = current calculated from adc channel 1 defined globally
def calccurrent(inval):
global i
i = ((inval) - v_i) / 0.066
return ((inval) - v_i) / 0.066
# calculates resistance using two global variables
# voltage/current = r
def calcresistance(volts):
global r
r = (volts/i)
示例7: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
from ABE_helpers import ABEHelpers
import os
import time
import RPi.GPIO as GPIO
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 12)
# Configure the GPIO pins
BUTTON_PIN = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# voltage reading from channel 1
v1 = adc.read_voltage(1)
# voltage reading from channel 2, used for current calc i2
v2 = (adc.read_voltage(2)) / 2
# calculates current from channel preceded by 'i' (eg. i2)
# i2 = current calculated from adc channel 2 defined globally
def calccurrent(inval):
global i
i = ((inval) - v2) / 0.066
return ((inval) - v2) / 0.066
# calculates resistance using two global variables
# voltage/current = r
def calcresistance(v_ref):
global r
r = (v_ref/i)
示例8: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
Requires python 3 smbus to be installed
run with: python3 demo-acs712-30.py
================================================
Initialise the ADC device using the default addresses and sample rate,
change this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 12)
# change the 2.5 value to be half of the supply voltage.
def calcCurrent(inval):
return ((inval) - 2.5) / 0.066
while (True):
# clear the console
os.system('clear')
# read from adc channels and print to screen
print ("Current on channel 1: %02f" % calcCurrent(adc.read_voltage(1)))
# wait 0.5 seconds before reading the pins again
time.sleep(0.5)
示例9: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
Initialise the ADC device using the default addresses and sample rate,
change this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 12)
while (True):
# clear the console
os.system('clear')
# read from adc channels and print to screen
print ("Channel 1: %02f" % adc.read_voltage(1))
print ("Channel 2: %02f" % adc.read_voltage(2))
print ("Channel 3: %02f" % adc.read_voltage(3))
print ("Channel 4: %02f" % adc.read_voltage(4))
print ("Channel 5: %02f" % adc.read_voltage(5))
print ("Channel 6: %02f" % adc.read_voltage(6))
print ("Channel 7: %02f" % adc.read_voltage(7))
print ("Channel 8: %02f" % adc.read_voltage(8))
# wait 0.5 seconds before reading the pins again
time.sleep(0.5)
示例10: wind_speed_meter
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
class wind_speed_meter():
def __init__(self):
self._a2d_chan_speed = a2d_chan_speed
self._a2d_chan_direction = a2d_chan_direction
self._a2d_chan_vcc = a2d_chan_vcc
self._file_report_period = file_report_period
self._i2c_helper = ABEHelpers()
self._bus = self._i2c_helper.get_smbus()
self._adc = ADCPi(self._bus, 0x68, 0x69, 12)
self._CtrlPort = 8200
self._IPAddr = self.get_local_ip()
self._samples = list()
self._samples_lock = threading.Lock()
self._thread_sampling = None
self._thread_reporting = None
self._stop_requested = False
def get_local_ip(self):
ifconfig_cmd = commands.getoutput("ifconfig")
patt = re.compile(r'inet\s*\w*\S*:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
addr_list = patt.findall(ifconfig_cmd)
for addr in addr_list:
if addr == "127.0.0.1":
continue
## if(self._nettype == NETTYPE.CELL):
## if(addr.find("192.168.") == 0):
## continue
if(addr.find('.')>0):
return addr
return "127.0.0.1"
def start(self):
self._stop_requested = False
self._thread_sampling = threading.Thread(target=self.thread_sampling)
self._thread_sampling.start()
self._thread_reporting = threading.Thread(target=self.thread_reporting)
self._thread_reporting.start()
def thread_reporting(self):
print("reporting thread started")
mc_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
#mc_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mc_socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 100)
mc_socket.bind((self._IPAddr, 0))
#mreq = struct.pack('4sl', socket.inet_aton("224.0.150.150"), socket.INADDR_ANY)
mreq = struct.pack('4sl', socket.inet_aton("224.0.1.200"), socket.INADDR_ANY)
mc_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
#mc_socket.sendto("init", ("224.0.150.150", 8100))
time_last_report = time.time()
while(self._stop_requested != True):
time_now = time.time()
if(time_now - time_last_report < self._file_report_period) or (time_now < time_last_report):
time.sleep(0.2)
continue
if(len(self._samples) > 0):
self._samples_lock.acquire()
smpl = self._samples.pop(0)
print("extracted speed: {} ({}). list len: {}".format(str(smpl.speed_m_per_sec), smpl.speed_pulses, len(self._samples)))
self._samples_lock.release()
time_last_report = time_now
def thread_sampling(self):
print("sampling thread started")
speed_voltage_last = 0
speed_pulses_counter = 0
last_sample_timestamp = 0
time_now = time.time()
time_last_sampling = time_now
while(self._stop_requested != True):
time_now = time.time()
#read voltage from speed sensor
speed_voltage = self._adc.read_voltage(self._a2d_chan_speed)
if( (speed_voltage > 0) and (speed_voltage_last == 0)):
#count only transitions from low to high signal
speed_pulses_counter += 1
#print("pulses: " + str(speed_pulses_counter))
speed_voltage_last = speed_voltage
if(time_now - time_last_sampling < self._file_report_period) or (time_now < time_last_sampling):
# time.sleep(0.001)
continue
smpl = sample()
smpl.sample_time_stamp = time_now
smpl.sample_time_span = time_now - time_last_sampling
smpl.speed_pulses = speed_pulses_counter
smpl.speed_m_per_sec = (speed_pulses_counter*2*math.pi*wind_sensor_radius)/smpl.sample_time_span
if(self._a2d_chan_vcc != -1):
vcc = self._adc.read_voltage(self._a2d_chan_vcc)
#.........这里部分代码省略.........
示例11: calccurrent
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
BUTTON1 = 5
BUTTON2 = 6
BUTTON3 = 13
BUTTON4 = 19
EXIT_BUTTON = 23
END_BUTTON = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(EXIT_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(END_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# voltage reading from channel 1 (v_supply) / 2, used for calccurrent()
v_i = (adc.read_voltage(1)) / float(2)
# voltage reading from channel 1 (v_supply), used for calccurrent()
v1 = (adc.read_voltage(1))
# calculates current from channel labelled 'v_i'
# i = current calculated from adc channel 1 defined globally
def calccurrent(inval):
global i
i = ((inval) - v_i) / 0.066
return ((inval) - v_i) / 0.066
# calculates resistance using two global variables
# voltage(V)/current(mA) = r
def calcresistance(volts):
return volts / float(i)
示例12: ABEHelpers
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
Initialise the ADC device using the default addresses and sample rate,
change this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x6A, 0x6B, 12)
while (True):
# clear the console
os.system('clear')
# read from adc channels and print to screen
print ("Channel 1: %02f" % (adc.read_voltage(1) * 100.0))
print ("Channel 2: %02f" % adc.read_voltage(2))
print ("Channel 3: %02f" % adc.read_voltage(3))
print ("Channel 4: %02f" % adc.read_voltage(4))
print ("Channel 5: %02f" % adc.read_voltage(5))
print ("Channel 6: %02f" % adc.read_voltage(6))
print ("Channel 7: %02f" % adc.read_voltage(7))
print ("Channel 8: %02f" % adc.read_voltage(8))
# wait 1.0 seconds before reading the pins again
time.sleep(1.0)
示例13: PIDController
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
#.........这里部分代码省略.........
valuelist.append(value)
setpointchanges = len(timelist) - 1
timenow = datetime.datetime.now()
if timenow < datetime.datetime.strptime(timelist[0], '%Y-%m-%d %H:%M:%S'):
self.setpoint = "off"
elif timenow == datetime.datetime.strptime(timelist[0], '%Y-%m-%d %H:%M:%S'):
self.setpoint = valuelist[0]
elif timenow >= datetime.datetime.strptime(timelist[setpointchanges], '%Y-%m-%d %H:%M:%S'):
self.setpoint = "off"
else:
self.setpoint = "off"
for x in range(setpointchanges, -1, -1):
# Check for current timeframe and adjust setpoint by interpolation
if datetime.datetime.strptime(timelist[x], '%Y-%m-%d %H:%M:%S') < timenow:
time1 = datetime.datetime.strptime(timelist[x], '%Y-%m-%d %H:%M:%S')
value1 = valuelist[x]
time2 = datetime.datetime.strptime(timelist[x+1], '%Y-%m-%d %H:%M:%S')
value2 = valuelist[x+1]
self.setpoint = ((timenow - time1) / (time2 - time1)) * (value2 - value1) + value1
break
# Autotune function
def autotune(self):
if self.variabledict['autotune_iterations'] == 0:
self.variabledict['sleeptime'] = self.variabledict['autotune_sleeptime']
self.variabledict['umin'] = 0
self.variabledict['umax'] = 100
self.variabledict['moutput'] = 0
self.outputdict['Status'] = 'Autotune started'
# Read New Measured Variable
mvchannel = self.variabledict['control_channel']
v = self.adc.read_voltage(mvchannel)
mv = self.variabledict['control_k1'] * v * v + self.variabledict['control_k2'] * v + self.variabledict['control_k3']
# Do assymetric relay output
if self.variabledict['autotune_gainsign'] >= 0:
if mv <= self.variabledict['autotune_temp'] - self.variabledict['autotune_hysteresis'] and self.variabledict['moutput'] == 0:
print('relay on')
self.variabledict['moutput'] = 100
self.variabledict['autotune_iterations'] += 1
elif mv >= self.variabledict['autotune_temp'] + self.variabledict['autotune_hysteresis'] and self.variabledict['moutput'] == 100:
print('relay off')
self.variabledict['moutput'] = 0
self.variabledict['autotune_iterations'] += 1
else:
if mv <= self.variabledict['autotune_temp'] - self.variabledict['autotune_hysteresis'] and self.variabledict['moutput'] == 100:
print('relay off')
self.variabledict['moutput'] = 0
self.variabledict['autotune_iterations'] += 1
elif mv >= self.variabledict['autotune_temp'] + self.variabledict['autotune_hysteresis'] and self.variabledict['moutput'] == 0:
print('relay on')
self.variabledict['moutput'] = 100
self.variabledict['autotune_iterations'] += 1
self.outputdict['Status'] = 'Autotune in progress - %s of %s' % (self.variabledict['autotune_iterations'], self.variabledict['autotune_maxiterations'])
# Update autotunedict
self.variabledict['autotune_dict']['time'].append(time.time())
self.variabledict['autotune_dict']['temp'].append(mv)
self.variabledict['autotune_dict']['output'].append(self.variabledict['moutput'])
if len(self.variabledict['autotune_dict']['output']) == 1:
if self.variabledict['autotune_dict']['output'][0] == 100:
self.variabledict['autotune_dict']['peaktype'] = 'min'
示例14: calccurrent
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
BUTTON1 = 5
BUTTON2 = 6
BUTTON3 = 13
BUTTON4 = 19
EXIT_BUTTON = 23
END_BUTTON = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(EXIT_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(END_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# voltage reading from channel 1 (v_supply), used for calccurrent()
v1 = (adc.read_voltage(1))
# calculates current from channel labelled 'v_i'
# i = current calculated from adc channel 1 defined globally
def calccurrent(inval):
global i
i = ((inval) - 1.65) / 0.066
return ((inval) - 1.65) / 0.066
# calculates resistance using two global variables
# voltage(V)/current(mA) = r
def calcresistance(volts):
return volts / float(i)
def pre_exit(channel):
print('Exit button pressed, quiting program...')
示例15: __init__
# 需要导入模块: from ABE_ADCPi import ADCPi [as 别名]
# 或者: from ABE_ADCPi.ADCPi import read_voltage [as 别名]
class PH:
ph = 0
temp = 0
opampGain = 5.25
phStep = 0
i2c1 = 0x68
i2c2 = 0x69
bitrate = 18
vRefPin = 1
sensorPin = 8
ph7 = 0
ph4 = 0
ds = 0
def __init__ (self, ph7, ph4, ds):
self.ph7 = ph7
self.ph4 = ph4
self.ds = ds
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
self.adc = ADCPi(bus, self.i2c1, self.i2c2, self.bitrate)
self.calcpHSlope()
def __readvRef (self):
return int(self.adc.read_voltage(self.vRefPin) * 1000)
def __readRaw (self):
return int(self.adc.read_voltage(self.sensorPin) * 1000)
def __readtemp (self):
f = open('/mnt/1wire/' + self.ds +'/temperature', 'r')
self.temp = float(f.readline())
f.close()
def calcpHSlope (self):
vRef = self.__readvRef()
self.phStep = ((vRef * (self.ph7 - self.ph4)) / self.opampGain ) / 3
def calibratepH7 (self):
self.ph7 = self.__readRaw()
def calibratepH4 (self):
self.ph4 = self.__readRaw()
def calcpH (self):
temp = self.__readtemp()
vRef = self.__readvRef()
mV = self.__readRaw() * vRef
tmp = ((vRef * self.ph7) - mV) / self.opampGain
self.ph = 7 - ( tmp / self.phStep );
return self.ph
def toString (self):
self.calcpH()
mV = self.__readRaw()
print("temp: %0.2f C, PH: %02.1f, mV: %s" % (self.temp, self.ph, mV))
def getTimestamp (self):
return int(round(time.time() * 1000))
def toJSON (self):
self.calcpH()
ms = self.getTimestamp()
mV = self.__readRaw()
return '{ "temp": %0.2f, "PH": %0.1f, "ms": %d, "mV": %d }' % (self.temp, self.ph, ms, mV)