#!/usr/bin/python3 # -*- coding: utf8 -*- #Get LM75 temperature value and send it to MQTT broker import smbus import paho.mqtt.publish as mqtt import datetime # MQTT settings mqtt_server = 'localhost' mqtt_port= 1883 mqtt_auth= {'username':'cameteo', 'password':'CaMeteo'} mqtt_client_id= 'papirus_temp2mqtt' mqtt_qos=2 mqtt_topic="pi/papirus_zero_hat/temperature/" #LM75B settings LM75B_ADDRESS = 0x48 LM75B_TEMP_REGISTER = 0 LM75B_CONF_REGISTER = 1 LM75B_THYST_REGISTER = 2 LM75B_TOS_REGISTER = 3 LM75B_CONF_NORMAL = 0 class LM75B(object): def __init__(self, address=LM75B_ADDRESS, busnum=1): self._address = address self._bus = smbus.SMBus(busnum) self._bus.write_byte_data(self._address, LM75B_CONF_REGISTER, LM75B_CONF_NORMAL) def getTempC(self): """Return temperature in degrees Celsius""" raw = self._bus.read_word_data(self._address, LM75B_TEMP_REGISTER) & 0xFFFF raw = (((raw << 8) & 0xFF00) + (raw >> 8) +128 ) /256 return raw ######## # Main # ######## #Get and print data date = datetime.datetime.now().replace(microsecond=0).isoformat() print(date) sens = LM75B() lm75_temp = sens.getTempC() print("Temperature (Papirus HAT LM75b) : %s °C" % lm75_temp) #Send data to MQTT broker mqtt_data = [{'topic':mqtt_topic + "value", 'payload':lm75_temp, 'qos':mqtt_qos, 'retain':True}, {'topic':mqtt_topic + "date", 'payload':date, 'qos':mqtt_qos, 'retain':True}, {'topic':mqtt_topic + "unit", 'payload':"\u00B0C", 'qos':mqtt_qos, 'retain':True}, {'topic':mqtt_topic + "desc", 'payload':"LM75b sensor", 'qos':mqtt_qos, 'retain':True}] mqtt.multiple(mqtt_data, hostname= mqtt_server , port= mqtt_port , auth= mqtt_auth , client_id= mqtt_client_id )