Posted on

RPI Alternative to Google Text to Speech

Installing Pico TTS

  • Open and check /etc/apt/sources.list if it contains the following. Remove the “#” if they are commented out.

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
deb-src http://mirror.ox.ac.uk/sites/archive.raspbian.org/archive/raspbian/ wheezy main contrib non-free rpi

  • Update the list of available packages and their versions

sudo apt-get update

  • Install some dependencies for the compilation

sudo apt-get install fakeroot debhelper automake autoconf libtool help2man libpopt-dev hardening-wrapper

  • Install fakeroot

mkdir pico_build
cd pico_build
apt-get source libttspico-utils

  • Prepare the packages

cd svox-1.0+git20110131
dpkg-buildpackage -rfakeroot -us -uc

Compiling the packages will take 15-20 minutes to complete.

  • If there is no error during the compilation, you should find at least 4 packages in the pico_build folder.

cd ..

ls

libttspico0_1.0+git20110131-2_armhf.deb
libttspico-data_1.0+git20110131-2_all.deb
libttspico-dev_1.0+git20110131-2_armhf.deb
libttspico-utils_1.0+git20110131-2_armhf.deb

  • We just need 3 out of 4 libraries and this have to be installed in order.

sudo dpkg -i libttspico-data_1.0+git20110131-2_all.deb
sudo dpkg -i libttspico0_1.0+git20110131-2_armhf.deb
sudo dpkg -i libttspico-utils_1.0+git20110131-2_armhf.deb

  • Pico TTS engine is now ready.

 

Testing Pico TTS

  • Test the library by typing

pico2wave -w test.wav “Yeey, it works! “
aplay test.wav

  • Create whatTimeIsIt.php and insert the following script

vi whatTimeIsIt.php

<?php
$tempAudioFile = “temp.wav”;
$currentTime = date(‘h’) * 1;
$message = “Its” . $currentTime . “o clock”;
exec(“pico2wave -w $tempAudioFile ‘$message’ && aplay $tempAudioFile && rm $tempAudioFile”);
?>

  • Change access permission

chmod 644 whatTimeIsIt.php

  • Execute the script

php whatTimeIsIt.php

  • If you want, you can create a cron job to execute the script automatically every hour.

crontab -e

Then insert:

0 * * * * sudo /usr/bin/php ~/Scripts/whatTimeIsIt.php

Change the text in blue according to where you saved your script.

Posted on

Fresh Installed Raspbian from NOOBS

Getting started with NOOBS… follow the instructions here:  https://www.raspberrypi.org/help/noobs-setup/

1.) Update the list of available packages and their versions

sudo apt-get update

2.) Install the newer versions of the packages

sudo apt-get upgrade

or

sudo apt-get dist-upgrade to get the new OS release

Also be aware that downloaded package files (.deb files) are kept in /var/cache/apt/archives. You can remove these in order to free up space with sudo apt-get clean.

3.) Update RPI’s FW:

sudo rpi-update

4.) Update MOTD (if you want)

  • Remove all text from motd and motd.tail:

sudoedit /etc/motd

sudoedit /etc/motd.tail

  • Remove last login

Find ‘PrintLastLog’ and change its value to ‘no’

sudoedit /etc/ssh/sshd_config

PrintLastLog no

  • Restart SSH

sudo service ssh restart

  • Install TCLSH

Simple shell containing Tcl interpreter

sudo aptitude install tcl

  • Download the customized MOTD script

wget  http://downloads.bitsflipper.com/Scripts/RPI/motd.tcl

  • Move the script to the /etc directory

sudo mv motd.tcl  /etc

  • Set Permissions for the Script

This allows all shell users to execute the script at login.

sudo chmod 755 /etc/motd.tcl

  • Integrate New Script into Shell Profile

Force the system to execute the script at user login.

sudoedit /etc/profile

Add following line to end of file

/etc/motd.tcl

You should be able to see the new motd on you next login.

5.) Install/Setup VNC (tightVNC)

  • Install the server software from the repositories:

