peterwoolery
Posts: 5
Joined: Mon Nov 17, 2014 8:02 pm

[SOLVED] Building a People Counter with a PIR sensor

Mon Nov 17, 2014 8:44 pm

Forgive me for being new to this. I have been looking around different sites and tutorials to find a solution, but can't seem to figure it out.

Issue: PIR sensor no longer working consistently.


Background: I am attempting to build a people counter for our retail store. The idea is based off of a tutorial on Ubidots, but is modified to upload to Google Drive in stead.

The system was working well when just assembled. However, when I bought a plastic box to mount the setup in, the PIR sensor routed through the GPIO pins no longer responded like normal.
  • Pin #7 (original used pin) - Blinks on an off until logging into to the RPi, then it no longer responds
  • Pin #8 - logs in just fine, and responds to movement as expected, but when I run the program, it reports that the pin is in use already. It will then print that pin 8 is 1, then 0, and say it cannot append to spreadsheet.
  • Pin # 11 - Simply does not respond
I Reinstalled Raspian Wheezy, thinking that it may have been an issue with powering off the Pi incorrectly, but it still does the same issue after reinstalling.

The pins on the back of the board came into contact with the plastic case and bent over a little. This doesn't seem like an issue as they seem to just be from soldering. Any advice on how to figure this out would be great.

Resources I used:
http://blog.ubidots.com/building-a-peop ... nd-ubidots
http://www.raspberrypi.org/documentatio ... windows.md
http://blogs.sequoiainc.com/blogs/raspb ... re-monitor


Code for my program:

Code: Select all

import subprocess
import re
import sys
import time
import datetime
import gspread
import RPi.GPIO as GPIO

email = '*******'
password = '******'
spreadsheet = 'motiondata'
sensorPin = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

prevstate = False
currState = False

while True:
        time.sleep(0.1)
        prevState = currState
        currState = GPIO.input(sensorPin)
        if currState != prevState:
                newState = "1" if currState else "0"
                print "GPIO pin %s is %s" % (sensorPin, newState)

                try:
                        gc=gspread.login(email, password)
                except:
                        print "Unable to login."

                try:
                        worksheet = gc.open(spreadsheet).sheet1
                except:
                        print "Unable to open spreadsheet"

                try:
                        values = [datetime.datetime.now(), newState]
                        worksheet.append_row(values)
                except:
                        print "Unable to append data. Check your connection?"
                        sys.exit

time.sleep(1.5)

Interior image:
https://drive.google.com/file/d/0B3wRtv ... sp=sharing

Exterior image:
https://drive.google.com/file/d/0B3wRtv ... sp=sharing
Last edited by peterwoolery on Tue Nov 18, 2014 6:10 am, edited 1 time in total.

peterwoolery
Posts: 5
Joined: Mon Nov 17, 2014 8:02 pm

Re: Building a People Counter with a PIR sensor

Tue Nov 18, 2014 6:09 am

Haha, I just realized I got a little ahead of my self... I plugged the wrong cable into the wrong pin :P

Oh well, since this post is up, I will update it with photos of it set up above the doors and how well it goes for other people to learn from my experience.

sasquatch606
Posts: 1
Joined: Fri Mar 20, 2015 8:28 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Fri Mar 20, 2015 8:49 pm

I have been wanting to try this out at my work. How has it been going? It looks like you have two USB devices plugged into the pi. I assume they are for a keyboard/mouse and the other for WiFi? I'm hoping I can follow your instructions as the Ubidots instructions seemed pretty simple although I still don't know why they show a battery pack. Doesn't look like you used one. What is the effective range of this? Anyway, thanks in advance.

peterwoolery
Posts: 5
Joined: Mon Nov 17, 2014 8:02 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Sun Mar 22, 2015 5:12 am

I will try to post more involved details latet, but this project has blossomed well into a real time for counting system that I have installed at 7 different businesses. The most recent version counts up for 15 minutes, then uploads all the data via a Google form to save space and ideally run for 10 years without hitting the 400,000 cell limit Google imposes.

The only usb device mine use is a wifi, though a keyboard helps make programming easier. The farthest I've used the device is 70 feet, but you can go farther with an antenna based USB and good router.

rrrikin
Posts: 1
Joined: Sun Apr 19, 2015 6:40 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Sun Apr 19, 2015 6:50 pm

Hey,

I got few questions for you since you have more than one working now. Can you do something like following?
https://www.youtube.com/watch?v=OWab2_ete7s

