How-to Read / Set Gain from SkyView and Gmap

Set%20Gain%20Rev-1-Jessie%20and%20Stretch%20FA

Does NOT require www-data to be added to sudoers group

The setgain.sh script is written by @wiedehopf. This original script is for Piaware SD card image. Necessary modifications in this script were made by @abcd567 to adopt it for dump1090-fa package install.

.
.

Only Step-1 is different for Jessie and Stretch, rest is same.

STEP-1 (JESSIE): Install necessary php packages and enable module

sudo apt update   
sudo apt install php5-cgi   
sudo lighty-enable-mod fastcgi-php   
sudo /etc/init.d/lighttpd force-reload   

STEP-1 (STRETCH): Install necessary php packages and enable module

sudo apt update   
sudo apt install php7.0-cgi   
sudo lighty-enable-mod fastcgi-php   
sudo /etc/init.d/lighttpd force-reload   

.

STEP-2: Gain Changing Script

2.1 - Create new directory

sudo mkdir /usr/local/sbin/gain  

2.2 - Create a new blank script file setgain.sh inside this new directory

sudo touch /usr/local/sbin/gain/setgain.sh  

2.3 Add entry in cron to run script setgain.sh at boot

sudo crontab -e  

#Scroll down and ADD following line at the end
@reboot /bin/bash /usr/local/sbin/gain/setgain.sh  

Save (Ctrl+o) and Close (Ctrl+x)

.
2.4 - Open file setgain.sh to edit

sudo nano /usr/local/sbin/gain/setgain.sh  

.
2.5 - Copy-paste following code in file setgain.sh

#!/bin/bash

# redirect all output and errors of this script to a log file
exec &>/usr/local/sbin/gain/log

# file that anyone can write to in order to set a new gain
fifo=/usr/local/sbin/gain/newgain

# remove $fifo so we are sure we can create our named pipe
rm -f $fifo

# create the named pipe with write access for everyone
mkfifo -m 666 $fifo

# read current gain and store in file currentgain
# script in gain.php will read gain value stored in currentgain and
# will display it on map as "Current Gain"
awk '{for(i=1;i<=NF;i++) if ($i=="--gain") print $(i+1)}' /etc/default/dump1090-fa > /usr/local/sbin/gain/currentgain


while sleep 1
do
        if ! [[ -r $fifo ]] || ! [[ -p $fifo ]]

        # exit the loop/script if $fifo is not readable or not a named pipe
        then break
        fi


        # read one line from the named pipe, remove all characters but numbers, dot, minus and newline and store it in $line
        read line < <(tr -cd  '.\-0123456789\n' < $fifo)

        #set new gain
        sed -i '/RECEIVER_OPTIONS=.*/c\RECEIVER_OPTIONS="--device-index 0 --gain '$line' --ppm 0 --net-bo-port 30005"' /etc/default/dump1090-fa

        #restart dump1090-fa to implement new gain value
        systemctl restart dump1090-fa

        # read updated gain and store in file currentgain
        awk '{for(i=1;i<=NF;i++) if ($i=="--gain") print $(i+1)}' /etc/default/dump1090-fa > /usr/local/sbin/gain/currentgain

        # script in gain.php will read the updated gain and display it on map

done




Save (Ctrl+o) and Close (Ctrl+x) the file.

.

STEP-3: Create a new file gain.php containing gain button, drop-down list of gains & current gain display. This file will be embeded in SkyView Map in Step - 4

.
3.1 - Create a new file gain.php in folder /usr/share/dump1090-fa/html

sudo nano /usr/share/dump1090-fa/html/gain.php   

3.2 - Copy-paste following code in newly created blank file gain.php

NOTE: Some of code lines are very long. Scroll Right to see these in full
.

<html>
 <form id="myform" action="gain.php" method="post" />
 <div><font color=#ff0000 face="'Helvetica Neue', Helvetica, Arial, sans-serif">Current Gain: <?php system('cat /usr/local/sbin/gain/currentgain');?> </font></div>
 <select name="gain" id="gain">
   <option value=-10>-10</option>
   <option value=49.6>49.6</option>
   <option value=48.0>48.0</option>
   <option value=44.5>44.5</option>
   <option value=43.9>43.9</option>
   <option value=43.4>43.4</option>
   <option value=42.1>42.1</option>
   <option value=40.2>40.2</option>
   <option value=38.6>38.6</option>
   <option value=37.2>37.2</option>
   <option value=36.4>36.4</option>
   <option value=33.8>33.8</option>
   <option value=32.8>32.8</option>
   <option value=29.7>29.7</option>
   <option value=28.0>28.0</option>
   <option value=25.4>25.4</option>
   <option value=22.9>22.9</option>
   <option value=20.7>20.7</option>
   <option value=19.7>19.7</option>
   <option value=16.6>16.6</option>
 </select>
 <input type="submit" value="Set Gain" style="color:#ffffff;background-color:#00A0E2;border-color:#00B0F0;" />
 </form>
</html>

<?php
function setgain(){
$gain="{$_POST['gain']}";
system("echo $gain > /usr/local/sbin/gain/newgain");
sleep(5);
header("Refresh:0");
}

if ("{$_POST['gain']}"){
setgain();
}

?>


Save (Ctrl+o) and Close (Ctrl+x)

.

STEP-4: Edit file index.html and add code to embed newly created file gain.php (the file gain.php contains “Set Gain” button & drop-down menu).

.
4.1 - BEFORE EDITING, make its backup copy. This will enable you to revert in case something goes wrong.

cd /usr/share/dump1090-fa/html/
sudo cp index.html index.html.orig

#Check to ensure back-up copy has been created by giving ls command

ls

#The output should contain both index.html and index.bak.html
config.js    formatter.js    layers.js         script.js
db           images          markers.js        spinny.gif
dbloader.js  index.html.orig  ol                style.css
flags.js     index.html      planeObject.js    test
flags-tiny   jquery          registrations.js  upintheair.json

.

4.2 - Open the file `index.html for editing

sudo nano /usr/share/dump1090-fa/html/index.html  

4.3 - Press Ctrl+w then type buttonContainer then press Enter key.
The cursor will jump to line containg word buttonContainer, as shown below

			<div class="logoContainer">
				<a href="http://flightaware.com/" target="_blank"><img src="images/fa_logo_color.png" alt="FlightAware" class="flightawareLogo" srcset="images/fa_logo_color.png 1x, images/fa_logo_color@2x.png 2x, images/fa_logo_color@3x.png 3x"></a>
				<img src="images/pa-sv-logo.png" alt="PiAware SkyView" class="adsbLogo piAwareLogo" srcset="images/pa-sv-logo.png 1x, images/pa-sv-logo@2x.png 2x, images/pa-sv-logo@3x.png 3x">
				<img src="images/ff-sv-logo.png" alt="FlightFeeder SkyView" class="adsbLogo flightfeederLogo" srcset="images/ff-sv-logo.png 1x, images/ff-sv-logo@2x.png 2x, images/ff-sv-logo@3x.png 3x" style="display: none;">
			</div>

			<div class="buttonContainer">
				<div class="dateTime" id="clock_div"></div>
				<div class="button buttonHeader" onclick="resetMap();"><span class="buttonText">Reset Map</span></div>

.

4.4 - Just above line <div class="buttonContainer">, insert following 3 lines

<div id="GAIN" style="text-align:center;width:175px;height:65px;">   
<iframe src=gain.php style="border:0;width:175px;height:65px;"></iframe>   
</div> <!----- GAIN --->   

.
After insertion of 3 lines, the code will be like shown below:

			<div class="logoContainer">
				<a href="http://flightaware.com/" target="_blank"><img src="images/fa_logo_color.png" alt="FlightAware" class="flightawareLogo" srcset="images/fa_logo_color.png 1x, images/fa_logo_color@2x.png 2x, images/fa_logo_color@3x.png 3x"></a>
				<img src="images/pa-sv-logo.png" alt="PiAware SkyView" class="adsbLogo piAwareLogo" srcset="images/pa-sv-logo.png 1x, images/pa-sv-logo@2x.png 2x, images/pa-sv-logo@3x.png 3x">
				<img src="images/ff-sv-logo.png" alt="FlightFeeder SkyView" class="adsbLogo flightfeederLogo" srcset="images/ff-sv-logo.png 1x, images/ff-sv-logo@2x.png 2x, images/ff-sv-logo@3x.png 3x" style="display: none;">
			</div>

<div id="GAIN" style="text-align:center;width:175px;height:65px;">   
<iframe src=gain.php style="border:0;width:175px;height:65px;"></iframe>   
</div> <!----- GAIN --->   

			<div class="buttonContainer">
				<div class="dateTime" id="clock_div"></div>
				<div class="button buttonHeader" onclick="resetMap();"><span class="buttonText">Reset Map</span></div>

.

STEP-5: Test and Run

5.1 - Reboot Pi for cron to run the script setgain.sh at startup.

5.2 - After Pi has rebooted, go to page IP-of-PI/dump1090-fa/ and check gain setting add-on appears as shown in screenshot below.

The value show in Red at browser load or re-load is your current gain value.

If you want to change it, click open drop-down menu, select the desired gain and click “Set Gain” button. After about 5 seconds, the Current Gain (red) will be updated.

Check the gain value by following command. The last line of output of this command will show the actual value of gain used.

sudo systemctl status dump1090-fa -l    

.
You can also check current gain by following command

cat /etc/default/dump1090-fa

.
.
CLICK ON IMAGE TO SEE LARGER SIZE