IsidroDaniel
Posts: 3
Joined: Tue Mar 27, 2018 9:46 pm

How to pass variables from HTML to CGI bash

Tue Mar 27, 2018 9:56 pm

I'm not a code expert and I have been trying to control 2 servos using an HTML page using a Raspberry Pi. I'm using an HTML file that reads inputs by on mouse click in button and triggers cgi files for each desired funtion . This part is working fine. Now would like to use keyboard inputs in parallel with this. I created a cgi file that is capable of recieving the key inputs and drive the servos in the console window, I could not find how to transmit a key press on the HMTL page in the borwser and send it to the cgi file. This cgi file allows me to make a kind of continuos pan and tilt of the servos by increments of 1, by a continuosly press of a key until they reach the limits defined in the conditios.

My code in the cgi flie is:

#!/bin/bash
a="0"
b="0"
while true; do
read -rsn1 input
if [ "$input" = "a" ] && [ $a -le "89" ]; then
echo 0=+1 >/dev/servoblaster
a=$((a + 1))
fi
if [ "$input" = "d" ] && [ $a -ge "-75" ]; then
echo 0=-1 >/dev/servoblaster
a=$((a - 1))
fi
if [ "$input" = "w" ] && [ $b -le "45" ]; then
echo 1=+1 >/dev/servoblaster
b=$((b + 1))
fi
if [ "$input" = "s" ] && [ $b -ge "-45" ]; then
echo 1=-1 >/dev/servoblaster
b=$((b - 1))
fi
done
For sure is not the most elegant code solution... but is working... :D

I have doodled on the HTML page code something with bits of code I took from tutorials on the web and was able to aquire key presses and visualize them on the web page.

On my HTML file I have the falowing code:

<body>
<script>
...
document.onkeydown = function(event)
{
var key_press = String.fromCharCode(event.keyCode);
document.getElementById('kp').innerHTML = key_press;
xmlhttp.open("GET","cgi-bin/keycontrol.cgi",true);
xmlhttp.send();
}
...
</script>
...
<div class="content">
....
<br>
Key Pressed : <span id="kp"></span>
....
</div>
</body>
Now I´m stuck on how to send these key presses and get the cgi bash file to read them... to be honest I'm not even sure if is that is possible to implement with my actual code structure!!! So if someone could give me a hint on how to do this I would apreciate very much, or at least to point me in the correct direction!!

Sorry for the long post... and thanks for the attention!

User avatar
DougieLawson
Posts: 37073
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to pass variables from HTML to CGI bash

Wed Mar 28, 2018 12:12 am

Use the HTML POST method rather than HTML GET and all the data you send in from the web browser appears as the stdin file for your CGI program.

For a crude test read it with

Code: Select all

#!/bin/bash

printf "Content-Type: text/html\r\n\r\n"
echo "<pre>"
#/usr/bin/env

read line
echo "$line"

echo "</pre>"

Code: Select all

<html><head><title>Crude Web Form</title>
</head><body>
<form action=cgi-bin/test.sh method=POST>
<input type=text name=f1 value=f1 maxlength=20>
<input type=text name=f2 value=f2 >
<input type=submit value=submit>
</form></body></html>
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

IsidroDaniel
Posts: 3
Joined: Tue Mar 27, 2018 9:46 pm

Re: How to pass variables from HTML to CGI bash

Wed Mar 28, 2018 5:14 pm

Hi DougieLawson ,

Thanks for your reply.
I will try to sumarize my results. I kept the exaple code you give me, just adding in the bach file one line for moving the servo and check if the value was passed from the HTML page to the CGI file:

Code: Select all

#!/bin/bash
printf "Content-Type: text/html\r\n\r\n"
echo "<pre>"
#/usr/bin/env
read line
echo "$line"
echo 0="$line"% >/dev/servoblaster
echo "</pre>"
On the HTML I used only one text box and pointed in <form action=cgi-bin/test.sh method=POST> to my CGI file
Uppon inserting a value in the text box I get a new page showing the value entered which I presume is a result of the return from the printf statement, correct?
If so, means the CGI file is able to read the value passed from the HTML page and return it back on another HTML page, but is not passed to the servo command!
If I run the file from the console command line and input a value I get servo moving!... for sure I'm doing something wrong... noobie style!!!

So... kindly I would like another kick in the butt on the propper direction! :mrgreen: :mrgreen: :mrgreen:

User avatar
DougieLawson
Posts: 37073
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to pass variables from HTML to CGI bash

Wed Mar 28, 2018 8:24 pm

Search Google: https://www.google.co.uk/search?q=bash+ ... hod%3Dpost there's a million and one examples of doing CGI with bash (although lots use the same lump of code).

https://oinkzwurgl.org/hacking/bash_cgi/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

IsidroDaniel
Posts: 3
Joined: Tue Mar 27, 2018 9:46 pm

Re: How to pass variables from HTML to CGI bash

Sat Apr 07, 2018 12:51 pm

Hi DougieLawson,

Thanks for your suggestions, after some digging and testing I was able to sort out how to implement a solution that suits my needs.
I went on a different approach and instead of passing the key inputs to the CGI file I choose to do all the control logic on a javascript and just call the files according to the keys pressed.
Later once I have a more polished code will post it here!

Thanks for your assistance!

Killertechno
Posts: 194
Joined: Wed Jan 02, 2013 8:28 am

Re: How to pass variables from HTML to CGI bash

Thu Feb 06, 2020 3:47 pm

Hi to all, I try to avoid to open new discussion.....

I have linux webserver (OpenWRT) running similar cgi-bin

Code: Select all

#!/bin/bash

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Welcome</title></head>"
echo "<body>"

name=John


echo "parameter is <$1>"
echo "<br>"
if [ "$1" = "John" ]
then
	echo "Name is John"


else
	echo "unknown"


fi
#echo "Welcome to your Bash CGI!"
echo "</body></html>" 
In code above I just write if argument is John or not.

I call this page using http://address/cgi-bin/simple.cgi?John

It doesn't work for me (I'm using lighttpd); I don't understand if there is something else to set or if I don't understand how passing arguments to cgi-bin....
Thanks.

Return to “General programming discussion”