Been having fun, sort of.
Starting to like Javascript, also starting to understand how the web + CGI stuff works.
HTML code
<div>
<button onclick="switchon()" id="onoff" style="font-weight:bold; font-size:250%; background-color:grey" >On</button>
<button onclick="switchoff()" id="offon" style="font-weight:bold; font-size:250%; background-color:red" >Off</button>
<button id="stepstatus" style="font-size:250%; background-color:yellow" >stopped</button>
</div>
....
<script>
function switchon(){
console.log("on");
document.getElementById("offon").style.background = "grey";
document.getElementById("onoff").style.background = "lightgreen";
document.getElementById("stepstatus").innerHTML = 'Running';
document.location="/cgi-bin/poweron.cgi";
}
function switchoff(){
console.log("off");
document.getElementById("onoff").style.background = "grey";
document.getElementById("offon").style.background = "red";
document.getElementById("stepstatus").innerHTML = 'Stopped';
document.location="/cgi-bin/poweroff.cgi";
}
poweron.cgi
#!/bin/sh
echo "HTTP/1.1 204 No Content"
echo ""
echo 1 > /sys/class/gpio/gpio17/value
By calling different xx.cgi files I don't need to read the query string just to turn a GPIO on and off.
console.log is useful for debugging, right click browser window "Inspect element", in Chrome anyway.
Could fancy it up by using image buttons etc.
Think I now have a good idea on how Webiopi might work, still seems like a lot of code to do the same thing?
Hi gavinmc42,
Good work! There is no "right" answer to your questions.
If you want to keep your image small, then you need to just use sh, html5, css and javascript as you have done. This is Steen's and my philosophy is developing piCorePlayer. If you are developing an application then lots of small files end up being a pain to manage. If you are just controlling a couple of inputs or outputs then is does not matter one way or the other.
I have used perl for scripting for the last 25 years, but in the piCore environment it is relatively big and bulky. So it may not be an option.
Python seems to be the preferred language for the Raspberry Pi. Just about everything example of code you ever see for the Raspberry Pi is python. Why? I must have been asleep for a decade or two, never noticed python becoming so popular.
For embedded systems lua seems to be the go. It is used on a lot of modems for their configuration environment. Have a look at dd-wrt etc.
As far as bulky code goes, it doesn't really matter. Once you work out the method, you just make it a routine and called it over and over. Here's a snippet of pcp-functions.
#=========================================================================================
# Banner, navigation, footer and controls html
#-----------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------
# $1 = title, $2 = author, $3 = seconds, $4 = url
#-----------------------------------------------------------------------------------------
pcp_html_head() {
echo 'Content-Type: text/html'
echo ''
echo '<!DOCTYPE html>'
echo '<!-- Start of pcp_html_head -->'
echo '<html>'
echo '<head>'
echo ' <meta charset="UTF-8">'
echo ' <meta http-equiv="Cache-Control" content="no-cache">'
echo ' <meta http-equiv="Pragma" content="no-cache">'
echo ' <meta http-equiv="Expires" content="0">'
if [ $DEBUG = 0 ]; then
if [ x"" != x"$3" ] && [ x"" != x"$4" ]; then
echo ' <meta http-equiv="Refresh" content="'$3'; url='$4'">'
fi
fi
echo ' <title>pCP - '$1'</title>'
echo ' <meta name="author" content="'$2'">'
echo ' <meta name="description" content="'$1'">'
echo ' <link rel="stylesheet" type="text/css" href="../css/piCorePlayer.css">'
echo ' <link rel="icon" href="../images/pCP.png" type="image/x-icon" />'
echo ' <script src="../js/piCorePlayer.js"></script>'
echo '</head>'
echo ''
echo '<body>'
echo '<!-- End of pcp_html_head -->'
}
pcp_html_head_no_content() {
echo -n "HTTP/1.1 204 No Content\n\n"
#cat <<EOF
#HTTP/1.1 204 No Content
#
#EOF
# printf "%s\n\n" "HTTP/1.1 204 No Content"
}
pcp_banner() {
echo '<!-- Start of pcp_banner -->'
echo '<table class="bgblack">'
echo ' <tr>'
echo ' <td height="148">'
echo ' <p class="banner">'
echo ' <a href="about.cgi" title="pCP Logo"><img src="../images/banner.png" alt="piCorePlayer" /></a>'
echo ' </p>'
echo ' </td>'
echo ' </tr>'
echo '</table>'
echo '<!-- End of pcp_banner -->'
}
piCorePlayer is now over 11,000 lines of mainly shell scripts. Developing a new script, is just a matter of cutting and pasting segments of existing code, then modifying it to suit. The stage you are going through is the difficult and tedious stage, working everything out for the first time. I am enjoying your thread as I am always looking out for better or easier to managed ways of doing things, especially as it is based on piCore, busybox-httpd, sh, html5, css and javascript, exactly the decisions Steen and I have made.
regards
Greg