Skip to content

Commit

Permalink
Merge pull request #303 from marcuswu/issue/14/Add_STEMMA_Soil_Sensor…
Browse files Browse the repository at this point in the history
…_Support

Add STEMMA Soil Sensor support
  • Loading branch information
brentru committed Sep 6, 2022
2 parents 828b03d + 967f8c0 commit b4a5d0d
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 6 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
version=1.0.0-beta.45
version=1.0.0-beta.46
author=Adafruit
maintainer=Adafruit <adafruitio@adafruit.com>
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, Adafruit MCP9600 Library, Adafruit TSL2591 Library, Adafruit SHT4x Library, Adafruit PM25 AQI Sensor, Adafruit LC709203F
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, Adafruit TSL2591 Library, Adafruit SHT4x Library, Adafruit PM25 AQI Sensor, Adafruit LC709203F, Adafruit seesaw Library
2 changes: 1 addition & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#endif

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

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down
37 changes: 36 additions & 1 deletion src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_lc->configureDriver(msgDeviceInitReq);
drivers.push_back(_lc);
WS_DEBUG_PRINTLN("LC709203F Sensor Initialized Successfully!");
} else if (strcmp("stemma_soil", msgDeviceInitReq->i2c_device_name) == 0) {
_ss =
new WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor(this->_i2c, i2cAddress);
if (!_ss->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize STEMMA Soil Sensor!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ss->configureDriver(msgDeviceInitReq);
drivers.push_back(_ss);
WS_DEBUG_PRINTLN("STEMMA Soil Sensor Initialized Successfully!");
} else {
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
_busStatusResponse =
Expand Down Expand Up @@ -402,6 +414,10 @@ void WipperSnapper_Component_I2C::updateI2CDeviceProperties(
drivers[i]->updateSensorUnitlessPercent(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_RAW:
drivers[i]->updateSensorRaw(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
default:
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSUPPORTED_SENSOR;
Expand Down Expand Up @@ -814,6 +830,25 @@ void WipperSnapper_Component_I2C::update() {
(*iter)->setSensorUnitlessPercentPeriodPrv(curTime);
}

// Raw sensor
curTime = millis();
if ((*iter)->sensorRawPeriod() != 0L &&
curTime - (*iter)->sensorRawPeriodPrv() > (*iter)->sensorRawPeriod()) {
if ((*iter)->getEventRaw(&event)) {
WS_DEBUG_PRINT("Sensor 0x");
WS_DEBUG_PRINTHEX((*iter)->getI2CAddress());
WS_DEBUG_PRINTLN("");
WS_DEBUG_PRINT("\tRaw: ");
WS_DEBUG_PRINTLN(event.data[0]);

fillEventMessage(&msgi2cResponse, event.data[0],
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_RAW);
} else {
WS_DEBUG_PRINTLN("ERROR: Failed to obtain Raw sensor reading!");
}
(*iter)->setSensorRawPeriodPrv(curTime);
}

// Did this driver obtain data from sensors?
if (msgi2cResponse.payload.resp_i2c_device_event.sensor_event_count == 0)
continue;
Expand All @@ -825,4 +860,4 @@ void WipperSnapper_Component_I2C::update() {
continue;
}
}
}
}
4 changes: 3 additions & 1 deletion src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD40.h"
#include "drivers/WipperSnapper_I2C_Driver_SHT4X.h"
#include "drivers/WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor.h"
#include "drivers/WipperSnapper_I2C_Driver_TSL2591.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
Expand Down Expand Up @@ -86,7 +87,8 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_PM25 *_pm25 = nullptr;
WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr;
WipperSnapper_I2C_Driver_LC709203F *_lc = nullptr;
WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr;
};
extern Wippersnapper WS;

#endif // WipperSnapper_Component_I2C_H
#endif // WipperSnapper_Component_I2C_H
94 changes: 93 additions & 1 deletion src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class WipperSnapper_I2C_Driver {
setSensorVoltagePeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_RAW:
enableSensorRaw();
setSensorRawPeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
break;
default:
break;
}
Expand Down Expand Up @@ -1316,6 +1321,89 @@ class WipperSnapper_I2C_Driver {
setSensorVoltagePeriod(period);
}

/****************************** SENSOR_TYPE: Raw
* *******************************/
/*******************************************************************************/
/*!
@brief Enables the device's Raw sensor, if it exists.
*/
/*******************************************************************************/
virtual void enableSensorRaw() { return; };

/*******************************************************************************/
/*!
@brief Disables the device's Raw sensor, if it exists.
*/
/*******************************************************************************/
virtual void disableSensorRaw() { _rawSensorPeriod = 0.0L; }

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

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

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

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

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

/*******************************************************************************/
/*!
@brief Gets a sensor's Raw value.
@param rawEvent
The Raw value.
@returns True if the sensor value was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
virtual bool getEventRaw(sensors_event_t *rawEvent) { return false; }

protected:
TwoWire *_i2c; ///< Pointer to the I2C driver's Wire object
uint16_t _sensorAddress; ///< The I2C driver's unique I2C address.
Expand Down Expand Up @@ -1371,6 +1459,10 @@ class WipperSnapper_I2C_Driver {
///< voltage sensor's value.
long _voltagePeriodPrv = 0L; ///< The time when the voltage sensor
///< was last read.
long _rawSensorPeriod =
0L; ///< The time period between reading the Raw sensor's value.
long _rawSensorPeriodPrv = 0L; ///< The time when the Raw sensor
///< was last read.
};

#endif // WipperSnapper_I2C_Driver_H
#endif // WipperSnapper_I2C_Driver_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*!
* @file WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor.h
*
* Device driver for the STEMMA Soil 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) Marcus Wu 2022
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor_H
#define WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor_H

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

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

/*******************************************************************************/
/*!
@brief Destructor for a STEMMA soil sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor() { delete _seesaw; }

/*******************************************************************************/
/*!
@brief Initializes the soil sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() { return _seesaw->begin(_sensorAddress); }

/*******************************************************************************/
/*!
@brief Gets the sensor'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 = _seesaw->getTemp();
return true;
}

/*******************************************************************************/
/*!
@brief Gets the sensor's current moisture sensor capacitance value.
@param rawEvent
Pointer to an Adafruit_Sensor event.
@returns True if the temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
uint16_t touchData = _seesaw->touchRead(0);

// seesaw->touchRead() will return 65535 on a read error
// see
// https://github.com/adafruit/Adafruit_Seesaw/blob/master/Adafruit_seesaw.cpp
if (touchData == 65535) {
return false;
}

// TODO: Update this should we add a capacitive moisture type to
// adafruit_sensor
rawEvent->data[0] = (float)touchData;
return true;
}

protected:
Adafruit_seesaw *_seesaw; ///< Seesaw object
};

#endif // WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor_H

0 comments on commit b4a5d0d

Please sign in to comment.