0

Hi everyone I am trying to run a python script on my raspberry pi 2 via web server running apache2, php, and raspbian jessie. What my plan is to do is have a timelapse camera using python (code below) and when I go to a webpage it and click a button and it will start to run the script below.

import time
import picamera
import RPi.GPIO as GPIO
from subprocess import call
from datetime import datetime
import math
import os


green = 16 #Running 
red = 20 #wating for sunset
startBut = 21 #button to start every thing


GPIO.setmode(GPIO.BCM)
GPIO.setup(startBut, GPIO.IN, GPIO.PUD_UP) #push button
GPIO.setup(green, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)


d = datetime.now()
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initSec = "%02s" % (d.second)
initMin = "%02s" % (d.minute)

folderToSave = "/media/TIMELAPS/timelapse_" + str(initYear) + "_" + str(initMonth) + "_" +  str(initDate) + "_" + str(initMin) + "_" + str(initSec) + "/"
#folderToSave2 = "timelapse_" + str(initYear) + "_" + str(initMonth) + "_" +  str(initDate) + "_" + str(initSec) + "/"
os.mkdir(folderToSave)
#GPIO.wait_for_edge(startBut, GPIO.FALLING)
with picamera.PiCamera() as camera:
    #GPIO.output(green, GPIO.HIGH)
    var = 1
    fileNum = 0
    while var == 1 :  # This constructs an infinite loop
        if fileNum <= 9:
            fileName = "0" + str(fileNum)
            print("file name = " + fileName)
        else:
            fileName = "" + str(fileNum)
            print("file name = " + fileName)


        camera.capture(folderToSave + fileName + ".jpg") #takes the photo to the flashdrive
        #camera.capture(folderToSave2 + fileName) #takes the photo and saves to the apple timecaplse disk
        fileNum = fileNum + 1



        time.sleep(5)
    print("Good Bye")

and here is my Html code

<!DOCTYPE html>
<html>
<head>
    <title>Raspbery Pi Timelapse camera</title>
    <script>
        function runPythonScript(file) {
            if (file == "time") {
            //run the Time_laps_camera.py  
            }
            else if (file == "aperture") {
            //run the pi_camera_aperture.py
            }
        }
    </script>
</head>

<body>
    <center>
    <div style="padding: 20px 10px;" ></div>
    <div>
        <button type="button" onclick="runPythonScript('time')">TimeLapse Camera</button>
    </div>
    <div style="padding: 15px;">
        <button type="button" onclick="runPythonScript('aperture')">TimeLapse Camera</button>
    </div>
    </center>

</body>
</html>

So basicly what I am trying to do is insted of me connecting to my pi via remote desktop and executing the script what I want to do is want to go to a webpage and hit a button and run the script.

3

You can use a CGI script - this basically means you can get scripts run by the webserver. Here are some samples from a old project I did (not necessarily the best way of doing stuff, but it worked quite well if with some latency over a WiFi connection):

controls-touchembed.html

<!DOCTYPE HTML>
<html>
<head>
<title>Robox Pi - Controls</title>
<link rel="shortcut icon" href="favicon.png"
<meta id="author" content="Wilf" >
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="content-type" content="text/html; charset=WINDOWS-1252" >
<style>
  body {
    font-family: Arial, Helvetica;
    font-size: 12px;
    color: #FFFFFF;
    background: url("texture.png");
  }

  #fixed {
    position: fixed;
    top: 0;
    left: 0;
  }

  #arrowPad {
   border: 1px #E5E5E5;
    }

  #arrowPadCell {
   border: 1px #E5E5E5;
   background-image: url(arrowpad4/bg.png);
    }

  #arrowPadCell.h {
   border: 1px #E5E5E5;
   background-image: url(arrowpad4/bg.png);
    }

  #arrowPadCell.h:hover {
   border: 1px #E5E5E5;
   background-image: url(arrowpad4/bg2.png);
    }
</style>
<body>
<div id="fixed">
      <table id="arrowPad">
            <tr>
            <td id="arrowPadCell" style="-webkit-border-top-left-radius: 20px; border-top-left-radius: 20px; -moz-border-radius-topleft: 20px;"></td>
            <td id="arrowPadCell" class="h"><a href="/cgi-bin/forward.cgi" ><img src="arrowpad4/2.png" id="a2" alt="Forward"></img></a></td>
            <td id="arrowPadCell" style="-webkit-border-top-right-radius: 20px; -moz-border-radius-topright: 20px; border-top-right-radius: 20px;"></td>
            </tr>
            <tr>
            <td id="arrowPadCell" class="h"><a href="/cgi-bin/left.cgi" ><img src="arrowpad4/4.png" id="a4" alt="Left"></img></a></td>
            <td id="arrowPadCell" class="h"><a href="/cgi-bin/clear.cgi" ><img src="arrowpad4/5.png" id="a5" alt="Stop"></img></a></td>
            <td id="arrowPadCell" class="h"><a href="/cgi-bin/right.cgi" ><img src="arrowpad4/6.png" id="a6" alt="Right"></img></a></td>
            </tr>
            <tr>
            <td id="arrowPadCell" style="-webkit-border-bottom-left-radius: 20px; border-bottom-left-radius: 20px; -moz-border-radius-bottomleft: 20px;"></td>
            <td id="arrowPadCell" class="h"><a href="/cgi-bin/backward.cgi" ><img src="arrowpad4/8.png" id="a8" alt="Backward"></img></a></td>
            <td id="arrowPadCell" style="-webkit-border-bottom-right-radius: 20px; border-bottom-right-radius: 20px; -moz-border-radius-bottomright: 20px;"></td>
            </tr>
      </table>
      </div>
</html>

cgi-bin/forward.cgi

#!/bin/bash

forward

echo "Status: 204 No Content"
echo "Content-type: text/plain"
echo "" 

This I think I did following this guide. My script above was used to invoke commands that are the $PATH, which were python scripts that used GPIOs to control motors etc. Note with some apache server setups, you may need so setup the security for a directory to have the CGI scripts in - this often is /usr/lib/cgi-bin/, /var/www/cgi-bin, etc.

I haven't done this using python scripts directly (I think CGI scripts can various languages, but you need to define the shebang on the first line)). There is also is a python module as well (see here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.