sudo apt-get install tightvncserver

  • Initialize the server

/usr/bin/tightvncserver

  • Adding Tightvnc to the startup

wget http://downloads.bitsflipper.com/Scripts/RPI/tightvncserver-init.txt

sudo mv tightvncserver-init.txt /etc/init.d/tightvncserver

You may need to change the variable on line 16. It is currently set to pi which is the default username on the Raspberry Pi Debian image. Set this to whatever username you want to login to TightVNC as. Do not put root as the user.

Change the file so it is owned by root (not strictly necessary, but is the standard ownership for init files

sudo chown root:root /etc/init.d/tightvncserver

Make the file executable by running

sudo chmod 755 /etc/init.d/tightvncserver

Add the script to the default runlevels with the command

sudo update-rc.d tightvncserver defaults

TightVNC will now start at startup. I’d recommend you reboot at this point to make sure it’s working, but you can just stop and start if you don’t want to reboot.

You can manually start and stop it using

sudo /etc/init.d/tightvncserver start

or

sudo /etc/init.d/tightvncserver stop

Note that this is designed for running only one instance of TightVNC on the computer. If you want to be able to start and stop specific instances of TightVNC then you will need to tweak the startup script appropriately.

6.) Configure  your system settings using “raspi-config”

sudo raspi-config

Posted on

Customized Wireless Home Security System

Have you thought about installing a home security system, but hesitated because it’s expensive and you don’t think that you really need it? You are not alone. 🙂

Installing home security system can be costly, but not installing one could cost you even more. The biggest reason why you should consider is that it offers protection to you and your loved ones, your home, and your treasured possessions.

There are many types of home security systems available in the market today. There are systems specifically designed for surveillance and recording purposes, and there are systems that provide more on detection and reporting. Like any other device, cost varies according to its features, efficiency, accuracy, resolution, form factor, etc. It’s hard to choose a system that is right for you off the shelf.  That is the reason why most people would prefer to sign contract with home security systems provider that offers customized/tailored fit home security solutions. But not me. 🙂

In this blog, I would like to share how to build our own customized home security system using open-source hardware and software, and a USB camera.

There are actually quite a number of posts related to this out there that does the same thing but programmed differently, but I like the way Marc-Olivier Schwartz put it in one of his posts.

Features

We are going to design here a system that has the following basic features:

  • Monitors and detects for movements.
  • When movement is detected, it will automatically take a snapshot of the event, save it locally, upload it to the cloud, then remove the local copy of the snapshot to optimise your SD card capacity.
  • On certain conditions, takes video and streams them as M-JPEG via HTTP locally or send the stream to YouTube so you can view the live streaming anywhere.

Hardware Needed

Required Parts

  • 1x Arduino Yun (with power supply)
  • 1x micro SD Card (at least 1 GB)
  • 1x USB video class or UVC Camera
  • 1x PIR Sensor
  • 1x Pushbutton Switch to trigger and stop video stream
  • 1x 220Ω 1/4W pull down resistor for the pushbutton switch
  • Jumper Wires

Optional Parts (used for debug and to display status)

  • 1x Tri-color LED
  • 3x 100Ω 1/4W Resistors
  • 1x 16×2 Alphanumeric LCD
  • 10KΩ Potentiometer
  • Additional jumper wires

Web Service Needed

  • Dropbox – Cloud storage service
  • Temboo – A layer that acts as an interface between the Dropbox’s API and your Yun.
  • YouTube – YouTube Live Event to broadcast our video stream live.

Setting up the Hardware

Schematic Diagram

schematic

Breadboard Layout

breadboard

 Actual Setup

actual-setup2

actual-setup1

Mount and connect all the parts according to the schematic diagram above, insert the micro SD card, then connect the camera to the USB port of the Yun and power up the board.

For the USB camera, you can refer to the following for the List of USB video class webcams.

http://en.wikipedia.org/wiki/List_of_USB_video_class_devices

