{"id":41,"date":"2011-01-25T11:10:57","date_gmt":"2011-01-25T16:10:57","guid":{"rendered":"http:\/\/ahdesign.us\/blog\/?p=41"},"modified":"2012-01-12T15:15:18","modified_gmt":"2012-01-12T20:15:18","slug":"dockstar-scripts","status":"publish","type":"post","link":"https:\/\/ahdesign.us\/blog\/dockstar-scripts\/","title":{"rendered":"Dockstar Scripts"},"content":{"rendered":"<p>This post is just to document all the scripts that were created\/reused to make the Dockstar run pretty much on its own&#8230;<\/p>\n<p><!--more--><\/p>\n<p>There&#8217;s basically just gonna be a lot of code and not much else.\u00a0 I&#8217;ll at least describe what each script is doing&#8230;<\/p>\n<p>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:<\/p>\n<p>\/etc\/rc.local<\/p>\n<pre class=\"qoate-code\">\r\n\r\n#!\/bin\/sh -e\r\n#\r\n# rc.local\r\n#\r\n# This script is executed at the end of each multiuser runlevel.\r\n# Make sure that the script will \"exit 0\" on success or any other\r\n# value on error.\r\n#\r\n# In order to enable or disable this script just change the execution\r\n# bits.\r\n#\r\n# By default this script does nothing.\r\necho \"Starting wireless...\" ; \\\r\nifconfig wlan0 up ; \\\r\niwconfig wlan0 txpower 15dbm ; \\\r\niwconfig wlan0 essid \"deathmeister\" ; \\\r\nsleep 1; \\\r\n\/etc\/wpa_reconnect.sh &amp;\r\nsleep 5;\r\necho \"Starting MJPG Streamer....\" ; \\\r\n\/root\/mjpg-streamer-r63\/mjpg_start_script.sh &amp;\r\nexit 0\r\n\r\n<\/pre>\n<p>I don&#8217;t remember if the slashes or semicolons at the end are needed or not.\u00a0 If not, feel free to remove them.\u00a0 Leaving them in doesn&#8217;t detract from the operation of the script, however.\u00a0 Setting the TXpower to 15dbm makes sure that the wifi adapter isn&#8217;t overdriving the output.\u00a0 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&#8217;s where the 15dbm number came from.\u00a0 Supposedly the wireless link quality improves when you&#8217;re not overdriving the output&#8230;.<\/p>\n<p>This next script is what does all the configurations for mjpg-streamer.\u00a0 In this script, the resolution, framerate, and some other options can be changed for streaming out of the Dockstar:<\/p>\n<p>\/root\/mjpg-streamer-r63\/mjpg_start_script.sh<\/p>\n<pre class=\"qoate-code\">\r\n\r\n#!\/bin\/sh\r\nexec \/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 &amp;\r\nsleep 1\r\ncurl \"http:\/\/user:password@127.0.0.1:9000\/?action=command&amp;command=led_blink\"\r\nsleep 5\r\ncurl \"http:\/\/user:password@127.0.0.1:9000\/?action=command&amp;command=led_off\"\r\n\r\n<\/pre>\n<p>The 1st line after #!\/bin\/bash\u00a0 and before sleep 1 is one, massive line for starting mjpg-streamer.\u00a0 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&#8217;s doing anything when other people see it (mainly smelly robbers&#8230;.I&#8217;m assuming they&#8217;re smelly).\u00a0 I&#8217;ve also removed the authentication credentials as no one needs to know what those are except me!<\/p>\n<p>This next one isn&#8217;t a script, rather a configuration file for wpa_supplicant.\u00a0 This is what wpa_supplicant uses for knowing how to connect to the wifi access point.\u00a0 The key has been removed from the post, but to generate the router key, you use wpa_passphrase:<\/p>\n<p>\/etc\/wpa_supplicant.conf<\/p>\n<pre class=\"qoate-code\">\r\n\r\n# This line enables the use of wpa_cli\r\n# if possible (to check for successful association)\r\nctrl_interface=\/var\/run\/wpa_supplicant\r\n\r\n#this should keep things connected\r\nfast_reauth=1\r\n\r\nnetwork={\r\nkey_mgmt=WPA-PSK\r\nscan_ssid=1\r\nssid=\"deathmeister\"\r\n#psk=\"xxxxxx\"\r\npsk=........\r\n}\r\n\r\n<\/pre>\n<p>Next up is the network interfaces config file.\u00a0 In here we set the wifi up for static IP since we don&#8217;t need it grabbing a new IP by accident as Zoneminder won&#8217;t know that happened and we&#8217;re screwed until we discover the new IP (kinda need that IP to get into the Dockstar to find out what its IP is&#8230;a little catch 22 here).\u00a0 The wireless interface is wlan0 if it wasn&#8217;t totally obvious.<\/p>\n<p>\/etc\/network\/interfaces<\/p>\n<pre class=\"qoate-code\">\r\n\r\n# This file describes the network interfaces available on your system\r\n# and how to activate them. For more information, see interfaces(5).\r\n\r\n# The loopback network interface\r\nauto lo\r\niface lo inet loopback\r\n\r\n# The primary network interface\r\nallow-hotplug eth0\r\niface eth0 inet dhcp\r\n\r\niface wlan0 inet static\r\naddress 192.168.1.201\r\nnetmask 255.255.255.0\r\nnetwork 192.168.1.0\r\nbroadcast 192.168.1.255\r\ngateway 192.168.1.1\r\ndns-nameservers 192.168.1.1\r\n\r\n<\/pre>\n<p>The following in &#8220;hosts&#8221; allows you to set the hostname of the box:<\/p>\n<p>\/etc\/hosts<\/p>\n<pre class=\"qoate-code\">\r\n\r\n127.0.0.1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 localhost\r\n127.0.1.1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 wificam2\r\n\r\n<\/pre>\n<p>This last script supposedly reconnects to the access point if it is discovered that wpa_supplicant isn&#8217;t connected anymore.\u00a0 I have yet to test this script out as I didn&#8217;t write it myself, but it looks like it kills wpa_supplicant off if it detects that the wifi is disconnected.\u00a0 A major issue w\/ wifi disconnecting and not reconnecting is that it&#8217;s the only remote way to get into the Dockstar.\u00a0 Luckily mine are just downstairs, but if they&#8217;re located someplace that&#8217;s not easy to get to, having wifi go down and not come back up is a giant pain in the butt.\u00a0 Then some poor dope has to go and physically reset the Dockstar to make wifi come back.\u00a0 Not fun.<\/p>\n<p>\/etc\/wpa_reconnect.sh<\/p>\n<pre class=\"qoate-code\">\r\n\r\n#!\/bin\/bash\r\n# Reconnect script. Be sure to have mutt installed, or comment that line out.\r\niface=wlan0\r\ndriver=wext\r\nwpaconfig=\/etc\/wpa_supplicant.conf\r\nlockfile=\/var\/run\/connstatus.pid\r\noutput=\/var\/log\/connstatus.log\r\niplog=\/var\/log\/last_ip\r\n#email=YOUR_EMAIL_ADDRESS_HERE\r\n\r\ncheck_newip () {\r\nnewip=`\/sbin\/ifconfig|\/bin\/grep \"Bcast\"`\r\noldip=`cat $iplog`\r\nif [ \"$newip\" != \"$oldip\" ]; then\r\n#echo $newip|mutt -s 'address changed' $email\r\necho Address changed, time: `date` &gt;&gt; $output\r\necho \"$newip\" &gt;&gt; $output\r\nif [ \"`wpa_cli status|grep wpa_state`\" == \"wpa_state=COMPLETED\" ]; then\r\necho \"$newip\" &gt; $iplog\r\nfi\r\nfi\r\n}\r\n\r\nreconnect () {\r\necho Reconnecting, time: `date` &gt;&gt; $output\r\nwhile [ \"`wpa_cli status|grep wpa_state`\" != \"wpa_state=COMPLETED\" ]; do\r\nkillall wpa_supplicant; sleep 1\r\nwpa_supplicant -B -D$driver -i$iface -c$wpaconfig &amp;\r\nfor i in `seq 1 10`;do\r\nsleep 4\r\nif [ \"`wpa_cli status|grep wpa_state`\" == \"wpa_state=COMPLETED\" ]; then\r\nbreak\r\nfi\r\ndone\r\ndone\r\n# setup the ip address and routing stuff again\r\nifconfig $iface 192.168.1.201\r\nroute add default gw 192.168.1.1\r\n\r\n#dhclient $iface #for getting DHCP addresses\r\n}\r\n\r\nif ( set -o noclobber; echo \"$$\" &gt; \"$lockfile\") 2&gt; \/dev\/null; then\r\ntrap 'rm -f \"$lockfile\"; exit $?' INT TERM EXIT\r\nwhile [ 1 ]; do\r\nif [ \"`wpa_cli status|grep wpa_state`\" == \"wpa_state=COMPLETED\" ]; then\r\n#echo -n \".\"\r\nsleep 1\r\n#check_newip\r\nelse\r\nreconnect\r\nfi\r\nsleep 10\r\ndone\r\nrm -f \"$lockfile\"\r\ntrap - INT TERM EXIT\r\nelse\r\necho \"Lockfile $lockfile is held by $(cat $lockfile).\"\r\nfi\r\n\r\n<\/pre>\n<p>I commented out the check for a new IP thingy since we&#8217;re using static IPs on the Dockstar.\u00a0 That piece of the script is only useful if you&#8217;re using DHCP to get your IP address.\u00a0 Oh yeah, you need to have<\/p>\n<pre class=\"qoate-code\">\r\n\r\nctrl_interface=\/var\/run\/wpa_supplicant\r\n\r\n<\/pre>\n<p>in your \/etc\/wpa_supplicant.conf file so that wpa_cli knows how to connect to the wpa_supplicant daemon.\u00a0 That&#8217;s already in my config up the page a bit.\u00a0\u00a0 The script recovers the wifi connection just fine.\u00a0 There were a few bugs I introduced (since I&#8217;m using static IPs and not dynamic), but it&#8217;s all fixed and working.\u00a0 If I force the wifi to disconnect (using wpa_cli or by killing wpa_supplicant), the connection comes back in 10 secs or less.<\/p>\n<p>I think these are all the majority of the custom settings in the Dockstar that are different from stock.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is just to document all the scripts that were created\/reused to make the Dockstar run pretty much on its own&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-wifi-webcam"],"_links":{"self":[{"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":5,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahdesign.us\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}