martes, 28 de mayo de 2019

Audio configuration


The Raspberry Pi has two audio output modes: HDMI and headphone jack. You can switch between these modes at any time.
If your HDMI monitor or TV has built-in speakers, the audio can be played over the HDMI cable, but you can switch it to a set of headphones or other speakers plugged into the headphone jack. If your display claims to have speakers, sound is output via HDMI by default; if not, it is output via the headphone jack. This may not be the desired output setup, or the auto-detection is inaccurate, in which case you can manually switch the output.

Changing the audio output

There are three ways of setting the audio output.

Desktop volume control

Right-clicking the volume icon on the desktop taskbar brings up the audio output selector; this allows you to select between the internal audio outputs. It also allows you to select any external audio devices, such as USB sound cards and Bluetooth audio devices. A green tick is shown against the currently selected audio output device — simply left-click the desired output in the pop-up menu to change this. The volume control and mute operate on the currently selected device.

Command line

The following command, entered in the command line, will switch the audio output to HDMI:
amixer cset numid=3 2
Here the output is being set to 2, which is HDMI. Setting the output to 1switches to analogue (headphone jack). The default setting is 0 which is automatic.

raspi-config

Open up raspi-config by entering the following into the command line:
sudo raspi-config
This will open the configuration screen:
raspi-config screen
Select Option 8 Advanced Options and press Enter, then select Option A6: Audio and press Enter:
Audio configuration screen
Now you are presented with the two modes explained above as an alternative to the default Auto option. Select a mode, press Enter and press the right arrow key to exit the options list, then select Finish to exit the configuration tool.

If you're still not getting sound via HDMI

In some rare cases, it is necessary to edit config.txt to force HDMI mode (as opposed to DVI mode, which does not send sound). You can do this by editing /boot/config.txt and setting hdmi_drive=2, then rebooting for the change to take effect.

domingo, 24 de marzo de 2019

python AMI event filter

#!/usr/bin/python
import os
import time
from settings import login, connection

from asterisk.ami import AMIClient

client = AMIClient(address='127.0.0.1',port=5038)
client.login(username='admin',secret='2256')


def event_listener(event,**kwargs):

       #print(vars(event))  ## print all info
    print(event[u'Channel'])  ##print only the channel name key
client.add_event_listener(
    event_listener,
    white_list='Newstate',
    ChannelStateDesc='Ringing'
)


client = AMIClient(**connection)
future = client.login(**login)
if future.response.is_error():
    raise Exception(str(future.response))


try:
    while True:
        time.sleep(10)
except (KeyboardInterrupt, SystemExit):
    client.logoff()

https://github.com/ettoreleandrotognoli/python-ami/blob/master/README.rst
https://pypi.org/project/asterisk-ami/

Python Socket

https://realpython.com/python-sockets/

jueves, 27 de diciembre de 2018

Python Asterisk AGI

AGI Scripting with Python

AGI stands for Asterisk Gateway Interface. It is very similar to CGI (common gateway interface) which is one of the first forms of web development.
AGI allows for Asterisk to communicate in real-time with an external process. Asterisk will send output to the external process through “Standard Out” (STDOUT), and will listen for input on “Standard In” (STDIN). STDOUT and STDIN are what you use whenever you are interacting with the command line. When we type “ls” in the terminal we are sending in that command to the terminal process through its STDIN, and when the terminal shows us the list of files it is sending that list to our screen through its STDOUT.
Calling AGI Scripts
To call an AGI script from the Asterisk dialplan you use the AGI command
exten => s,1,Answer();
exten => s,n,AGI(/home/ck987/asterisk_agi/agi_test.py);
By default Asterisk looks for the AGI script in the agi-bin (/var/lib/asterisk/agi-bin). You can place them anywhere but my instructions will assume they’re in your asterisk_agi directory.
Debugging
In the Asterisk administration console you can type: “agi set debug on” to enable AGI debugging.  “agi set debug off” will turn off AGI debugging.
Basic AGI Scripting in Python
#!/usr/bin/env python
import sys
from asterisk.agi import *

