简体   繁体   中英

Programming Metronome Python Script with PubNub for Raspberry Pi

I have created an Android mobile app which sends a BPM variable (between 0 and 160) over PubNub.

My goal here is to make a LED blink on my Raspberry Pi (GPIO 18) on every beat based off on the BPM value. As you can see in the code. However, when I launch my script. Nothing seems to be happening. The LED stays turned off. When I try to print data of my variables (for example the BPM value) - to see if anything is happening. Nothing pops up on my Raspberry Pi

I have no idea what could be missing or wrong with my script since it doesn't give me any error messages either. I have also verified that my app sends the data through PubNub.

Here is my script so far:

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import timeit
from threading import Thread
from pubnub import Pubnub
GPIO.setwarnings(False)


GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.OUT)

global BlinkLED = 160   

pubnub = Pubnub(publish_key = 'cencored',
subscribe_key = 'cencored')
channel = 'metronoom' 

def _callback(msg, n):

    def BlinkLED(BPM):
        BeatsPerSecond = BPM / 60
        while true:
            strStatus = "LED is turned on"
            GPIO.output(18,False); time.sleep(BeatsPerSecond)
            GPIO.output(18,True)
            strStatus = "LED is turned off"
            print (strStatus)
    BlinkLED(msg['BPM'])    


def _error(m):
    print(m)

pubnub.subscribe(channels=channel, callback=_callback, error=_error)

I've found the solution and it works right now. I'll share it here. I still have a problem that it's stuck in a loop because of the "while: True". I can't find a way to exit the loop once a new BPM value has been sent to the Pi. So I have to rerun the script every time I want to get a different tempo.

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import timeit
from pubnub import Pubnub


pubnub = Pubnub(publish_key = 'pub-c-b03db6ec-221e-483a-9582-9875ca362260',
subscribe_key = 'sub-c-13c8a43a-df92-11e5-aff5-02ee2ddab7fe')
channel = 'metronoom' 

global BPM
global Divider
Divider = 60.000000
Divider2 = 2.000000
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
def BlinkLED(BPM):  
    while True:
        GPIO.setwarnings(False)
        format(BPM, '.6f')
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(18, GPIO.OUT)
        BeatsPerSecond = Divider / BPM
        Interval = BeatsPerSecond / Divider2
        print(BPM)
        print(BeatsPerSecond)
        GPIO.output(18,True)
        GPIO.output(18,False)
        time.sleep(BeatsPerSecond)

def _callback(msg, n):
    print(msg)
    BlinkLED(msg["BPM"])

def _error(m):
    print(m)

pubnub.subscribe(channels=channel, callback=_callback, error=_error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM