-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_gcp.py
207 lines (143 loc) · 4.79 KB
/
car_gcp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from sense_hat import SenseHat
import datetime
import time
import jwt
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# To connect Ultra-Sonic Distance Sensor
TRIG=16
ECHO=18
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
# For connecting PIR Motion Sensor
GPIO.setup(22,GPIO.IN)
# For connecting Sound Sensor
GPIO.setup(40,GPIO.IN)
def left():
GPIO.output(15,True)
GPIO.output(11,False)
GPIO.output(7,False)
GPIO.output(13,False)
# Initially all wheels are stop
GPIO.output(15,False)
GPIO.output(11,False)
GPIO.output(7,False)
GPIO.output(13,False)
# Here we are defining the connection to the GCP IOT Core Console/.
# Define some project-based variables to be used below. This should be the only
# block of variables that you need to edit in order to run this script
ssl_private_key_filepath = '/home/pi/demo_private.pem'
ssl_algorithm = 'RS256' # Either RS256 or ES256
root_cert_filepath = '/home/pi/roots.pem'
project_id = 'practice-308413'
gcp_location = 'asia-east1'
registry_id = 'my_registry'
device_id = 'my_device'
# end of user-variables
cur_time = datetime.datetime.utcnow()
def create_jwt():
token = {
'iat': cur_time,
'exp': cur_time + datetime.timedelta(minutes=60),
'aud': project_id
}
with open(ssl_private_key_filepath, 'r') as f:
private_key = f.read()
return jwt.encode(token, private_key, ssl_algorithm)
_CLIENT_ID = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project_id, gcp_location, registry_id, device_id)
_MQTT_TOPIC = '/devices/{}/events'.format(device_id)
client = mqtt.Client(client_id=_CLIENT_ID)
# authorization is handled purely with JWT, no user/pass, so username can be whatever
client.username_pw_set(
username='unused',
password=create_jwt())
def error_str(rc):
return '{}: {}'.format(rc, mqtt.error_string(rc))
def on_connect(unusued_client, unused_userdata, unused_flags, rc):
print('on_connect', error_str(rc))
def on_publish(unused_client, unused_userdata, unused_mid):
print('on_publish')
client.on_connect = on_connect
client.on_publish = on_publish
client.tls_set(ca_certs=root_cert_filepath) # Replace this with 3rd party cert if that was used when creating registry
client.connect('mqtt.googleapis.com', 8883)
client.loop_start()
# Could set this granularity to whatever we want based on device, monitoring needs, etc
while True:
GPIO.output(TRIG,0)
time.sleep(1)
# Throwing a signal for 0.00001 second
GPIO.output(TRIG,1)
time.sleep(0.00001)
# Making the sensor idle for 1 second
GPIO.output(TRIG,0)
time.sleep(1)
# Taking the outputs given by PIR and sound sensor
x=GPIO.input(22)
y=GPIO.input(40)
while GPIO.input(ECHO)==0:
pass
# If the ultrasonic wave is just starting to be received
pulse_start=time.time()
while GPIO.input(ECHO)==1:
pass
pulse_end=time.time()
# Stop at the time when it just gets over at the receiiving end
pulse_time=pulse_end-pulse_start # Calculating the time from starting till end
# Approx distance is calculated
distance=pulse_time*17150
distance=round(distance,2)
if distance<100:
print()
if x==1:
xflag="Motion Detected"
print(xflag)
elif x==0:
xflag="Motion Not Detected"
print(xflag)
if y==1:
yflag="Sound Detected"
print(yflag)
elif y==0:
yflag="Sound Not Detected"
print(yflag)
GPIO.output(15,False)
GPIO.output(11,False)
GPIO.output(7,False)
GPIO.output(13,False)
# Payload made to be sent to Google IOT Core Console
payload = f'Stopped at an object distance of {distance},{xflag},{yflag}'
client.publish(_MQTT_TOPIC, payload, qos=1)
print(payload)
time.sleep(1)
exit()
elif distance>100:
print()
if x==1:
xflag="Motion Detected"
print(xflag)
elif x==0:
xflag="Motion Not Detected"
print(xflag)
if y==1:
yflag="Sound Detected"
print(yflag)
elif y==0:
yflag="Sound Not Detected"
print(yflag)
GPIO.output(15,True)
GPIO.output(11,True)
GPIO.output(7,False)
GPIO.output(13,False)
# Payload made to be sent to Google IOT Core Console
payload = f'Object distance at {distance} ,{xflag},{yflag}'
client.publish(_MQTT_TOPIC, payload, qos=1)
print(payload)
GPIO.cleanup()
client.loop_stop()