I've built a set of musical stairs with python, motion sensors and a raspberry pi, and a web app that lets you choose which type of instrument sound you want to make. The type of instrument is stored in a MySQL database which I have connected to the python code (which makes the sounds when a beam is broken) and to a web app which allows users to select the instrument type from the database.
I am just wondering is there a way of querying the database from the python code that would mean only when a row is selected from the database, run a particular block of code.
Eg, someone clicks "Drum" on the web app. instrumentType "Drum" is selected from MySQL database Drumsound.play() should run on the python code.
Is there any way I could do this on python?
This is for a raspberry pi 3 running python 2.7, mySQLdb5 and apache2.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="*****",
pw="*****",
db="stairs"
)
cursor = mydb.cursor()
cursor.execute("SELECT variableValue FROM stairs WHERE variableValue =
'instrumentType'")
import RPi.GPIO as GPIO # GPIO
import pygame.mixer # To make sound
pygame.mixer.init()
''' GPIO setup '''
GPIO.setmode(GPIO.BCM) # GPIO setmode
GPIO.setwarnings(False)
'''Define steps and pins here'''
step1 = 4
'''Motion sensor setup here'''
GPIO.setup(step1, GPIO.IN, GPIO.PUD_UP)
'''Piano files here'''
C1 = pygame.mixer.Sound("piano/C1.wav")
'''Drum files here'''
drum1 = pygame.mixer.Sound("drum/C1.wav")
def play(pin):
sound = sound_pins[pin]
print("Playing note from pin %s" % pin)
sound.play()
'''Dictionary of steps and sounds'''
sound_pins = {
step1: C1,
step2: D,
step3: E,
step4: F,
step5: G,
step6: A,
step7: B,
step8: C2,
}
for pin in sound_pins:
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, play, 100)