Setting up Temboo Application and Dropbox API

The first service that we need to set is Temboo – a library of programming processes (virtualised codes) that makes the life of the developer, like us, a lot easier. In this case, Temboo will act as an interface layer between the Arduino Yun and Dropbox.

Go over to https://www.temboo.com. Login to your account if you already have, else, sign up for a free account. Once you are in, click “Account”, then Applications and create New Application. Take note of your account name, the name of the application that you just created and your API key, you will need them later.

Then go over to https://www.temboo.com/python to download the latest Temboo Python SDK. Once downloaded, extract the folder to the micro SD card. Yes, correct :), you need to remove the SD card from the Yun, plug it in to your PC, copy the folder over to the root directory, then insert the SD card back to the Yun.

The next service that we need to set is Dropbox. If you don’t have a Dropbox account, you need to create one.

https://www.dropbox.com/home

 Then go over to the developers section of Dropbox to create an app for you to be able to integrate with Dropbox.

https://www.dropbox.com/developers/apps

Click “Create App” and follow the snapshot below.

dropbox

Once you have create your app, write down the “App Key” and “App Secret” because these will be your input in the next step.

Go over to https://temboo.com/library/Library/Dropbox/OAuth/InitializeOAuth, then input the Dropbox App Key and App Secret to get the next set of data for the next authorization process.

You will be asked to visit a URL to grant your application access to your Dropbox data. Write down the OAuthTokenSecret and the CallbackID. You’ll need them, together with the previous two data (App Key and App Secret) to run the final authorization.

Go over to https://temboo.com/library/Library/Dropbox/OAuth/FinalizeOAuth, input the four data you’ve saved previously to get the Dropbox Access Token, Access Token Secret, and UserID associated with the access token and secret. Write them down because you’ll need them later.

Then the last service that you need to set is Youtube. You need to have a google account to stream videos on Youtube. You can create one from here: https://accounts.google.com.

Setting Up your Arduino Yun

The details on how to connect and setup your Yun to your network will not be covered here. It is assumed that your board is already set and properly connected to your WiFi network.

We are going to login to our Yun using SSH from a terminal and we’re going to install all the packages needed for our application. We are also going to test the USB camera to see if everything are ok.

Open a terminal and type the following to connect to your Yun via SSH.

ssh root@yourYunName.local

or using your Yun’s private IP address if you know.

ssh root@xxx.xxx.xxx.xxx

 If your connection is successful, you should see some nice ASCII art (default is LININO):

terminal

We can now install the required packages. We’ll start with an update of the package manager:

opkg update

UVC drivers:

opkg install kmod-video-uvc

python-openssl package:

opkg install python-openssl

fswebcam utility (we will use this utility to take snapshot (picture) of an event)

         opkg install fswebcam

mjpg streaming library:

opkg install mjpg-streamer

The installation process should be smooth.

Testing the USB Webcam

Since we have all the drivers and dependencies installed, we are now ready to test the webcam. Make sure that the SD card is mounted into the Yun.

cd /mnt/sda1

You should be able to see the Temboo Python SDK folder that you extracted earlier.

To test the camera and take a picture, just execute the following command.

fswebcam test.png

You should see something like this:

test

You should also see the test.png when you do an “ls”

You can check the picture by removing the SD card from your Yun and open the picture on your PC.

Motion Detection

Now that the infrastructure (both HW and SW) are set, it’s time to add in the program. The program is categorized into two – the microcontroller sketch (FW) that handles the inputs (triggers), LCD and indicators, and the Shell and Python scripts that handles webcam, snapshot, streams and uploads.

You can get all the files needed here: http://downloads.bitsflipper.com/Docs/Sikyo.zip

Motion detection is done in the microcontroller side. We use a passive infrared (PIR) sensor to detect motion by measuring changes in the infrared (heat) levels emitted by surrounding objects. When motion is detected the PIR sensor outputs a high signal on its output pin. We use this change of the pin state to execute “fswebcam” utility to take a snapshot of the event and save in the SD card first.