agi = AGI()
agi.verbose("python agi started")
callerId = agi.env['agi_callerid']
agi.verbose("call from %s" % callerId)
while True:
  agi.stream_file('vm-extension')
  result = agi.wait_for_digit(-1)
  agi.verbose("got digit %s" % result)
  if result.isdigit():
    agi.say_number(result)
  else:
   agi.verbose("bye!")
   agi.hangup()
   sys.exit()

Upload, Test and Run
Save the above script to a file, let’s call it “easy.py” for now. Using Fetch or another SFTP application upload the script to the “asterisk_agi” in your home directory. Log into the server using SSH and navigate to the directory containing the agi scripts (cd asterisk_agi) and execute the command required to make the file executable (chmod 755 easy.py).
To test that the file runs without error, type the following:
./easy.py
This should start the execution of the file, if it doesn’t there is an error that you need to fix. The application should wait indefinitely for input (which it is expecting from Asterisk through STDIN). You can simulate this by hitting return a couple of times. You should see some semblance of the commands that are sent to Asterisk via STDOUT.
The Asterisk AGI library I chose for Python was pyst2, which is actively maintained but has little documentation.  Your best bet is to look at the source code here: https://github.com/rdegges/pyst2
More Fun Get the current weather!
#!/usr/bin/env python
import urllib2
import xml.etree.ElementTree as ET
import sys
from asterisk.agi import *

agi = AGI()
# for a complete list of US cities, go to
# http://www.weather.gov/xml/current_obs/
weatherURL = []
weatherURL.append("http://w1.weather.gov/xml/current_obs/KNYC.xml") #NYC
weatherURL.append("http://w1.weather.gov/xml/current_obs/KJFK.xml") #JFK
weatherURL.append("http://w1.weather.gov/xml/current_obs/KART.xml") #Watertown, NY
weatherURL.append("http://w1.weather.gov/xml/current_obs/KBGM.xml") #Binghamton
weatherURL.append("http://w1.weather.gov/xml/current_obs/KBUF.xml") #Buffalo
weatherURL.append("http://w1.weather.gov/xml/current_obs/KDKK.xml") #Chautauqua County
weatherURL.append("http://w1.weather.gov/xml/current_obs/KDSV.xml") #Dansville
weatherURL.append("http://w1.weather.gov/xml/current_obs/KELM.xml") #Corning, NY
weatherURL.append("http://w1.weather.gov/xml/current_obs/KHPN.xml") #Westchester
weatherURL.append("http://w1.weather.gov/xml/current_obs/KFRG.xml") #Farmingdale, NY

while True:
  agi.stream_file('vm-extension')
  result = agi.wait_for_digit(-1)
  if result.isdigit():
   request = urllib2.Request(weatherURL[int(result)], headers={"Accept" : "application/xml"})
   xml = urllib2.urlopen(request)
   tree = ET.parse(xml)
   root = tree.getroot()
   temp_f = root.find('temp_f').text
   agi.say_number(temp_f)
  else:
   sys.exit()
http://www.itp-redial.com/class/weekly-notes/week4-notes/agi-scripting-with-python

martes, 25 de diciembre de 2018

python MSSQL

https://docs.microsoft.com/en-us/sql/connect/python/pymssql/step-3-proof-of-concept-connecting-to-sql-using-pymssql?view=sql-server-2017

http://pymssql.org/en/stable/pymssql_examples.html

martes, 18 de diciembre de 2018

python for in dictionary

>>> notas=[80,89,110]
>>> for x in notas:
...     print(notas)
...     if x>100:
...      print("buen trabajo")





>>> notas=[89,90,101]
>>> for x in notas:
...     print(x)
...     if x>100:
...         print("demasiado bn")
...
89
90
101

domingo, 11 de noviembre de 2018