Dockstar Scripts

This post is just to document all the scripts that were created/reused to make the Dockstar run pretty much on its own…

There’s basically just gonna be a lot of code and not much else.  I’ll at least describe what each script is doing…

This first script (by the way, these are in no particular order, and some depend on each other) is what gets the wireless connected and mjpg-streamer going too so that Zoneminder can use the video from this camera:

/etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "Starting wireless..." ; \
ifconfig wlan0 up ; \
iwconfig wlan0 txpower 15dbm ; \
iwconfig wlan0 essid "deathmeister" ; \
sleep 1; \
/etc/wpa_reconnect.sh &
sleep 5;
echo "Starting MJPG Streamer...." ; \
/root/mjpg-streamer-r63/mjpg_start_script.sh &
exit 0

I don’t remember if the slashes or semicolons at the end are needed or not.  If not, feel free to remove them.  Leaving them in doesn’t detract from the operation of the script, however.  Setting the TXpower to 15dbm makes sure that the wifi adapter isn’t overdriving the output.  The driver defaults to 23dbm (I think), but the max output for the card is supposed to be 15dbm (again if I remember right), and that’s where the 15dbm number came from.  Supposedly the wireless link quality improves when you’re not overdriving the output….

This next script is what does all the configurations for mjpg-streamer.  In this script, the resolution, framerate, and some other options can be changed for streaming out of the Dockstar:

/root/mjpg-streamer-r63/mjpg_start_script.sh


#!/bin/sh
exec /root/mjpg-streamer-r63/mjpg_streamer -i "/root/mjpg-streamer-r63/plugins/input_uvc.so -d /dev/video0 -r 800x600 -f 5" -o "/root/mjpg-streamer-r63/plugins/output_http.so -w /root/mjpg-streamer-r63/www -p 9000 -c user:password" -b &
sleep 1
curl "http://user:password@127.0.0.1:9000/?action=command&command=led_blink"
sleep 5
curl "http://user:password@127.0.0.1:9000/?action=command&command=led_off"

The 1st line after #!/bin/bash  and before sleep 1 is one, massive line for starting mjpg-streamer.  Since mjpg-streamer provides a web interface for controlling the camera, we can cause the camera LED to blink to show that things are working, and then turn the camera LED off to make it less obvious it’s doing anything when other people see it (mainly smelly robbers….I’m assuming they’re smelly).  I’ve also removed the authentication credentials as no one needs to know what those are except me!

This next one isn’t a script, rather a configuration file for wpa_supplicant.  This is what wpa_supplicant uses for knowing how to connect to the wifi access point.  The key has been removed from the post, but to generate the router key, you use wpa_passphrase:

/etc/wpa_supplicant.conf


# This line enables the use of wpa_cli
# if possible (to check for successful association)
ctrl_interface=/var/run/wpa_supplicant

#this should keep things connected
fast_reauth=1

network={
key_mgmt=WPA-PSK
scan_ssid=1
ssid="deathmeister"
#psk="xxxxxx"
psk=........
}

Next up is the network interfaces config file.  In here we set the wifi up for static IP since we don’t need it grabbing a new IP by accident as Zoneminder won’t know that happened and we’re screwed until we discover the new IP (kinda need that IP to get into the Dockstar to find out what its IP is…a little catch 22 here).  The wireless interface is wlan0 if it wasn’t totally obvious.

/etc/network/interfaces


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

iface wlan0 inet static
address 192.168.1.201
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.1

The following in “hosts” allows you to set the hostname of the box:

/etc/hosts


127.0.0.1       localhost
127.0.1.1       wificam2

This last script supposedly reconnects to the access point if it is discovered that wpa_supplicant isn’t connected anymore.  I have yet to test this script out as I didn’t write it myself, but it looks like it kills wpa_supplicant off if it detects that the wifi is disconnected.  A major issue w/ wifi disconnecting and not reconnecting is that it’s the only remote way to get into the Dockstar.  Luckily mine are just downstairs, but if they’re located someplace that’s not easy to get to, having wifi go down and not come back up is a giant pain in the butt.  Then some poor dope has to go and physically reset the Dockstar to make wifi come back.  Not fun.

/etc/wpa_reconnect.sh


#!/bin/bash
# Reconnect script. Be sure to have mutt installed, or comment that line out.
iface=wlan0
driver=wext
wpaconfig=/etc/wpa_supplicant.conf
lockfile=/var/run/connstatus.pid
output=/var/log/connstatus.log
iplog=/var/log/last_ip
#email=YOUR_EMAIL_ADDRESS_HERE

check_newip () {
newip=`/sbin/ifconfig|/bin/grep "Bcast"`
oldip=`cat $iplog`
if [ "$newip" != "$oldip" ]; then
#echo $newip|mutt -s 'address changed' $email
echo Address changed, time: `date` >> $output
echo "$newip" >> $output
if [ "`wpa_cli status|grep wpa_state`" == "wpa_state=COMPLETED" ]; then
echo "$newip" > $iplog
fi
fi
}

reconnect () {
echo Reconnecting, time: `date` >> $output
while [ "`wpa_cli status|grep wpa_state`" != "wpa_state=COMPLETED" ]; do
killall wpa_supplicant; sleep 1
wpa_supplicant -B -D$driver -i$iface -c$wpaconfig &
for i in `seq 1 10`;do
sleep 4
if [ "`wpa_cli status|grep wpa_state`" == "wpa_state=COMPLETED" ]; then
break
fi
done
done
# setup the ip address and routing stuff again
ifconfig $iface 192.168.1.201
route add default gw 192.168.1.1

#dhclient $iface #for getting DHCP addresses
}

if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
while [ 1 ]; do
if [ "`wpa_cli status|grep wpa_state`" == "wpa_state=COMPLETED" ]; then
#echo -n "."
sleep 1
#check_newip
else
reconnect
fi
sleep 10
done
rm -f "$lockfile"
trap - INT TERM EXIT
else
echo "Lockfile $lockfile is held by $(cat $lockfile)."
fi

I commented out the check for a new IP thingy since we’re using static IPs on the Dockstar.  That piece of the script is only useful if you’re using DHCP to get your IP address.  Oh yeah, you need to have


ctrl_interface=/var/run/wpa_supplicant

in your /etc/wpa_supplicant.conf file so that wpa_cli knows how to connect to the wpa_supplicant daemon.  That’s already in my config up the page a bit.   The script recovers the wifi connection just fine.  There were a few bugs I introduced (since I’m using static IPs and not dynamic), but it’s all fixed and working.  If I force the wifi to disconnect (using wpa_cli or by killing wpa_supplicant), the connection comes back in 10 secs or less.

I think these are all the majority of the custom settings in the Dockstar that are different from stock.

This entry was posted in Wifi Webcam. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.