#!/usr/bin/python3 import glob import time import paho.mqtt.publish as publish Broker = '127.0.0.1' auth = { 'username': 'emonpi', 'password': 'emonpimqtt2016', } Sensors = {'SENSOR_A':'28-800000abc123', 'SENSOR_B':'28-800000def456', 'SENSOR_C':'28-800000abc456'} def read_temp(sensorid): valid = False temp = 0 device_file = '/sys/bus/w1/devices/' + sensorid + '/w1_slave' with open(device_file, 'r') as f: for line in f: if line.strip()[-3:] == 'YES': valid = True temp_pos = line.find(' t=') if temp_pos != -1: temp = float(line[temp_pos + 3:]) / 1000.0 if valid: return temp else: return None while True: for name, id in Sensors: temp = read_temp(id) if temp is not None: pub_topic = 'emon/Temperatures/' + name + '/values' publish.single(pub_topic, str(temp), hostname=Broker, port=1883, auth=auth,) time.sleep(6)