1

Hi i have been tasked to do a online counter using passive infrared sensor and a Raspberry PI. I need to be able to post the count on a webpage page which i had to create on my own.However, i am unable to get the counter output which i had coded in python to my webpage page.

I have tried using count(output),exec() and shell_exec() and it did not work.

Is it possible not to try to use flask? As i am very new to webpage designing and python.

I am not very good in coding python or php.

These is the python codes i have written

import RPi.GPIO as GPIO #1

import math

import os

import threading

import subprocess 

import time

import os.path

import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="brandon", 
db="menagerie")
cur =db.cursor()
GPIO.setmode(GPIO.BOARD)

GPIO.setup(12,GPIO.IN)
GPIO.setup(11,GPIO.IN) #16

peoplecount=0   #18
while(1):



 if GPIO.input(12):
 time.sleep(2)
 peoplecount = peoplecount+1
 time.sleep(1.5)
 cur.execute("INSERT INTO people(vistor,Date) VALUES(1,now())")
 db.commit()
 print peoplecount
 print "hello" #30 

 if(peoplecount < 1):
 peoplecount=0  

 elif GPIO.input(11):
 time.sleep(2)
 peoplecount = peoplecount-1
 time.sleep(1.5)
 print peoplecount
 print "come back again"


conn.commit()        
cur.close()

These are my webpage codes :

<!DOCTYPE html>
<html>
<head>
<style>
table,th,td {
border:1px solid black;
}
</style>
</head>
<body>

<?php

echo "welcome to PI LAB<br>";
echo date ('y-m-d H:i:s');

$conn = new mysqli("localhost","root","brandon","menagerie");

if($conn->connect_error)
{ 
die("connection failed:" . $conn->connect_error);
}

$sql = "SELECT * FROM people";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Vistor</th><th>Entry</th></tr>";
while($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["vistor"]. " </td><td> " . $row["Date"]. "</td>
</tr>";
}
echo"</table>";
}
$conn->close();

?>



</body>
</html>
nurul98
  • 117
  • 3
  • 10
  • To be clear, if you open up your database directly - either with PHPMyAdmin or with the mysql shell - is there any data in the `people` table? – Joe Brailsford Jun 20 '17 at 09:05
  • There is data in the people table. I am able to display the data i acquire from the webpage i created. – nurul98 Jun 20 '17 at 09:33
  • So what is the problem exactly? If the page displays the data, what are you actually trying to achieve? – Joe Brailsford Jun 20 '17 at 09:43
  • I am trying to get my count output to display. I will be using my raspberry pi running on python to count and i need the count which is calculated to display in my webpage i have created. – nurul98 Jun 20 '17 at 09:48
  • A live count? Or do you just want to display the total number of people that are in the table? – Joe Brailsford Jun 20 '17 at 09:50
  • No sir in another corner if possible. Ideal place would be below the php – nurul98 Jun 20 '17 at 09:57

1 Answers1

0

enter image description hereI'm not sure I quite understand what you want, but if you just want a total visitor count, and you have one row in your table per visitor:

<!DOCTYPE html>
<html>
    <head>
        <style>
            table,th,td {
                border:1px solid black;
            }
        </style>
    </head>
    <body>

    <?php
        $conn = new mysqli("localhost","root","brandon","menagerie");

        if($conn->connect_error)
        { 
            die("connection failed:" . $conn->connect_error);
        }

        echo "welcome to PI LAB<br>";
        echo date('y-m-d H:i:s');

        $sql = "SELECT * FROM people";
        $result = $conn->query($sql);

        echo "Total visitors: ", $result->num_rows;

        if ($result->num_rows > 0) {
            echo "<table><tr><th>Vistor</th><th>Entry</th></tr>";
            while($row = $result->fetch_assoc()) {
                echo "<tr><td>" . $row["vistor"] . " </td><td> " . $row["Date"]. "</td></tr>";
            }
            echo"</table>";
        }
        $conn->close();
    ?>
    </body>
</html>
nurul98
  • 117
  • 3
  • 10
  • Hi sir I am sorry that I am lousy at describing, but sir I am not sure how to display the count that the raspberry pi counted in this webpage. If it's possible make the count live. I am very thankful that u tried to offer a soultion. – nurul98 Jun 20 '17 at 11:13
  • To make the counter live, you will need to use JS/AJAX. The simplest way would be to make the page automatically refresh ever few seconds. – Joe Brailsford Jun 20 '17 at 11:15
  • But I am unable to make the count come out at the page – nurul98 Jun 20 '17 at 11:19
  • I am unable to make the count display in the webpage. I am not worried about making it live. It's a bonus if I can make the page live. – nurul98 Jun 20 '17 at 11:21
  • Can you post a screenshot of what the page looks like using the code I supplied? – Joe Brailsford Jun 20 '17 at 11:21
  • Hi sir i have update my webpage using your code in the post already. However sir i have customized the code to add and not delete entry. As i want the data to stay in my database instead of deleting it away. – nurul98 Jun 21 '17 at 00:53
  • I don't understand what you want the page to do. – Joe Brailsford Jun 21 '17 at 07:29
  • Sir my project is suppose to count people coming in or going out ( if someone walks in, it will increase by 1 if someone goes out, it will decrease by 1.) after getting the counts I am suppose to immediately transmit the raspberry pi output to my webpage. Example: if someone walks in and the raspberry pi counted 2 people inside the lab, the webpage is able to display 2 at the top of the table just below "welcome to pi lab". – nurul98 Jun 21 '17 at 09:11
  • I am sorry for the bad description, i have uploaded a picture to show you – nurul98 Jun 21 '17 at 09:40