Upload Snapshots Automatically to Dropbox

After a snapshot is taken, a Python script “upload_picture.py” will be executed to upload the snapshot to the Dropbox folder we’ve set earlier. There few items that you need to change in order for the script to work. Now is the time to put all those details that you wrote down earlier to use.

Change the following lines accordingly with your Temboo Application details:

# Declare Temboo session and Choreo to upload files

session = TembooSession(‘ACCOUNT’, ‘APPLICATION’, ‘KEY’)

uploadFileChoreo = UploadFile(session)

Change the following lines accordingly with your Dropbox App details:

# Set inputs

uploadFileInputs.set_AppSecret(“DropboxAppSecret”)

uploadFileInputs.set_AccessToken(“AccessToken”)

uploadFileInputs.set_AccessTokenSecret(“AccessTokenSecret”)

uploadFileInputs.set_AppKey(“DropboxAppKey”)

You also need to ensure that the folder of the Temboo Python SDK you extracted earlier in the root directory of the SD card is named “temboo“.

Once the upload process is completed, the sketch will execute another shell command to remove the local copy of the uploaded snapshot.

Streaming Video Locally

In this project, for simplicity, we are going to use a pushbutton switch to trigger (start) and stop video stream. When the switch is pressed the first time, the sketch will execute the “streamViaHttp.sh” to initiate mjpg streamer utility to capture JPG frames from a connected webcam and stream it locally via HTTP.

mjpg_streamer -i “input_uvc.so -d /dev/video0 -r 640×480 -f 25” -o “output_http.so -p 8080 -w /www/webcam” &

You can change the resolution to the right number for your webcam.

To test if your setup could stream locally, press and release the pushbutton switch. You should be able to see that you the built-in red LED of your Yun is lit up and if your camera has LED, it should lit up also indicating that the camera is used.

Then go to http://xxx.xxx.xxx.xxx:8080/stream.html or http://YourYunName.local:8080/stream.html to see the live stream.

You should see something like this:

mjpg-streamer

Press again the pushbutton switch to stop streaming. Your onboard LED and camera LED should be turned OFF.

 Streaming Live Video to YouTube

In order for YouTube to broadcast your live content, you need to send them an encoded stream. YouTube Live provides support for a variety of encoders, but since I’ve tried Wirecast before without issue, we’ll use the same here.

If you don’t have Wirecast yet, you can get the installer here:

http://www.telestream.net/wirecastforyoutube/cb-landing.htm

If you already have, don’t launch it yet. Create a new live event first in YouTube.

https://www.youtube.com/my_live_events

Once done, launch Wirecast and setup as follows:

wirecast

 

  1. Go to Sources and click New Web Stream… Start demo
  2. Select “HTTP” for protocol and “Motion JPEG” for “Format Options”
  3. Tick “Live Stream”
  4. Put http://YourYunIP:8080/?action=stream for URI. You can also use your home network’s public IP provided that you have forwarded port 8080 to your Yun’s private IP.
  5. Click  the “Connect” button. Video stream should appear in the video frame.
  6. Click the “Stream” button on the top left corner. It will ask you for destination. Choose “YouTube”. Then authenticate using your Google Account.
  7. Choose your event and click OK.
  8. Look for the Master Layer and click the + sign to add camera. Choose the Web Stream Source that we configured earlier.
  9. Select the source. You should see the video.

video

Now, go back to YouTube and click the Live event control room, you should see something like this:

good

Click Preview. It may take a while before you will be able to click “Start Streaming”. Once done, you will be able to view your live video like this:

preview

Congratulations! You just completed your first Customized Home Wireless Security System hack!

There are so many things that you can do in order to make this base design more interesting. For example, you can add feature to automatically turn ON your light or send you a tweet or SMS when motion is detected. You may also want to program the device to monitor only at a specific time period. There are endless tweaks that you can do.

Snapshots of the working setup

working1 working2

working3 working4

Posted on

OpenVPN Configuration on RPI

