Set Gain from Browser


#1

This is an experiment, mainly of academic interest.
.


.

It started when forum member @evangelyul added a Gains drop-down menu to the table at right of SkyView map. He then contacted me to find out what code will be required to change the gain setting in Pi. At first it looked simple, as in the < select > dropdown, the selected values can be passed by javascript command <select onChange="function();" />.

However when I tried, it did not work. After some Google Search I discovered that javascript works on the same computer on which the browser is located. Here the situation is different. The browser is located on Client (Windows/Mac) whereas the command is required to be executed on Server (RPi). This is possible with PHP or AJAX, but not with javascript.

As I have very little knowledge of PHP, and Zero knowledge of Ajax, I apologized to @evangelyul that I cannot help him.

This weekend I had some spare time, and after lot of Googling could workout a solution.
The solution comprises of:

  1. PHP code snippets to pass the commands from Client (Win/Mac computer) to Server (RPi).

  2. Adding required packages and modules to enable Pi to handle PHP pages.

  3. Commands sent from Client (Win/Mac) to Server (RPI), failed to execute with “Not Permitted” response . I found that the commands from Client browser are treated as from user www-data which for security reasons does not have sudo powers. I then assigned a strong password to user www-data, and then added it to sudoers group. Now the commands started executing.
    .

Here is the step-bystep method

Piaware%20SD%20Card%20Image

This post is for Piaware SD Card image.

For Raspbian Jessie/Stretch + dump1090-mutability package install, click here

For Raspbian Jessie/Stretch + dump1090-fa package install, click here

.

STEP-1: Install necessary php packages and enable module

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

.

STEP-2: Assign strong password to user www-data, and add it to sudoer’s group

sudo passwd www-data
sudo adduser www-data sudo
groups www-data

.
REBOOT for sudo permission of user www-data to be implemented.

sudo reboot

STEP-3: Create new directory and files

(a) Create new directory

sudo mkdir /var/www/html/setgain

(b) Move inside the new directory

cd /var/www/html/setgain/

(c ) Inside directory /var/www/html/setgain, create a new blank file setgain.sh, make it executable

sudo touch setgain.sh
sudo chmod +x setgain.sh

(d) Open newly created file in nano editor

sudo nano setgain.sh

(e) Add code to the new file
(In code below, replace xxxxxxxxx by the actual password you have set for user www-data)

#!/bin/bash
echo 'xxxxxxxxxxx' | sudo -S piaware-config rtlsdr-gain "$1"

echo 'xxxxxxxxxxx' | sudo -S systemctl restart dump1090-fa

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

(f) Inside directory /var/www/html/setgain, create a new blank file findgain.sh, make it executable

sudo touch findgain.sh
sudo chmod +x findgain.sh

(g) Open newly created file in nano editor

sudo nano findgain.sh

(h) Add code to the new file
(In code below, replace xxxxxxxxx by the actual password you have set for user www-data)

#!/bin/bash
echo 'xxxxxxxxxxx' | sudo -S piaware-config -show rtlsdr-gain

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

(i) Inside directory /var/www/html/setgain, create new php file

sudo nano index.php

(j) Copy-paste following code in it

<?php
echo <<<HTML
 <form id="myform" name="myform" action="{$_SERVER['PHP_SELF']}" method="post" />
    ENTER GAIN VALUE: <input type="text" id="gain" name="gain" value="-10" />
    <input type="submit" name="submit" value="Click to Set Gain" />
 </form>
HTML;

echo "Your gain was = ";
system("/var/www/html/setgain/findgain.sh");
?>
<BR>
<?php
function setgain(){
$_SERVER['PHP_SELF'];
$gain="{$_POST['gain']}";
echo "You changed gain to = $gain";
system("/var/www/html/setgain/setgain.sh $gain  >> /tmp/gain.log 2>&1");
}

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

.
Save (Ctrl+o) and close (Ctrl+x)

.

STEP-4: Load the gain setting file in browser of Win/Mac computer, and set gain.

IP-OF-PI/setgain/

STEP-5: Check if the gain change has actually taken place

piaware-config -show rtlsdr-gain
sudo systemctl status dump1090-fa -l

#Check log for any error messages
cat /tmp/gain.log


Do I Need A Filter?
How-to Read / Set Gain from SkyView and Gmap
#2

thanks abcd567 currently makings trials on a pi without piaware image
but i get a /var/mail/root message the says

Return-Path: <www-data@raspberrypi>
X-Original-To: root
Delivered-To: root@raspberrypi
Received: by raspberrypi (Postfix, from userid 33)
        id 1BB803F678; Mon, 11 Jun 2018 11:05:12 +0300 (EEST)
