Skip to content

Commit

Permalink
Merge pull request #296 from brentru/add-sht40
Browse files Browse the repository at this point in the history
Add SHT4X Support
  • Loading branch information
brentru authored Aug 17, 2022
2 parents 5021d47 + 6a11595 commit a54f61f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 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.42
version=1.0.0-beta.43
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, Adafruit MCP9600 Library, Adafruit TSL2591 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, Adafruit TSL2591 Library, Adafruit SHT4x Library
25 changes: 20 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,37 @@ default_envs = featheresp32v2, featheresp32s2
framework = arduino
monitor_speed = 115200
lib_compat_mode = strict
lib_deps =
adafruit/Adafruit TinyUSB Library@^1.10.3
lib_extra_dirs = ~/Documents/Arduino/libraries


; WipperSnapper local build environment for Adafruit Feather ESP32 v2
[env:featheresp32v2]
platform = https://github.com/platformio/platform-espressif32.git
monitor_filters = time, esp32_exception_decoder, colorize
board = adafruit_feather_esp32_v2
build_flags = -D ARDUINO_ADAFRUIT_FEATHER_ESP32_V2
upload_port = /dev/cu.usbserial-10
monitor_port = /dev/cu.usbserial-10
upload_port = /dev/cu.usbserial-11330
monitor_port = /dev/cu.usbserial-11330
lib_deps = adafruit/Adafruit TinyUSB Library@^1.10.3

; WipperSnapper local build environment for Adafruit Feather ESP32-S2
[env:featheresp32s2]
platform = espressif32
board = featheresp32-s2
upload_port = /dev/cu.usbmodem01
monitor_port = /dev/cu.usbmodem1330
build_flags = -D ARDUINO_ADAFRUIT_FEATHER_ESP32S2
monitor_filters = time, esp32_exception_decoder, colorize
monitor_filters = time, esp32_exception_decoder, colorize
extra_scripts =
pre:rename_usb_config.py
lib_deps = adafruit/Adafruit TinyUSB Library@^1.10.3

; WipperSnapper local build environment for Adafruit Feather ESP8266
[env:featheresp8266]
platform = https://github.com/platformio/platform-espressif8266.git
board = huzzah
build_flags = -D ARDUINO_ESP8266_ADAFRUIT_HUZZAH
lib_ignore = Adafruit TinyUSB Library
build_src_filter = +<*> -<../.pio/libdeps/featheresp8266/Adafruit TinyUSB Library>
upload_port = /dev/cu.usbserial-11330
monitor_port = /dev/cu.usbserial-11330
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.42" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.43" ///< 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 @@ -293,6 +293,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_scd40->configureDriver(msgDeviceInitReq);
drivers.push_back(_scd40);
WS_DEBUG_PRINTLN("SCD40 Initialized Successfully!");
} else if (strcmp("SHT40", msgDeviceInitReq->i2c_device_name) == 0) {
_sht4x = new WipperSnapper_I2C_Driver_SHT4X(this->_i2c, i2cAddress);
if (!_sht4x->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize sht4x!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_sht4x->configureDriver(msgDeviceInitReq);
drivers.push_back(_sht4x);
WS_DEBUG_PRINTLN("SHT4X 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 @@ -27,6 +27,7 @@
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#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_TSL2591.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
Expand Down Expand Up @@ -80,6 +81,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_MCP9601 *_mcp9601 = nullptr;
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;
WipperSnapper_I2C_Driver_SCD40 *_scd40 = nullptr;
WipperSnapper_I2C_Driver_SHT4X *_sht4x = nullptr;
};
extern Wippersnapper WS;

Expand Down
103 changes: 103 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_SHT4X.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*!
* @file WipperSnapper_I2C_Driver_SHT4X.h
*
* Device driver for the SHT4X Temperature and Humidity 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) Marni Brewster 2022 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_SHT4X_H
#define WipperSnapper_I2C_Driver_SHT4X_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_SHT4x.h>
#include <Wire.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for the SHT4X sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_SHT4X : public WipperSnapper_I2C_Driver {

public:
/*******************************************************************************/
/*!
@brief Constructor for a SHT4X sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_SHT4X(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Initializes the SHT4X sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_sht4x = new Adafruit_SHT4x();
if (!_sht4x->begin(_i2c))
return false;

// Use HIGH PRECISION
_sht4x->setPrecision(SHT4X_HIGH_PRECISION);
// default, NO HEATER
_sht4x->setHeater(SHT4X_NO_HEATER);

return true;
}

/*******************************************************************************/
/*!
@brief Gets the SHT4X'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) {
sensors_event_t humidityEvent;
// populate temp and humidity objects with fresh data
if (!_sht4x->getEvent(&humidityEvent, tempEvent))
return false;
return true;
}

/*******************************************************************************/
/*!
@brief Gets the SHT4X's current relative humidity reading.
@param humidEvent
Pointer to an Adafruit_Sensor event.
@returns True if the humidity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
sensors_event_t tempEvent;
// populate temp and humidity objects with fresh data
if (!_sht4x->getEvent(humidEvent, &tempEvent))
return false;
return true;
}

protected:
Adafruit_SHT4x *_sht4x; ///< SHT4X object
};

#endif // WipperSnapper_I2C_Driver_SHT4X

0 comments on commit a54f61f

Please sign in to comment.