OpenVPN is an open-source software application that implements virtual private network (VPN) techniques for creating secure point-to-point or site-to-site connections in routed or bridged configurations and remote access facilities. It uses a custom security protocol that utilizes SSL/TLS for key exchange. It is capable of traversing network address translators (NATs) and firewalls.

VPN gives extremely secure connections between private networks linked through the Internet. It allows remote computers to act as though they were on the same secure, local network. Allows you to be at home and access your company’s computers in the same way, as if you were sitting at work, and vice versa.

 

On RPI (Server Configuration):

For Raspbian or similar distribution

Step 1. Switch to root.

WARNING: Mistakes while in superuser mode could lead to data loss or the deletion of important system files.  Please double-check your command before hitting the “Enter” key.

Step 2. Update the list of available packages and their versions

apt-get update

Step 3. Install OpenVPN and OpenSSL

apt-get install openvpn openssl

Step 4. Change the current working directory to /etc/openvpn and copy the easy-rsa templates from /usr/share/doc/openvpn/examples/easy-rsa/2.0

cd /etc/openvpn

cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0 ./easy-rsa

Step 5. Now open easy-rsa/vars and update the following line

change

export EASY_RSA=”`pwd`”

to

export EASY_RSA=”/etc/openvpn/easy-rsa”

nano easy-rsa/vars

vars

Watch the double quotes! “Ctrl + x”, then “y” to save and exit.

Step 6. Perform the following steps.

Change the current working directory to easy-rsa:

cd easy-rsa

Execute vars:

source vars

Refresh /etc/openvpn/easy-rsa/keys:

./clean-all

Generate 1024 bit RSA private key template

./pkitool –initca

Create symbolic link for openssl.cnf:

ln -s openssl-1.0.0.cnf openssl.cnf

Step 7. Build the client-server keys. For beginners, it is recommended to hit the “enter” key (use default answers) on all questions. Answer “y” on all [y/n] questions.

./build-ca OpenVPN

./build-ca OpenVPN

The next command is going to take few minutes. Be patient :).

./build-dh

Step 8. Go back to /etc/openvpn directory

cd ..

Create a new file “openvpn.conf” with the following content:

dev tun

proto udp

port 1194

ca /etc/openvpn/easy-rsa/keys/ca.crt

cert /etc/openvpn/easy-rsa/keys/server.crt

key /etc/openvpn/easy-rsa/keys/server.key

dh /etc/openvpn/easy-rsa/keys/dh1024.pem

user nobody

group nogroup

server 10.8.0.0 255.255.255.0

persist-key

persist-tun

status /var/log/openvpn-status.log

verb 3

client-to-client

push “redirect-gateway def1”

#set the dns servers

push “dhcp-option DNS 8.8.8.8”

push “dhcp-option DNS 8.8.4.4”

log-append /var/log/openvpn

comp-lzo

nano openvpn.conf 

openvpn.conf

You can change the DNS-servers to any DNS you like.

Again watch if the double quotes did not change into some other characters after you copy and paste it. “Ctrl + x”, then “y” to save and exit.

Step 9. Configure the iptables for OpenVPN:

echo 1 > /proc/sys/net/ipv4/ip_forward

Get the Pi’s private IP address

ifconfig

Execute this:

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT –to [Pi’s IP Address]

Step 10. Enable pocket forwarding:

cd ..

nano sysctl.conf

Uncomment net.ipv4.ip_forward=1 line to enable packet forwarding for IPv4

change from

#net.ipv4.ip_forward=1

to

net.ipv4.ip_forward=1

sysctl.conf

“Ctrl + x”, then “y” to save and exit.

Step 11. Add to rc.local to run the iptables command when the Pi boots.

nano /etc/rc.local

Add the following lines just above “exit 0”

iptables -t nat -A INPUT -i eth0 -p udp -m udp –dport 1194 -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT –to-source [RASPBERRY IP ADDRESS]

rc.local

“Ctrl + x”, then “y” to save and exit.