To: root@raspberrypi
From: www-data@raspberrypi
Auto-Submitted: auto-generated
Subject: *** SECURITY information for rpiSDsan8 ***
Message-Id: <20180611080512.1BB803F678@raspberrypi>
Date: Mon, 11 Jun 2018 11:05:12 +0300 (EEST)

rpiSDsan8 : Jun 11 11:05:12 : www-data : **user NOT in sudoers** ; 
TTY=unknown ; PWD=/var/www/html/setgain ; USER=root ;
COMMAND=/bin/systemctl restart dump1090-fa

fyi i always work on it as root never as pi (lazy to repeat sudo everytime
and no big deal if it breaks due to security issues. I hate security :wink: )


#3

@evangelyul
First time to see an email from Pi. Is it installed with Web Portal by J Prochazka?

I have modified the steps in my Original Post so that the current gain (gain before one changes it by clicking the button) is also displayed.

It required adding a file findgain.sh and adding few lines of code in the file index.php.


#4

Try sudo usermod -aG sudo www-data

adduser can assign a new user to a group when the user account is created if the -G switch is used
e.g. adduser -G sudo www-data


#5

yes, installed the dump1090-fa, then installed Joe’s scripts (to have the graphs) .
it is a bit of a … salad :wink:


#6

thanks xlr99 will try it


#7

@xlr99
@evangelyul

Actually the user www-data already exists by default, and there is no need to re-create it

In this case, the command sudo adduser www-data sudo is NOT creating any new user, but adding existing user www-data to sudoer's group


#8

it did not
I run visudo and manually added it


# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
www-data    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

then in /etc/sudoers.d copied file 010-pi-nopasswd to www-data
with one line www-data ALL=(ALL) NOPASSWD: ALL

still not working… updated your code.


#9

Did you try this code?

#First assign a password for user www-data
sudo passwd www-data 

#Now add user www-data to sudoer's group
sudo adduser www-data sudo

#Now check if user www-data has been added to sudoer's group
groups www-data 

The last command will give this output:

www-data : www-data sudo

#10

one more thing I notice now from my previous post with the dump
it says <www-data@raspberrypi> but I have changed already the
host name to rpiSDsan8 (rpi using sandisk 8GB SD to be able to keep track of the SD’s)
isn’t it strange ?


#11

yes I did and get that response, - btw I use Mobaxterm to access the pi - and when
I try to change user su www-data first time asked for password and got that ‘warning e-mail’
now it says

This account is currently not available.```

I try to avoid reboot cause of the damaged prostick which means I must run
 to 3d floor and unplug/re-plug...

#12

@evangelyul
Very strange indeed!
I have tried my code on two microSD cards, both freshly written with Piaware SD card image 3.5.3, and everything worked perfectly well.


#13

:smiley::smile: great ! well it could be due to the…'salad ’
I think I 'll try on the other pi that has just Joe’s dump1090-mutability and see what happens…
at least I wont have to run up and down…


#14

This wont work on Raspbian + package install of dump1090-mut/dump1090-fa. It is designed to work ONLY on Piaware SD card image.

If you read the first bold line in my original post, you will find this remark.

The reason is that in package install, the command sudo piaware-config rtlsdr-gain xx does NOT change gain of dump1090 (fa/mut), because in package install they read their settings from following files:

dump1090-fa: /etc/default/dump1090-fa

dump1090-mutability: /etc/default/dump1090-mutability


#15

@evangelyul
@xlr99

Currently the commands in files setgain.sh issue commands sudo piaware-config rtlsdr-gain xx, which modifies gain setting in file /etc/piaware.conf, and dump1090-fa reads its gain setting from it when restarted.

Similarly findgain.sh issues command sudo piaware-config -show rtlsdr-gain to read current gain setting.

Both the above commands suite Piaware SD Card install.

For package install, the code in the files setgain.txt and findgain.txt should be modified to write/read gain values to/from files /etc/default/dump1090-mutability or /etc/default/dump1090-fa, as the case may be.


#16

In order to get your file in the /etc/sudoers.d directory to work,
the last line in /etc/sudoers needs to be uncommented.


#17

@evangelyul
@xlr99

[S O L V E D]

I now burned my microSD card with Raspbian Stretch image and did following

  1. Assigned Password to EXISTING user www-data
    sudo passwd www-data

  2. Added user www-data to sudoers group
    sudo adduser www-data sudo

  3. Checked, found www-data has been added to group sudo
    groups www-data
    www-data : www-data sudo

  4. Now started browser
    IP/setgain/index.php
    Entered a gain value and clicked “Click to set gain” button

  5. Checked stdout+stderr
    cat /tmp/a.txt
    [sudo] password for www-data: www-data is not in the sudoers file. This incident will be reported.

  6. Did NOT make any addition/alterations to file /etc/sudoers and files inside directory /etc/sudoers.d. Instead REBOOTED RPI.

  7. After reboot, again started browser
    IP/setgain/index.php
    Entered a gain value and clicked “Click to set gain” button

  8. Checked stdout+stderr
    cat /tmp/a.txt
    Bingo! The “www-data is not in the sudoers file” message no more there.
    Only following error messages (as I have not yet installed Piaware and dump1090-fa):
    [sudo] password for www-data: sudo: piaware-config: command not found
    Failed to restart dump1090-fa.service: Unit dump1090-fa.service not found.

SOLUTION: Reboot RPi after “sudo adduser www-data sudo

.


#18

excellent abcd567 !
now time for the rest…:wink:


#19

.

Jessie%20and%20Stretch%20Mutab

For Raspbian Jessie/Stretch + dump1090-mutability v1.15~dev 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-get update   
sudo apt-get 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-get update   
sudo apt-get install php7.0-cgi   
sudo lighty-enable-mod fastcgi-php   
sudo /etc/init.d/lighttpd force-reload   

STEP-2: Assign strong password to user www-data, and add it to sudoer’s group

sudo passwd www-data   
sudo adduser www-data sudo   
groups www-data   
  

REBOOT for sudo permission of user www-data to be implemented.

sudo reboot

.

STEP-3: Create new directory and files

(a) Create new directory

sudo mkdir /var/www/html/setgain   

(b) Move inside the new directory

cd /var/www/html/setgain/   

(c ) Inside directory /var/www/html/setgain, create a new blank file setgain.sh, make it executable

sudo touch setgain.sh   
sudo chmod +x setgain.sh   

(d) Open newly created file in nano editor

sudo nano setgain.sh   

(e) Add code to the new file
(In code below, replace xxxxxxxxx by the actual password you have set for user www-data)

#!/bin/bash   
echo 'xxxxxxxxxx' | sudo -S sed -i '/GAIN=.*/c\GAIN='$1 /etc/default/dump1090-mutability  

echo 'xxxxxxxxxx' | sudo -S systemctl restart dump1090-mutability  

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

(f) Inside directory /var/www/html/setgain, create a new blank file findgain.sh, make it executable

sudo touch findgain.sh   
sudo chmod +x findgain.sh   

(g) Open newly created file in nano editor

sudo nano findgain.sh   

(h) Add code to the new file
(In code below, replace xxxxxxxxx by the actual password you have set for user www-data)

#!/bin/bash
echo 'xxxxxxxxxx' | sudo -S sed -n 's/\GAIN=//p' /etc/default/dump1090-mutability  

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

(i) Inside directory /var/www/html/setgain, create new php file

sudo nano index.php

(j) Copy-paste following code in it

<?php
echo <<<HTML
 <form id="myform" name="myform" action="{$_SERVER['PHP_SELF']}" method="post" />
    ENTER GAIN VALUE: <input type="text" id="gain" name="gain" value="-10" />
    <input type="submit" name="submit" value="Click to Set Gain" />
 </form>
HTML;

echo "Your gain was = ";
system("/var/www/html/setgain/findgain.sh");
?>
<BR>
<?php
function setgain(){
$_SERVER['PHP_SELF'];
$gain="{$_POST['gain']}";
echo "You changed gain to = $gain";
system("/var/www/html/setgain/setgain.sh $gain  >> /tmp/gain.log 2>&1");
}

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

.
Save (Ctrl+o) and close (Ctrl+x)

.

STEP-4: Load the gain setting file in browser of Win/Mac computer, and set gain.

IP-OF-PI/setgain/

STEP-5: Check if the gain change has actually taken place

(a) Check gain changed or not, and what is its current value

sudo systemctl status dump1090-mutability -l 

sudo sed -n '/GAIN.*/p' /etc/default/dump1090-mutability  

.
(b) Check for error messages

cat /tmp/gain.log  

.


#20

hi abcd567
progressing well I see…

can you try following

#!/usr/bin/python2

x = 0
currentgain = ""

filez = open('/etc/default/dump1090-mutability', 'r')

for line in filez:
  if line.startswith('GAIN'):
    x = int(filter(str.isdigit, line)),
    currentgain = float(int(filter(str.isdigit, line)))*.1
    print currentgain
exit

filez.close

x gets all digits from string
current gain converts number found to 1 decimal. don’t know about -10.

works for me in finding gain but unable to set it.
tks/brgds
evangel