Skip to content

Instantly share code, notes, and snippets.

@enlavin
Last active June 21, 2022 16:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enlavin/e2f28275e575f5fd99ec85d70d0efc6e to your computer and use it in GitHub Desktop.
Save enlavin/e2f28275e575f5fd99ec85d70d0efc6e to your computer and use it in GitHub Desktop.
Telegram bot to monitor temperatures. Created to itch my scratch of wanting to monitor the temperature of a brewing vessel using ESP8266/DS18B20 and a mosquitto broker. Temperatures are logged using RRDTOOL and recorded every 10 seconds.
import __main__
import os
import tempfile
import paho.mqtt.client as mqtt
import rrdtool
import telegram
import telegram.ext
TOKEN_ID = '<your bot id>'
MQTT_SERVER = '<your mqtt server>'
# temperature read from the sensor and shared between the telegram thread and
# the main thread. This is a read only variable and I don't really need
# precision here, so I'm not even worrying about protecting any concurrent
# access. That's how I roll.
temp = 85
# -- telegram section
updater = telegram.ext.Updater(token=TOKEN_ID)
dispatcher = updater.dispatcher
def on_beertemp(bot, update):
# global variables!
global temp
bot.send_message(chat_id=update.message.chat_id, text=temp)
def beer_rrd():
return os.path.join(os.path.dirname(__main__.__file__), 'beer.rrd')
def on_beertempchart(bot, update):
# call rrdtool to generate a chart
fd, fname = tempfile.mkstemp('.png')
rrdtool.graph(
fname,
'-w', '800',
'-h', '300',
'-a', 'PNG',
'--start', '-{0}'.format(6*5*60*24),
'--end', 'now',
'DEF:temp1={0}:temp1:MAX'.format(beer_rrd()),
'LINE1:temp1#ff0000:"cerveza"'
)
try:
with open(fname) as f:
bot.send_photo(chat_id=update.message.chat_id, photo=f)
finally:
os.unlink(fname)
# register /beer and /chart command handlers
beertemp_handler = telegram.ext.CommandHandler('beer', on_beertemp)
dispatcher.add_handler(beertemp_handler)
beertempchart_handler = telegram.ext.CommandHandler('chart', on_beertempchart)
dispatcher.add_handler(beertempchart_handler)
updater.start_polling()
# -- paho MQTT section
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/topic/temp1")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global temp
temp = float(msg.payload)
# call rrd tool to update chart
rrdtool.update(beer_rrd(), 'N:{0}'.format(temp))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment