Guest User

Untitled

a guest
Jan 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # ------------------------------------------------------------------------------
  4. # MQTTWatcher
  5. # Marco Panato 2018/01/13
  6. #
  7. # Simple utility that can monitor topics on a broker and notify users with
  8. # messages sent by a telegram bot.
  9. # ------------------------------------------------------------------------------
  10.  
  11.  
  12. import ssl
  13. import time
  14. import telegram # sudo pip3 install python-telegram-bot
  15. import paho.mqtt.client as mqtt # sudo pip3 install paho-mqtt
  16.  
  17.  
  18. # ------------------------------------------------------------------------------
  19. # Params
  20.  
  21. TELEGRAM_BOT_TOKEN = 'TOKEN'
  22.  
  23. # Mqtt broker address and port
  24. MQTT_BROKER_ADDR = 'ADDRESS'
  25. MQTT_BROKER_PORT = 1883
  26.  
  27. # Mqtt broker credentials, comment these lines if anonymous access is granted
  28. #MQTT_BROKER_USERNAME = 'USERNAME'
  29. #MQTT_BROKER_PASSWORD = 'PASSWORD'
  30.  
  31. # Mqtt broker ssl
  32. MQTT_BROKER_SSL_ENABLED = False
  33. MQTT_BROKER_SSL_CA_PATH = ''
  34. MQTT_BROKER_SSL_CERT_PATH = ''
  35. MQTT_BROKER_SSL_KEY_PATH = ''
  36.  
  37. # A list of topics to subscribe to
  38. TOPIC_TO_WATCH = [
  39. 'prova/topic1'
  40. ]
  41.  
  42. # Telegram (user-id:username) tuple list
  43. USERS_TO_NOTIFY = [
  44. (33343434, 'USERNAME 1') # 33343434 is a RANDOM userid
  45. ]
  46. # ------------------------------------------------------------------------------
  47.  
  48.  
  49. # ------------------------------------------------------------------------------
  50. # Mqtt callbacks
  51. def on_message(mqttc, userdata, message):
  52. print('[TOPIC] Topic %s changed to %s!' % (message.topic, str(message.payload)))
  53. text = 'MqttWatcher\n%s : %s' % (message.topic,str(message.payload))
  54. for user in USERS_TO_NOTIFY:
  55. print('[MQTT] Notifying %s' % user[1])
  56. userdata.send_message(chat_id=user[0], text=text)
  57.  
  58.  
  59. def on_connect(mqttc, userdata, flags, rc):
  60. print("[MQTT] Connected with result code "+str(rc))
  61.  
  62. # Subscribing in on_connect() means that if we lose the connection and
  63. # reconnect then subscriptions will be renewed.
  64. for topic in TOPIC_TO_WATCH:
  65. print('[MQTT] Subscribing to topic: %s' % topic)
  66. mqttc.subscribe(topic, 0)
  67.  
  68.  
  69. def on_disconnect(mqttc, userdata, rc):
  70. print('[MQTT] Connection closed!')
  71. if rc != 0:
  72. print('[MQTT] Trying to reconnect...')
  73. # ------------------------------------------------------------------------------
  74.  
  75. # ------------------------------------------------------------------------------
  76. # Main loop
  77. if __name__ == '__main__':
  78. print('MQTT Topic Watcher\nMarco Panato 2018/01/13')
  79.  
  80. # Telegram bot
  81. print('[MAIN] Initializing TelegramBot')
  82. telegrambot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)
  83.  
  84. # Mqtt client
  85. print('[MAIN] Initializing MQTT Client')
  86. mqttc = mqtt.Client(userdata = telegrambot)
  87. mqttc.on_connect = on_connect
  88. mqttc.on_disconnect = on_disconnect
  89. mqttc.on_message = on_message
  90. if 'MQTT_BROKER_USERNAME' in dir() and 'MQTT_BROKER_PASSWORD' in dir():
  91. print('[MAIN] Setting username and password for MQTT')
  92. mqttc.username_pw_set(MQTT_BROKER_USERNAME, MQTT_BROKER_PASSWORD)
  93. if MQTT_BROKER_SSL_ENABLED:
  94. print('[MAIN] Enabling SSL support for MQTT')
  95. 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)
  96. mqttc.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT)
  97.  
  98. # Main loop
  99. mqttc.loop_start()
  100. print('[MAIN] App ready! Press CTRL-C to quit...')
  101. try:
  102. while True:
  103. time.sleep(100)
  104. except KeyboardInterrupt:
  105. pass
  106.  
  107. print('[MAIN] Quitting...')
  108. mqttc.loop_stop()
  109. mqttc.disconnect()
  110. # ------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment