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
5
6import board
7
8import adafruit_mcp9808
9
10i2c = board.I2C() # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
12
13# To initialise using the default address:
14mcp = adafruit_mcp9808.MCP9808(i2c)
15
16# To initialise using a specified address:
17# Necessary when, for example, connecting A0 to VDD to make address=0x19
18# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)
19
20while True:
21 tempC = mcp.temperature
22 tempF = tempC * 9 / 5 + 32
23 print(f"Temperature: {tempC} C {tempF} F ")
24 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
9
10import board
11
12import adafruit_mcp9808
13
14i2c = board.I2C() # uses board.SCL and board.SDA
15# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
16mcp = adafruit_mcp9808.MCP9808(i2c)
17
18# Change the values according to the desired values
19print("Setting Temperature Limits")
20mcp.upper_temperature = 23
21mcp.lower_temperature = 10
22mcp.critical_temperature = 100
23
24# To verify the limits we need to read the temperature value
25print(mcp.temperature)
26time.sleep(0.3) # This is the time temperature conversion at maximum resolution
27
28# Showing temperature Limits
29while True:
30 if mcp.below_lower:
31 print("too cold!")
32 if mcp.above_upper:
33 print("getting hot!")
34 if mcp.above_critical:
35 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 json
9import time
10from array import array
11
12import board
13import numpy as np
14import paho.mqtt.client as mqtt
15
16import adafruit_mcp9808
17
18i2c = board.I2C() # uses board.SCL and board.SDA
19# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
20
21# To initialise using the default address:
22mcp = adafruit_mcp9808.MCP9808(i2c)
23
24broker_address = "Broker IP"
25port = 1883
26user = "mqttuser"
27password = "mqttpassword"
28client = mqtt.Client("P1") # create new instance
29client.username_pw_set(user, password=password)
30client.connect(broker_address, port=port)
31client.loop_start()
32# To initialise using a specified address:
33# Necessary when, for example, connecting A0 to VDD to make address=0x19
34# mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=0x19)
35
36
37# Create autodiscovery topic for Home assistant
38# "ha" is autodiscovery prefix in home assistant
39send_msg = {
40 "state_topic": "ha/sensor/sensorLivingroom/state",
41 "device_class": "temperature",
42 "unit_of_measurement": "°C",
43 "value_template": "{{ value_json.temperature }}",
44 "device": {
45 "identifiers": ["rpisensorgatewayn01"],
46 "manufacturer": "Raspberry",
47 "model": "RPI 3B",
48 "name": "Livingroom temperature",
49 "sw_version": "MCU9808",
50 },
51 "name": "Livingroom temperature",
52 "unique_id": "rpisensorgateway_0x01",
53}
54client.publish(
55 "ha/sensor/sensorLivingroom/config",
56 payload=json.dumps(send_msg),
57 qos=0,
58 retain=True,
59) # publish
60temp1m = array(
61 "d", [0, 0, 0, 0, 0, 0, 0, 0, 0]
62) # using array to aproximate 10 temperature readings
63avgtemp = 0
64while True:
65 print(len(temp1m))
66 for count in range(0, 9):
67 temp1m[count] = mcp.temperature
68 print(f"Temperature: {mcp.temperature} C ")
69 avgtemp = round(np.average(temp1m), 1)
70 print(f"avgtemp {avgtemp} C")
71 time.sleep(10)
72 send_msg = {"temperature": avgtemp}
73 client.publish(
74 "ha/sensor/sensorLivingroom/state", payload=json.dumps(send_msg)
75 ) # publish result in mqtt
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/mcp9808_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6import time
7
8import board
9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_mcp9808
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20mcp = adafruit_mcp9808.MCP9808(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label(s) in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (
29 4,
30 board.DISPLAY.height // 2 - 60,
31)
32
33# add the label(s) to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41 # update the text of the label(s) to show the sensor readings
42 display_output_label.text = f"Temperature: {mcp.temperature:1f} C"
43 # wait for a bit
44 time.sleep(2)