changes
This commit is contained in:
28
mqtt2console.py
Normal file
28
mqtt2console.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
'''
|
||||||
|
Created on 20.05.2015
|
||||||
|
|
||||||
|
@author: wn
|
||||||
|
'''
|
||||||
|
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import json
|
||||||
|
import pymongo
|
||||||
|
import datetime
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
j = json.loads(msg.payload)
|
||||||
|
print(json.dumps(j, indent=2, separators=(',',':')))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Starting client ...")
|
||||||
|
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_message = on_message
|
||||||
|
client.connect("mqttbroker", 1883, 60)
|
||||||
|
for i in sys.argv[1:]:
|
||||||
|
client.subscribe(i)
|
||||||
|
|
||||||
|
client.loop_forever()
|
||||||
|
|
@ -7,15 +7,15 @@ Created on 20.05.2015
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import json
|
import json
|
||||||
import pymongo
|
import pymongo
|
||||||
|
import datetime
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
print(msg.topic+" "+str(msg.payload))
|
|
||||||
j = json.loads(msg.payload)
|
j = json.loads(msg.payload)
|
||||||
|
j['metadata']['timestamp'] = datetime.datetime.now()
|
||||||
print(j)
|
print(j)
|
||||||
mongoClient = pymongo.MongoClient('localhost')
|
mongoClient = pymongo.MongoClient('localhost')
|
||||||
db = mongoClient.iot
|
db = mongoClient.iot
|
||||||
db.iot.insert(j)
|
r = db.iot.insert(j)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -25,6 +25,7 @@ if __name__ == '__main__':
|
|||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
client.connect("mqttbroker", 1883, 60)
|
client.connect("mqttbroker", 1883, 60)
|
||||||
client.subscribe("IoT/Measurement/#")
|
client.subscribe("IoT/Measurement/#")
|
||||||
|
client.subscribe("IoT/WiFiPowerMeter/Measurement")
|
||||||
|
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user