If you need to get Arduino and Raspberry Pi working together, here's a little demo.
Processing code for Arduino.
#define trigPin 13
#define echoPin 12
#define piPin 7
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(piPin, OUTPUT);
}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds( 1000 );
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if( distance >= 200 || distance <= 0){
Serial.println("Out of range");
}else{
Serial.print(distance);
Serial.println(" cm");
if( distance < 100 ){
digitalWrite(piPin, HIGH);
Serial.println("sending intruder alert");
}
}
delay(500);
}
Python code for Raspberry Pi.
import RPi.GPIO as GPIO
import sys
import wave
import pyaudio
import time
#Read
channel = 7
#Read the audio file I want to play
CHUNK = 1024
#Set up the board
GPIO.setmode(GPIO.BOARD)
#Receive electrical signals from arduino
GPIO.setup(channel,GPIO.IN)
#Play audio files once the signal comes int
p = pyaudio.PyAudio()
#Received a signal from arduino
while True:
if GPIO.input(channel) == GPIO.HIGH:
print("Intruder Alert")
wf = wave.open("intruder.wav", "rb")
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getchannels(), rate=wf.getframerate(), output=True)
#read in the stream
data = wf.readframes(CHUNK)
#while data is not empty
while data !='':
#play data
stream.write(data)
#read the next chunk
data = wf.readframes(chunk)
stream.stop_stream()
stream.close()
else:
print("No intruder")
#don't peg the CPU
time.sleep(1)
#terminal the audio
p.terminate()
GPIO.cleanup()
Subscribe to new posts
Processing your application
Please check your inbox and click the link to confirm your subscription
There was an error sending the email