changes
This commit is contained in:
parent
1d5077f7d5
commit
3a00c54e0f
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 json
|
||||
import pymongo
|
||||
|
||||
import datetime
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
j = json.loads(msg.payload)
|
||||
j['metadata']['timestamp'] = datetime.datetime.now()
|
||||
print(j)
|
||||
mongoClient = pymongo.MongoClient('localhost')
|
||||
db = mongoClient.iot
|
||||
db.iot.insert(j)
|
||||
r = db.iot.insert(j)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@ -25,6 +25,7 @@ if __name__ == '__main__':
|
||||
client.on_message = on_message
|
||||
client.connect("mqttbroker", 1883, 60)
|
||||
client.subscribe("IoT/Measurement/#")
|
||||
client.subscribe("IoT/WiFiPowerMeter/Measurement")
|
||||
|
||||
client.loop_forever()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user