Ultimate pi zero beacon

Projet: Utiliser un orange pi avec interface TRX pour faire une balise avec les fonctions balise CW et répéteur.

 

Vous pouvez télécharger le script et les autres fichiers ICI.

1.Installation des paquets

apt-get install python-dev libttspico-utils sox qt4-qmake libx11-dev qt4-qmake cmake build-essential qt4-qmake cw python-configparser

git clone https://github.com/duxingkei33/orangepi_PC_gpio_pyH3

nano orangepi_PC_gpio_pyH3//pyA20/gpio/mapping.h

ligne 88 ajouter:

{   « PA14 »,  SUNXI_GPA(14),  10  },
{   « PA15 », SUNXI_GPA(15),  19  },

cd orangepi_PC_gpio_pyH3
python setup.py install

2.Mise en place du script principale:

mkdir /etc/balise

nano /etc/balise/ultimatebeacon.py

#!/usr/bin/python
#-*- coding: utf-8 -*-

import os, sys, time, subprocess, threading, commands
from ConfigParser import SafeConfigParser

scriptspath = os.path.dirname(sys.argv[0]) + "/"

config = SafeConfigParser()
config.read(scriptspath + 'config.ini')

if not os.getegid() == 0:
	sys.exit('Script must be run as root')

from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port


PTTSTAT = False


#********Variables à adapter**************

PTTPIN = port.PA14
COSPIN = port.PA15

alsadeviceinput = config.get('main', 'alsadeviceinput')
alsadeviceoutput = config.get('main', 'alsadeviceoutput')

TimeToPTT = config.getint('main', 'TimeToPTT')
TimeToReleasePTT = config.getint('main', 'TimeToReleasePTT')

BeaconInterval = config.getint('main', 'BeaconInterval')
BeaconIntervalMin = config.getint('main', 'BeaconIntervalMin')

parrotisAlive = config.getint('main', 'parrotisAlive')
beaconisAlive = config.getint('main', 'beaconisAlive')


#****************************************

#GPIO
gpio.init()
gpio.setcfg(PTTPIN, gpio.OUTPUT)
gpio.setcfg(COSPIN, gpio.INPUT)
gpio.pullup(COSPIN, gpio.PULLUP)

def savparam(param,value):
	global config
	config.set('main', param, str(value))

def stop_if_already_running():
	script_name = os.path.basename(__file__)
	pid = str(os.getpid())
	l = commands.getstatusoutput("ps aux | grep -e "+script_name+" | grep -v grep | grep -v "+ pid +" | grep -v awk |  awk '{print $2}'")
	if l[1]:
		sys.exit(0);

def playwav(chemin):
	os.system("aplay "+chemin+" -q -N -D "+alsadeviceoutput)

def PTT(status):
	CTTPTT = int(round(time.time() * 1000))
	global PTTSTAT
	if status == True:
		while(PTTSTAT == True):
			time.sleep(1)
		PTTSTAT = True
		gpio.output(PTTPIN, 1)
		while ((CTTPTT + TimeToPTT) > int(round(time.time() * 1000))):
			time.sleep(0.1)
		return True
	else:
		CTTPTT = int(round(time.time() * 1000))
		while ((CTTPTT + TimeToReleasePTT) > int(round(time.time() * 1000))):
			time.sleep(0.1)
		gpio.output(PTTPIN, 0)
		PTTSTAT = False
		return False
		
def COS():
	if gpio.input(COSPIN) == 1:
		return False
	else:
		return True

def mes(N):
	print N
	PTT(True)
	try:
		playwav(scriptspath + N +".wav")
	except:
		pass
	PTT(False)

def play_line_pico(text):
		os.system("pico2wave -l fr-FR -w /dev/shm/temp.wav '" + text + "';")
		os.system("sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /dev/shm/mess.wav")
		PTT(True)
		os.system("aplay  -q -N -r 48000 -c 2 -D "+alsadeviceoutput+" /dev/shm/mess.wav")
		PTT(False)
	
def action(mess):
	global parrot, beacon, BeaconInterval, BeaconIntervalMin, commande
	if mess.find("0") != -1:
		mes("som")
	if mess.find("1") != -1:
		if beacon.isAlive() == False:
			mes("baon")
			if len(commande) > 1 :
				if int(commande[1:]) >= BeaconIntervalMin:
					BeaconInterval = int(commande[1:])
				else:
					BeaconInterval = BeaconIntervalMin
					play_line_pico("attention interval minimal fixé à : " + str(BeaconIntervalMin) + " secondes")
			play_line_pico("intervale: " + str(BeaconInterval) + " secondes")
			beacon = beaconClass()
			beacon.start()
		else:
			try:
				beacon.stop()
				mes("baoff")
				beacon = beaconClass()
			except:
				pass
	if mess.find("2") != -1:
		if parrot.isAlive() == False:
			mes("paon")
			parrot.start()
		else:
			try:
				parrot.stop()
				mes("paoff")
				parrot = parrotClass()
			except:
				pass
	if mess.find("3") != -1:
		mes("hello")
	if mess.find("9") != -1:
		mes("reboot")
		time.sleep(5)
		os.system("shutdown -r -f now")
	if mess.find("*") != -1:
		savparam("BeaconInterval",BeaconInterval)
		if parrot.isAlive() == True:
			savparam("parrotisAlive",1)
		else:
			savparam("parrotisAlive",0)
		if beacon.isAlive() == True:
			savparam("beaconisAlive",1)
		else:
			savparam("beaconisAlive",0)
		mes("savparam")
		with open(scriptspath + 'config.ini', 'w') as f:
			config.write(f)
		
		
		
