-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasure_dummyValues.py
205 lines (175 loc) · 6.56 KB
/
measure_dummyValues.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
import time
import json
import random
import paho.mqtt.client as mqtt
from datetime import datetime
from requests import post
from sds011 import SDS011
from time import sleep
import gpsd
import config
# Thingsboard platform credentials
PUBLISH_THINGSBOARD = config.PUBLISH_THINGSBOARD
THINGSBOARD_HOST = config.THINGSBOARD_HOST
ACCESS_TOKEN = config.ACCESS_TOKEN
PUBLISH_OPEN_SENSE_MAP = config.PUBLISH_OPEN_SENSE_MAP
SENSEBOX_ID = config.SENSEBOX_ID
SENSEBOX_PM25_ID = config.SENSEBOX_PM25_ID
SENSEBOX_PM10_ID = config.SENSEBOX_PM10_ID
OSM_HEADERS = {"content-type": "application/json"}
osm_post_url = "https://api.opensensemap.org/boxes/" + SENSEBOX_ID + "/data"
PM_USB = config.PM_USB
MEASURE_TIME = config.MEASURE_TIME
MEASURE_INTERVAL = config.MEASURE_INTERVAL
attributes = {
'name': config.DEVICE_NAME,
'measuring': config.MEASURE_ON_START
}
# init PM-sensor
#pm_sensor = SDS011(PM_USB, use_query_mode=True)
#pm_sensor.sleep(sleep=False)
print('started PM - waiting 5 secs')
#sleep(5)
# init GPS-sensor
#gpsd.connect()
print('started GPS - waiting 5 secs')
#sleep(5)
def measure_interval(measure_time_sec=30, measure_interval_sec=2):
'''
measure every measure_interval_sec for measure_time_sec (sec)
and return payloads in json format for publishing all at once to minimize traffic
:param measure_time_sec: seconds until publishind
:param measure_interval_sec: measure every second
:return: thingsboard_payload, osm_payload --> thingsboard and OSM specific payloads
'''
next_reading = time.time()
stop_reading = next_reading + measure_time_sec
thingsboard_payload = []
osm_payload = []
while time.time() < stop_reading:
# get pm-sensor data
pm25, pm10 = round(random.uniform(5.0, 10.0), 2), round(random.uniform(7.0, 15.0), 2)
#pm25, pm10 = pm_sensor.query()
print('PM25: {pm25}, \tPM10: {pm10}'.format(pm25=pm25, pm10=pm10))
# get gps-sensor data
#lat, lon = gpsd.get_current().position()
lat, lon = 52.470908, 13.441448
print('lat: {lat}, \tlon: {lon}'.format(lat=lat, lon=lon))
timestamp = datetime.now().timestamp() * 1000 # in ms
# publish to thingsboard
if PUBLISH_THINGSBOARD:
measure_data = {
"ts": timestamp,
"values": {
'PM25': pm25, 'PM10': pm10
}
}
if lon or lat:
# measure_data['location'] = {
# 'lat': lat,
# 'lng': lon
# }
measure_data['values']['lat'] = lat
measure_data['values']['lng'] = lon
thingsboard_payload.append(measure_data)
# publish to opensensemap
if PUBLISH_OPEN_SENSE_MAP:
osm_timestamp = datetime.utcnow()
osm_timestamp = osm_timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
pm25_osm_data = {
"sensor": SENSEBOX_PM25_ID,
"value": str(pm25),
"createdAt": osm_timestamp
}
pm10_osm_data = {
"sensor": SENSEBOX_PM10_ID,
"value": str(pm10),
"createdAt": osm_timestamp
}
if lon or lat:
pm25_osm_data['location'] = {
'lat': lat,
'lng': lon
}
pm10_osm_data['location'] = {
'lat': lat,
'lng': lon
}
osm_payload.append(pm25_osm_data)
osm_payload.append(pm10_osm_data)
next_reading += measure_interval_sec
sleep_time = next_reading - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
return thingsboard_payload, osm_payload
def publishValue(client):
'''
if attributes['measuring'] == True: loop forever and measure, publish sensor values
:param client: thingsboard client
:return:
'''
print('Start Measuring and publishing...')
while attributes['measuring']:
thingsboard_payload, osm_payload = measure_interval(measure_time_sec=MEASURE_TIME,
measure_interval_sec=MEASURE_INTERVAL)
if PUBLISH_THINGSBOARD:
rc = client.publish('v1/devices/me/telemetry', json.dumps(thingsboard_payload), 1)
# print(rc)
print('published to thingsboard')
# print(payload)
if PUBLISH_OPEN_SENSE_MAP:
r = post(osm_post_url, json=osm_payload, headers=OSM_HEADERS)
print('published to OpenSenseMap')
print('OpenSenseMap HTTP Response:', r.status_code)
# MQTT on_connect callback function
def on_connect(client, userdata, flags, rc):
'''
get called when connecting to thingsboard
:param client: thingsboard mqtt client
'''
# print("rc code:", rc)
client.subscribe('v1/devices/me/rpc/request/+')
client.publish('v1/devices/me/attributes', json.dumps(attributes), 1)
# MQTT on_message callback function
def on_message(client, userdata, msg):
'''
get called, when getting a rpc-message from thingsboard
:param client: thingsboard mqrr client
:param msg rpc-message
'''
# print('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload))
if msg.topic.startswith('v1/devices/me/rpc/request/'):
requestId = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)]
# get rpc-msg payload
data = json.loads(msg.payload)
# compare method string and do something
# e.g. turning on/off sensor, setting measurement intervall, etc
# todo
'''if data['method'] == 'getValue':
#print("getvalue request\n")
#print("sent getValue : ", sensor_data)
client.publish('v1/devices/me/rpc/response/' + requestId, json.dumps(sensor_data['temperature']), 1)
if data['method'] == 'setValue':
#print("setvalue request\n")
params = data['params']
setValue(params)'''
# create a thingsboard mqtt client instance
client = mqtt.Client()
if PUBLISH_THINGSBOARD:
try:
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(ACCESS_TOKEN)
client.connect(THINGSBOARD_HOST, 1883, 60)
client.loop_start()
except KeyboardInterrupt:
client.disconnect()
except Exception as e:
print(e)
print('cant connect to thingsboard. set PUBLISH_THINGSBOARD to false')
PUBLISH_THINGSBOARD = False
# start measuring
try:
publishValue(client)
except Exception as e:
print(e)