What Is My IP?

UPDATE 5/27/15: WhatsMyIP.Me now gets 5,000 hits an hour! Incredible!

I recently have been experiencing issues at work where one of my two ISP’s would go down. I have a Cisco firewall that handles the fail over to my backup connection, so most of my users were unaffected.

The only users who even noticed were those who frequented websites who had additional security featues enabled to prevent against session hijacking. The constant ISP failover behaved like a multi-homed network that would randomly serve web pages across more than one Internet provider on a page by page basis.

This was a bit tricky to debug, so my first step was to create a php script that would let me know what is my external ip address, whatsmyip.me . I felt the need to create my own as many of the existing services that tell you your IP address are covered in advertisements and I just wanted a tool that would return the external IP address and nothing more. Also, most of them do a terrible job at detecting proxies and return incorrect information.

whatsmyip.me is a single php script and the code is below:

<?PHP
function whatsMyIP()
{ 
  foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
  {
      if (array_key_exists($key, $_SERVER) === true)
      {
        foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
          {
              if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
              {
                  return $ip;
              }
          }
      }
    }
}
 
$users_ip = whatsMyIP();
echo $users_ip; // Output IP address [Ex: 123.12.41.42]
?>

I also put this on github under “What Is My IP”

The way this script works by analyzing the php $_SERVER variable ( an array containing information such as headers, paths, and script locations created by the webserver) and returning the most reliable IP address possible. Please note, the only address you can really trust is REMOTE_ADDR, because it is the source IP of the TCP connection and cant be changed by spoofing/changing an http header. While it is technically possible to bidirectionally spoof IP addresses at the Border Gateway level, but you would have to have control over an ISP to do so.

After I had the webservice to accurately give me my ip address, i created a single bash file that would echo my IP address every 15 seconds for the next 24 hours.

MPB:dconroy$ for i in `seq 1 5760`;do  date;curl http://whatsmyip.me;sleep 15; done

If your linux box is in a different timezone, you can use the TZ environment variable to report back the correct time. In my case i just added ‘TZ=America/New_York’ directly before ‘date’.

Using this, I was able to pinpoint the exact windows where my ISP would drop making my technical support calls much more productive. This is what I love about linux and raspberry pi’s – if the tool you are looking for doesnt exist, build one!

Read More

Unboxing Kano: A Computer You Make Yourself

November 20th 2013, Alex Klein reached out to me about a kick starter project he was about to launch for a product he created, Kano. His idea was simple:

“We’ve created a simple, fun computer and coding kit, inspired by Lego. It’s designed for all ages, all over the world”

By the next day (18 hours, to be exact) he and his team had already reached their initial funding goal of $100,000.

Those of you who have read my blog before know how much I love Raspberry Pi’s. I still believe they are one of the best tools to learn linux. But what Alex and his team has done is special, they’ve created ‘A computer anyone can make’. This kit bridges the gap between the novice computer enthusiast and IT crowd who normally build computers.

I apologize now for how long overdue this post is, but I still wanted to share my unboxing experience with my readers here, and also offer my 1000% seal of approval on this truly amazing piece kit.

The packaging is very deliberate – every step is educational and exciting. The kit contains everything you need regardless if you are interested in building, gaming, or programming. If you know anyone, regardless of age, who is interested in technology, there is no better gift than a Kano. Their blog is also fantastic and is updated regularly.

 

Special thanks again to team at Kano and Lizzy at Brew PR for sending me a kit to review.

Read More

Turn your Raspberry Pi into a Translator with Speech Recognition and Playback (60+ languages)

UPDATE
This project has been picked up by Make Magazine and Radioshack to create this great step by step guide for their Weekend Project Campaign. Check out the guide here, and the amazingly awesome video below:


I get many requests from people who are still looking for cheap, easy, and fun project ideas for their Raspberry Pi’s, so I wanted to share this translator project I’ve been working on. With very little effort, we can turn this 35$ mini-computer into a feature rich language translator that not only supports voice recognition and native speaker playback, but also is capable of dynamically translating between 1000’s of language pairs, FREE! Even if you are not interested in building this exact translational tool, there are still many parts of this tutorial that might be interesting to you (speech recognition, text to speech, Microsoft/Google translation APIs). Just like the rest of my posts, this one starts with our shopping list. Most of my readers will probably already have most of these items around the house:

Shopping List