I would like to define the region of the counting and also count up and down separately so i can do math at the end. Also, thinking about putting this on top of the entrance door.

I volunteer at a temple and they are looking for something like this. They usually have an event on Sunday evening and they make food for the people who visit. They would like to know how many people are in the building at particular time (assuming people who walked out gets deducted from that count). So they can cook accordingly and possibly reduce the food wastage or shortage. There are pre-manufactured devices i can buy but it cost a lot so since this is a non profit org so looking to get something done cheaper. I have Pi2 and gona play with this code. Any help will be appreciated.
Thank you

gleega
Posts: 4
Joined: Sat Jun 06, 2015 8:35 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Sat Jun 06, 2015 8:38 pm

Would like to know more about this. Is there a way to contact you?

peterwoolery
Posts: 5
Joined: Mon Nov 17, 2014 8:02 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Sun Jun 07, 2015 3:12 pm

Ok guys, this may be a little rushed, but here is a lot of the info I have put together. I kept putting this off and just wanted to get something up here for you guys to work off of.

People Counter Walkthrough 1: https://youtu.be/WqzO-XEST10
Program Walkthrough: http://youtu.be/IH7C-xgYo3I
Form Setup Part 1: http://youtu.be/bSeI8VjXjKI
Form Setup Part 2: http://youtu.be/Kky9DfGASFY

Code: Select all

###################################################
###################################################
#########  People Counter v1-1 ############
###################################################

################################
###Setting up Python Modules####
################################
import sys
import time
import datetime
import RPi.GPIO as GPIO
import mechanize

################################
##### Setting up GPIO pins #####
################################
h1 = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(h1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

################################
######Setting up Counters ######
################################
peoplecount = 0
uploadcount = 0
door = 1    # <- Use this to designate multiple doors for tracking
location = 'Location'  # <- Use this to designate multiple locations for tracking in one form

################################
######## Setup Mechanize #######
################################
br = mechanize.Browser()

###################################################
############# MAIN BODY OF PROGRAM ################
###################################################
while True:

## This waits for a specified minute of the hour, checks if anyone has been detected since the last upload, then uploads the data to a Google Form.
	if time.strftime("%M") in ("13", "28", "43", "58") and peoplecount > 0 and uploadcount == 0:
		try:
			url = "https://docs.google.com/forms/d/1j6B3RUf0TLjMPgdKuoubKKUgBtSKxqr0UygGDWiTXZg/formResponse?ifq&entry.854434225=%s&submit=Submit&fbzx=4499308717411757586" % (location, door, peoplecount, time.strftime("%H"), time.strftime("%m/%d/%Y"))
			response = br.open(url) 
			print "People count uploaded with value %s on door %s at %s" % (peoplecount, door, location)
			uploadcount = 1
			peoplecount = 0
			print "values reset"
			sys.exit
		except:
			print "Cannot Access Page"
			

	elif time.strftime("%M") in ("14", "29", "44", "59") and uploadcount == 1:
		uploadcount = 0
		
	elif GPIO.input(h1) == True:
		peoplecount = peoplecount + 1
		print "Motion Detected: Door %s at %s on %s. Count is %s" % (door, time.strftime("%H:%M:%S"), time.strftime("%A"), peoplecount)
		sys.exit
		
		time.sleep(3)



References:
Ubidots Article: http://blog.ubidots.com/building-a-peop ... nd-ubidots
Google Forms help: http://justingale.com/2013/09/url-trick ... responses/
ModMyPi Modular Case: http://www.modmypi.com/raspberry-pi/cas ... case-black (I use nylon screws to hold the PIR sensor in place, and add one spacer to give it clearance)
Parallax PIR sensor (Most consistently reliable I have found: https://www.parallax.com/product/555-28027
Wifi Modules (Don't cheap out! This is the biggest part when it comes to reliability): http://www.adafruit.com/product/1030
Daily Reboots: http://raspberrypi.stackexchange.com/qu ... cific-time

peterwoolery
Posts: 5
Joined: Mon Nov 17, 2014 8:02 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Sun Jun 07, 2015 3:14 pm

OPEN CV

I want to eventually get to this point as it would allow us to count individuals and directionally (though you can do that using 2 PIR sensor set up one in front of the other as well). BUT Open CV takes a lot more work to set up and summer is a busy time of year for me, so it will be pushed off.

Aveqt
Posts: 1
Joined: Fri May 27, 2016 3:08 pm

Re: [SOLVED] Building a People Counter with a PIR sensor

Fri May 27, 2016 5:18 pm

I am really bad at python, and I don't currently understand pretty much anything. When I am using code you used with modified google forms link (I made sure it works from any computer), but still somewhy I face an error "Cannot Access Page".

I already tried update Mechanize thing and use urllib on code, but those things didin't fix anything. I am thinking should I download some browser to raspberry pi, or can it visit on url without browser installed?

I also have problem with PIR sensor and this code; it does detect movement even tho there is no movemet. This means it triggers everytime after sleep time is over. I tried already 3 different PIR sensors and tested using BCM 7 as it stands for the same pin that BOARD 26 does. I also tried things as you guided; using pin BOARD 7.
What could be fix for this? I tried other really simple Motion Detection scripts, but others did not detect the movement at all or they just randomly triggered from nothing, but then didin't trigger from moving hand infront of sensor. It could be that I should use resistor since on alot of problems like these the reason has been that the sensor is too sensitivy and people recommend to use resistors.

Any help is good, I am doing this to learn python so problems are just good for my learning process.


-EDIT-

Problem with Google Forms may also be that Raspberry can't somewhy connect to url. I tried Ubidots guide and it also gives error "Couldn't connect to the API, check your Internet connection". Raspberry can ping to google.com etc and Internet connection should be 100% fine.


Thanks!




peterwoolery wrote:Ok guys, this may be a little rushed, but here is a lot of the info I have put together. I kept putting this off and just wanted to get something up here for you guys to work off of.

People Counter Walkthrough 1: https://youtu.be/WqzO-XEST10
Program Walkthrough: http://youtu.be/IH7C-xgYo3I
Form Setup Part 1: http://youtu.be/bSeI8VjXjKI
Form Setup Part 2: http://youtu.be/Kky9DfGASFY

Code: Select all

###################################################
###################################################
#########  People Counter v1-1 ############
###################################################

################################
###Setting up Python Modules####
################################
import sys
import time
import datetime
import RPi.GPIO as GPIO
import mechanize

################################
##### Setting up GPIO pins #####
################################
h1 = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(h1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

################################
######Setting up Counters ######
################################
peoplecount = 0
uploadcount = 0
door = 1    # <- Use this to designate multiple doors for tracking
location = 'Location'  # <- Use this to designate multiple locations for tracking in one form

################################
######## Setup Mechanize #######
################################
br = mechanize.Browser()

###################################################
############# MAIN BODY OF PROGRAM ################
###################################################
while True:

## This waits for a specified minute of the hour, checks if anyone has been detected since the last upload, then uploads the data to a Google Form.
	if time.strftime("%M") in ("13", "28", "43", "58") and peoplecount > 0 and uploadcount == 0:
		try:
			url = "https://docs.google.com/forms/d/1j6B3RUf0TLjMPgdKuoubKKUgBtSKxqr0UygGDWiTXZg/formResponse?ifq&entry.854434225=%s&submit=Submit&fbzx=4499308717411757586" % (location, door, peoplecount, time.strftime("%H"), time.strftime("%m/%d/%Y"))
			response = br.open(url) 
			print "People count uploaded with value %s on door %s at %s" % (peoplecount, door, location)
			uploadcount = 1
			peoplecount = 0
			print "values reset"
			sys.exit
		except:
			print "Cannot Access Page"
			

	elif time.strftime("%M") in ("14", "29", "44", "59") and uploadcount == 1:
		uploadcount = 0
		
	elif GPIO.input(h1) == True:
		peoplecount = peoplecount + 1
		print "Motion Detected: Door %s at %s on %s. Count is %s" % (door, time.strftime("%H:%M:%S"), time.strftime("%A"), peoplecount)
		sys.exit
		
		time.sleep(3)



References:
Ubidots Article: http://blog.ubidots.com/building-a-peop ... nd-ubidots
Google Forms help: http://justingale.com/2013/09/url-trick ... responses/
ModMyPi Modular Case: http://www.modmypi.com/raspberry-pi/cas ... case-black (I use nylon screws to hold the PIR sensor in place, and add one spacer to give it clearance)
Parallax PIR sensor (Most consistently reliable I have found: https://www.parallax.com/product/555-28027
Wifi Modules (Don't cheap out! This is the biggest part when it comes to reliability): http://www.adafruit.com/product/1030
Daily Reboots: http://raspberrypi.stackexchange.com/qu ... cific-time

Return to “Troubleshooting”