Generating HTML file by using bash [updated]

This page is a part of big project , here we discus how to get data from raspberry pi via bash (command line ) to display it on html page .

the idea is to scan the data by command line commands and add these data to HTML file and all previous steps is periodically run for interval of time which the first file do


1st step is to write a simple script of  HTML file and test it .
try this one :
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_basic_document
you can use this editor for fast testing your HTML code
and there is some good different examples here  also
http://www.w3schools.com/html/html_examples.asp

2nd step is to combine HTML code with bash script
 this tutorials illustrate the method of combination :
http://linuxcommand.org/wss0030.php
don't forget to change the mode of the bash as executable file to be easy to run
   chmod u=rwx,g=rx,o=r myfile  

3rd step is to adding variable from command line and bash environment to HTML script
these two links are enough for doing this
http://linuxcommand.org/wss0040.php
http://linuxcommand.org/wss0050.php

note : bash is very sensitive

4th step to write script for periodic run
this is my code and you can get the idea from it

----------------------------------------------------------------------
#!/bin/bash

# make_page - A script to update data on HTML file

for(( ; ; ))
do
bash ./bash_file_to_generate_html.sh > ./index.html;
sleep 11;
done

exit 0
---------------------------------------------------------------------

note: sleep here is for waiting or delay time between regenerating the HTML file


Finally for auto run these scripts after reloading the system while starting up
add command that run the file of the periodic script to the file /etc/rc.local before exit 0 line
using nano command
--------------------------------------------------------------------
sudo nano /etc/rc.local
--------------------------------------------------------------------


-------- my final   rc.local  file -------------------------------
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# for dns to access internet
echo 'nameserver 8.8.8.8' >> /etc/resolv.conf;
echo 'name server 8.8.4.4' >> /etc/resolv.conf;

# for updating server page
bash /var/www/update_html.sh &

exit 0
-------------------------------------------------------------


--- my  periodic file update_html.sh -----------------
#!/bin/bash

# make_page - A script to update data on HTML file

for(( ; ; ))
do
bash /var/www/bash_file_to_generate_html.sh > /var/www/index.html;
sleep 11;
done

exit 0
-------------------------------------------------------------

------ bash_file_to_generate_html.sh ----------------
#!/bin/bash

# make_page - A script to produce an HTML file
now_hour=$(date +%I:%M) 
now_pm_am=$(date +%p)
now_date=$(date +%F)
Now_hourly="$now_date $now_hour $now_pm_am"   

temperature=$(vcgencmd measure_temp)
volts_core=$(vcgencmd measure_volts core)

Global_IP=$(curl icanhazip.com)

cat <<- _EOF_
<html><body bgcolor="#122C2C" text="#25DC10">
<meta http-equiv="refresh" content="30" >
<center>
<h1> *  RPi-Soubhi  * </h1>

<p>CPU $temperature</p>
<p>Core $volts_core</p>
<p>Global ip $Global_IP</p>

<P>Updated on $(date +"%F %r %Z") by Raspberry pi</p>
</center>
</body></html>
_EOF_

---------------------------------------------------------------------
Printscreen for the output from my android device :)

تم بحمد الله 
و في انتظار التقدم القادم بإذن الله 
^__^


updated in a new page 



No comments:

Post a Comment