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:
while true; do
read -rsn1 input
if [ "$input" = "a" ] && [ $a -le "89" ]; then
echo 0=+1 >/dev/servoblaster
a=$((a + 1))
if [ "$input" = "d" ] && [ $a -ge "-75" ]; then
echo 0=-1 >/dev/servoblaster
a=$((a - 1))
if [ "$input" = "w" ] && [ $b -le "45" ]; then
echo 1=+1 >/dev/servoblaster
b=$((b + 1))
if [ "$input" = "s" ] && [ $b -ge "-45" ]; then
echo 1=-1 >/dev/servoblaster
b=$((b - 1))
For sure is not the most elegant code solution... but is working...
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:
document.onkeydown = function(event)
var key_press = String.fromCharCode(event.keyCode);
document.getElementById('kp').innerHTML = key_press;
Key Pressed : <span id="kp"></span>
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!