
Raspberry Pi – Notifying new emails and calendar appointments
I've been wanting to pass you a couple of scripts that I usually use to make a Raspberry Pi that I have connected to a speaker talk to me for a long time; in this document we will only see how to configure the Pi to talk to us through the speaker and tell us if we have received an email, and if so, it will indicate the sender and the subject of the email; as well as if we have any appointments on the calendar in the next few years. 5 minutes, will remind us of it over the loudspeaker!
Converting Text to Speech,
We start by setting up the system that will speak to us, using a text-to-speech converter, a Text-to-Speech (TTS) We will use the 'festival' voice synthesizer, we will install it on our Raspberry Pi, that supports any language, Later we will download the diphones in Spanish from the Guadalinex forge, We will have two voices, a female (Silvia) and a male (Pedro), everyone can use the one they like 😉 the most
We install festival on our Raspberry Pi:
[sourcecode language=”Plain”]sudo apt-get -y install festival[/SourceCode]
If we want to use The Exit 3,5 audio jack for sound instead of HDMI we will run:
[sourcecode language=”Plain”]amixer cset numid=3 1[/SourceCode]
It will be interesting to put the volume we want to hear well, and we record the changes:
[sourcecode language=”Plain”]alsamixer
sudo alsactl store[/SourceCode]
GOOD, Now we will download the languages we are interested in, put the one you want, I leave you in order first female voice and the other male voice. We also install them and indicate in the configuration file which one to use:
[sourcecode language=”Plain”]
wget https://github.com/guadalinex-archive/hispavoces/raw/master/packages/festvox-sflpc16k_1.0-1_all.deb
wget https://github.com/guadalinex-archive/hispavoces/raw/master/packages/festvox-palpc16k_1.0-1_all.deb
sudo dpkg -i festvox-sflpc16k_1.0-1_all.deb
sudo dpkg -i festvox-palpc16k_1.0-1_all.deb
sudo vim /etc/festival.scm [/SourceCode]
At the end we add the one we are interested in, and we comment on the others:
[sourcecode language=”Plain”];(set! voice_default 'voice_JuntaDeAndalucia_es_pa_diphone)
(set! voice_default 'voice_JuntaDeAndalucia_es_sf_diphone)[/SourceCode]
And we can try to convert a text into audio with:
[sourcecode language=”Plain”]echo Hello Manolo, how are you | festival –tts[/SourceCode]
If we are interested, we can generate a script that when executed with a parameter reads that parameter, more useful to then call it from other scripts, In my case I call him ‘textoAvoz.sh':
[sourcecode language=”Plain”]ECHO $1 | festival –tts[/SourceCode]
Read calendar from the Raspberry Pi,
If we want to read our calendar, The best experience I have had was with 'Gcalcli', since any calendar can be managed, we can add as many calendars to our Gmail account that we are interested in, whether from our Exchange or other systems, as well as subscribing to calendars that interest us…
We install gcalcli or
[sourcecode language=”Plain”]sudo apt-get install git python-pip python-gdata python-dateutil python-gflags python-vobject python-parsedatetime
sudo pip install gcalcli[/SourceCode]
Once installed, we will run the following command to obtain the URL that allows us to obtain the activation code and give it permissions in our Gmail account. We run the following command and open the URL in a browser, We'll paste the code and that's it!
[sourcecode language=”Plain”]gcalcli list –noauth_local_webserver[/SourceCode]
We try to see if we see the calendars in our account:
[sourcecode language=”Plain”]gcalcli list[/SourceCode]
And we try to show us if we have events in the next few years. 15 minutes:
[sourcecode language=”Plain”]gcalcli remind 15 ‘echo %s'[/SourceCode]
We create the following script ‘lee_calendario.sh’ and make it executable:
[sourcecode language=”Plain”]
#!/bin/bash
CITA='/usr/local/bin/gcalcli remind 4 'echo %s’ –cal=Personal'
Echo $CITA
if [ "$CITA" == "" ]; then
echo "No appointments"
else
echo "There are dates"
TIME=$(cut -c-7 ≪≪≪ "$CITA")
Echo $HORA
SUBJECT=${APPOINTMENT:7:1000}
PHRASE1="Hector, in five minutes, at "
SENTENCE2=" Remember "
PHRASE="$FRASE 1 $HORA $FRASE 2 $ASUNTO"
Echo $FRASE
Echo $FRASE | /usr/bin/festival –tts
fi
[/SourceCode]
And we tried to execute it!
[sourcecode language=”Plain”]./lee_calendario.sh[/SourceCode]
We put it in crontab so that it runs every 5 minutes and you're done!
[sourcecode language=”Plain”]*/5 * * * * /home/pi/lee_calendario.sh[/SourceCode]
Read mail from the Raspberry Pi,
With this simple python script we will be able to obtain unread emails through an IMAP connection to our mail server, it will read us the sender and the subject of each email. We create the 'lee_mail.py' file:
[sourcecode language=”Plain”]
import imaplib
from email.header import decode_header
mail = imaplib. IMAP4_SSL('FQDN_SERVIDOR_CORREO')
mail.login(‘USUARIO@DOMINIO’, ‘CONTRASEÑA’)
mail.select(‘INBOX’, readonly=True)
typ, new = mail.search(None, "UNSEEN")
new = new[0].split()
If Len(new) > 1:
Mails = ‘Tienes ‘ + str(len(new)) + ‘ correos nuevos’
import subprocess
subprocess.call(["/home/pi/textoAvoz.sh", Mails])
elif len(new) == 1:
Mails = ‘Tienes un correo nuevo’
import subprocess
subprocess.call(["/home/pi/textoAvoz.sh", Mails])
if (len(new) > 0):
res = list()
res1 = list()
for msg in new:
typ, data = mail.fetch(Msg, ‘(BODY[HEADER. FIELDS (SUBJECT)])’) # FROM, DATE, etc
data = data[0][1].strip()
decoded = decode_header(date[9:])[0] # "Subject: " is 9 characters long
text = decoded[0].Replace("rn", "")
if (decoded[1] != None):
text = unicode(text, decoded[1])
res.append(text.encode(‘utf-8’))
typ, data1 = mail.fetch(Msg, ‘(BODY[HEADER. FIELDS (FROM)])’) # FROM, DATE, etc
data1 = data1[0][1].strip()
Arroba = data1.index(‘<‘)
decoded1 = decode_header(data1[6:Arroba-1])[0] # "Subject: " is 9 characters long
text1 = decoded1[0].Replace(‘"’, "")
if (decoded1[1] != None):
text1 = unicode(text1, decoded1[1])
res1.append(text1.encode(‘utf-8’))
text1 = unicode(text1)
text = unicode(text)
import unicodedata
text1output = unicodedata.normalize(‘NFKD’, text1).encode(‘ascii’,’ignore’)
textoutput = unicodedata.normalize(‘NFKD’, text).encode(‘ascii’,’ignore’)
Text = '"Mail from ‘ + text1output + ‘, with subject: ‘ + textoutput + ‘"’
print (Text)
import subprocess
subprocess.call(["/home/pi/textoAvoz.sh", Text])
[/SourceCode]
We remember to make it executable, put it in the cron anyway if we are interested in the programming that interests us.
Easy and simple how to know if we have appointments on the calendar or emails and we want a sweet voice to tell us! I hope you enjoy it!