Monday, May 4, 2015

Human Voices on PLC Application using Raspberry Pi 2




Human voices/sounds on PLC application such as sounds/voices for emergency call, supply part request, and others.
This application using Raspberry Pi 2 and Python Programming for play sound or MP3 Player.
For play voices use command from PLC programming with modbus protocol communication between Raspberry Pi 2 and PLC via RS232 Serial Port.
See video about human voices on PLC application:



Hardware for Human Voices on PLC:
1. Raspberry Pi 2
2. 5V Micro USB Power Supply for the Raspberry Pi
3. Active Speakers with 3.5mm audio jack cable
4. RS-232 PLC Cable: RS232 PPI Multi Master Cable
5. TTL to RS232 Module with male connector
6. Siemens S7-200 PLC
7. 2 PCS Limit Switch for PLC Programming Test:
  - 1 PCS connected to I0.0 on PLC
  - 1 PCS connected to I0.3 on PLC

Hardware for Human Voices on PLC

Hardware Connections of Human Voices on PLC Application:

Hardware Connections of Human Voices on PLC Application

Download Project File for Human Voices on PLC Application:
1. For Raspberry Pi 2 Project file, click here
Extract project file to USB Flash disk, and insert to USB on Raspberry Pi, and then Copy Paste to folder /home/pi on Raspberry Pi.
2. PLC Ladder Programming project file, click here

Step by Step Raspberry Pi Setup for Human Voices on PLC Application:
This Video about how to use project file for Raspberry Pi:



STEP 1: Auto Start the Desktop (LXDE)
1.sudo raspi-config
2.Select Enable Boot to Desktop/Scratch from the menu and press Enter
3.Select Desktop Login as user pi at the Graphical Desktop.
4.Select and Enter, then select to reboot.

STEP 2: Hardware setup of Serial Port
In LXTerminal:
sudo cp /boot/cmdline.txt /boot/cmdline.bak
sudo cp /etc/inittab /etc/inittab.bak
sudo nano /boot/cmdline.txt
Remove "console=ttyAMA0,115200"
Ctrl+X to exit, Y to save followed by enter twice
sudo nano /etc/inittab
Put a '#' before "T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Ctrl+X to exit, Y to save followed by enter twice

STEP 3: Copy Paste Project file
Copy Paste All Project file to folder /home/pi on Raspberry Pi 2

STEP 4: Serial Module Install
In LXTerminal
type cd /home/pi
press Enter
type cd pyserial-2.7
press Enter
type sudo python setup.py install
press Enter

STEP 5: Modbus Module Install
In LXTerminal
type cd /home/pi
press Enter
type cd MinimalModbus-0.6
press Enter
type sudo python setup.py install
press Enter

STEP 6: Auto-Run Python Script
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
Scroll to the bottom and add the following line :
@python /home/pi/sound-on-plc.py
Ctrl+X to exit, Y to save followed by enter twice

Voices Setting in Python on Raspberry Pi:

Voices Setting in Python on Raspberry Pi



Voices Setting in PLC Ladder Programming:
Voices example: emergency in machine 1
emergency with sound index 2
in with sound index 4
machine with sound index 5
1 with sound index 8
Set in PLC Ladder:
VW1100 = 2
VW1102 = 4
VW1104 = 5
VW1106 = 8

Voices Setting in PLC Ladder Programming

Python Code Programming:
import serial
import minimalmodbus
import os
import pygame
from datetime import datetime

#Load the sound drivers
os.system('sudo modprobe snd_bcm2835')
#Setup for the 3.5mm jack output
os.system('sudo amixer cset numid=3 1')
#Master Volume
os.system('amixer cset numid=1 -- 90%')

#Pygame init
pygame.mixer.init()
#Volume in pygame
pygame.mixer.music.set_volume(1.0)

folder='mp3file/'
sound_array=[]
sound_array.append('') #sound index 0
sound_array.append('no_sound_500ms.mp3') #sound index 1
sound_array.append('emergency.mp3')#sound index 2
sound_array.append('Empty.mp3')#sound index 3
sound_array.append('in.mp3')#sound index 4
sound_array.append('machine.mp3')#sound index 5
sound_array.append('stop.mp3')#sound index 6
sound_array.append('Supply.mp3')#sound index 7
sound_array.append('1.mp3')#sound index 8
sound_array.append('2.mp3')#sound index 9
sound_array.append('3.mp3')#sound index 10
sound_array.append('4.mp3')#sound index 11
sound_array.append('5.mp3')#sound index 12

#ModBus Setting , Address Slave = 1
ins = minimalmodbus.Instrument('/dev/ttyAMA0',1)
ins.serial.baudrate = 9600
ins.serial.bytesize = 8
ins.serial.parity = serial.PARITY_EVEN
ins.serial.stopbits = 1
ins.serial.timeout = 0.2
ins.mode = minimalmodbus.MODE_RTU

def playsound(filename):
if filename=='': return
print filename
pygame.mixer.music.load(folder+filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy()==True:
ins.write_register(1,1)

while True:
try:
#reg0=time running/modul ready
#reg1=status, play=1, no play=0
#reg2=Play Command, 1=play, 0=no play
#start reg3 = index of word sound
wordtot=20
t=datetime.now()
ins.write_register(0,t.second)
cmd = ins.read_register(2)
if int(cmd)>0:
wordnum=ins.read_registers(3,wordtot)
for i in wordnum:
playsound(sound_array[i])
ins.write_registers(1,[0]*2)
except Exception, e:
pass


Labels:





Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner