Controlling GPIO pins with cgi scripts - raspberry-pi

I am trying to control GPIO pins over the web. I have apache server installed on my raspberry-pi(Raspbian wheezy) and I have created a web page with Html and javascript. What I want to do is to control the gpio pins when a user clicks a button on a page.
I have bash script on a .cgi file in /usr/lib/cgi-bin directory and I made certain arrangements on apache configuration file so that it can access the file on that location.
Here is the .cgi file content:
#!/bin/bash
gpio -g mode 7 out
gpio -g write 7 1
echo "Status: 204 No Content"
echo "Content-type: text/html"
echo ""
changes to the conf file of apache: /etc/apache2/sites-enabled/000-default
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .cgi // I added this line
</Directory>
but when I click button on the page it shows no response. I have also made the .cgi file executable with this command:
sudo chmod a+x setu.cgi
page code:
head section:
<head>
<script language="JavaScript" type="text/JavaScript">
function setu()
{
document.location="cgi-bin/setu.cgi";
}
function clearall(event)
{
document.location="cgi-bin/clearall.cgi";
}
</script>
</head>
body section:
<form name="form1" method="post" action="">
<p align="center"> </p>
<p align="center">
<input name="up" type="button" id="up" value="UP" onmousedown="setu()" onmouseup="clearall(event)" >
<p align="center"> </p>
</form>
Any help on this topic would be great.

Enable mod_cgi module for apache2
sudo a2enmod mod_cgi
Depending on what architecture your processor is it will enable cgi or cgid module
Worked for me.

Related

MAMP 500 Internal Server Error

I got a little problem.
I made two virtual hosts "web-backend.local" and "oplossingen.web-backend.local".
But I always have a "500 Internal Server Error"
What am I doing wrong?
Hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
# MAMP VirtualHost Mappings
127.0.0.1 web-backend.local
127.0.0.1 oplossingen.web-backend.local
httpd-vhosts.conf:
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost web-backend.local:* >
DocumentRoot "/Users/yawuarsernadelgado/Documents/web-backend/cursus"
ServerName web-backend.local
<Directory "/Users/yawuarsernadelgado/Documents/web-backend/cursus">
AllowOverride All
Require all granted
Options +Indexes
IndexOptions NameWidth=*
</Directory>
ServerAlias web-backend.local
</VirtualHost>
<VirtualHost oplossingen.web-backend.local:* >
DocumentRoot "/Users/yawuarsernadelgado/Documents/web-backend/oplossingen"
ServerName oplossingen.web-backend.local
<Directory "/Users/yawuarsernadelgado/Documents/web-backend/oplossingen">
Allow from all
Require all granted
Options +Indexes
IndexOptions NameWidth=*
</Directory>
</VirtualHost>
Although it's reported MAMP 500 Server Error, it usually has little to do with MAMP.
500 internal server errors indicate you have crashed php pages. the solution is to add following error-reporting php code in your potential crashed page:
// display php error
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if you use jQuery.Ajax, then I am afraid the code above in action.php won't work, there is another method, which is followed by
add this to index.html or index.php
<div id="show-error" style="position: fixed; z-index: 10000; height: 20vh; width: 100vw; background:#f08080; border-radius:20px; opacity: 0.8; display: flex; font-size: 2rem; justify-content: center; align-items: center; display: none;"></div>
in the php file called by ajax, set a success code and failue code
try{
// main
echo json_encode(['status' => 1, 'msg' => 'succeed']);
}catch(Exception $e){
echo json_encode(['status' => 0, 'msg' => $e->getMessage()]);
}
finallly, in the ajax javascript file, catch error if it occurs
$.ajax({
url: '../php/action.php',
method: 'POST',
data: array('some data'),
}).done(function(result){
var output = JSON.parse(result);
if(output.status == 0){
$('#show-error').show();
$('#show-error').html(output.msg);
exit();
}else{
// no error occurred
}
});
Remove the following lines from httpd-vhosts.conf file from each virtual host:
<Directory "/Users/yawuarsernadelgado/Documents/web-backend/cursus">
AllowOverride All
Require all granted
Options +Indexes
IndexOptions NameWidth=*
</Directory>
Worked for me :D

Sinatra can't see styles.css

In my directory I have:
shop.rb (App < Sinatra::Base)
lib directory - Ruby models inside
views directory - erb files inside
Gemfile
Where should I place my style.css ? I tried every place possible, I created public folder and tried with also every possible place, but my styles doesn't work.
In layout I have:
<link rel="stylesheet" href="/style.css"/>
I tried also with:
<link rel="stylesheet" href="style.css"/>
It might be because the app is not configured to find the public directory/ First make sure the public directory exists relative to the main entry file of your application (assuming shop.rb) and use the following configuration:
class Shop < Sinatra::Base
configure do
set :public_folder, File.expand_path('../public', __FILE__)
set :views , File.expand_path('../views', __FILE__)
set :root , File.dirname(__FILE__)
end
get '/' do
erb :index
end
end
Once set up, Sinatra should now know where to find your public directory and you can just include the asset file like this:
CSS:
<link rel="stylesheet" href="/style.css"/>
Image:
<img src="/img/background.png" alt="Image" />
JavaScript:
<script src="/js/main.js"></script>

Submit Form not Working in CodeIgniter 3.0.3

I was tasked to write a simple program in CodeIgniter.I am coding in Symfony 2 and I have no trouble creating forms for testing since Symfony 2 offers a nice CRUD through command line which CodeIgniter lacks.So i have to create manually everytime I am creating forms for testing..But in chrome, the form does nothing when submit button is hit, and in Mozilla, and error message something like
The address wasn't understood
Firefox doesn't know how to open this address, because one of the following protocols (localhost) isn't associated with any program or is not allowed in this context.
You might need to install other software to open this address.
Controller
public function add_makers()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Add New Car Maker';
$data['main_content'] = 'makers/add_makers';
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('nation_id', 'Nation_Id', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/template', $data);
//echo "ok";
}
else
{
$this->makers_model->new_makers();
//$this->load->view('makers/success');
}
Form
<h1><?php echo $title; ?></h1>
<?php echo form_open('makers/add_makers'); ?>
<label for="name">Name</label>
<?php
$data = array(
'type' => 'text',
'name' => 'name'
);
echo form_input($data);
?>
<label for="description">Description</label>
<?php
$data = array(
'type' => 'text',
'name' => 'description'
);
echo form_textarea($data);
?>
<label for="nation_id">Country</label>
<?php
$data = array(
'name' => 'nation_id',
'type' => 'text'
);
echo form_input($data);
?>
templates
<?php
/*
*Load header contents, and footer
*
*/
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
I am running development in vagrant(ubuntu trusty) installed in Windows and I have no problem using this in Symfony2 or Laravel project.Rewrite is all enable by running
sudo a2enmod rewrite
Inside Apache, I configure virtual host this way
<VirtualHost *:80>
#ServerAdmin admin#yourdomain.com
DocumentRoot /var/www/CodeIgniter-3.0.3/
#ServerName yourdomain.com
#ServerAlias www.yourdomain.com
<Directory /var/www/CodeIgniter-3.0.3/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
#ErrorLog /var/www/CodeIgniter-3.0.3/logs/httpd/yourdomain.com-error_log
#CustomLog /var/log/httpd/yourdomain.com-access_log common
</VirtualHost>
Out in the box, I have this .ht access pre configured inside
Project
/Application
.htaccess
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
And lastly routes
$route['makers/add_makers'] = 'makers/add_makers';
//$route['news/(:any)'] = 'news/view/$1';
//$route['news'] = 'news';
//$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'services/index';
I already searched Google for this and found no solution.Is there something missing in my files?
You could check the html code generated by CI, and try to Enter the full post URL in the address bar, maybe something happen, and then check the log file of apache and php, may it be helpful to solve your problem
Found the solution
I modify the .htaccess and move it outside application folder and put in root folder
RewriteEngine On
RewriteCond $1 !^(index\.php|styles|scripts|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
#<IfModule mod_gzip.c>
# mod_gzip_on Yes
# mod_gzip_dechunk Yes
# mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
# mod_gzip_item_include handler ^cgi-script$
# mod_gzip_item_include mime ^text/.*
# mod_gzip_item_include mime ^application/x-javascript.*
# mod_gzip_item_exclude mime ^image/.*
# mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
#</IfModule>
Refactor apache 2.4
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
#ServerAdmin webmaster#localhost
DocumentRoot /var/www/Codeigniter
AcceptPathInfo On
<Directory /var/www/Codeigniter>
AllowOverride None
Require all granted
</Directory>
Then inside config/config.php
$config['base_url'] = '';//note the auto guessing.If I set this to localhost:8090/something, this form will not submitting
$config['index_page'] = '';
I tried many solutions provided in google and other suggestions but none works.Just got it right by spending many hours in trying possible solutions.
I am using Vagrant and Ubuntu trusty in development.Previously , I used Wamp for this project and I have no problem submitting forms.

Premature end of script headers (perl+index.html)

Respected ppl ...
Im on openSUSE 12.2 and my Apache server is running fine ...
I copied the index.html which contains :
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Program 7b</title></head>
<body>
<form method="POST" action="http://localhost/cgi-bin/7b.pl">
Please Enter the Command :
<input type="text" name="command" id="command" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
And the perl file as follows :
#!/usr/bin/perl
print "Content-type:text/html\n\n";
use CGI
$a = new CGI;
$comm = $a->param("command");
print "The Output of the entered command is:<br />";
system($comm);
Both files in the cgi-bin directory
But i get the error :
Premature end of script headers
On running localhost/cgi-bin/index.html in any browser ....
I have set the execution of perl files in the apache configuration ...
Kindly help with the problem ...
Regards
-SkyKOG
Apache is running index.html as a script, not simply displaying an HTML document. Apache is probably configured to run anything in the cgi directory as a script.
Move the html file into another directory.
Try the following working code :
#!/usr/bin/perl
use strict; use warnings;
use CGI;
print "Content-type:text/html\n\n";
my $a = new CGI;
my $comm = $a->param("command");
print "The Output of the entered command is:<br />";
system($comm);
An advice : never forget use strict; use warnings; and declare variables with my or our.

Deploy VisiFire On JBoss Portal

I am trying to improve my JBoss Portal dashboard using some analytical charting tools then I found VisiFire. So I started to investigate how I could deploy a simple VisiFire page as a portlet onto JBoss Portal dashboard. Below is what I did:
- I created a dummy portlet (display "Hello World" on the index page, index.html)
- Replaced the content of the index.html with a simple chart which would be rendered by VisiFire:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Visifire Charts</title>
<script language="javascript" type="text/javascript" >
</script>
</head>
<body>
<!-- To embed in existing html copy the code below -->
<script type="text/javascript" src="Visifire.js"></script>
<div id="VisifireChart">
<script language="javascript" type="text/javascript">
var chartXmlString = ''
+'<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts" Width="500" Height="300" BorderThickness="0" Theme="Theme1" ToolBarEnabled="True" >'
+'<vc:Chart.Titles>'
+'<vc:Title Text="Global Fortune 5 Companies 2007" />'
+'</vc:Chart.Titles>'
+'<vc:Chart.AxesX>'
+'<vc:Axis Title="Companies" />'
+'</vc:Chart.AxesX>'
+'<vc:Chart.AxesY>'
+'<vc:Axis Title="Revenue in Million dollars" AxisType="Primary" />'
+'</vc:Chart.AxesY>'
+'<vc:Chart.Series>'
+'<vc:DataSeries RenderAs="Column" AxisYType="Primary" >'
+'<vc:DataSeries.DataPoints>'
+'<vc:DataPoint AxisXLabel="Wall-Mart" YValue="351139" />'
+'<vc:DataPoint AxisXLabel="Exxon Mobil" YValue="345254" />'
+'<vc:DataPoint AxisXLabel="Shell" YValue="318845" />'
+'<vc:DataPoint AxisXLabel="BP" YValue="274316" />'
+'<vc:DataPoint AxisXLabel="General Motors" YValue="207349" />'
+'</vc:DataSeries.DataPoints>'
+'</vc:DataSeries>'
+'</vc:Chart.Series>'
+'</vc:Chart>';
var vChart = new Visifire("SL.Visifire.Charts.xap" , 500 , 300 );
vChart.setDataXml(chartXmlString);
vChart.render("VisifireChart");
</script>
</div>
<!-- Copy till here -->
</body>
</html>
I downloaded the VisiFire 3.6.1 Silverlight binaries and copied into the same directory as the index.html
I updated the web.xml with the MIME type:
xamlapplication/xaml+xml
xapapplication/x-silverlight-app
I deployed it onto my local JBoss AS, but the portlet is blank
I have been google around but could not find any useful information on how to deploy a portlet contains VisiFire Silverlight chart onto JBoss Portal. Have I don't anything stupidly unreasonable? Any hints or sample codes would be appreciated!
Thanks in advance
There is no "Silverlight" icon on the right click drop down menu.
I think JBoss is not allowing to download the file SL.Visifire.Charts.xap. Please check whether SL.Visifire.Charts.xap and Visifire.js are uploaded at correct directory. Also type the direct path of SL.Visifire.Charts.xap in address bar of the browser and try to download it.
Also try setting direct path like below
var vChart = new Visifire("http://www.example.com/SL.Visifire.Charts.xap" , 500 , 300 );
Can you please check whether you are able to see Silverlight on right click.