37 lines
877 B
Python
37 lines
877 B
Python
'''
|
|
Created on 20.05.2015
|
|
|
|
@author: wn
|
|
'''
|
|
|
|
import paho.mqtt.client as mqtt
|
|
import json
|
|
import pymongo
|
|
import datetime
|
|
|
|
def on_message(client, userdata, msg):
|
|
j = json.loads(msg.payload)
|
|
now = datetime.datetime.now()
|
|
midnight = now.replace(now.year, now.month, now.day, 0,0,0,0)
|
|
seconds = (now - midnight).seconds
|
|
j['metadata']['timestamp'] = datetime.datetime.now()
|
|
j['metadata']['seconds'] = seconds
|
|
j['metadata']['day'] = midnight
|
|
print(j)
|
|
mongoClient = pymongo.MongoClient('localhost')
|
|
db = mongoClient.iot
|
|
r = db.iot.insert_one(j)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print("Starting client ...")
|
|
|
|
client = mqtt.Client()
|
|
client.on_message = on_message
|
|
client.connect("mqttbroker", 1883, 60)
|
|
client.subscribe("IoT/Measurement/#")
|
|
client.subscribe("IoT/WiFiPowerMeter/Measurement")
|
|
|
|
client.loop_forever()
|
|
|