Skip to content

Instantly share code, notes, and snippets.

@criptography
Last active December 12, 2018 17:01
Show Gist options
  • Save criptography/6f07e494a579e41cc7bfd7a4a7bb39b2 to your computer and use it in GitHub Desktop.
Save criptography/6f07e494a579e41cc7bfd7a4a7bb39b2 to your computer and use it in GitHub Desktop.
Exhibition Setup: A ringing phone plays a sound when picked up (Raspberry Pi + Piezo + Flex-Sensor)

A ringing phone plays a sound when picked up (Raspberry Pi + Piezo + Flex-Sensor)

To show the complexity of political positions regarding the General Data Protection Regulation in the exhibition Privacy Arena the media artists Mike Huntemann, Isabel Paehr and Jörn Röder built an installation in which phones constantly ring. This gist shows the code I wrote to make the phones react to the visitor's actions. I used a Raspberry Pi with a piezo to create annoying sounds and a flex sensor to measure if visitors interact with the phones. Once a visitor pulls down a phone to answer it, a sound is played that reads a quote from a politician/media/lobbyist/etc involved in the process of making the General Data Protection Regulation. The Raspberry Pi runs a Python script.

Exhibition view with phones hanging from the ceiling

Resources

How to read sensors with a Raspberry Pi: https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/overview

#!/usr/bin/env python
# Phone-Setup for the exhibition <Privacy Arena>.
# This script makes a Raspberry Pi create sounds with a piezo speaker and plays sounds from a USB stick
# when a flex sensor is bended.
# SETUP: LIBRARIES
import RPi.GPIO as GPIO, time, os
import pygame
from random import randint
import json
# SETUP: MUSICTRACKS FROM AN EXTERNAL DEVICE
# Use your USB stick's name instead of 'USBSTICK' and adjust the tracknames to your liking.
trackOne = "/media/pi/USBSTICK/01.mp3"
trackTwo = "/media/pi/USBSTICK/02.mp3"
trackThree = "/media/pi/USBSTICK/03.mp3"
trackFour = "/media/pi/USBSTICK/04.mp3"
trackBusy = "/media/pi/USBSTICK/hangup.mp3"
# SETUP: PINS
# Piezo / output is connected to pin 22 on the Rasperry Pi.
DEBUG = 1
GPIO.setmode(GPIO.BCM)
piezo = 22 #piezo
# SETUP: IMPORT GLOBAL VARIABLES FROM SETTINGS.JSON
# This section reads variables from a .json file on the external USB stick. With it you can individualize different phones.
with open("/media/pi/USBSTICK/settings.json") as data_file:
data = json.load(data_file)
variables = data["data"]
timeBetweenCalls = variables["timeBetweenCalls"] #defines how long a phone will wait until it rings again
ringPause = variables["ringPause"] #defines the frequency of the ringing
ringDuration = variables["ringDuration"] #defines the length of the ringing
hzFrequency = variables["hzFrequency"] #defines the altitude of the ringing
threshold = variables["threshold"] #number to compare with sensorinput
# PLAY A SOUNDSNIPPET
def playSound( soundSnippet ):
pygame.mixer.init() #initialize mixer
pygame.mixer.music.load(soundSnippet) #load the sound snippet
pygame.mixer.music.play() #play the soundsnippet
while pygame.mixer.music.get_busy() == True:
continue
return
# CHOOSE A SOUNDSNIPPET, THEN CALL PLAYSOUND (ABOVE)
def chooseSound ( i ):
if (i == 1):
playSound( soundSnippet=trackOne )
elif (i == 2):
playSound( soundSnippet=trackTwo )
elif (i == 3):
playSound( soundSnippet=trackThree )
elif (i == 4):
playSound( soundSnippet=trackFour )
playSound( soundSnippet=trackBusy )
time.sleep(float(timeBetweenCalls))
return
# PIEZO OUTPUT
def playPiezo ( pin, hz ):
GPIO.setup(piezo, GPIO.OUT) #piezo setup
GPIO.output(piezo, GPIO.LOW) #piezo output setup
p = GPIO.PWM(pin, hz)
return p
# READ PINS
# Reads the pins in a defined frequency and returns the result
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1) #frequency
GPIO.setup(RCpin, GPIO.IN)
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
#piezo setup
p = playPiezo ( pin=piezo, hz=float(hzFrequency) )
p.start(0)
#piezo ranges
for dc in range(0, 20, 20):
p.ChangeDutyCycle(dc)
#test every 0.01 if new input is higher than threshold
for ringPauseTimer in range (0, int(ringPause)):
print(ringPauseTimer)
time.sleep(0.01)
reading = RCtime(4)/100 #this calibrates the sensor's reading.
if (reading >= float(threshold)): #if the threshold is higher than what was read >>
r = randint(1, 4) #generate a random number and pass it on to the function chooseSound
chooseSound ( i=r )
for dc in range(int(ringDuration), -1, -5):
p.ChangeDutyCycle(dc)
time.sleep(0.1)
time.sleep(0.1)
{
"data": {
"timeBetweenCalls": "50",
"ringPause": "8",
"ringDuration": "15",
"hzFrequency": "500",
"threshold" : "300"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment