-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkWeather-modified.py
153 lines (129 loc) · 5.53 KB
/
checkWeather-modified.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
# Copyright 2018 Google LLC - modified by Gauthier Robe
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/python
import time
import datetime
import uuid
import json
import jwt
from Adafruit_BME280 import *
from tendo import singleton
import paho.mqtt.client as mqtt
me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running
# Create the JWT token
def create_jwt(cur_time, tokenLife, projectID, privateKeyFilepath, algorithmType):
token = {
'iat': cur_time,
'exp': cur_time + datetime.timedelta(minutes=tokenLife),
'aud': projectID
}
with open(privateKeyFilepath, 'r') as f:
private_key = f.read()
return jwt.encode(token, private_key, algorithm=algorithmType)
# Read sensor data
def read_sensor(weathersensor):
tempF = weathersensor.read_temperature_f()
# pascals = sensor.read_pressure()
# hectopascals = pascals / 100
pressureInches = weathersensor.read_pressure_inches()
dewpoint = weathersensor.read_dewpoint_f()
humidity = weathersensor.read_humidity()
temp = '{0:0.2f}'.format(tempF)
hum = '{0:0.2f}'.format(humidity)
dew = '{0:0.2f}'.format(dewpoint)
pres = '{0:0.2f}'.format(pressureInches)
return (temp, hum, dew, pres)
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')
# Create the JSON payload
def createJSON(id, timestamp, zip, lat, long, temperature, humidity, dewpoint, pressure):
data = {
'sensorID' : id,
'timecollected' : timestamp,
'zipcode' : zip,
'latitude' : lat,
'longitude' : long,
'temperature' : temperature,
'humidity' : humidity,
'dewpoint' : dewpoint,
'pressure' : pressure
}
json_str = json.dumps(data)
return json_str
def main():
# read configuration - change all values in JSON configuration file
with open('IoT_config.json', 'r') as f:
config = json.load(f)
# constants - change to fit your project and location
SEND_INTERVAL = config['CONSTANTS']['SEND_INTERVAL']#seconds
# sensor = config['CONSTANTS']['sensor']
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
## change project to your Project ID
project=config['PROJECT']['project']
## change topic to your PubSub topic name
topic = config['PROJECT']['topic']
## set the following four constants to be indicative of where you are placing your weather sensor
sensorID = config['SENSOR']['sensorID']
sensorZipCode = config['SENSOR']['sensorZipCode']
sensorLat = config['SENSOR']['sensorLat']
sensorLong = config['SENSOR']['sensorLong']
## set crypto info
ssl_private_key_filepath=config['CRYPTO']['ssl_private_key_filepath']
ssl_algorithm=config['CRYPTO']['ssl_algorithm']
root_cert_filepath=config['CRYPTO']['root_cert_filepath']
token_life = config['CRYPTO']['token_life']
## set MQTT info
gcp_location=config['MQTT']['gcp_location']
registry_id=config['MQTT']['registry_id']
device_id=config['MQTT']['device_id']
sensorID=registry_id + "." + device_id
googleMQTTURL=config['MQTT']['googleMQTTURL']
googleMQTTPort=config['MQTT']['googleMQTTPort']
_CLIENT_ID = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project, gcp_location, registry_id, device_id)
_MQTT_TOPIC = '/devices/{}/events'.format(device_id)
while True:
client = mqtt.Client(client_id=_CLIENT_ID)
cur_time = datetime.datetime.utcnow()
# authorization is handled purely with JWT, no user/pass, so username can be whatever
client.username_pw_set(
username='unused',
password=create_jwt(cur_time, token_life, project, ssl_private_key_filepath, ssl_algorithm))
client.on_connect = on_connect
client.on_publish = on_publish
client.tls_set(ca_certs=root_cert_filepath) # Using the Google Certs created when registering the device
client.connect(googleMQTTURL, googleMQTTPort)
jwt_refresh = time.time() + ((token_life - 1) * 60) #set a refresh time for one minute before the JWT expires
client.loop_start()
last_checked = 0
while time.time() < jwt_refresh: # as long as the JWT isn't ready to expire, otherwise break this loop so the JWT gets refreshed
try:
if time.time() - last_checked > SEND_INTERVAL:
last_checked = time.time()
temp, hum, dew, pres = read_sensor(sensor)
currentTime = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
s = ", "
weatherJSON = createJSON(sensorID, currentTime, sensorZipCode, sensorLat, sensorLong, temp, hum, dew, pres)
client.publish(_MQTT_TOPIC, weatherJSON, qos=1)
print("{}\n".format(weatherJSON))
time.sleep(0.5)
except Exception as e:
print "There was an error"
print (e)
client.loop_stop()
if __name__ == '__main__':
main()