-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-counter.py
109 lines (93 loc) · 2.51 KB
/
firebase-counter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python
import os
import time
import urllib2
import RPi.GPIO as GPIO
from time import sleep
import datetime
from firebase import firebase
import Adafruit_DHT
import urllib2, urllib, httplib
import json
import os
from functools import partial
import datetime
#GPIO SETUP:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pin2 = 27 #connect sensor "2" to white cord
pin3 = 22 #connect sensor "3" to blue cord
GPIO.setup(pin2,GPIO.IN)
GPIO.setup(pin3,GPIO.IN)
#needed variables for state machine:
state = 0
counterIN=0
counterOUT=0
ENTERING = 0
EXITING = 0
nbrOfPeopleInRoomBefore = 0
nbrOfPeopleInRoom = 0
#firebase usage:
firebase = firebase.FirebaseApplication('https://peoplestalker-318b4.firebaseio.com/', None)
def update_firebase(entrance):
nbrOfPeopleInRoom = counterIN - counterOUT
data = {"PeopleInRoom": nbrOfPeopleInRoom,"Entrance": entrance, "DateTime": datetime.datetime.now()}
print('Posting, number of people in room:')
print(nbrOfPeopleInRoom)
print('Entrance:')
print(entrance)
#upload status only if connected to the internet
while True:
try:
urllib2.urlopen("http://www.google.com").close()
except urllib2.URLError:
print "Not Connected"
time.sleep(1)
else:
print "Connected"
firebase.post('/sensor/dht', data)
nbrOfPeopleInRoomBefore = nbrOfPeopleInRoom
break
while True:
if state == 0: # no sensors broken, start state machine
if GPIO.input(pin2) and GPIO.input(pin3) == 0 :
state = 1
ENTERING = 1
if (GPIO.input(pin2) == 0) and (GPIO.input(pin3) == 1):
state = 1
EXITING = 1
elif state == 1: # first sensor broken, left or right
if GPIO.input(pin2) ==0 and GPIO.input(pin3) == 0 : #both unbroken, go back to 0
ENTERING = 0
EXITING = 0
state = 0
if GPIO.input(pin2) and GPIO.input(pin3) :
state = 3
elif state == 3:
if GPIO.input(pin2) ==0 and GPIO.input(pin3) == 0 : #both unbroken, go back to 0
ENTERING = 0
EXITING = 0
state = 0
if GPIO.input(pin2) == 0 and GPIO.input(pin3) and ENTERING :
state = 4
if GPIO.input(pin2) and GPIO.input(pin3) == 0 and EXITING :
state = 4
elif state == 4:
if GPIO.input(pin2) == 0 and GPIO.input(pin3) == 0 and EXITING :
counterOUT += 1
ENTERING = 0
EXITING = 0
state = 0
if 0 > counterIN - counterOUT:
counterIN = 0
counterOUT = 0
print('NEGATIVE NUMBER OF PEOPLE')
else:
update_firebase(0)
if GPIO.input(pin2) == 0 and GPIO.input(pin3) == 0 and ENTERING :
counterIN += 1
ENTERING = 0
EXITING = 0
state = 0
update_firebase(1)
GPIO.cleanup()