Skip to content

Commit 0e79d86

Browse files
authored
Merge pull request #228 from brentru/add-mcp9808-guide
Add driver for MCP9808 Temperature Sensor
2 parents 71d2cea + 88ac201 commit 0e79d86

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_IO_Arduino
99
architectures=*
10-
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
10+
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

src/Wippersnapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#endif
6363

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

6767
// Reserved Adafruit IO MQTT topics
6868
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
244244
_scd30->configureDriver(msgDeviceInitReq);
245245
drivers.push_back(_scd30);
246246
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
247+
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
248+
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
249+
if (!_mcp9808->isInitialized()) {
250+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP9808!");
251+
_busStatusResponse =
252+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
253+
return false;
254+
}
255+
_mcp9808->configureDriver(msgDeviceInitReq);
256+
drivers.push_back(_mcp9808);
257+
WS_DEBUG_PRINTLN("MCP9808 Initialized Successfully!");
247258
} else {
248259
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
249260
_busStatusResponse =

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
2424
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
2525
#include "drivers/WipperSnapper_I2C_Driver_DPS310.h"
26+
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
2627
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
2728

2829
#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
@@ -72,6 +73,7 @@ class WipperSnapper_Component_I2C {
7273
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
7374
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
7475
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
76+
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
7577
};
7678
extern Wippersnapper WS;
7779

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_MCP9808.h
3+
*
4+
* Device driver for the MCP9808 Temperature sensor.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2022 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_MCP9808_H
16+
#define WipperSnapper_I2C_Driver_MCP9808_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include <Adafruit_MCP9808.h>
20+
21+
/**************************************************************************/
22+
/*!
23+
@brief Class that provides a driver interface for a MCP9808 sensor.
24+
*/
25+
/**************************************************************************/
26+
class WipperSnapper_I2C_Driver_MCP9808 : public WipperSnapper_I2C_Driver {
27+
public:
28+
/*******************************************************************************/
29+
/*!
30+
@brief Constructor for a MCP9808 sensor.
31+
@param _i2c
32+
The I2C interface.
33+
@param sensorAddress
34+
The 7-bit I2C address of the sensor.
35+
*/
36+
/*******************************************************************************/
37+
WipperSnapper_I2C_Driver_MCP9808(TwoWire *_i2c, uint16_t sensorAddress)
38+
: WipperSnapper_I2C_Driver(_i2c, sensorAddress) {
39+
// Called when a MCP9808 component is created
40+
setI2CAddress(sensorAddress); // sets the driver's I2C address
41+
_mcp9808 = new Adafruit_MCP9808();
42+
_isInitialized = _mcp9808->begin();
43+
}
44+
45+
/*******************************************************************************/
46+
/*!
47+
@brief Destructor for an MCP9808 sensor.
48+
*/
49+
/*******************************************************************************/
50+
~WipperSnapper_I2C_Driver_MCP9808() {
51+
// Called when a MCP9808 component is deleted.
52+
delete _mcp9808;
53+
}
54+
55+
/*******************************************************************************/
56+
/*!
57+
@brief Gets the MCP9808's current temperature.
58+
@param tempEvent
59+
Pointer to an Adafruit_Sensor event.
60+
@returns True if the temperature was obtained successfully, False
61+
otherwise.
62+
*/
63+
/*******************************************************************************/
64+
bool getEventAmbientTemperature(sensors_event_t *tempEvent) {
65+
tempEvent->temperature = _mcp9808->readTempC();
66+
return true;
67+
}
68+
69+
protected:
70+
Adafruit_MCP9808 *_mcp9808; ///< Pointer to MCP9808 temperature sensor object
71+
};
72+
73+
#endif // WipperSnapper_I2C_Driver_MCP9808

0 commit comments

Comments
 (0)