Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # ------------------------------------------------------------------------------
- # MQTTWatcher
- # Marco Panato 2018/01/13
- #
- # Simple utility that can monitor topics on a broker and notify users with
- # messages sent by a telegram bot.
- # ------------------------------------------------------------------------------
- import ssl
- import time
- import telegram # sudo pip3 install python-telegram-bot
- import paho.mqtt.client as mqtt # sudo pip3 install paho-mqtt
- # ------------------------------------------------------------------------------
- # Params
- TELEGRAM_BOT_TOKEN = 'TOKEN'
- # Mqtt broker address and port
- MQTT_BROKER_ADDR = 'ADDRESS'
- MQTT_BROKER_PORT = 1883
- # Mqtt broker credentials, comment these lines if anonymous access is granted
- #MQTT_BROKER_USERNAME = 'USERNAME'
- #MQTT_BROKER_PASSWORD = 'PASSWORD'
- # Mqtt broker ssl
- MQTT_BROKER_SSL_ENABLED = False
- MQTT_BROKER_SSL_CA_PATH = ''
- MQTT_BROKER_SSL_CERT_PATH = ''
- MQTT_BROKER_SSL_KEY_PATH = ''
- # A list of topics to subscribe to
- TOPIC_TO_WATCH = [
- 'prova/topic1'
- ]
- # Telegram (user-id:username) tuple list
- USERS_TO_NOTIFY = [
- (33343434, 'USERNAME 1') # 33343434 is a RANDOM userid
- ]
- # ------------------------------------------------------------------------------
- # ------------------------------------------------------------------------------
- # Mqtt callbacks
- def on_message(mqttc, userdata, message):
- print('[TOPIC] Topic %s changed to %s!' % (message.topic, str(message.payload)))
- text = 'MqttWatcher\n%s : %s' % (message.topic,str(message.payload))
- for user in USERS_TO_NOTIFY:
- print('[MQTT] Notifying %s' % user[1])
- userdata.send_message(chat_id=user[0], text=text)
- def on_connect(mqttc, userdata, flags, rc):
- print("[MQTT] Connected with result code "+str(rc))
- # Subscribing in on_connect() means that if we lose the connection and
- # reconnect then subscriptions will be renewed.
- for topic in TOPIC_TO_WATCH:
- print('[MQTT] Subscribing to topic: %s' % topic)
- mqttc.subscribe(topic, 0)
- def on_disconnect(mqttc, userdata, rc):
- print('[MQTT] Connection closed!')
- if rc != 0:
- print('[MQTT] Trying to reconnect...')
- # ------------------------------------------------------------------------------
- # ------------------------------------------------------------------------------
- # Main loop
- if __name__ == '__main__':
- print('MQTT Topic Watcher\nMarco Panato 2018/01/13')
- # Telegram bot
- print('[MAIN] Initializing TelegramBot')
- telegrambot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)
- # Mqtt client
- print('[MAIN] Initializing MQTT Client')
- mqttc = mqtt.Client(userdata = telegrambot)
- mqttc.on_connect = on_connect
- mqttc.on_disconnect = on_disconnect
- mqttc.on_message = on_message
- if 'MQTT_BROKER_USERNAME' in dir() and 'MQTT_BROKER_PASSWORD' in dir():
- print('[MAIN] Setting username and password for MQTT')
- mqttc.username_pw_set(MQTT_BROKER_USERNAME, MQTT_BROKER_PASSWORD)
- if MQTT_BROKER_SSL_ENABLED:
- print('[MAIN] Enabling SSL support for MQTT')
- mqttc.tls_set(ca_certs=MQTT_BROKER_SSL_CA_PATH, certfile=MQTT_BROKER_SSL_CERT_PATH, keyfile=MQTT_BROKER_SSL_KEY_PATH, cert_reqs=ssl.CERT_NONE)
- mqttc.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT)
- # Main loop
- mqttc.loop_start()
- print('[MAIN] App ready! Press CTRL-C to quit...')
- try:
- while True:
- time.sleep(100)
- except KeyboardInterrupt:
- pass
- print('[MAIN] Quitting...')
- mqttc.loop_stop()
- mqttc.disconnect()
- # ------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment