Guest User

Untitled

a guest
Dec 10th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import __main__
  2. import os
  3. import tempfile
  4.  
  5. import paho.mqtt.client as mqtt
  6. import rrdtool
  7. import telegram
  8. import telegram.ext
  9.  
  10.  
  11. TOKEN_ID = '<your bot id>'
  12. MQTT_SERVER = '<your mqtt server>'
  13.  
  14. # temperature read from the sensor and shared between the telegram thread and
  15. # the main thread. This is a read only variable and I don't really need
  16. # precision here, so I'm not even worrying about protecting any concurrent
  17. # access. That's how I roll.
  18. temp = 85
  19.  
  20.  
  21. # -- telegram section
  22. updater = telegram.ext.Updater(token=TOKEN_ID)
  23. dispatcher = updater.dispatcher
  24.  
  25. def on_beertemp(bot, update):
  26. # global variables!
  27. global temp
  28. bot.send_message(chat_id=update.message.chat_id, text=temp)
  29.  
  30. def beer_rrd():
  31. return os.path.join(os.path.dirname(__main__.__file__), 'beer.rrd')
  32.  
  33. def on_beertempchart(bot, update):
  34. # call rrdtool to generate a chart
  35. fd, fname = tempfile.mkstemp('.png')
  36. rrdtool.graph(
  37. fname,
  38. '-w', '800',
  39. '-h', '300',
  40. '-a', 'PNG',
  41. '--start', '-{0}'.format(6*5*60*24),
  42. '--end', 'now',
  43. 'DEF:temp1={0}:temp1:MAX'.format(beer_rrd()),
  44. 'LINE1:temp1#ff0000:"cerveza"'
  45. )
  46.  
  47. try:
  48. with open(fname) as f:
  49. bot.send_photo(chat_id=update.message.chat_id, photo=f)
  50. finally:
  51. os.unlink(fname)
  52.  
  53. # register /beer and /chart command handlers
  54. beertemp_handler = telegram.ext.CommandHandler('beer', on_beertemp)
  55. dispatcher.add_handler(beertemp_handler)
  56. beertempchart_handler = telegram.ext.CommandHandler('chart', on_beertempchart)
  57. dispatcher.add_handler(beertempchart_handler)
  58. updater.start_polling()
  59.  
  60. # -- paho MQTT section
  61. # The callback for when the client receives a CONNACK response from the server.
  62. def on_connect(client, userdata, flags, rc):
  63. print("Connected with result code "+str(rc))
  64.  
  65. # Subscribing in on_connect() means that if we lose the connection and
  66. # reconnect then subscriptions will be renewed.
  67. client.subscribe("/topic/temp1")
  68.  
  69. # The callback for when a PUBLISH message is received from the server.
  70. def on_message(client, userdata, msg):
  71. global temp
  72. temp = float(msg.payload)
  73. # call rrd tool to update chart
  74. rrdtool.update(beer_rrd(), 'N:{0}'.format(temp))
  75.  
  76. client = mqtt.Client()
  77. client.on_connect = on_connect
  78. client.on_message = on_message
  79.  
  80. client.connect(MQTT_SERVER, 1883, 60)
  81.  
  82. # Blocking call that processes network traffic, dispatches callbacks and
  83. # handles reconnecting.
  84. # Other loop*() functions are available that give a threaded interface and a
  85. # manual interface.
  86. client.loop_forever()
Add Comment
Please, Sign In to add comment