def __init__(self, scl, sda, gain=1):
        self.i2c = busio.I2C(scl, sda)
        self.ads = ADS.ADS1115(self.i2c)
        self.channel = analog_in.AnalogIn(self.ads, ADS.P0, ADS.P1)
        self.ads.gain = gain

        self.gain_options = [2 / 3, 1, 2, 4, 8, 16]
Exemplo n.º 2
0
    def __init__(self, ads=None):
        if ads is None:
            if not adc is None:
                ads = adc
            else:
                try:
                    if ADS_TYPE == 1015:
                        ads = ads1015.ADS1015(board.I2C(),
                                              gain=ADC_GAIN,
                                              address=ADC_ADDR)
                    else:
                        ads = ads1115.ADS1115(board.I2C(),
                                              gain=ADC_GAIN,
                                              address=ADC_ADDR)
                # except (OSError, ValueError) as err:
                except:
                    print("[LevelSensor]  No ADS1x15 breakout detected!")
                    ads = None

        super().__init__(analog_in.AnalogIn(ads, LevelSensor.L_PIN))

        self.history = [0.0] * NSAMPLES
        self._sampleCnt = 0
        self._idx = 0

        if WARM_UP_LEVEL_SENSOR:
            for i in range(NSAMPLES * 3):
                self._level = self.readSensor()
                if PRINTS_ON:
                    print('.', end='')
                time.sleep(1)
        if PRINTS_ON:
            print('\n=== SENSORS INITIALIZED ===\n')
Exemplo n.º 3
0
    def __init__(self, ads=None):
        if ads is None:
            if not adc is None:
                ads = adc
            else:
                try:
                    if ADS_TYPE == 1015:
                        ads = ads1015.ADS1015(board.I2C(),
                                              gain=ADC_GAIN,
                                              address=ADC_ADDR)
                    else:
                        ads = ads1115.ADS1115(board.I2C(),
                                              gain=ADC_GAIN,
                                              address=ADC_ADDR)
                except:
                    print("[PHSensor]  No ADS1x15 breakout detected!")
                    ads = None

        super().__init__(analog_in.AnalogIn(ads, PHSensor.P_PIN))
Exemplo n.º 4
0
def measure(func):
    # var list reqd.
    global GAIN
    global curState
    global thresh
    global P
    global T
    global stateChanged
    global sampleCounter
    global lastBeatTime
    global firstBeat
    global secondBeat
    global Pulse
    global IBI
    global rate
    global amp
    global lastTime

    # read from the ADC
    signal = analog_in.AnalogIn(adc, ads.P0).value  # get input from channel a0
    curtime = int(time.time() * 1000)

    sampleCounter += curtime - lastTime  # # keep track of the time in mS with this variable
    lastTime = curtime
    n = sampleCounter - lastBeatTime  # # monitor the time since the last beat to avoid noise

    # find the peak and trough of the pulse wave
    if signal < thresh and n > (IBI / 5.0) * 3.0:  # # avoid dichrotic noise by waiting 3/5 of last IBI
        if signal < T:  # T is the trough
            T = signal  # keep track of lowest point in pulse wave 

    if signal > thresh and signal > P:  # thresh condition helps avoid noise
        P = signal  # P is the peak
        # keep track of highest point in pulse wave

    #  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
    # signal surges up in value every time there is a pulse
    if n > 250:  # avoid high frequency noise
        if (signal > thresh) and not Pulse and (n > (IBI / 5.0) * 3.0):
            Pulse = True  # set the Pulse flag when we think there is a pulse
            IBI = sampleCounter - lastBeatTime  # measure time between beats in mS
            lastBeatTime = sampleCounter  # keep track of time for next pulse

            if secondBeat:  # if this is the second beat, if secondBeat == TRUE
                secondBeat = False  # clear secondBeat flag
                for i in range(0, 10):  # seed the running total to get a realisitic bpm at startup
                    rate[i] = IBI

            if firstBeat:  # if it's the first time we found a beat, if firstBeat == TRUE
                firstBeat = False  # clear firstBeat flag
                secondBeat = True  # set the second beat flag
                return  # IBI value is unreliable so discard it

            # keep a running total of the last 10 IBI values
            running_total = 0  # clear the running_total variable    

            for i in range(0, 9):  # shift data in the rate array
                rate[i] = rate[i + 1]  # and drop the oldest IBI value 
                running_total += rate[i]  # add up the 9 oldest IBI values

            rate[9] = IBI  # add the latest IBI to the rate array
            running_total += rate[9]  # add the latest IBI to running_total
            running_total /= 10  # average the last 10 IBI values 
            bpm = analog_in.normalise(60000 / running_total)  # how many beats can fit into a minute? that's bpm!
            func('bpm: {}'.format(bpm))
            payload = {
                    "api_key": "M2QHPYFQQCMH9B0X",
                    'field1': bpm
            }
            requests.post("https://api.thingspeak.com/update", data=payload)
            
            

    if signal < thresh and Pulse:  # when the values are going down, the beat is over
        Pulse = False  # reset the Pulse flag so we can do it again
        amp = P - T  # get amplitude of the pulse wave
        thresh = amp / 2 + T  # set thresh at 50% of the amplitude
        P = thresh  # reset these for next time
        T = thresh

        if n > 2500:  # if 2.5 seconds go by without a beat
            thresh = 512  # set thresh default
            P = 512  # set P default
            T = 512  # set T default
            lastBeatTime = sampleCounter  # bring the lastBeatTime up to date
            firstBeat = True  # set these to avoid noise
            secondBeat = False  # when we get the heartbeat back
            print("no beats found")
Exemplo n.º 5
0
    dl10 = LevelSensor()

    while True:
        try:
            data = [get_ts(), dl10.level, dl10.voltage, dl10._value]
            print(fstr.format(*data))
            time.sleep(loop_delay)
        except KeyboardInterrupt:
            break

else:
    from adafruit_ads1x15 import ads1015, analog_in
    import board

    adc = ads1015.ADS1015(board.I2C(), gain=(2 / 3), address=0x48)
    dl10 = analog_in.AnalogIn(adc, 0)

    ready = input("Press enter to begin test...")

    while True:
        try:
            volts = dl10.voltage
            mA = voltage_to_mA(volts)
            # level = mA_to_level(mA)
            # level = voltage_to_level(volts)
            level = v_to_mA_to_level(volts)
            data = [get_ts(), level, volts, dl10.value]
            print(fstr.format(*data))
            time.sleep(loop_delay / 2)
        except KeyboardInterrupt:
            break