abhi143
Posts: 129
Joined: Mon Oct 30, 2017 4:17 am

Web interfacing with CGI script /C++

Thu Aug 09, 2018 12:04 pm

I am trying to access gpio of raspberry Pi via web interface. When button pressed on web page, Led would be flash

This is simple CGI script . I tested it works
sudo nano /usr/lib/cgi-bin/test.cgi

Code: Select all


#!/bin/sh
echo "Content-type:text/html\n"
echo '<!DOCTYPE html>'
echo '<html>'
echo '  <body>'
echo 'Hello World!'
echo '  </body>'
echo '</html>'
sudo chmod 755 /usr/lib/cgi-bin/test.cgi

http://localhost/cgi-bin/test.cgi

I wrote c++ program that flash led

Code: Select all

#include <iostream>
using namspace std;
#include <wiringPi.h>
 
int main (void)
{
  cout<<"Raspberry Pi blink" <<endl;
 
  if (wiringPiSetup () == -1)
    return 1 ;
 
  pinMode (0, OUTPUT) ;         // aka BCM_GPIO pin 17
 
  for (;;)
  {
    digitalWrite (0, 1) ;       // On
    delay (500) ;               // mS
    digitalWrite (0, 0) ;       // Off
    delay (500) ;
  }
  return 0 ;
}

Code: Select all

<button type="button">flash</button>
When i press flash button led should be start flashing. How to make it working

fbe
Posts: 347
Joined: Thu Aug 17, 2017 9:08 pm

Re: Web interfacing with CGI script /C++

Thu Aug 09, 2018 8:03 pm

Just compile it and call it from a CGI program e.g. from /usr/lib/cgi-bin/test.cgi. But you will run into problems if you do so.

Your program will run in a webserver session as user www-data and doesn't have the privilege to access the gpio. So it will crash and you get an error message (e.g. http 500).
This problem solved, your program will run in an endless loop and the browser will wait for a response. Maybe you get a timeout error after a few minutes.

abhi143
Posts: 129
Joined: Mon Oct 30, 2017 4:17 am

Re: Web interfacing with CGI script /C++

Fri Aug 10, 2018 1:37 am

fbe wrote:
Thu Aug 09, 2018 8:03 pm
This problem solved, your program will run in an endless loop and the browser will wait for a response. Maybe you get a timeout error after a few minutes.
I removed forever loop so when button click, led would flash only one time

Code: Select all

#include <iostream>
using namspace std;
#include <wiringPi.h>
 
int main (void)
{
  cout<<"Raspberry Pi blink" <<endl;
 
  if (wiringPiSetup () == -1)
    return 1 ;
 
  pinMode (0, OUTPUT) ;         // aka BCM_GPIO pin 17
 
    digitalWrite (0, 1) ;       // On
    delay (10000) ;               // mS
    digitalWrite (0, 0) ;       // Off
    delay (10000) ;
  
  return 0 ;
}
How to do it via button on web page

abhi143
Posts: 129
Joined: Mon Oct 30, 2017 4:17 am

Re: Web interfacing with CGI script /C++

Mon Aug 13, 2018 2:02 pm

I have written html and c++ program but I don't understand how to mix html button code and c++ code.

Can anyone help me how to make complete program

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Web interfacing with CGI script /C++

Tue Aug 14, 2018 3:15 am

abhi143,
I have written html and c++ program but I don't understand how to mix html button code and c++ code.

Can anyone help me how to make complete program
This very well written tutorial on doing CGI with C++ tells you everything you need to know:
https://www.tutorialspoint.com/cplusplu ... amming.htm

Or perhaps this:
http://www.yolinux.com/TUTORIALS/LinuxT ... ++CGI.html

If you google around for "web programming C++" and such like you will find a lot of other solutions.

abhi143
Posts: 129
Joined: Mon Oct 30, 2017 4:17 am

Re: Web interfacing with CGI script /C++

Tue Aug 14, 2018 3:43 pm

Heater wrote:
Tue Aug 14, 2018 3:15 am
This very well written tutorial on doing CGI with C++ tells you everything you need to know:
https://www.tutorialspoint.com/cplusplu ... amming.htm
Or perhaps this:
http://www.yolinux.com/TUTORIALS/LinuxT ... ++CGI.html
If you google around for "web programming C++" and such like you will find a lot of other solutions.
Hello heater

I have been visited on the first link. Can you help with the my program. I have been tried second link I don't know whats problem Why led doesn't work ?

Heater
Posts: 10003
Joined: Tue Jul 17, 2012 3:02 pm

Re: Web interfacing with CGI script /C++

Tue Aug 14, 2018 6:36 pm

Sorry but no.

I'm sure that all the information you need regarding C++ and CGI is in the links I gave.

If not, a few google searches away. You have to work with it yourself.

fbe
Posts: 347
Joined: Thu Aug 17, 2017 9:08 pm

Re: Web interfacing with CGI script /C++

Wed Aug 15, 2018 6:07 pm

abhi143 wrote:
Tue Aug 14, 2018 3:43 pm
I don't know whats problem Why led doesn't work ?
Look into /var/log/apache2/access.log to see wether your cgi program is called or not. If not, your HTML (or JavaScript if you use it) is wrong.
If your cgi program is called look into /var/log/apache2/error.log to see wether there are error messages.

Return to “C/C++”