Step 12. Start OpenVPN daemon:

/etc/init.d/openvpn start

Step 13. Change the current working directory to /etc/openvpn/easy-rsa/keys and create raspberrypi.ovpn.

cd /etc/openvpn/easy-rsa/keys

Create raspberrypi.ovpn and add the following content:

dev tun

client

proto udp

remote [Raspberry PI IP Address] 1194

resolv-retry infinite

nobind

persist-key

persist-tun

ca ca.crt

cert client1.crt

key client1.key

comp-lzo

verb 3

Replace “[Raspberry PI IP Address]” with your Pi’s private IP address, DNS or DDNS.

nano raspberrypi.ovpn

raspberrypi.ovpn

“Ctrl + x”, then “y” to save and exit.

Step 14. Now we need to copy the keys from “/etc/openvpn/easy-rsa/keys” to a non-root folder. It doesn’t matter where. In this example we’ll copy it in  /home/pi/openvpn directory.

tar czf openvpn-keys.tgz ca.crt ca.key client1.crt client1.csr client1.key raspberrypi.ovpn

mkdir /home/pi/openvpn | mv openvpn-keys.tgz /home/pi/openvpn/

chown pi:pi -R /home/pi/openvpn

Exit from superuser mode:

exit

Step 15. Restart OpenVPN daemon:

/etc/init.d/openvpn start


On clients:

Windows:

  • Download the installer from https://openvpn.net/index.php/open-source/downloads.html.
  • Start with the installation. Select the default options and continue.
  • Once the installation is complete, copy the openvpn keys from the server (openvpn-keys.tgz) and extract it in C:\Program Files\OpenVPN\config.

You can also access the config folder thru the Start Menu

click on [Start]->[All Programs]->[OpenVPN]->[Shortcuts]->[OpenVPN configuration file directory].

desktop

  • Connect to VPN.

connect

  • Verify if VPN is indeed connected.

conencted

Type the whatismyip.bitsflipper.com on your browser. It should show your servers public IP address.

 

Iphone:

  • Download and install the OpenVPN Connect client (by OpenVPN Technologies) from the App Store.

iPhone

  • Launch the OpenVPN app when it’s done downloading, and tap “Using iTunes Sync, select your device, go to OpenVPN under the “apps” tab, and drop your .ovpn and related cert/key files into the file sharing window.”
  • Connect your iPhone, iPod, or iPad to your computer with the provided cable.
  • Open up iTunes if it didn’t start automatically.
  • Click on your Phone in the left pane of iTunes.
  • Click on Apps on the top row, scroll down to “File Sharing”, and click on the “OpenVPN” app.
  • Click Add File…
  • Now back to your iOS (iPhone, iPod, iPad) device, you should see the profile were transferred successfully. Tap the green plus (+)  and drag the connect/disconnect bar to connect.
  • Open a browser and type whatismyip.bitsflipper.com. Check if the returned IP is the public IP address of your VPN server.

 

Posted on

Customized RPI MOTD

1.Remove Default MOTD

Remove all text from motd and motd.tail

$ sudoedit /etc/motd
$ sudoedit /etc/motd.tail 

2. Remove last login

Find ‘PrintLastLog’ and change its value to ‘no’

$ sudoedit /etc/ssh/sshd_config

PrintLastLog no

3. Restart SSH

$ sudo service ssh restart

4. Install TCLSH

Tclsh is a useful shell application which evaluates Tcl commands in a file.

$ sudo aptitude install tcl

5. Create new MOTD script

Change directory to /etc

$ cd /etc

Download a copy of the customized motd script

$ sudo wget http://downloads.bitsflipper.com/Scripts/RPI/motd.tcl

6. Set Permissions for the Script

This allows all shell users to execute the script at login.

$ sudo chmod 755 /etc/motd.tcl

7. Integrate New Script into Shell Profile

This forces the system to execute the script at user login.

$ sudoedit /etc/profile

Add following line to the end of the file

/etc/motd.tcl

motd