Simple test

Ensure your device works with this simple test.

examples/mcp9808_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_mcp9808
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10
11# To initialise using the default address:
12mcp = adafruit_mcp9808.MCP9808(i2c)
13
14# To initialise using a specified address:
15# Necessary when, for example, connecting A0 to VDD to make address=0x19
16# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)
17
18while True:
19    tempC = mcp.temperature
20    tempF = tempC * 9 / 5 + 32
21    print("Temperature: {} C {} F ".format(tempC, tempF))
22    time.sleep(2)

Temperature Limit test

Show the MCP9808 to setup different temperature values

examples/mcp9808_temperature_limits.py
 1# SPDX-FileCopyrightText: 2021 Jose David M.
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Show the MCP9808 to setup different temperature values
 6"""
 7
 8import time
 9import board
10import adafruit_mcp9808
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
14mcp = adafruit_mcp9808.MCP9808(i2c)
15
16# Change the values according to the desired values
17print("Setting Temperature Limits")
18mcp.upper_temperature = 23
19mcp.lower_temperature = 10
20mcp.critical_temperature = 100
21
22# To verify the limits we need to read the temperature value
23print(mcp.temperature)
24time.sleep(0.3)  # This is the time temperature conversion at maximum resolution
25
26# Showing temperature Limits
27while True:
28    if mcp.below_lower:
29        print("too cold!")
30    if mcp.above_upper:
31        print("getting hot!")
32    if mcp.above_critical:
33        print("Above critical temp!")

MQTT with HomeAssistant

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.py
 1# SPDX-FileCopyrightText: 2022 Taxelas
 2# SPDX-License-Identifier: MIT
 3"""
 4python script to read mcp9808 temperature and publish it in mqtt.
 5Using discovery topic to create entity in Home Assistant.
 6"""
 7
 8import time
 9import json
10from array import array
11import board
12import paho.mqtt.client as mqtt
13import numpy as np
14import adafruit_mcp9808
15
16
17i2c = board.I2C()  # uses board.SCL and board.SDA
18# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
19
20# To initialise using the default address:
21mcp = adafruit_mcp9808.MCP9808(i2c)
22
23broker_address = "Broker IP"
24port = 1883
25user = "mqttuser"
26password = "mqttpassword"
27client = mqtt.Client("P1")  # create new instance
28client.username_pw_set(user, password=password)
29client.connect(broker_address, port=port)
30client.loop_start()
31# To initialise using a specified address:
32# Necessary when, for example, connecting A0 to VDD to make address=0x19
33# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)
34
35
36# Create autodiscovery topic for Home assistant
37# "ha" is autodiscovery prefix in home assistant
38send_msg = {
39    "state_topic": "ha/sensor/sensorLivingroom/state",
40    "device_class": "temperature",
41    "unit_of_measurement": "°C",
42    "value_template": "{{ value_json.temperature }}",
43    "device": {
44        "identifiers": ["rpisensorgatewayn01"],
45        "manufacturer": "Raspberry",
46        "model": "RPI 3B",
47        "name": "Livingroom temperature",
48        "sw_version": "MCU9808",
49    },
50    "name": "Livingroom temperature",
51    "unique_id": "rpisensorgateway_0x01",
52}
53client.publish(
54    "ha/sensor/sensorLivingroom/config",
55    payload=json.dumps(send_msg),
56    qos=0,
57    retain=True,
58)  # publish
59temp1m = array(
60    "d", [0, 0, 0, 0, 0, 0, 0, 0, 0]
61)  # using array to aproximate 10 temperature readings
62avgtemp = 0
63while True:
64    print(len(temp1m))
65    for count in range(0, 9):
66        temp1m[count] = mcp.temperature
67        print("Temperature: {} C ".format(mcp.temperature))
68        avgtemp = round(np.average(temp1m), 1)
69        print("avgtemp {} C".format(avgtemp))
70        time.sleep(10)
71    send_msg = {"temperature": avgtemp}
72    client.publish(
73        "ha/sensor/sensorLivingroom/state", payload=json.dumps(send_msg)
74    )  # publish result in mqtt