Friday, November 1, 2013

Rapspberry Pi Webcam

Next trial with RP, this time setting up a webcam over HTTP.

I followed the steps:

1. Connect webcam to USB port, check device is recognized


lsusb
I got:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 046d:0802 Logitech, Inc. Webcam C200
Bus 001 Device 005: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter


2. Install Motion



sudo apt-get install motion
3. configure Motion



sudo nano /etc/motion/motion.conf
This is my file:

daemon=on
norm=3
input=8
auto_brightness=on
framerate=2
height=480
width=640
jpeg_filename=motion/%Y%m%d/%v-%H%M%S-%q
noise_tune=on
output_all=off
output_motion=off
output_normal=on
quality=100
snapshot_interval=300
target_dir=/home/pi
text_left=(c) Nestcam
text_right=%d %b %Y\n%k:%M:%S
threshold=500
v4l2_palette=6
videodevice=/dev/video0
webcam_localhost=off
webcam_maxrate=2
webcam_port=8080
wecam_quality=100

sudo nano /etc/default/motion
It should be:

start_motion_daemon=yes

4. Start motion daemon:

sudo /etc/init.d/motion start

Check logfile for any errors:

tail -f /var/log/syslog

If all is fine open in browser http://<your Raspberry Pi IP address>:8080



Raspberry magic

Long time since I've written last time on my English blog page. Time to switch a bit from Polish piczkowskipl.blogspot.com to this one.

Some time ago I met Raspberry Pi for the first time. I had a chance to play with it and went as far as setting it in headless mode.

Today I got it back from dust and gave it next chance.
My aim was to set it up as small home server and media center.

At home I have a dynamic IP which updates more-less every 24 hours. I know solutions like DynDNS. I even used it but it was not perfect. Very often the IP stored in cloud service was not refreshed with new IP of my rooter at home.

My idea was to create a daemon program that would run in background periodically and check if external IP of the router changed. Then it should send me an email with new IP.
As genial as simple :) I could then easily use this IP to connect with remote desktop to my home server.
Surely it would not be useful if I want to setup web page and release it in the Internet to public because still the IP will change but this is not my purpose. My aim is to connect with home server by IP and use it for many purposes like monitoring what's going on at home (smart house) or working on my home machine (upload/download/program/watch a movie?? whatever comes to my mind).

Raspberry Pi seems to be interesting in this context.

Today I will explain how I set up virtual desktop connection with TightVNC.

1. Install TightVNC Server on Raspberry Pi

sudo apt-get install xrdp

2. Start remote desktop server


/usr/bin/tightvncserver
you will be asked for password, type password and do not ser view-only password when you will be asked for this.

3. Check ports on which xtightvncserver is running

sudo netstat -tulpn
Then you will have to setup your router to forward ports, e.g:


192.168.1.107 is the IP of Raspberry Pi in home network behind the router.

On your machine from where you want to connect to Raspberry Pi install TightVNC Viewer, on Ubuntu you can do this with command:

sudo apt-get install xtightvncviewer
Then connect to Raspberry Pi

xtightvncviewer 83.29.84.77:5901

83.29.84.77 is my router external IP, the one that I would like to email myself from PI whenever it changes.

How to email anything from Raspberry Pi?
Very easy with Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import smtplib
 
# Specifying the from and to addresses
 
fromaddr = 'fromuser@gmail.com'
toaddrs  = 'touser@gmail.com'
 
# Writing the message (this message will appear in the email)
 
msg = 'Enter you message here'
 
# Gmail Login
 
username = 'username'
password = 'password'
 
# Sending the mail 
 
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

How to check your external IP address?
Very easy with service like http://ifconfig.me/


curl -s http://ifconfig.me
Putting it all together. Python script below gets IP address and compares it with address saved in local file. If for any reason IP is empty or same as the IP in local file then no email is sent. Otherwise an email is sent with new IP and the IP is saved in local file (dyndns.txt)

#!/usr/bin/env python

import smtplib
import subprocess
import sys

def send(ip):    # send mail
 # Specifying the from and to addresses
 fromaddr = 'change_me'
 toaddrs  = 'change_me'
  
 # Writing the message (this message will appear in the email)
  
 msg = "Your IP is  "+ip 
  
  
 # Sending the mail  
  
 server = smtplib.SMTP('smtp.gmail.com:587')
 server.starttls()
 server.login('marcin.piczkowski','mypassword')
 server.sendmail(fromaddr, toaddrs, msg)
 server.quit()

proc = subprocess.Popen(["curl","-s","http://ifconfig.me"], stdout=subprocess.PIPE)
(out,err)=proc.communicate()
ip = out.strip()
print "IP is " + ip

if ip == "": 
 raise SystemExit
else:
 txtf = open("dyndns.txt","rw")
 lines = txtf.readlines()
 oldip = lines[0].strip()
 txtf.close()
 
 if oldip != ip:
  print "Sending ip " + ip
  txtf = open("dyndns.txt","w")
  txtf.write(ip)
  send(ip)
  txtf.close()
 else:
  print "IPs are the same"
  txtf.close()
  raise SystemExit
The script must be given execution rights and dyndns.txt file read/write access

sudo chmod +x myscript.py
sudo chmod 666 dyndns.txt
Then you can schedule with cron to execute this script every 15 minutes:
sudo crontab -e
Add this line at the end of the file

*/15 * * * * python /etc/init.d/myscript.py >> /var/log/dyndns.log 2>&1 &
Above line is correct when you have your script placed in /etc/init.d/
It will store logs in /var/logs/dyndns.log but first you need to create such file and give it read/write access:
sudo touch /var/log/dyndns.log
sudo chmod 666 /var/log/dyndns.log

You can check your cron logs with

grep CRON /var/log/syslog

Now you should get mail with IP in 15 minutes. Let's verify quickly that you really can connect to Raspberry Pi on this IP.
We will start HTTP server on Pi with python, type this in console:

python -m SimpleHTTPServer
Server would listen on port 8000 so you need to forward this port in rooter configuration.
Then type in browser: http://<IP from email>:8000
Server should response with content of folder in which you started SimpleHTTPServer.

In future post we will have a look at how we can run Node.js on Raspberry Pi. Stay tuned!!

Used resources:
[1] http://programmaticponderings.wordpress.com/2012/12/26/installing-tightvnc-on-the-raspberry-pi/
[2] http://www.pythonforbeginners.com/systems-programming/sending-emails-using-google/
[3] http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python

Saturday, August 10, 2013

Switching from cloudfoundry V1 to V2

Beta version of Cloudfoundry is down since long time ago. Now Pivotal gave users V2, brand new commercial version of former service. It's yet 67 days for free. I decided to migrate my V1 app there. Let's see how deployment process differs from the old one.
Previously there was grails plugin that made it as simple as invoking several groovy commands. Now it's a bit different. Quick manual is available at http://docs.cloudfoundry.com/docs/dotcom/getting-started.html#install-cf

I'm not Ruby hacker but was able to install Ruby on my machine, yet starting with first command I encountered an issue. DevKit was missing on my machine, so i donwloaded it from here (DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe), extracted in my groovy_home/DevKit and installed with commands:
cd C:\Ruby193\DevKit
ruby dk.rb init
ruby dk.rb review
ruby dk.rb install
Then lets return to cloudfoundry install cf.

gem install cf

cf target api.run.pivotal.io

When we have done it already we can build war file (grails prod war) and upload it.

cf push --path consulner.war

When we do this for the first time we will be asked to choose environment (dev, staging, prod) I've chosen production. Then I answered questions as follows:



Name> consulner

Instances> 1

1: 128M
2: 256M
3: 512M
4: 1G
Memory Limit> 3

Creating consulner... OK

1: consulner
2: none
Subdomain> consulner

1: cfapps.io
2: none
Domain> cfapps.io

Binding consulner.cfapps.io to consulner... OK

Create services for application?> n

Save configuration?> y

Saving to manifest.yml... OK
Uploading consulner... OK
Preparing to start consulner... OK
-----> Downloaded app package (28M)
Downloading JDK...
Copying openjdk-1.7.0_25.tar.gz from the buildpack cache ...
Unpacking JDK to .jdk
Downloading Tomcat: apache-tomcat-7.0.41.tar.gz
Copying apache-tomcat-7.0.41.tar.gz from the buildpack cache ...
Unpacking Tomcat to .tomcat
Copying mysql-connector-java-5.1.12.jar from the buildpack cache ...
Copying postgresql-9.0-801.jdbc4.jar from the buildpack cache ...
Downloading auto-reconfiguration-0.7.1.jar from https://s3.amazonaws.com/maven.springframework.org/milestone/org/cloudfoundry/auto-reconfiguration/0.7.1 ...
-----> Uploading droplet (65M)
Checking status of app 'consulner'.....
  0 of 1 instances running (1 starting)
  0 of 1 instances running (1 starting)
  1 of 1 instances running (1 running)
Push successful! App 'consulner' available at http://consulner.cfapps.io


That's it! Now we have app running on prod. Unfortunately this was not that simple, I struggled to find out why the app could not start t first time and there was nothing inspiring in logs or crushlogs. Finally I tried increasing default memory from 256M to 512M and it worked :)