Skip to content

Commit

Permalink
Merge pull request #231 from brentru/add-mcp9601
Browse files Browse the repository at this point in the history
Add MCP9601 Sensor
  • Loading branch information
brentru authored Mar 14, 2022
2 parents b97f0a5 + be97647 commit 63a58fc
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 10 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit WipperSnapper Beta
version=1.0.0-beta.25
version=1.0.0-beta.27
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino client for Adafruit.io WipperSnapper
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, Adafruit MCP9808 Library
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, Adafruit MCP9600 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.26" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.27" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down
2 changes: 1 addition & 1 deletion src/Wippersnapper_Boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
#define BOARD_ID "adafruit-huzzah-32-v2"
#define USE_STATUS_NEOPIXEL
#define STATUS_NEOPIXEL_PIN NEOPIXEL_PIN
#define STATUS_NEOPIXEL_PIN PIN_NEOPIXEL
#define STATUS_NEOPIXEL_NUM 1
#define USE_LITTLEFS
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
Expand Down
48 changes: 44 additions & 4 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(
// Initialize I2C bus
#if defined(ARDUINO_ARCH_ESP32)
_i2c = new TwoWire(msgInitRequest->i2c_port_number);
if (!_i2c->begin(msgInitRequest->i2c_pin_sda,
msgInitRequest->i2c_pin_scl)) {
if (!_i2c->begin((int)msgInitRequest->i2c_pin_sda,
(int)msgInitRequest->i2c_pin_scl)) {
_isInit = false; // if the peripheral was configured incorrectly
} else {
_isInit = true; // if the peripheral was configured incorrectly
}
_i2c->setClock(msgInitRequest->i2c_frequency);
_i2c->setClock(50000);
#elif defined(ARDUINO_ARCH_ESP8266)
_i2c = new TwoWire();
_i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl);
_i2c->setClock(msgInitRequest->i2c_frequency);
_i2c->setClock(50000);
_isInit = true;
#else
// SAMD
Expand Down Expand Up @@ -254,6 +254,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_mcp9808->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp9808);
WS_DEBUG_PRINTLN("MCP9808 Initialized Successfully!");
} else if (strcmp("mcp9601", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9601 = new WipperSnapper_I2C_Driver_MCP9601(this->_i2c, i2cAddress);
if (!_mcp9601->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP9601!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_mcp9601->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp9601);
WS_DEBUG_PRINTLN("MCP9601 Initialized Successfully!");
} else {
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
_busStatusResponse =
Expand Down Expand Up @@ -286,6 +297,10 @@ void WipperSnapper_Component_I2C::updateI2CDeviceProperties(
drivers[i]->updateSensorAmbientTemperature(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE:
drivers[i]->updateSensorObjectTemp(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_RELATIVE_HUMIDITY:
drivers[i]->updateSensorRelativeHumidity(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
Expand Down Expand Up @@ -458,6 +473,31 @@ void WipperSnapper_Component_I2C::update() {
}
}

// AMBIENT_TEMPERATURE sensor
curTime = millis();
if ((*iter)->sensorObjectTempPeriod() != 0L &&
curTime - (*iter)->sensorObjectTempPeriodPrv() >
(*iter)->sensorObjectTempPeriod()) {
if ((*iter)->getEventObjectTemp(&event)) {
WS_DEBUG_PRINT("Sensor 0x");
WS_DEBUG_PRINTHEX((*iter)->getI2CAddress());
WS_DEBUG_PRINTLN("");
WS_DEBUG_PRINT("\tTemperature: ");
WS_DEBUG_PRINT(event.temperature);
WS_DEBUG_PRINTLN(" degrees C");

// pack event data into msg
fillEventMessage(
&msgi2cResponse, event.temperature,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE);

(*iter)->setSensorObjectTempPeriodPrv(curTime);
} else {
WS_DEBUG_PRINTLN(
"ERROR: Failed to get object temperature sensor reading!");
}
}

// RELATIVE_HUMIDITY sensor
curTime = millis();
if ((*iter)->sensorRelativeHumidityPeriod() != 0L &&
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_MCP9601.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"

Expand Down Expand Up @@ -74,6 +75,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr;
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
WipperSnapper_I2C_Driver_MCP9601 *_mcp9601 = nullptr;
};
extern Wippersnapper WS;

Expand Down
103 changes: 101 additions & 2 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class WipperSnapper_I2C_Driver {
setSensorAltitudePeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_OBJECT_TEMPERATURE:
enableSensorObjectTemp();
setSensorObjectTempPeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
default:
break;
}
Expand Down Expand Up @@ -667,6 +671,97 @@ class WipperSnapper_I2C_Driver {
setSensorAltitudePeriod(period);
}

/**************************** SENSOR_TYPE: Object_Temperature
* ****************************/
/*******************************************************************************/
/*!
@brief Enables the device's object temperature sensor, if it exists.
*/
/*******************************************************************************/
virtual void enableSensorObjectTemp(){};

/*******************************************************************************/
/*!
@brief Disables the device's object temperature sensor, if it exists.
*/
/*******************************************************************************/
virtual void disableSensorObjectTemp() { _objectTempSensorPeriod = 0.0L; }

/*********************************************************************************/
/*!
@brief Base implementation - Returns the object temperature sensor's
period, if set.
@returns Time when the object temperature sensor should be polled, in
seconds.
*/
/*********************************************************************************/
virtual long sensorObjectTempPeriod() { return _objectTempSensorPeriod; }

/*******************************************************************************/
/*!
@brief Set the object temperature sensor's return frequency.
@param period
The time interval at which to return new data from the
object temperature sensor.
*/
/*******************************************************************************/
virtual void setSensorObjectTempPeriod(float period) {
if (period == 0)
disableSensorObjectTemp();
// Period is in seconds, cast it to long and convert it to milliseconds
_objectTempSensorPeriod = (long)period * 1000;
}

/*********************************************************************************/
/*!
@brief Base implementation - Returns the previous time interval at
which the object temperature sensor was queried last.
@returns Time when the object temperature sensor was last queried,
in seconds.
*/
/*********************************************************************************/
virtual long sensorObjectTempPeriodPrv() {
return _objectTempSensorPeriodPrv;
}

/*******************************************************************************/
/*!
@brief Sets a timestamp for when the object temperature sensor
was queried.
@param period
The time when the object temperature sensor was queried last.
*/
/*******************************************************************************/
virtual void setSensorObjectTempPeriodPrv(long period) {
_objectTempSensorPeriodPrv = period;
}

/*******************************************************************************/
/*!
@brief Base implementation - Reads a object temperature sensor and
converts the reading into the expected SI unit.
@param objectTempEvent
object temperature sensor reading, in meters.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
virtual bool getEventObjectTemp(sensors_event_t *objectTempEvent) {
return false;
}

/*******************************************************************************/
/*!
@brief Updates the properties of a object temperature sensor.
@param period
The time interval at which to return new data from the
object temperature sensor.
*/
/*******************************************************************************/
virtual void updateSensorObjectTemp(float period) {
setSensorObjectTempPeriod(period);
}

protected:
TwoWire *_i2c; ///< Pointer to the I2C driver's Wire object
uint16_t _sensorAddress; ///< The I2C driver's unique I2C address.
Expand All @@ -692,8 +787,12 @@ class WipperSnapper_I2C_Driver {
///< was last read.
long _altitudeSensorPeriod =
0L; ///< The time period between reading the altitude sensor's value.
long _altitudeSensorPeriodPrv = 0L; ///< The time when the altitude sensor
///< was last read.
long _altitudeSensorPeriodPrv = 0L; ///< The time when the altitude sensor
///< was last read.
long _objectTempSensorPeriod = 0L; ///< The time period between reading the
///< object temperature sensor's value.
long _objectTempSensorPeriodPrv = 0L; ///< The time when the object
///< temperature sensor was last read.
};

#endif // WipperSnapper_I2C_Driver_H
125 changes: 125 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP9601.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*!
* @file WipperSnapper_I2C_Driver_MCP9601.h
*
* Device driver for the MCP9601 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_MCP9601_H
#define WipperSnapper_I2C_Driver_MCP9601_H

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

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a MCP9601 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_MCP9601 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a MCP9601 sensor.
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_MCP9601(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an MCP9601 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_MCP9601() {
// Called when a MCP9601 component is deleted.
delete _MCP9601;
}

/*******************************************************************************/
/*!
@brief Initializes the MCP9601 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_MCP9601 = new Adafruit_MCP9601();
if (!_MCP9601->begin((uint8_t)_sensorAddress))
return false;

// Configure MCP9601's settings
// Set resolution
_MCP9601->setADCresolution(MCP9600_ADCRESOLUTION_18);
// Set thermocouple type (NOTE: We do not have advanced settings in WS, set
// to 'K'-type for now)
_MCP9601->setThermocoupleType(MCP9600_TYPE_K);
// Set filter coefficient
_MCP9601->setFilterCoefficient(3);
// Enable sensor
_MCP9601->enable(true);
return true;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9601'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) {
uint8_t status = _MCP9601->getStatus();
if (status & MCP9601_STATUS_OPENCIRCUIT) {
return false; // don't continue, since there's no thermocouple
}
if (status & MCP9601_STATUS_SHORTCIRCUIT) {
return false; // don't continue, since the sensor is not working
}

tempEvent->temperature = _MCP9601->readAmbient();
return true;
}

/*******************************************************************************/
/*!
@brief Gets the MCP9601's current object temperature (thermocouple).
@param objectTempEvent
Pointer to an Adafruit_Sensor event.
@returns True if the object temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventObjectTemp(sensors_event_t *objectTempEvent) {
uint8_t status = _MCP9601->getStatus();
if (status & MCP9601_STATUS_OPENCIRCUIT) {
return false; // don't continue, since there's no thermocouple
}
if (status & MCP9601_STATUS_SHORTCIRCUIT) {
return false; // don't continue, since the sensor is not working
}

objectTempEvent->temperature = _MCP9601->readThermocouple();
return true;
}

protected:
Adafruit_MCP9601 *_MCP9601; ///< Pointer to MCP9601 temperature sensor object
};

#endif // WipperSnapper_I2C_Driver_MCP9601

0 comments on commit 63a58fc

Please sign in to comment.