QTY Required Items Price(USD)*
1 Raspberry PI $35.00
1 Micro USB cable $5.49
1 Logitech USB Headset $28.53
1 SD Card (class 4 and 4gb minimum) $13.10
Total: $82.12
Optional Items
1 Power Supply $9.95
1 HDMI Cable $2.28
1 Case $12.75

*There are definitely cheaper options available for USB Headsets, I chose the logitech as it is plug and play. For alternatives, check this list for verified Raspberry Pi supported sound cards

Assumptions

This tutorial assumes your Raspberry Pi has:
-the latest version of Raspian installed
-an internet connection
-the correct sound card drivers for your headset

Configuring and Testing Your Headset

Before we start writing any code, lets ensure that we can record and playback sound using our USB Headset. The easiest way to do this is with the built in linux commands ‘arecord’ and ‘aplay’. But first lets make sure our file system is up to date.

sudo apt-get update
sudo apt-get upgrade

Now, plug in your USB Headset and run the following commands

cat /proc/asound/cards
cat /proc/asound/modules

You should see that the Logitech Headset is listed as card 1. Additionally, the second command should show that the driver for card 0 (the default raspberry pi output) is snd_bcm2835 and the driver for card 1 (our logitech headset) is snd_usb_audio.

alsa cards module usb headset

This is a problem because it shows that Raspberry Pi defaults to transmitting sound over its built in hardware, and does not have an audio input device configured. To solve this, we need to update ALSA (Advanced Linux Sound Architecture) to use our Headset as default for audio input and output. This can be done by a quick change to the ALSA config file located in /etc/modprobe.d/alsa-base.conf:

sudo nano /etc/modprobe.d/alsa-base.conf

Near the end of this file, change the line that says

options snd-usb-audio index=-2

to

options snd-usb-audio index=0

Save and close the file and reboot the Raspberry Pi using the following command:

sudo reboot

After the system comes back online, the sound system should be reloaded so that when we rerun the above commands…

cat /proc/asound/cards
cat /proc/asound/modules

…we should see the USB Headset is now the default input/output device (card 0) as shown below.

alsa after update

We can now test this out by recording a 5 second clip from the microphone:

arecord -d 5 -r 48000 daveconroy.wav

and play it back through the headphone speakers:

aplay daveconroy.wav

To adjust the levels you can use the built in utility alsamixer. This tool handles both audio input and output levels.

sudo alsamixer

Now that our headset is configured, we can move onto the next step of converting from Speech to Text.

Speech to Text or Speech Recognition with a Raspberry Pi

There are a few options for speech recognition with rPi’s, but I thought the best solution for this tutorial was to use Google’s Speech to Text service. This service allows us to upload the file we just recorded and convert it to text (which we will later use to translate).

Let’s create a shell script to handle this process for us.

sudo nano stt.sh

with the following contents

echo "Recording your Speech (Ctrl+C to Transcribe)"
arecord -D plughw:0,0 -q -f cd -t wav -d 0 -r 16000 | flac - -f --best --sample-rate 16000 -s -o daveconroy.flac;
 
echo "Converting Speech to Text..."
wget -q -U "Mozilla/5.0" --post-file daveconroy.flac --header "Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" | cut -d\" -f12  > stt.txt
 
echo "You Said:"
value=`cat stt.txt`
echo "$value"

Make it executable

sudo chmod +x stt.sh

The last step before we can run the script is to install the FLAC Codec that is not included in the standard Raspian image.

sudo apt-get install flac

Now we can run the Script

./stt.sh

This will automatically start recording your voice, just press Ctrl+C when you are done speaking. At that point the script uploads the sound file to Google, they transcribe it and return it so it can be displayed on our screen. Pretty impressive for only a few lines of code! Sample output below:
here is an example speech recognition raspberry pi

Microsoft Translation and Google Text to Speech

Now that we can record our voice and convert it into text, we need to translate it to our desired foreign language. I would love to be able to use Google’s Translate tool for this, but unfortunately there is a 20$ sign up fee for use of this API. I plan on purchasing this for myself, but I wanted to make this project free so every one had an opportunity to try it.

As an alternative, we will be using Microsoft’s translate service which currently is still free for public use. The list of supported languages and their corresponding codes can be found here. In our previous example we used a simple shell script, but for the translation and playback process – I’ve written a more powerful python script.

All of this code can be found on my github repository (contributions welcome!).

