From 5cd5efb3931c71785266ee44854fcc3ec8076eaf Mon Sep 17 00:00:00 2001 From: Taxelas <40238109+Taxelas@users.noreply.github.com> Date: Sun, 6 Feb 2022 22:53:58 +0100 Subject: [PATCH 1/2] Create mcp9808_average temp+mqtt My python script to read mcp9808 temperature and publish it in mqtt. Using discovery topic to create entity in Home Assistant --- examples/mcp9808_average temp+mqtt | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 examples/mcp9808_average temp+mqtt diff --git a/examples/mcp9808_average temp+mqtt b/examples/mcp9808_average temp+mqtt new file mode 100644 index 0000000..477a9ed --- /dev/null +++ b/examples/mcp9808_average temp+mqtt @@ -0,0 +1,61 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import adafruit_mcp9808 +import paho.mqtt.client as mqtt +import numpy as np +import json +i2c = board.I2C() # uses board.SCL and board.SDA + +# To initialise using the default address: +mcp = adafruit_mcp9808.MCP9808(i2c) + +broker_address="Broker IP" +port = 1883 +user = "mqttuser" +password = "mqttpassword" +client = mqtt.Client("P1") #create new instance +client.username_pw_set(user, password=password) +client.connect(broker_address, port=port) +client.loop_start() +# To initialise using a specified address: +# Necessary when, for example, connecting A0 to VDD to make address=0x19 +# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19) +from array import * +from decimal import Decimal +#Create autodiscovery topic for Home assistant +#"ha" is autodiscovery prefix in home assistant +send_msg = { + "state_topic": "ha/sensor/sensorLivingroom/state", + "device_class": "temperature", + "unit_of_measurement": "°C", + "value_template": "{{ value_json.temperature }}", + "device": { + "identifiers": [ + "rpisensorgatewayn01" + ], + "manufacturer": "Raspberry", + "model": "RPI 3B", + "name": "Livingroom temperature", + "sw_version": "MCU9808" + }, + "name": "Livingroom temperature", + "unique_id": "rpisensorgateway_0x01" + } +client.publish("ha/sensor/sensorLivingroom/config", payload=json.dumps(send_msg), qos=0, retain=True) #publish +temp1m = array('d',[0,0,0,0,0,0,0,0,0] ) #using array to aproximate 10 temperature readings +avgtemp = 0 +while True: + print(len(temp1m)) + for count in range (0,9): + temp1m[count] = mcp.temperature + print("Temperature: {} C ".format(mcp.temperature)) + avgtemp = round(np.average(temp1m), 1) + print("avgtemp {} C".format(avgtemp)) + time.sleep(10) + send_msg = { + 'temperature': avgtemp + } + client.publish("ha/sensor/sensorLivingroom/state",payload=json.dumps(send_msg)) #publish result in mqtt From 5fefd763a05ae02bc5d9050e00eb55c902f8bc9c Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 7 Mar 2022 10:13:45 -0600 Subject: [PATCH 2/2] rename to py file. Fix pylint errors --- ...temp+mqtt => mcp9808_average_temp_mqtt.py} | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) rename examples/{mcp9808_average temp+mqtt => mcp9808_average_temp_mqtt.py} (56%) diff --git a/examples/mcp9808_average temp+mqtt b/examples/mcp9808_average_temp_mqtt.py similarity index 56% rename from examples/mcp9808_average temp+mqtt rename to examples/mcp9808_average_temp_mqtt.py index 477a9ed..67acab8 100644 --- a/examples/mcp9808_average temp+mqtt +++ b/examples/mcp9808_average_temp_mqtt.py @@ -1,61 +1,73 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-FileCopyrightText: 2022 Taxelas # SPDX-License-Identifier: MIT +""" +python script to read mcp9808 temperature and publish it in mqtt. +Using discovery topic to create entity in Home Assistant. +""" import time +import json +from array import array import board -import adafruit_mcp9808 import paho.mqtt.client as mqtt import numpy as np -import json +import adafruit_mcp9808 + + i2c = board.I2C() # uses board.SCL and board.SDA # To initialise using the default address: mcp = adafruit_mcp9808.MCP9808(i2c) -broker_address="Broker IP" +broker_address = "Broker IP" port = 1883 user = "mqttuser" password = "mqttpassword" -client = mqtt.Client("P1") #create new instance +client = mqtt.Client("P1") # create new instance client.username_pw_set(user, password=password) client.connect(broker_address, port=port) client.loop_start() # To initialise using a specified address: # Necessary when, for example, connecting A0 to VDD to make address=0x19 # mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19) -from array import * -from decimal import Decimal -#Create autodiscovery topic for Home assistant -#"ha" is autodiscovery prefix in home assistant + + +# Create autodiscovery topic for Home assistant +# "ha" is autodiscovery prefix in home assistant send_msg = { "state_topic": "ha/sensor/sensorLivingroom/state", "device_class": "temperature", "unit_of_measurement": "°C", "value_template": "{{ value_json.temperature }}", "device": { - "identifiers": [ - "rpisensorgatewayn01" - ], + "identifiers": ["rpisensorgatewayn01"], "manufacturer": "Raspberry", "model": "RPI 3B", "name": "Livingroom temperature", - "sw_version": "MCU9808" + "sw_version": "MCU9808", }, "name": "Livingroom temperature", - "unique_id": "rpisensorgateway_0x01" - } -client.publish("ha/sensor/sensorLivingroom/config", payload=json.dumps(send_msg), qos=0, retain=True) #publish -temp1m = array('d',[0,0,0,0,0,0,0,0,0] ) #using array to aproximate 10 temperature readings + "unique_id": "rpisensorgateway_0x01", +} +client.publish( + "ha/sensor/sensorLivingroom/config", + payload=json.dumps(send_msg), + qos=0, + retain=True, +) # publish +temp1m = array( + "d", [0, 0, 0, 0, 0, 0, 0, 0, 0] +) # using array to aproximate 10 temperature readings avgtemp = 0 while True: print(len(temp1m)) - for count in range (0,9): + for count in range(0, 9): temp1m[count] = mcp.temperature print("Temperature: {} C ".format(mcp.temperature)) avgtemp = round(np.average(temp1m), 1) print("avgtemp {} C".format(avgtemp)) time.sleep(10) - send_msg = { - 'temperature': avgtemp - } - client.publish("ha/sensor/sensorLivingroom/state",payload=json.dumps(send_msg)) #publish result in mqtt + send_msg = {"temperature": avgtemp} + client.publish( + "ha/sensor/sensorLivingroom/state", payload=json.dumps(send_msg) + ) # publish result in mqtt