hello, can someone help how can i combine my python script with blynk script so that my blynk apps run at the same time with my hardware? so basically my hardware have the ultrasonic sensor with buzzer. the buzzer will triggered once the ultrasonic sensor measure a distance that is less than 30cm. i wanted to make the value display widget in blynk run at the same time that ultrasonic measure the distance.
these are my python script :
import _thread
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_TRIGGER = 4
GPIO_ECHO = 17
GPIO_BUZZER = 18
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.setup(GPIO_BUZZER, GPIO.OUT)
GPIO.output(GPIO_TRIGGER, False)
GPIO.output(GPIO_BUZZER, False)
keepRunning = True
distance = 0
def measureDistance():
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO) == 1:
stop = time.time()
elapsed = stop - start
distance = elapsed * 17150
return distance
def playSound(threadName, delay):
keepRunning
distance
while keepRunning:
if distance <= 30:
GPIO.output(GPIO_BUZZER, True)
time.sleep(0.01 * distance)
GPIO.output(GPIO_BUZZER, False)
time.sleep(0.05 * distance)
time.sleep(delay)
try:
distance = measureDistance()
_thread.start_new_thread(playSound, ("BuzzerThread1", 0.01))
while True:
print (distance)
time.sleep(0.1)
distance = measureDistance()
except:
keepRunning = False
time.sleep(1)
GPIO.cleanup()
and these is the blynk script:
import BlynkLib
import time
BLYNK_AUTH = 'YourAuthToken'
# initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# register the task running every 3 sec
# (period must be a multiple of 50 ms)
def my_user_task():
# do any non-blocking operations
print('Action')
blynk.virtual_write(2, time.ticks_ms() // 1000)
blynk.set_user_task(my_user_task, 3000)
# start Blynk (this call should never return)
blynk.run()
please help me