In my previous blog post, titled Reading out the weather using eSpeak , we installed eSpeak on the Intel Edison to read out the weather via a speaker connected to the USB sound card. Continuing on the same theme, and since I bought a new bluetooth speaker this weeks from my local electronic store for a 50% discount, we are going to enable bluetooth on Intel Edison and pair the new speaker. In addition, we are going to use tweepy, which is a python wrapper library for the twitter API to read tweets , and then use eSpeak to create a wav file, which is then read out by gstreamer.
While putting the python code together and testing it out, I found out that Workshopshed (Andy Clark) is working on an interesting 3D part for his project - Interactive Racing Car Driver , hmm I wonder if that is going to be a part of his next blog post.. Sorry Workshopshed , but the cat is out of the bag ! ...Here are the list of tweets read out as part of the video above-
Here are commands to follow, to enable bluetooth on the Intel Edison and then pair and connect to the bluetooth speaker , the speaker I bought is the Vivitar portable bluetooth speaker.
SSH into the Intel Edsion and run the following command as shown in the pictures below
rfkill unblock bluetooth
bluetoothctl
This puts the bluetooth on the Intel Edison in discoverable mode and then you can scan for bluetooth device using the command
scan on
Make a note of the mac address of your speaker( in my case it is [NEW] Device 00:25:DB:14:53:30 Vivitar Speaker) and pair it with the Edison, using
pair 00:25:DB:14:53:30
And to connect to the speaker, use the command
connect 00:25:DB:14:53:30
Then quit the bluetoothctl prompt using
quit
Now run the following command to verify that the speaker is recognized in pulse audio
pactl list sinks
Configure the default sink to use pulse audio server with the following command, replacing with the details of your device.
pactl set-default-sink bluez_sink.00_25_DB_14_53_30
It is now finally! time to play some sound on the bluetooth speaker using gStreamer
gst-launch-1.0 filesrc location= /usr/share/sounds/alsa/Front_Center.wav ! wavparse ! pulsesink
Note: we are able use the Front_Center.wav, because we installed ALSA-utils in previous blog post, but we don’t really need to install ALSA as we are using gStreamer to play the audio wav file.
{gallery} Connecting to Bluetooth Speaker |
---|
Intel Edison and Vivitar portable bluetooth speaker |
Pairing and connecting to the bluetooth speaker. |
output of the command - pactl list sinks |
Playing wav file using gStreamer |
Now if you are following this blog independently, and not in conjunction with previous blog here are the steps to install eSpeak and create a wav file that you can play on the bluetooth speaker using gStreamer. To install eSpeak run the following command
opkg install espeak
Run the following command to create a wav file
espeak -w speech.wav "hello from the Intel Edison bluetooth test successful"
Now run the gStreamer to play the sound via the bluetooth speaker using the wav file created
gst-launch-1.0 filesrc location= speech.wav ! wavparse ! pulsesink
To read tweets, we will install tweepy which is python wrapper library for the twitter API, use the following command to install tweepy
pip install tweepy
Note: If you run into errors while installing tweepy this could be because you have an older version PIP installed, you need pip version 9/higher.To upgrade pip run the following command -
pip install --upgrade pip
Now you will have to go to - https://dev.twitter.com/ and create an App, and generate access tokens. And make a note of the following, which you will have to replace in the python program below
- consumer key
- consumer secret
- access_token key
- access_token secret
In addition, modify the hashTag variable if you want to scrape twitter for a different hash tag.
#!/usr/bin/python #create by Carmelito to use espeak to read out the tweets based on a hashTag on a speaker connected to the Intel Edison #Based on the tweepy libary http://www.tweepy.org/ #Follow the blog on element14 to install espeak and tweepy import tweepy import re,string import os,subprocess consumer_key = "XXXXXXXXXXXXXXXXXX" consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXX" access_token_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" access_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" hashTag = "inteledison" OAUTH_KEYS = {'consumer_key':consumer_key, 'consumer_secret':consumer_secret, 'access_token_key':access_token_key, 'access_token_secret':access_token_secret} auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret']) api = tweepy.API(auth) getTweet = tweepy.Cursor(api.search, q=hashTag).items(30) #Filtering the un-readable things in the tweet.text def strip_links(text): link_regex = re.compile('((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)', re.DOTALL) links = re.findall(link_regex, text) for link in links: text = text.replace(link[0], ', ') return text def strip_all_entities(text): entity_prefixes = ['@','#'] for separator in string.punctuation: if separator not in entity_prefixes : text = text.replace(separator,' ') words = [] for word in text.split(): word = word.strip() if word: if word[0] not in entity_prefixes: words.append(word) return ' '.join(words) file = open("tweetsFile.txt","w") for tweet in getTweet: #filtering tweets in english(en), and also filtering retweets (RT @) based on tweepy docs if tweet.lang == 'en' and 'RT @' not in tweet.text: fileText = 'From '+ tweet.author.name.encode('utf8') + ' the tweets reads ' + strip_all_entities(strip_links(tweet.text.encode('utf8'))) + " " # added encode('utf8') to resolve the encoding error on some tweets file.write(fileText) print fileText print "Name:", tweet.author.name.encode('utf8') #this is what we need print "Screen-name:", tweet.author.screen_name.encode('utf8') print "Tweet created:", tweet.created_at print "Tweet:", tweet.text.encode('utf8') #this is what we need to apply a filter #print "Retweeted:", tweet.retweeted #print "Favourited:", tweet.favorited #print "Location:", tweet.user.location.encode('utf8') #print "Time-zone:", tweet.user.time_zone #print "Geo:", tweet.geo print "-------------------------------" file.close() #creating a wav file using espeak and the txt file create above os.system("espeak -ven+f3 -s 150 -f tweetsFile.txt -w readTweets.wav") #using gstream to play the wav file above on the bluetooth speakser os.system ("gst-launch-1.0 filesrc location= readTweets.wav ! wavparse ! volume volume=0.1 ! pulsesink")
Now, it’s pretty obvious that if you restart your Intel Edison you will lose connection with the bluetooth speaker, to solve this minor inconvinence if you don’t have access to a Terminal, we are going to setup service to execute when the intel Edison starts to connect to the bluetooth speaker.
Create a shell script - startBluetooth.sh to pair the device
vi bluetoothConnect.sh
#!/bin/sh rfkill unblock bluetooth sleep 1 bluetoothctl << EOF connect 00:25:DB:14:53:30 EOF sleep 1
And then make the file executable using
chmod +x startbluetooth.sh
Now go to the following folder and create startBluetooth.service
Cd /lib/systemd/system/
Vi startBluetooth.service
#!/bin/sh [Unit] Description=Sets up the Edison's Bluetooth interface and connects it to a paired device. [Service] ExecStart=/home/root/bluetoothConnect.sh Type=idle [Install] WantedBy=basic.target
To enable the service run
systemctl enable startBluetooth.service
Now when you reboot the Edison reboot it will automatically connect to the bluetooth speaker.
Reference -
Play Audio from Your Intel Edison via Bluetooth -https://software.intel.com/en-us/articles/play-audio-from-your-intel-edison-via-bluetooth-using-advanced-audio-distribution-profile
Tweepy Documentation -http://www.tweepy.org/
eSpeak Documentation - http://espeak.sourceforge.net/commands.html
Automatically connect to a device through Bluetooth - https://communities.intel.com/docs/DOC-102152
Top Comments