aminsadati
aminsadati

Reputation: 31

How can I execute the second function?

I have connected a heart rate sensor correctly to a Raspberry Pi.

now:[The program runs without errors, but the second function(send_data) does not work.]

The first function:Reads sensor values. The second function: sending data.

The sensor works properly. But the sensor data is not sent and the second function does not work. I have put in a lot of effort to be able to solve this problem.

'''

import requests
import time
import Adafruit_ADS1x15

thingspeak_key = "API_KEY" 

class hr:
    def __init__(self):

        adc = Adafruit_ADS1x15.ADS1015()
        GAIN = 2/3  
        curState = 0
        thresh = 525  
        P = 512
        T = 512
        stateChanged = 0
        sampleCounter = 0
        lastBeatTime = 0
        firstBeat = True
        secondBeat = False
        Pulse = False
        IBI = 600
        rate = [0]*10
        amp = 100

        lastTime = int(time.time()*1000)

        while True:
            Signal = adc.read_adc(0, gain=GAIN)   
            curTime = int(time.time()*1000)

            sampleCounter += curTime - lastTime;      
            lastTime = curTime
            N = sampleCounter - lastBeatTime;    

            if Signal < thresh and N > (IBI/5.0)*3.0 :  
                if Signal < T :                     
                  T = Signal;                       

            if Signal > thresh and  Signal > P:          
                P = Signal;                             
                                         
            if N > 250 :                                  
                if  (Signal > thresh) and  (Pulse == False) and  (N > (IBI/5.0)*3.0)  :       
                  Pulse = True;                               
                  IBI = sampleCounter - lastBeatTime;         
                  lastBeatTime = sampleCounter;               

                  if secondBeat :                        
                    secondBeat = False;                 
                    for i in range(0,10):             
                      rate[i] = IBI;                      

                  if firstBeat :                        
                    firstBeat = False;                  
                    secondBeat = True;                  
                    continue                            

                  runningTotal = 0;               

                  for i in range(0,9):                
                    rate[i] = rate[i+1];               
                    runningTotal += rate[i];            

                  rate[9] = IBI;                          
                  runningTotal += rate[9];              
                  runningTotal /= 10;                    
                  BPM = 60000/runningTotal;               
                  self.data_hr=str(BPM)
                  print ('BPM: {}'.format(self.data_hr))

            if Signal < thresh and Pulse == True :   
                Pulse = False;                      
                amp = P - T;                           
                thresh = amp/2 + T;             
                P = thresh;                           
                T = thresh;

            if N > 2500 :                      
                thresh = 512;                        
                P = 512;                            
                T = 512;                            
                lastBeatTime = sampleCounter;           
                firstBeat = True;                     
                secondBeat = False;                 
                print ("no beats found")
    def send_data(self):
        
        r = requests.post('https://api.thingspeak.com/update?api_key=API_KEY', data = {'api_key':thingspeak_key, 'field1':format(self.data_hr)})
        
        time.sleep(0.005)

if __name__ == "__main__":
    x=hr()
    x.self.send_data()

'''

Upvotes: 3

Views: 103

Answers (2)

pecey
pecey

Reputation: 681

You are passing "self" to __init__, and hence your self variable is becoming a string.

A better way would be to do the following:

if __name__ == "__main__":
 x = hr()
 x.send_data()

Also, you might want to create different functions rather than doing everything in init.

Upvotes: 0

SSBakh
SSBakh

Reputation: 1532

Your problem is coming from:

if __name__ == "__main__":
  __init__("self")

You're trying to call the constructor of the class directly, and python doesn't know what __init__ is outside of the class that it has been defined in because of it's local scope. Also, "self" is a string, not an object.

You can just replace this with:

if __name__ == '__main__':
    heartrate = hr()

This creates an object and calls the constructor as well. You don't need to add self because python already knows what the class constructor is referring to.

However, you'll get stuck in your while loop in your hr class constructor, so I suggest splitting your constructor (the __init__ function) into separate functions and calling them separately.

Upvotes: 1

Related Questions