Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Created February 1, 2017 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LarsBergqvist/e27ab8f47c2575e4f6cd64b1324998b3 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/e27ab8f47c2575e4f6cd64b1324998b3 to your computer and use it in GitHub Desktop.
Measure the CPU temperature on a Raspberry Pi and publish the data with MQTT
#!/usr/bin/env python3
import paho.mqtt.publish as publish
from subprocess import check_output
from re import findall
def get_temp():
temp = check_output(["vcgencmd","measure_temp"]).decode("UTF-8")
return(findall("\d+\.\d+",temp)[0])
def publish_message(topic, message):
print("Publishing to MQTT topic: " + topic)
print("Message: " + message)
publish.single(topic, message, hostname="192.168.1.16")
temp = get_temp()
publish_message("Home/RPI3/Temp", temp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment