[Raspberry Pi] Video Doorbell with Raspberry Pi zero (Ongoing)

My traditional doorbell recently stopped to work, makes me thinking about getting a fancy replacement.

There are a few “smart doorbell” on the market. The most famous one might be Ring Video Doorbell or the Pro version. The price on Amazon is ~$250, even with deal it’s still around $200. Besides the price, with Ring products, I need to use their app on my phone to get the call from doorbell, it’s not that easy to integrate with other smart devices, or add my own features. Say I would like to open the door after I talk with the person rings the doorbell, or I want to have a picture taken when it rings and display on my cellphone. So almost no consideration, I decided to build my own video doorbell.

Initial thoughts on what I want:
1. Has an illuminated button, when pressed, change color or do some blinking to tell the visitor button pressed.
2. Rings traditional bell. Rings my home phone with different tone.
3. Use SIP protocol to talk with other devices.
4. Take a picture when pressed, display it on the screen when phone rings.
5. Send a notice to my Samsung smartwatch
6. Video capture on motion.
7. Can call my doorbell from regular phone.
8. Silent time for different devices.
9. Take outside temperature and humidity.

Hardware:
1. Raspberry Pi Zero or Zero W Raspberry PI 2 or 3 (See this post).
2. Camera with night vision IR LED.
3. 15 Pin 1.0mm Pitch to 22 Pin 0.5mm Camera Cable.
4. Step-down DC-DC regulator.
5. mVox MV100 USB Speakerphone.
6. DHT11 Temperature Humidity sensor.
7. Usb Hub module(Optional for Zero W).
8. Usb Wifi module(not necessary for Zero W)
9. 3D printed housing.
10. 12V 2A power supply.
11. An illuminated push button.

[OpenWRT] Setup USB drive and SAMBA/NAS on OpenWrt from scratch

After flashing the TP-LINK 703N router from U-boot failsafe webserver, you are having a brand new router. Software need to be installed to perform certain tasks. In this article, we are talking about setup a NAS using a USB drive from scratch.

By default, Openwrt doesn’t include any USB driver, so the first thing you need to do is to install the USB driver.

First you need to setup 703N in router mode (WAN mode), or AP mode (LAN mode). That makes sure 703N can access the internet. The client (your PC) should connect to 703N via wireless.

Lucky, it’s very easy to install software in Openwrt Luci web page.

Go to System menu, software

nas1

Like every Linux system, package list need to be updated before install the software. Press the Update button.

In the web page or SSH, install the following packages:

#Those are USB drivers
kmod-usb-core
kmod-usb2
kmod-usb-storage
kmod-usb-storage-extras
block-mount
kmod-usb-uhci
kmod-usb-ohci

#Those are file system support, you may choose only the ones you are using:
kmod-fs-exfat
kmod-fs-ext4
kmod-fs-ntfs
kmod-fs-vfat

#IO charset, IMPORTANT
kmod-nls-cp437
kmod-nls-iso8859-1

#This one is for auto mount
block-mount

#These are samba server and web page configuration software:
luci-app-samba
luci-i18n-samba-en
samba36-server

To enable the auto mount feature, so that you don’t need to mount the USB device manually each time you plug it in, run the following in SSH:

root@OpenWrt:~# block detect > /etc/config/fstab
root@OpenWrt:~# cat /etc/config/fstab
config 'global'
        option  anon_swap       '0'
        option  anon_mount      '0'
        option  auto_swap       '1'
        option  auto_mount      '1'
        option  delay_root      '5'
        option  check_fs        '0'

config 'mount'
        option  target  '/mnt/sda1'
        option  uuid    '40e0-6302'
        option  enabled '0'

root@OpenWrt:~# /etc/init.d/fstab enable

Simply plug in a USB drive formatted in FAT, reboot the router and check if USB device is mounted by:

df -h

The USB drive should be here in the list other than a few system partitions.

Then let’s configure samba, there are two config files we need to edit. First, edit /etc/config/samba, this file tells samba where are the share point in the system. Here is my configuration:

config samba
	option 'name'			'openwrt'
	option 'workgroup'		'openwrt'
	option 'description'	        'openwrt'
	option 'homes' 			'1'
		
config sambashare
	option 'name'			'mnt'
	option 'path'			'/mnt/'
	option 'read_only'		'no'
	option 'guest_ok'		'yes'
	option 'create_mask'	        '0700'
	option 'dir_mask'		'0700'
	#option 'users'			'abc'


config sambashare
	option 'name'			'root'
	option 'path'			'/'
	option 'read_only'		'no'
	option 'guest_ok'		'no'
	option 'create_mask'	        '0700'
	option 'dir_mask'		'0700'
	#option 'users'			'abc'

It shares the file system as well as the mount point.

The second file is /etc/samba/smb.conf, this is the standard samba config file. Refer to Samba manual for detail. Here is my smb.conf file:

[global]
        netbios name = |NAME| 
        workgroup = |WORKGROUP|
        server string = |DESCRIPTION|
        syslog = 10
        encrypt passwords = true
        passdb backend = smbpasswd
        obey pam restrictions = yes
        socket options = TCP_NODELAY
        unix charset = utf-8
        preferred master = yes
        os level = 20
        security = user
        guest account = nobody
# For safety, disable root user
#        invalid users = root
        smb passwd file = /etc/samba/smbpasswd

enable samba auto start and reboot the router again:

/etc/init.d/samba enable

Samba should be able to start automatically and can get access to USB immediately after reboot.

[TPLINK 703N] Best U-boot mod for TP-LINK 703N

If you purchased the 703N from our Amazon store or ebay store, your router is pre-loaded with this U-boot by default. You can jump to part two to learn how to use it. If you get your router from somewhere else, you can follow this article to update your u-boot so you won’t need you worry about bricking the router some day.

pepe2k from Openwrt forum made a very impressive U-boot for 703N. It starts from wr703n-uboot-with-web-failsafe project.

The best thing about this bootloader is, you can barely “brick” your router. You can do whatever you want to change IP, change Ethernet port configuration, enable/disable wireless. Of course, you can also play with the firmware in any way you want. After you make something evil, all you need to do is to load the default firmware in failsafe mode. After a reboot, your router is brand new again. The uboot has a few more features, we will talk about two modes that most people use most to recover 703N.

In this article we are mainly focus on how to flash your 703N to this amazing uboot, and also how to use the uboot. If you are interested in details, you can dig into the uboot code, since this project is open sourced.

Part 1 – Installation

For safety consideration, most Openwrt firmware disabled the uboot write permission, in other word, you cannot update the uboot with a normal firmware download from official website. I compiled this firmware, enabled the uboot write permission.

Download TP-link-703N-uboot-writable.bin here.

Upload it to the /tmp directory (You may need WinSCP to do it).

Open a SSH window, login with “root” account. And run the following command:

root@703N:~# mtd -r write /tmp/TP-LINK-uboot-writable.bin  firmware
Unlocking firmware ...

Writing from /tmp/TP-LINK-uboot-writable.bin to firmware ...
Rebooting ...

After a short while, you will see your 703N automatically reboots. After the LED becomes solid, connect to router using WIFI – the default SSID is OpenWRT, with no password. Login to the webpage 192.168.1.1. Setup the root user password. Note you need to setup root password to use the SSH interface.

Now, download the uboot here.

connect to SSH and run:

root@OpenWrt:~# mtd -r write /tmp/TP-LINK-updated-uboot.bin u-boot
Unlocking u-boot ...

Writing from /tmp/TP-LINK-updated-uboot.bin to u-boot ...
Rebooting ...

After the reboot, the new uboot is in your router.

Part 2 – How to use the uboot failsafe feature.

By pressing and holding the reset button for different duration, there are three modes you can choose:
1. web server (for ~3 sec)
2. U-Boot console (for ~5 sec)
3. NetConsole (for ~7 sec)

For web server mode and NetConsole mode, you need to set IP of your computer to 192.168.1.2(it needs to by exact 192.168.1.2!) with netmask of 255.255.255.0

When you push and hold the reset and power up, the LED should flash once a second. count to 3, and release the reset button, you will be able to ping and access the webpage by browsing 192.168.1.1

webserver

You can upload any firmware here to fix your “bricked” router.

When using NetConsole mode, instead push and hold the reset button 3 seconds before release, you count to 7. In NetConsole mode, only UDP port 6666 is open, in other word, you cannot ping the IP to see if it’s in correct mode or not. But you can use UDP tool Hercules setup utility to connect to UDP port 6666.

netconsole

There are many command you can run here in Netconsole, BUT make sure you know what you are doing. You are directly talk to uboot, that you may easily brick the router for real. Once you messed up with uboot here, you need a programmer to get it back. The most common command we use is “setmac”. It write the MAC address to the flash so you can easily “clone” IP from other devices. I will have another article on it.

The U-Boot console is enabled if you press and hold reset button for 5 seconds. You need to open the router and connect a serial cable to the PCB, and talk with the router with serial port. Normally you don’t really need to do so, since you have the Netconsole can do almost the same thing. So, I will save some pages describing it. If you need further information on it, contact me privately.

[TPLINK 703N] Buy Modified/rebranded TP-LINK 703N 16MB FLASH and 64MB RAM

Finally, I got the modified TP-LINK 703N for sell online at Amazon and Ebay. The device is a rebranded Mercury version with exactly the same PCB, only the enclosure is different. I get them professional modified with brand new 16MB flash and 64MB RAM, which makes life much easier.

Buy it on Amazon, or
Buy it on eBay!

Some detail photos compare original TP-LINK 703N and modified Mercury version:

1

2

3

4

5

6

7

8

[Web design] Deploy web2py server with Nginx in Debian

Web2py is a great python web platform. However I don’t find any article on how to integrate it to Nginx. I also found a few issues that were not that easy to solve.

Web2py doesn’t need to be installed. Simple download the latest version.

cd /www
wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
rm web2py_src.zip

Install uwsgi by:

apt-get install uwsgi
apt-get install uwsgi-plugin-python

Add a setting profile to /etc/uwsgi/apps-available/web2py.xml

<uwsgi>
    <socket>/tmp/web2py.socket</socket>
    <pythonpath>/www/web2py/</pythonpath>
    <mount>/=wsgihandler:application</mount>
    <master/>
    <processes>1</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <uid>nginx</uid>
    <gid>nginx</gid>
    <no-orphans/>
    <plugins>python</plugins>
</uwsgi>

make a link to app-enable:

ln -s /etc/uwsgi/apps-available/web2py.xml /etc/uwsgi/apps-enabled/

restart uwsgi service:

service uwsgi restart

Modify nginx vhost file, adding the following to server section:

        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
            alias /www/web2py/applications/$1/static/$2;
            #remove next comment on production
            #expires max;
        }
        location / {
            root /www/web2py/applications/;
            uwsgi_pass      unix:///tmp/web2py.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
        }

change the folder name as you need.
Go to /www/web2py directory and set the password for 80 port, change owner of the folder to nginx:

chown -R nginx:nginx /www/web2py
cd /www/web2py
sudo -u nginx python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),80)"

till now, you should be able to visit the default welcome project by the top level domain.

If you need to deploy the project at a subdirectory, you need to change the nginx settings to:

        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
            alias /www/web2py/applications/$1/static/$2;
            #remove next comment on production
            #expires max;
        }
        location /app/ {
            root /www/web2py/applications/;
            uwsgi_pass      unix:///tmp/web2py.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
            uwsgi_param SCRIPT_NAME /app;
            uwsgi_modifier1 30;
        }

Web2py is located at http://yourdomain.com/app now.
Due to the security reason, by default, web2py doesn’t allow you to connect from remote. To change this, you need to modify the file applications/admin/model/access.py by adding

request.is_local=True

before

if request.is_https:
    session.secure()
elif not request.is_local and not DEMO_MODE:
    raise HTTP(200, T('Admin is disabled because insecure channel'))

[*nix Server] How to automatically mount SMB network sharing drive to Mac OS

I decided to move all my important data to the NAS with Raid 1 protection. I need to mount it on all my desktop/laptop so I can access the files easily. Mount to Windows system is extremely easy. However, it takes me a little while for MAC OS. This article is about how to automatically mount SMB drive to MAC OS.

By saying “automatically”, my goal is to mount a Samba or Windows share on MAC OS to a mount point I desired, have it autoremount whenever it is reachable, and have it automatically reconnect after a reboot, and have the share be accessible by all users(root or non-root).

MAC OS comes with a good feature called “automount”, all we need is to edit “/etc/auto_master” file and add the following line to it:

/- auto_smb

It tells the system to load /etc/auto_smb automatically.
then, edit auto_smb to include all the mount point you want, the format is:

/Volumes/mount/to/point -fstype=smbfs,soft ://user:password@server_ip/share

if you are using guest without password, use:

/Volumes/mount/to/point -fstype=smbfs,soft ://guest:@server_ip/share

Finally, run the command below to enable it, or simply reboot the computer

automount -vc

when you finish all those steps, whenever you try to access the files, the system automatically try to connect to the server.

It’s not a problem to me, but some users report that they cannot access the mounted files as non-root user, looks like it’s a known bug of MAC OS, here is a workaround:

Add the following codes to a script, run it after each reboot. It unmount the mount point with root privileges and adding back using the non-root user:

sudo umount /Volumes/mount/to/point
cd /Volumes/mount/to/point

[*nix Server] Mount FTP space to Debian as local hard drive

Online.net dedicated servers come with 100GB backup spaces that I never use since got the server. I figure I might want to use it as a real backup space somehow. I found CurlFTPFS is a pretty good solution on this.

Do the following you will get additional 100GB HD on your server with decent speed:

Edit/create new file at /root/.netrc

machine FTP.URL.COM
login ftp-user
password ftp-password

Change permission to avoid others seeing your password:

chmod 600 /root/.netrc

look up your id and group information for NON-ROOT user on your server

su NON-ROOT-USER
id

and edit /etc/fstab file, so that every time you reboot, you will get the FTP automatically mounted.

curlftpfs#FTP.URL.COM /mnt/ftp fuse allow_other,uid=1000,gid=1000,umask=0022 0 0

Simply reboot the server, you will see the FTP is mounted on /mnt/ftp folder. If you don’t want to reboot, do

mount -a

you will get it immediately.

Let’s check the I/O speed now:

root@localhost:/mnt/ftp#  dd if=/dev/zero of=test_$$ bs=64k count=16k conv=fdatasync && rm -f test_$$
16384+0 records in
16384+0 records out
1073741824 bytes (1.1 GB) copied, 11.717 s, 91.6 MB/s 

It’s even faster than my local hard drive!

[Web design] Vanilla forum – Simple way to get friendly URL(URLs rewriting) work in Nginx

I am not sure if it’s a good decision yet to build a forum in Geekologie, since I am not getting too much traffic to the site. I did it anyway.

After spending a lot of time on the LowEndTalk forum, where I am actively involving on the VPS/dedicated server discussions, I decided to try Vanilla forum. Installation is simple, just as all other blog/forum php software. But when I open the dashboard, oh woo, is it too simple? I can hardly find any useful settings.

Anyway, I need to get the URL rewriting work first. URL rewriting translate URL such as:
http://www.geekologie.net/forum/index?p=categories
to
http://www.geekologie.net/forum/categories
which looks more user friendly and also SEO friendly.

There are tons of articles on how to setup Vanilla forum URL rewriting in Nginx, but they are too complicated. Actually, if you have a working PHP+Nginx environment. It’s quite easy:

1. put the following configuration in your Nginx vhost or global setting:

 location /forum
 {
 try_files $uri $uri/ @forum;
 }
 location @forum
 {
 rewrite ^/forum(.+)$ /forum/index.php?p=$1 last;
 }

This assumes you have the forum installed in /forum directory. Modify it if necessary.
2. Modify /forum/conf/config.php, change “RewriteUrls” setting from “False” to “True”.

That’s it, enjoy beautiful URLs.

[*nix Server] UptimeRobot status page

UptimeRobot is a great choice to monitor all your server for free. However, they are not providing any public status page. I found a great one here.

1. To install it, you need to enable the UptimeRobot API in your account settings.

uptimerobot_enable_api

2. Download the latest status page code from here.
3. Place all the contents from the unzipped file in your web folder.
4. Change config file in statuspage/config.php with your own UptimeRobot API Key from step 1.
5. Basically that’s it! View the index.html with your browser and see your uptimerobot public status page!

uptimerobot_status_page

[*nix Server] Uptimerobot – a great Server monitor service

have been using Pingdom to monitor my server. Pingdom only support monitoring one server for the free account, and the interval between to checks is 30 min, which is too long for my need (also for everyone I believe). And you need to pay over $40 a month to get the public status page feature.

As I got more and more VPSes and Dedicated servers, I starts to look for a better service to monitor all of them. After trying a few other services, I decided to go with Uptime Robot which provide up to 50 monitors and 5 min intervals with their free plan, and 1 min intervals for paid plan (only $4.5 a month). You can monitor via webpage/keyword, ping and custom port.

2015-01-15_205809

They don’t provide status page either, but they have very nice APIs – I will write wrote another article on this. After testing it for months on 15 servers, I am completely happy with it.