class parrotClass(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.event = threading.Event()

    def run(self):
		global processparrot, commande
		while not self.event.is_set():
			try:
				processparrot = subprocess.Popen("arecord -q -N -f S32_LE -r 48000 -c 2 -D " + alsadeviceinput + " -t raw |  sox -t raw -r 48000 -b 16 -L -e signed -c 1 - -t raw - silence 1 0.1 3% 5 1.0 5% trim 0 60 highpass 300 lowpass 5000  | sox -t raw  -r 48000 -b 32 -L -e signed -c 2 - /dev/shm/parrot.wav  speed 0.48 gain -n -2 compand 0.1,0.1 -60,-60,-40,-10,-20,-8,-10,-6,-6,-5,-3,-3 -6", shell=True, stdout=subprocess.PIPE)
				processparrot.wait()
				if commande == "":
					PTT(True)
					processparrot = subprocess.Popen("aplay /dev/shm/parrot.wav -q -N -D "+alsadeviceoutput, shell=True, stdout=subprocess.PIPE)
					processparrot.wait()
					PTT(False)
			except:
				pass

    def stop(self):
		global processparrot
		processparrot.kill()
		os.system("ps -ef | grep /dev/shm/parrot.wav | grep -v grep | awk '{print $2}' | xargs kill -9")
		self.event.set()
		
parrot = parrotClass()

class beaconClass(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.event = threading.Event()

    def run(self):
        while not self.event.is_set():
			global BeaconInterval
			print BeaconInterval
			mes("cw")
			self.event.wait( BeaconInterval )

    def stop(self):
        self.event.set()
		
beacon = beaconClass()
		
class getcommand(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.event = threading.Event()

    def run(self):
			global multimon_ng, commande
			multimon_ng = subprocess.Popen("(arecord -t raw -D "+alsadeviceinput+" -r 48000 -c 2 -f S32_LE | sox -t raw -e signed-integer -b32 -r 48000 -c 2 - -t raw -e signed-integer -b 16 -r 22050 - |  multimon-ng -a DTMF -q  -t raw -)",
				stdout=subprocess.PIPE,
				stderr=subprocess.PIPE,
				shell=True)
			ready = False
			while(COS()==True):
				nextline = multimon_ng.stdout.readline()
				multimon_ng.poll()
				if nextline.__contains__("DTMF:"):
					nextline = nextline.split('DTMF: ')[1]
					if nextline.find("#") != -1:
						ready = True
					if nextline.find("#") == -1 and ready == True: 
						commande += str(nextline.rstrip())


    def stop(self):
		global multimon_ng
		multimon_ng.kill()
		os.system("ps -ef | grep multimon-ng | grep -v grep | awk '{print $2}' | xargs kill -9")
		self.event.set()
	
if __name__ == '__main__':
	stop_if_already_running()
	try:
		global commande
		mes("start")
		if parrotisAlive == 1:
			commande = "2"
			action(commande[0])
			time.sleep(1)
		if beaconisAlive == 1:
			commande = "1" + str(BeaconInterval)
			action(commande[0])
			time.sleep(1)
		while(True):
			if COS()==True:
				commande = ""
				print "rx start"
				getC = getcommand()
				getC.start()
				while(COS()==True):
					time.sleep(0.5)
				getC.stop()
				print "rx stop"
				if len(commande) > 0 :
					print commande
					action(commande[0])

			time.sleep(0.1)
	except KeyboardInterrupt:
		PTT(False)
		try:
			getC.stop()
		except:
			pass
		try:
			beacon.stop()
		except:
			pass
		try:
			parrot.stop()
		except:
			pass
		print ("Goodbye.")
	except:
		PTT(False)
		try:
			getC.stop()
		except:
			pass
		try:
			beacon.stop()
		except:
			pass
		try:
			parrot.stop()
		except:
			pass
		pass
		
		
		
		

chmod 755 /etc/balise/ultimatebeacon.py

3.Mise en place des éléments secondaires

cd /tmp && wget https://github.com/Kerrick/cwwav/archive/master.tar.gz --output-document cwwav.tar.gz
tar -zxvf cwwav.tar.gz && cd cwwav-master
make
apt-get install libsndfile-dev
make
make install

mkdir /etc/balise

cd /etc/balise

pico2wave -l fr-FR -w /dev/shm/temp.wav "démarage de la balise"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/start.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Sommaire 1 activer ou désactiver la balise 2 activer ou désactiver le peroquet"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/som.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Mode balise activé"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/baon.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Mode balise désactivé"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/baoff.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Mode répéteur activé"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/paon.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Mode répéteur désactivé"
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/paoff.wav

pico2wave -l fr-FR -w /dev/shm/temp.wav "Redémarage en cours"
root@orangepizero:~# sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/reboot.wav

echo "QRA DE ET / ET DE QRA" | cwwav -f 700 -w 20 -o /dev/shm/temp.wav
sox -t wav /dev/shm/temp.wav -esigned-integer -b32 -r 48000 -c 2 -t wav /etc/balise/cw.wav

 

4.Lancement au démarage

nano /etc/balise/startU.sh

#!/bin/bash
sleep 20
nice -n -15 /usr/bin/python /etc/balise/ultimatebeacon.py &

nano /etc/rc.local

ajouter:

/etc/balise/startU.sh &

avant exit 0

 

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.