Thursday, November 21, 2013

Create a web and webcam server to control the hardware of the target

 I am running the target with ubuntu 12.04 with 2 partitions: boot.img and uImage (rootfs). Linux swap file. Web server is Lighttpd.  It is dual boot machine in eMMC runs Angstrom (mmcblk1p2) and Ubuntu 12.04 (mmcblk0p2). The external sd card is Ubuntu 12.04 if it is in sd card slot it will boot from Ubuntu if it is not it will boot from eMMC which has Angstrom. eMMC(embedded non-volatile memory) is like NANG Flash but it has 3 components, the MMC(multimedia card) interface, the flash memory and the flash memory controller.

root@ubuntu-armhf:/var/www# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/mmcblk0p2   3778576 3054108    554612  85% /           (mmcblk0p2 is SD card)
devtmpfs          253768       4    253764   1% /dev
none               50784     656     50128   2% /run
none                5120       0      5120   0% /run/lock
none              253912     224    253688   1% /run/shm
/dev/mmcblk0p1      1004     472       532  48% /boot/uboot (mmcblk0p1 is SD card)
/dev/mmcblk1p1     71133   55238     15895  78% /media/BEAGLEBONE (mmcblk1p1 eMMC)
/dev/mmcblk1p2   1738184 1339028    309192  82% /media/Angstrom   (eMMC)
root@ubuntu-armhf:/var/www# cat /etc/fstab
proc /proc proc defaults 0 0
/dev/mmcblk0p2      /              auto   errors=remount-ro   0   1
/dev/mmcblk0p1      /boot/uboot    auto   defaults            0   0
/mnt/512M.swap     none            swap   sw                  0   0
root@ubuntu-armhf:/var/www# free
             total       used       free     shared    buffers     cached
Mem:        507824     500892       6932          0      33792     189020
-/+ buffers/cache:     278080     229744
Swap:       524284       4288     519996
NOTE: it is confusing
/dev/mmcblk0p2 partition 2 of SD card for ubuntu kernel
/dev/mmcblk0p1 partition 1 of SD card for ubuntu uboot
/dev/mmcblk1p2 partition 2 of eMMC for Angstrom kernel
/dev/mmcbkl1p1 partition 1 of eMMC for Angstrom uboot
eMMC has 2 GB
SD card has 4 GB

How to turn the user LED on Beaglebone on there are a few ways the easy way is to write into /dev file sysfs on Linux, write in C/C++ to convert to CGI (Common Getaway Interface) using like this "gcc led.c -o led.cgi) and the browser can execute it and turn the led on.
 This application seems easy but the problem is most of browsers do not allow to run as root. The default users is www-data. To login into linux as root won't solve the problem because the browser is still runnung as www-data user, unless recompile the web server after modification. I am using Lighttpd. Using this script to test and I see it is different when running at shell and browser.
 whoiam.php
 <?php
  echo exec('whoami');
  echo "\n";
?>

root@ubuntu-armhf:/var/www# php whoami.php
root
root@ubuntu-armhf:/var/www#

I am running as root since I login as root or sudo su (root).
But when running whoami.php in the browsers it returns as normal user and not root
 

http://localhost/xampp/www/beaglebone/whoami.php
www-data or my username but it never returns as root.


In order to do that following this link to download the source code of Lighttpd and recompile with modifying these 2 two lines which prevent the web server to run as root. It is not good but at least for testing purpose inside the private LAN.
root@ubuntu-armhf:/var/www# sudo service lighttpd restart
 * Stopping web server lighttpd                                          [ OK ]
 * Starting web server lighttpd                                                 2013-11-21 13:52:28: (plugin.c.131) Cannot load plugin mod_fastcgi more than once, please fix your config (we may not accept such configs in future releases
2013-11-21 13:52:28: (server.c.762) I will set uid to 0 because you are God
2013-11-21 13:52:28: (server.c.775) I will set uid to 0 because you are God


                                                                         [ OK ]

Now when running whoami.php in the browsers
http://localhost/xampp/www/beaglebone/whoami.php
root


root@ubuntu-armhf:/var/www#


ubuntu@ubuntu-armhf:~$ ifconfig
eth0    Link encap:Ethernet  HWaddr c8:a0:30:a9:28:05
          inet addr:192.168.0.55  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::caa0:30ff:fea9:2805/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23753 errors:0 dropped:4 overruns:0 frame:0
          TX packets:4405 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2443911 (2.4 MB)  TX bytes:588773 (588.7 KB)
          Interrupt:56

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:433 errors:0 dropped:0 overruns:0 frame:0
          TX packets:433 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:178275 (178.2 KB)  TX bytes:178275 (178.2 KB)

wlan0     Link encap:Ethernet  HWaddr c8:d3:a3:a7:83:0c
          inet addr:192.168.0.112  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::cad3:a3ff:fea7:830c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:20486 errors:0 dropped:3 overruns:0 frame:0
          TX packets:727 errors:0 dropped:1 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2339512 (2.3 MB)  TX bytes:107645 (107.6 KB)



This is my web server runs from the target and execute the php script called led.php. It is a web server the LED can be turned from tablet or any devices can run web browsers.
<?php
//echo exec('whoami');
shell_exec('/bin/echo 0 > /sys/class/leds/beaglebone:green:usr3/brightness');
echo "led off";
echo "\n";
?>



To read it GPIO for LEDS I do:
ubuntu@ubuntu-armhf:/sys/class/leds/beaglebone:green:usr0$ cat /sys/class/leds/beaglebone:green:usr3/brightness
1
ubuntu@ubuntu-armhf:/sys/class/leds/beaglebone:green:usr0$ cat /sys/class/leds/beaglebone:green:usr3/brightness
0

ubuntu@ubuntu-armhf:/sys/kernel$ sudo su
[sudo] password for ubuntu:
root@ubuntu-armhf:/sys/kernel# cd debug
root@ubuntu-armhf:/sys/kernel/debug# ls
asoc          gpio      mmc1              regmap          ubi
atmel_mxt_ts  hid       musb-hdrc.0.auto  regulator       usb
bdi           iio       musb-hdrc.1.auto  remoteproc      wakeup_sources
bluetooth     kprobes   pinctrl           sched_features
dri           memblock  pm_debug          suspend_stats
f2fs          mmc0      pwm               tracing
root@ubuntu-armhf:/sys/kernel/debug# cat gpio
GPIOs 0-31, gpio:

GPIOs 32-63, gpio:
 gpio-52  (eMMC_RSTn           ) out lo
 gpio-53  (beaglebone:green:usr) out lo
 gpio-54  (beaglebone:green:usr) out lo
 gpio-55  (beaglebone:green:usr) out lo
 gpio-56  (beaglebone:green:usr) out hi (usr3) is on same as the website
 gpio-59  (McASP Clock Enable P) out hi

Server IP is set to static it is easy because it will always has the same IP, in this case it is always 192.168.0.55
root@ubuntu-armhf:/sys/kernel/debug# cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# loopback network interface
auto lo
iface lo inet loopback

# primary network interface
auto eth0
iface eth0 inet static
  address 192.168.0.55
  netmask 255.255.255.0
  gateway 192.168.1.1

That is local static IP address 192.168.0.55 and I am using port forward from ISP router and configuring it to connect to the internet. From anywhere in the world I can turn the light on/off. I tried and it works perfectly. Set up the web server port 192.168.0.55:80 port 80 mapping to ISP router IP: xx.xx.xx.xx:80. I have been thinking about doing something about it for a long time like you can turn the light of your home or look at the webcam at your home. You need to know layer 1, 2, 3, 4 and layer 7 of OSI layers in order to appreciate it. Layer 7 is web application and the bottom layers relating to hardware. Web is unlimited and I am using LAMP Linux Apache MySQL and PHP in the LAMP stack you can do anything you want. We don't need Java swing/awt or Qt/GTK/OpenGL for GUI. PHP/HTML/CSS/Javascript/Jquery are powerful enough to do anything that I want it and coding is a breeze and it shows.
I am using the tablet and open the Firefox or any browser to turn the light on/off for this project. I design web sites and all I am using is LAMP, it is stunning with Adobe Photoshop/Illustrator/Dreamweaver that is the whole packet together with CMS as Drupal/Wordpress/Joomla.  If I write in Java then the browser needs to install Java SE for it to run. I already know good enough C/C++ and recently Java. Python and Perl that all I need if I find the time. The rest of my time is to review what I have already known and make the best use of it.

Webcam server
I am using Logitech C270 using mpeg-streamer or motion this is no need to compile just installed, they are both from Raspberry Pi, I tested only with mpeg-streamer on Beaglebone. I set up the web pages to turn the led on/off and see webcam as the same time.
The next step is port forward to map from LAN local IP address to internet IP address of the router providing by my ISP. Webcam is using port 8080 and web server is using port 80 make sure they are different ports otherwise the router will not allow to map to internet it will return with port busy.
mpeg-streamer using default port 8080 and motion using default port 8081.
Port forward
Need to setup the local IP address to the internet IP address in order to access from any places
Device Allowed applications Application type Protocol Port number(s) Public IP
Static IP:192.168.xx.yy Web Server - TCP 80 aa.bb.cc.dd

Webcam - TCP 8080 aa.bb.cc.dd






After the port forward http://aa.bb.cc.dd anywhere in the internet can control the led and see action from the webcam as well. As you can see I just turn the led on and read it back and display the blue light and the same time I can see Led on at the board as well.


The problem with my 2wire router provides by the ISP will change external IP address when it is power off or TCP session expired but I have seen its IP changes a few times without power off, probably cheaper for ISP to have a dynamic IP address instead of static IP for each router they provide for the customer, therefore to host web server will not work DNS server mapping the IP address of web hosting (web server) to a domain name that web hosting must always have the same IP address.
This is a diagram of the network. It is nice that it has Ethernet and USB, the whole project costs around 80 dollars (BSP Beaglebone $45+ $10 USB Hub + $10 Wifi Adapter + $9 Micro HDMI + shipping) , the rest like keyboard, webcam, mouse and monitor which I already have from the PC.
Ethernet has a static IP is connected my LAN router and wifi is connected to my Internet Service provider (ISP) router.
I have been thought about this project for a while how I can turn the light of my house or turn on the webcam to check the house even if I was away from home or set up the alarm. I have to 2 routers. By nature the routers act as a getaway forward the LAN traffic to WAN traffic using NAT. In this scenario we want to make one router as a true router and one router as a gateway. In order to switch to switch the router from getaway to switch/router we need a firmware dd-wrt it has option to switch these functions Basic setup ->advanced routing->operating mode -> Router or Gateway then add static route for it. It also allows two subnet/netmask to talk to each other. The tradition router we can only communicate the same router with in the same subnet mask. The firmware d-link dir615 is only operating in gateway mode which router the traffic from home LAN to the internet WAN, it has 2 functions: default getaway using its IP address and router, this is the reason it need to upgrade firmware to dd-wrt to make it complete a router. It is confusing when we call it a router but only router traffic from LAN to WAN instead of router any subnets to any other subnets. The term that d-link is called dir615 is d-link ROUTER ACCESS POINT. After setup a 2 subnets can ping each other.
ubuntu@ubuntu-armhf:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr c8:a0:30:a9:28:05
          inet addr:192.168.1.55  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:496446 errors:0 dropped:823 overruns:0 frame:0
          TX packets:587000 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:54020379 (54.0 MB)  TX bytes:641817484 (641.8 MB)
          Interrupt:56

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:26 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2879 (2.8 KB)  TX bytes:2879 (2.8 KB)

ubuntu@ubuntu-armhf:~$ ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_req=1 ttl=64 time=0.619 ms
64 bytes from 192.168.2.1: icmp_req=2 ttl=64 time=0.707 ms
64 bytes from 192.168.2.1: icmp_req=3 ttl=64 time=0.888 ms
64 bytes from 192.168.2.1: icmp_req=4 ttl=64 time=0.756 ms
--- 192.168.2.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.619/0.742/0.888/0.101 ms
ubuntu@ubuntu-armhf:~$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_req=1 ttl=64 time=0.567 ms
64 bytes from 192.168.1.1: icmp_req=2 ttl=64 time=0.774 ms
64 bytes from 192.168.1.1: icmp_req=3 ttl=64 time=0.971 ms
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.567/0.770/0.971/0.168 ms
ubuntu@ubuntu-armhf:~$ traceroute 192.168.2.1
traceroute to 192.168.2.1 (192.168.2.1), 30 hops max, 60 byte packets
 1  dlinklocal (192.168.2.1)  0.642 ms  0.572 ms  0.393 ms
ubuntu@ubuntu-armhf:~$ traceroute 192.168.1.1
traceroute to 192.168.1.1 (192.168.1.1), 30 hops max, 60 byte packets
 1  dlink (192.168.1.1)  0.532 ms  0.281 ms  0.259 ms
ubuntu@ubuntu-armhf:~$ traceroute yahoo.com
traceroute to yahoo.com (206.190.36.45), 30 hops max, 60 byte packets
 1  dlink (192.168.1.1)  0.375 ms  0.153 ms  0.259 ms
 2  10.125.58.129 (10.125.58.129)  7.098 ms  6.733 ms  6.594 ms
 3  69.63.255.149 (69.63.255.149)  15.760 ms  15.559 ms  18.215 ms
 4  gw01.etob.phub.net.cable.rogers.com (66.185.83.197)  16.296 ms  16.263 ms  16.159 ms
 5  ae2_2140-bdr03-tor.teksavvy.com (69.196.136.131)  15.590 ms ae2_2110-bdr03-tor.teksavvy.com (69.196.136.41)  17.338 ms ae0_2110-bdr04-tor.teksavvy.com (69.196.136.36)  14.762 ms
 6  xe-0-0-0_0-bdr01-nyc.teksavvy.com (206.248.155.98)  26.513 ms^C
ubuntu@ubuntu-armhf:~$

Before I can ping 192.168.1.1 from router2 but I cannot ping 192.168.2.1 from router1 because both routers are router access point with direction gateway from LAN to WAN via NAT.
You can just add static route on 1.55 which is Linux
#ip route add 192.168.2.0/24 via 192.168.1.2
because Route2 is not going forward to 2.50 because it is getaway (access pointer router using NAT to forward from LAN to WAN only) need to set it up as router/switch in order to forward the packet to 2.50 (192.168.2.50)
Block diagram of 2 routers have dd-wrt firmware:
 http://www.patrikdufresne.com/en/multiple-subnets-routing-with-dd-wrt/

Using winexe to launch and run windows in order to execute pop up notification on the windows desktop screen. In order to make it work we need 2 things: the program itself and security/


http://support.microsoft.com/kb/951016  (to open windows port)
and enable windows firewall as well for for 2 rules: ICMP and winexe to go through.


and rule for winexe go through.
Control Panel --> System and security --> Windows Firewall --> Advanced settings --> Inbound rules --> New rule --> custom rule
Add this line in the existing index.php on the web server.
exec("echo 0 > /sys/class/leds/beaglebone:green:usr0/brightness");  // turn led on
exec("/home/ubuntu/Downloads/winexe-1.00/source4/bin/winexe -U tv% //192.168.2.50 'msg tv someone turns the LED0 off'");  //send a message



As you can see the webpage has web server and 2 webcam servers. The 2 webcams embedded in <iframe> or <img>. Chrome is only working with <img> tag and firefox works for both tags. It was wrote in PHP, CSS and HTML.