Skip to content

Commit

Permalink
Merge pull request #228 from brentru/add-mcp9808-guide
Browse files Browse the repository at this point in the history
Add driver for MCP9808 Temperature Sensor
  • Loading branch information
brentru authored Mar 8, 2022
2 parents 71d2cea + 88ac201 commit 0e79d86
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, Adafruit MCP9808 Library
2 changes: 1 addition & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#endif

#define WS_VERSION \
"1.0.0-beta.25" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.26" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_scd30->configureDriver(msgDeviceInitReq);
drivers.push_back(_scd30);
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
if (!_mcp9808->isInitialized()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP9808!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_mcp9808->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp9808);
WS_DEBUG_PRINTLN("MCP9808 Initialized Successfully!");
} else {
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
_busStatusResponse =
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
Expand Down Expand Up @@ -72,6 +73,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
};
extern Wippersnapper WS;

Expand Down
73 changes: 73 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP9808.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*!
* @file WipperSnapper_I2C_Driver_MCP9808.h
*
* Device driver for the MCP9808 Temperature sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2022 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_MCP9808_H
#define WipperSnapper_I2C_Driver_MCP9808_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_MCP9808.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a MCP9808 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_MCP9808 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a MCP9808 sensor.
@param _i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_MCP9808(TwoWire *_i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
// Called when a MCP9808 component is created
setI2CAddress(sensorAddress); // sets the driver's I2C address
_mcp9808 = new Adafruit_MCP9808();
_isInitialized = _mcp9808->begin();
}

/*******************************************************************************/
/*!
@brief Destructor for an MCP9808 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_MCP9808() {
// Called when a MCP9808 component is deleted.
delete _mcp9808;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9808's current temperature.
@param tempEvent
Pointer to an Adafruit_Sensor event.
@returns True if the temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventAmbientTemperature(sensors_event_t *tempEvent) {
tempEvent->temperature = _mcp9808->readTempC();
return true;
}

protected:
Adafruit_MCP9808 *_mcp9808; ///< Pointer to MCP9808 temperature sensor object
};

#endif // WipperSnapper_I2C_Driver_MCP9808

0 comments on commit 0e79d86

Please sign in to comment.