Lets first create the file:

sudo nano PiTranslate.py

and add the following contents

import json
import requests
import urllib
import subprocess
import argparse
 
parser = argparse.ArgumentParser(description='This is a demo script by DaveConroy.com.')
parser.add_argument('-o','--origin_language', help='Origin Language',required=True)
parser.add_argument('-d','--destination_language', help='Destination Language', required=True)
parser.add_argument('-t','--text_to_translate', help='Text to Translate', required=True)
args = parser.parse_args()
 
## show values ##
print ("Origin: %s" % args.origin_language )
print ("Destination: %s" % args.destination_language )
print ("Text: %s" % args.text_to_translate )
 
text = args.text_to_translate
origin_language=args.origin_language
destination_language=args.destination_language
 
 
def speakOriginText(phrase):
    googleSpeechURL = "http://translate.google.com/translate_tts?tl="+ origin_language +"&q=" + phrase
    subprocess.call(["mplayer",googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
def speakDestinationText(phrase):
    googleSpeechURL = "http://translate.google.com/translate_tts?tl=" + destination_language +"&q=" + phrase
    print googleSpeechURL
    subprocess.call(["mplayer",googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
args = {
        'client_id': '',#your client id here
        'client_secret': '',#your azure secret here
        'scope': 'http://api.microsofttranslator.com',
        'grant_type': 'client_credentials'
    }
 
oauth_url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
oauth_junk = json.loads(requests.post(oauth_url,data=urllib.urlencode(args)).content)
translation_args = {
        'text': text,
        'to': destination_language,
        'from': origin_language
        }
 
headers={'Authorization': 'Bearer '+oauth_junk['access_token']}
translation_url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
translation_result = requests.get(translation_url+urllib.urlencode(translation_args),headers=headers)
translation=translation_result.text[2:-1]
 
speakOriginText('Translating ' + translation_args["text"])
speakDestinationText(translation)

For the script to run we need to import a few python libraries and a media player.

sudo apt-get install python-pip mplayer
sudo pip install requests

The last thing we need to do before we can run the script is sign up for a Microsoft Azure Marketplace API key. To do so, simply visit the marketplace, register an application, and then enter your client id and secret passcode into the script above.

Now we can run the script:

sudo python PiTranslate.py -o en -d es -t "hello my name is david conroy"

The script has 3 required inputs:
-o orignation language
-d destination language
-t “text to translate”

hola me nombre david conroy

The above command starts in English and translates to Spanish. My favorite part about the whole tutorial is how quickly you can change between languages you are translating, and how the returned voice changes according to the destination language.

Putting it all Together

It is actually very easy to combine the two scripts we created in this tutorial. In fact, it only takes one line of code to be added to the bottom of stt.sh shell script we created earlier (assuming PiTranslate.py and stt.sh are in the same directory).

sudo nano stt.sh

python PiTranslate.py -o en -d es -t "$value"

For those of you who skipped around in this tutorial, here is the entire script again with that line added:

echo "Recording your Speech (Ctrl+C to Transcribe)"
arecord -D plughw:0,0 -f cd -t wav -d 0 -q -r 16000 | flac - -s -f --best --sample-rate 16000 -o daveconroy.flac;
 
echo "Converting Speech to Text..."
wget -q -U "Mozilla/5.0" --post-file daveconroy.flac --header "Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" | cut -d\" -f12  > stt.txt
 
echo "You Said:"
value=`cat stt.txt`
echo "$value"
 
#translate from English to Spanish and play over speakers
python PiTranslate.py -o en -d es -t "$value"

Now, run the Speech To Text script again, and it will translate it from English to Spanish by default.

./stt.sh

Change your origin and destination languages in the last line as desired, and the PiTranslate.py script will do the rest! There are literally 1000’s of language pairs supported here. Here is a screenshot:

PiTranslate

Video Demo

I apologize this video is a little shaky, it was difficult holding the headset to the phone while running the scripts.

Known Limitations and Additional Resources

Both the origin and destination languages have to be supported by Microsoft Translate and Google Translate in order for this script to work.

Language Codes:
Microsoft
Google

Some special characters in certain languages will also cause trouble with the translation services, but I am working on a fix for that.

Conclusion

I really enjoyed working on this project as it incorporates a wide range of technology and tools to create something immediately useful and fun to play with. Plus, its all FREE. If you have any questions at all regarding this project, just leave a comment below or on github and I’d be happy to help you!

Read More

How to Turn Your Raspberry Pi into a Fully Functional Web Server

Raspberry Pi

First we will start with the shopping list. You will need one of each of the required items. This list is assuming you already have a USB keyboard and USB mouse.

QTY Required Items Price(USD)*
1 Raspberry PI $35.00
1 Micro USB cable $5.79
1 SD Card (class 10, minimum 4GB recommended) $7.63
Total: $48.42
Optional Items
1 Power Supply $9.95
1 HDMI Cable $2.28
1 Case $12.75
1 Wireless Card $6.82
Total Including Optional Items: $80.25

*Prices are subject to change

After you’ve collected your hardware, you need to flash your operating system onto your SD Card. For this tutorial we will be using Raspian. I recommend it because it is an optimized version of Debian built specifically for Raspberry Pi’s. The last stable downloads can be found here.

To flash your SD Card, you will need to unzip the image and write it your SD card using Win32DiskImager. This tool can also be used to after our initial setup to create an image of our finalized implementation(very useful as a backup).

windiskimager

After the image is flashed, you can boot your device. At this point you can use your HDMI Cable/Mouse/Keyboard for your initial configuration, or you can use an SSH Client like Putty to connect. The default hostname, login, and password are as follows:

host:raspberrypi
Username: pi
Password: raspberry

raspberry pi putty

For this tutorial, I will be using putty. On first boot, you will be prompted with a configuration tool called Raspi-Config. If the raspi-config doesnt load automatically, just enter the following command from the shell to get started.

sudo raspi-config

raspi-config-screenshot

The settings I recommend you update are

update
expand_rootfs
change_pass
change_timezone
memory_split

The usual distribution images are 2 GB. When you copy the image to a larger SD card you have a portion of that card unused. expand_rootfs expands the initial image to expand to fill the rest of the SD card, giving you more space. By default, 64mb is reserved for the Graphical UI. Since we plan on using this as a web server, I reduce this to 32mb with the memory_split command.

After you finish your changes to the raspi-config, you should reboot your pi using the following command:

sudo shutdown -r now

At this point you have a fully functional linux server, but we still need to install apache, php, and mysql. This part is actually rather straightforward.

First, run the following commands from the shell to install Apache and PHP.

sudo apt-get update

sudo apt-get install apache2 php5 libapache2-mod-php5

The Locations of the Apache and PHP files are respectively

/etc/apache2/apache2.conf

/etc/php5/apache2/php.ini

Next, run the following command to install MySQL.

sudo apt-get install mysql-server mysql-client php5-mysql

You will need to create a password for the MySQL Root Account

mysql root

After this is completed, give it a try! To test Apache, navigate to the IP or Hostname of your Rasberry Pi in your browser, and you should see the famous “It works!” Screen.

it works apache

To test the PHP, create a text file under your /var/www/ directory called phpinfo.php with the following contents:

phpinfo1

Navigate to the file:

phpinfo2

And that’s it! You’re done. You’ve got a fully functionally Raspberry Pi Web Server!

Some Extras:
FTP
For ease of use you might want to install an FTP Server. You have a few options PROFTP, VSFTP. I prefer VSFTP and that can be installed with the following commands:

sudo chown -R pi /var/www
sudo apt-get install vsftpd

The configuration file for vsftp can be found here:

/etc/vsftpd.conf

Samba
If you are in a windows environment, you might find it useful to install SAMBA for network shares. To install samba, use the following commmands

sudo apt-get install samba

The configuration file for samba can be found here:

/etc/samba/smb.conf

Raspberry PI Full Specs:
Features
•Broadcom BCM2835 700MHz ARM1176JZFS processor with FPU and Videocore 4 GPU
•GPU provides Open GL ES 2.0, hardware-accelerated OpenVG, and 1080p30 H.264 high-profile decode
•GPU is capable of 1Gpixel/s, 1.5Gtexel/s or 24GFLOPs with texture filtering and DMA infrastructure
•512MB RAM
•Boots from SD card, running a version of the Linux operating system
•10/100 BaseT Ethernet socket
•HDMI video out socket
•2 x USB 2.0 sockets
•RCA composite video out socket
•SD card socket
•Powered from microUSB socket
•3.5mm audio out jack
•Size: 85.6 x 53.98 x 17mm”

Read More