### Initialize and Read Temperature with MCP9808 Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/index.md Example code demonstrating how to import necessary libraries, initialize the I2C bus, and create an MCP9808 sensor object to read the current temperature in Celsius. ```python3 import board import adafruit_mcp9808 # Do one reading with board.I2C() as i2c: t = adafruit_mcp9808.MCP9808(i2c) # Finally, read the temperature property and print it out print(t.temperature) ``` -------------------------------- ### MCP9808 Temperature Limit Configuration Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/examples.md This example demonstrates how to set upper, lower, and critical temperature limits on the MCP9808 sensor. It then enters a loop to check if the current temperature crosses these thresholds and prints corresponding messages. ```Python # SPDX-FileCopyrightText: 2021 Jose David M. # SPDX-License-Identifier: MIT """ Show the MCP9808 to setup different temperature values """ import time import board import adafruit_mcp9808 i2c = board.I2C() # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller mcp = adafruit_mcp9808.MCP9808(i2c) # Change the values according to the desired values print("Setting Temperature Limits") mcp.upper_temperature = 23 mcp.lower_temperature = 10 mcp.critical_temperature = 100 # To verify the limits we need to read the temperature value print(mcp.temperature) time.sleep(0.3) # This is the time temperature conversion at maximum resolution # Showing temperature Limits while True: if mcp.below_lower: print("too cold!") if mcp.above_upper: print("getting hot!") if mcp.above_critical: print("Above critical temp!") ``` -------------------------------- ### Install MCP9808 Library via pip Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/README.rst Commands to install the adafruit-circuitpython-mcp9808 library on Linux systems. Supports local user installation, system-wide installation, and virtual environment setup. ```shell pip3 install adafruit-circuitpython-mcp9808 sudo pip3 install adafruit-circuitpython-mcp9808 mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate pip3 install adafruit-circuitpython-mcp9808 ``` -------------------------------- ### Initialize MCP9808 Sensor Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/api.md Demonstrates how to import the library, initialize the I2C bus, and instantiate the MCP9808 sensor object. ```python import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) ``` -------------------------------- ### DisplayIO Temperature Visualization Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt This script shows how to display real-time temperature readings on a screen connected to a CircuitPython board using the DisplayIO library. ```python import time import board from adafruit_display_text.bitmap_label import Label from displayio import Group from terminalio import FONT import adafruit_mcp9808 main_group = Group() i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) display_output_label = Label(FONT, text="", scale=2) display_output_label.anchor_point = (0, 0) display_output_label.anchored_position = (4, board.DISPLAY.height // 2 - 60) main_group.append(display_output_label) board.DISPLAY.root_group = main_group while True: display_output_label.text = f"Temperature: {mcp.temperature:.1f} C" time.sleep(2) ``` -------------------------------- ### DisplayIO Simpletest with MCP9808 Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/examples.md This is a simple test for boards with built-in displays, demonstrating how to initialize the MCP9808 sensor and create display labels to show temperature readings. It sets up the I2C bus and the sensor, then prepares to display the temperature. ```Python # SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries # SPDX-FileCopyrightText: 2024 Jose D. Montoya # # SPDX-License-Identifier: MIT import time import board from adafruit_display_text.bitmap_label import Label from displayio import Group from terminalio import FONT import adafruit_mcp9808 # Simple demo of using the built-in display. # create a main_group to hold anything we want to show on the display. main_group = Group() # Initialize I2C bus and sensor. i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) # Create Label(s) to show the readings. If you have a very small ``` -------------------------------- ### MQTT Integration with Home Assistant Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt This script demonstrates how to publish averaged temperature data from an MCP9808 sensor to a Home Assistant MQTT broker. It includes auto-discovery configuration and uses a rolling average for stable readings. ```python import json import time from array import array import board import numpy as np import paho.mqtt.client as mqtt import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) broker_address = "192.168.1.100" port = 1883 user = "mqttuser" password = "mqttpassword" client = mqtt.Client("P1") client.username_pw_set(user, password=password) client.connect(broker_address, port=port) client.loop_start() discovery_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(discovery_msg), qos=0, retain=True) temp_readings = array("d", [0] * 9) while True: for i in range(9): temp_readings[i] = mcp.temperature print(f"Temperature: {mcp.temperature} C") time.sleep(10) avg_temp = round(np.average(temp_readings), 1) print(f"Average temperature: {avg_temp} C") client.publish("ha/sensor/sensorLivingroom/state", payload=json.dumps({"temperature": avg_temp})) ``` -------------------------------- ### Set Temperature Thresholds Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Configures upper, lower, and critical temperature limits to enable monitoring of environmental boundaries. ```python import time import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) mcp.upper_temperature = 23 mcp.lower_temperature = 10 mcp.critical_temperature = 100 print(f"Upper limit: {mcp.upper_temperature} C") print(f"Lower limit: {mcp.lower_temperature} C") print(f"Critical limit: {mcp.critical_temperature} C") ``` -------------------------------- ### Initialize and Display Temperature Readings with CircuitPython Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/examples.md This code snippet initializes a display label, sets its position, and updates it with temperature readings from an MCP9808 sensor in a loop. It requires the 'displayio' module, 'board', and 'time'. The output is a formatted string showing the temperature in Celsius. ```python import board import time from adafruit_display_text.label import Label from adafruit_bitmap_font import bitmap_font # Assuming 'mcp' is an initialized MCP9808 sensor object # Assuming 'FONT' is a loaded bitmap font object # Assuming 'main_group' is a displayio.Group object # display you may need to change to scale=1. display_output_label = Label(FONT, text="", scale=2) # place the label(s) in the middle of the screen with anchored positioning display_output_label.anchor_point = (0, 0) display_output_label.anchored_position = ( 4, board.DISPLAY.height // 2 - 60, ) # add the label(s) to the main_group main_group.append(display_output_label) # set the main_group as the root_group of the built-in DISPLAY board.DISPLAY.root_group = main_group # begin main loop while True: # update the text of the label(s) to show the sensor readings display_output_label.text = f"Temperature: {mcp.temperature:1f} C" # wait for a bit time.sleep(2) ``` -------------------------------- ### Temperature Thresholds and Alerts Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Configuring temperature limits and monitoring alert flags. ```APIDOC ## PROPERTIES upper_temperature, lower_temperature, critical_temperature ### Description Sets the temperature thresholds in Celsius for alert triggers. ## PROPERTIES above_critical, above_upper, below_lower ### Description Read-only boolean flags indicating if the current temperature has breached the configured thresholds. ### Response - **alert_flag** (bool) - True if the threshold condition is met. ``` -------------------------------- ### Temperature and Resolution Configuration Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Methods for reading the current temperature and setting the measurement resolution. ```APIDOC ## PROPERTY temperature ### Description Returns the current ambient temperature in Celsius. ### Response - **temperature** (float) - Current temperature in Celsius. ## PROPERTY resolution ### Description Sets the measurement precision. Values range from 0 (0.5°C) to 3 (0.0625°C). ### Parameters - **resolution** (int) - Required - Value between 0 and 3. ``` -------------------------------- ### Read Temperature Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/api.md Shows how to retrieve the current ambient temperature in Celsius from the sensor instance. ```python temperature = mcp.temperature ``` -------------------------------- ### MCP9808 Temperature to MQTT for Home Assistant Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/examples.md This Python script reads temperature from the MCP9808 sensor and publishes it to an MQTT broker. It uses Home Assistant's discovery topic to automatically create a temperature entity. It calculates and publishes an averaged temperature over several readings. ```Python # 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 json import time from array import array import board import numpy as np import paho.mqtt.client as mqtt import adafruit_mcp9808 i2c = board.I2C() # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller # 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) # 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(f"Temperature: {mcp.temperature} C ") avgtemp = round(np.average(temp1m), 1) print(f"avgtemp {avgtemp} C") time.sleep(10) send_msg = {"temperature": avgtemp} client.publish( "ha/sensor/sensorLivingroom/state", payload=json.dumps(send_msg) ) # publish result in mqtt ``` -------------------------------- ### Configure Temperature Resolution Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Sets the measurement precision of the sensor. Higher resolution settings provide more accurate data but increase conversion time. ```python import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) # Resolution settings: 0 (0.5C) to 3 (0.0625C) mcp.resolution = 3 print(f"Current resolution setting: {mcp.resolution}") print(f"Temperature: {mcp.temperature} C") ``` -------------------------------- ### Read Temperature with MCP9808 Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/README.rst Initializes the I2C bus and the MCP9808 sensor to read and print the current temperature in Celsius. Requires the board and adafruit_mcp9808 modules. ```python import board import adafruit_mcp9808 with board.I2C() as i2c: t = adafruit_mcp9808.MCP9808(i2c) print(t.temperature) ``` -------------------------------- ### MCP9808 Class Initialization Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Initializes the MCP9808 sensor instance using an I2C bus and an optional address. ```APIDOC ## CLASS MCP9808 ### Description Initializes the MCP9808 sensor object. It verifies the device ID on the I2C bus. ### Parameters - **i2c** (I2C object) - Required - The I2C bus instance. - **address** (int) - Optional - The I2C address of the sensor (default 0x18). ### Request Example ```python import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) ``` ``` -------------------------------- ### Simple MCP9808 Temperature Reading Source: https://github.com/adafruit/adafruit_circuitpython_mcp9808/blob/main/docs/examples.md This snippet shows how to initialize the MCP9808 sensor and continuously read and print the temperature in Celsius and Fahrenheit. It uses the default I2C address. ```Python # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import adafruit_mcp9808 i2c = board.I2C() # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller # To initialise using the default address: mcp = adafruit_mcp9808.MCP9808(i2c) # 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) while True: tempC = mcp.temperature tempF = tempC * 9 / 5 + 32 print(f"Temperature: {tempC} C {tempF} F ") time.sleep(2) ``` -------------------------------- ### Read Ambient Temperature Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Retrieves the current temperature in Celsius using the temperature property and converts it to Fahrenheit. ```python import time import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) while True: tempC = mcp.temperature tempF = tempC * 9 / 5 + 32 print(f"Temperature: {tempC} C {tempF} F ") time.sleep(2) ``` -------------------------------- ### Monitor Temperature Alert Flags Source: https://context7.com/adafruit/adafruit_circuitpython_mcp9808/llms.txt Checks the status of alert flags to determine if the current temperature has crossed defined thresholds. ```python import time import board import adafruit_mcp9808 i2c = board.I2C() mcp = adafruit_mcp9808.MCP9808(i2c) while True: temp = mcp.temperature if mcp.below_lower: print("Alert: Temperature is below lower limit!") if mcp.above_upper: print("Alert: Temperature is above upper limit!") if mcp.above_critical: print("CRITICAL: Temperature exceeds critical threshold!") time.sleep(2) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.