macrocell_office_iot/scripts/simple_subscriber.py

32 lines
909 B
Python
Raw Normal View History

#!/usr/bin/env python3
'''
/* ESP8266 environmental sensor project
*
* This script provides a very simplistic MQTT subscriber that subscribes to any topic of an MQTT broker running on localhost.
* It can be used perfectly for debugging sensors with a local broker like mosquitto_sub.
*
* Developed on and tested with Python 3.5.
*
* (C) 2019 Macrocell - Environmental sensing solutions
* proudly presented by Macrocell - FPGA Innovators
*/ '''
import paho.mqtt.client as mqtt
import datetime
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("#")
def on_message(client, userdata, msg):
print(str(datetime.datetime.now()) + " : " + msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_forever()