Добавила все файлы

This commit is contained in:
Lina committed 2026-02-21 21:38:42 +03:00
1 parent 1904868800
commit f29fdd4ac4
44 files changed
+3632 -1

No files matched your search

@@ -0,0 +1,245 @@
/*!
* @file Adafruit_SHT31.cpp
*
* @mainpage Adafruit SHT31 Digital Humidity & Temp Sensor
*
* @section intro_sec Introduction
*
* This is a library for the SHT31 Digital Humidity & Temp Sensor
*
* Designed specifically to work with the SHT31 Digital sensor from Adafruit
*
* Pick one up today in the adafruit shop!
* ------> https://www.adafruit.com/product/2857
*
* These sensors use I2C to communicate, 2 pins are required to interface
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*
* @section author Author
*
* Limor Fried/Ladyada (Adafruit Industries).
*
* @section license License
*
* BSD license, all text above must be included in any redistribution
*/
#include "Adafruit_SHT31.h"
/*!
* @brief SHT31 constructor using i2c
* @param *theWire
* optional wire
*/
Adafruit_SHT31::Adafruit_SHT31(TwoWire *theWire) {
_wire = theWire;
humidity = NAN;
temp = NAN;
}
/**
* Destructor to free memory in use.
*/
Adafruit_SHT31::~Adafruit_SHT31() {
if (i2c_dev) {
delete i2c_dev; // remove old interface
}
}
/**
* Initialises the I2C bus, and assigns the I2C address to us.
*
* @param i2caddr The I2C address to use for the sensor.
*
* @return True if initialisation was successful, otherwise False.
*/
bool Adafruit_SHT31::begin(uint8_t i2caddr) {
if (i2c_dev) {
delete i2c_dev; // remove old interface
}
i2c_dev = new Adafruit_I2CDevice(i2caddr, _wire);
if (!i2c_dev->begin()) {
return false;
}
reset();
return readStatus() != 0xFFFF;
}
/**
* Gets the current status register contents.
*
* @return The 16-bit status register.
*/
uint16_t Adafruit_SHT31::readStatus(void) {
writeCommand(SHT31_READSTATUS);
uint8_t data[3];
i2c_dev->read(data, 3);
uint16_t stat = data[0];
stat <<= 8;
stat |= data[1];
// Serial.println(stat, HEX);
return stat;
}
/**
* Performs a reset of the sensor to put it into a known state.
*/
void Adafruit_SHT31::reset(void) {
writeCommand(SHT31_SOFTRESET);
delay(10);
}
/**
* Enables or disabled the heating element.
*
* @param h True to enable the heater, False to disable it.
*/
void Adafruit_SHT31::heater(bool h) {
if (h)
writeCommand(SHT31_HEATEREN);
else
writeCommand(SHT31_HEATERDIS);
delay(1);
}
/*!
* @brief Return sensor heater state
* @return heater state (TRUE = enabled, FALSE = disabled)
*/
bool Adafruit_SHT31::isHeaterEnabled() {
uint16_t regValue = readStatus();
return (bool)bitRead(regValue, SHT31_REG_HEATER_BIT);
}
/**
* Gets a single temperature reading from the sensor.
*
* @return A float value indicating the temperature.
*/
float Adafruit_SHT31::readTemperature(void) {
if (!readTempHum())
return NAN;
return temp;
}
/**
* Gets a single relative humidity reading from the sensor.
*
* @return A float value representing relative humidity.
*/
float Adafruit_SHT31::readHumidity(void) {
if (!readTempHum())
return NAN;
return humidity;
}
/**
* Gets a reading of both temperature and relative humidity from the sensor.
*
* @param temperature_out Where to write the temperature float.
* @param humidity_out Where to write the relative humidity float.
* @return True if the read was successful, false otherwise
*/
bool Adafruit_SHT31::readBoth(float *temperature_out, float *humidity_out) {
if (!readTempHum()) {
*temperature_out = *humidity_out = NAN;
return false;
}
*temperature_out = temp;
*humidity_out = humidity;
return true;
}
/**
* Performs a CRC8 calculation on the supplied values.
*
* @param data Pointer to the data to use when calculating the CRC8.
* @param len The number of bytes in 'data'.
*
* @return The computed CRC8 value.
*/
static uint8_t crc8(const uint8_t *data, int len) {
/*
*
* CRC-8 formula from page 14 of SHT spec pdf
*
* Test data 0xBE, 0xEF should yield 0x92
*
* Initialization data 0xFF
* Polynomial 0x31 (x8 + x5 +x4 +1)
* Final XOR 0x00
*/
const uint8_t POLYNOMIAL(0x31);
uint8_t crc(0xFF);
for (int j = len; j; --j) {
crc ^= *data++;
for (int i = 8; i; --i) {
crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
}
}
return crc;
}
/**
* Internal function to perform a temp + humidity read.
*
* @return True if successful, otherwise false.
*/
bool Adafruit_SHT31::readTempHum(void) {
uint8_t readbuffer[6];
if (!writeCommand(SHT31_MEAS_HIGHREP))
return false;
delay(20);
if (!i2c_dev->read(readbuffer, sizeof(readbuffer)))
return false;
if (readbuffer[2] != crc8(readbuffer, 2) ||
readbuffer[5] != crc8(readbuffer + 3, 2))
return false;
int32_t stemp = (int32_t)(((uint32_t)readbuffer[0] << 8) | readbuffer[1]);
// simplified (65536 instead of 65535) integer version of:
// temp = (stemp * 175.0f) / 65535.0f - 45.0f;
stemp = ((4375 * stemp) >> 14) - 4500;
temp = (float)stemp / 100.0f;
uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4];
// simplified (65536 instead of 65535) integer version of:
// humidity = (shum * 100.0f) / 65535.0f;
shum = (625 * shum) >> 12;
humidity = (float)shum / 100.0f;
return true;
}
/**
* Internal function to perform and I2C write.
*
* @param cmd The 16-bit command ID to send.
*/
bool Adafruit_SHT31::writeCommand(uint16_t command) {
uint8_t cmd[2];
cmd[0] = command >> 8;
cmd[1] = command & 0xFF;
return i2c_dev->write(cmd, 2);
}
@@ -0,0 +1,84 @@
/*!
* @file Adafruit_SHT31.h
*
* This is a library for the SHT31 Digital Humidity & Temp Sensor
*
* Designed specifically to work with the Digital Humidity & Temp Sensor
* -----> https://www.adafruit.com/product/2857
*
* These sensors use I2C to communicate, 2 pins are required to interface
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit andopen-source hardware by purchasing products
* from Adafruit!
*
* Limor Fried/Ladyada (Adafruit Industries).
*
* BSD license, all text above must be included in any redistribution
*/
#ifndef ADAFRUIT_SHT31_H
#define ADAFRUIT_SHT31_H
#include "Arduino.h"
#include <Adafruit_I2CDevice.h>
#define SHT31_DEFAULT_ADDR 0x44 /**< SHT31 Default Address */
#define SHT31_MEAS_HIGHREP_STRETCH \
0x2C06 /**< Measurement High Repeatability with Clock Stretch Enabled */
#define SHT31_MEAS_MEDREP_STRETCH \
0x2C0D /**< Measurement Medium Repeatability with Clock Stretch Enabled */
#define SHT31_MEAS_LOWREP_STRETCH \
0x2C10 /**< Measurement Low Repeatability with Clock Stretch Enabled*/
#define SHT31_MEAS_HIGHREP \
0x2400 /**< Measurement High Repeatability with Clock Stretch Disabled */
#define SHT31_MEAS_MEDREP \
0x240B /**< Measurement Medium Repeatability with Clock Stretch Disabled */
#define SHT31_MEAS_LOWREP \
0x2416 /**< Measurement Low Repeatability with Clock Stretch Disabled */
#define SHT31_READSTATUS 0xF32D /**< Read Out of Status Register */
#define SHT31_CLEARSTATUS 0x3041 /**< Clear Status */
#define SHT31_SOFTRESET 0x30A2 /**< Soft Reset */
#define SHT31_HEATEREN 0x306D /**< Heater Enable */
#define SHT31_HEATERDIS 0x3066 /**< Heater Disable */
#define SHT31_REG_HEATER_BIT 0x0d /**< Status Register Heater Bit */
extern TwoWire Wire; /**< Forward declarations of Wire for board/variant
combinations that don't have a default 'Wire' */
/**
* Driver for the Adafruit SHT31-D Temperature and Humidity breakout board.
*/
class Adafruit_SHT31 {
public:
Adafruit_SHT31(TwoWire *theWire = &Wire);
~Adafruit_SHT31();
bool begin(uint8_t i2caddr = SHT31_DEFAULT_ADDR);
float readTemperature(void);
float readHumidity(void);
bool readBoth(float *temperature_out, float *humidity_out);
uint16_t readStatus(void);
void reset(void);
void heater(bool h);
bool isHeaterEnabled();
private:
/**
* Placeholder to track humidity internally.
*/
float humidity;
/**
* Placeholder to track temperature internally.
*/
float temp;
bool readTempHum(void);
bool writeCommand(uint16_t cmd);
TwoWire *_wire; /**< Wire object */
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
};
#endif
@@ -0,0 +1,26 @@
# Adafruit SHT31-D Temperature and Humidity Sensor Breakout [![Build Status](https://github.com/adafruit/Adafruit_SHT31/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_SHT31/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_SHT31/html/index.html)
<a href="https://www.adafruit.com/product/2857"><img src="assets/board.jpg?raw=true" width="500px"></a>
This is a library for the SHT31 Digital Humidity + Temp sensor.
It is designed specifically to work with the SHT31 Digital in the Adafruit shop:
* https://www.adafruit.com/products/2857
These sensors use **I2C** to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
Check out the links above for our tutorials and wiring diagrams
## Installation
Use the Arduino Library Manager to install this library. If you're unfamiliar
with how this works, we have a great tutorial on Arduino library installation
at: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
Binary file not shown.

After

Width:  |  Height:  |  Size: 421 KiB

@@ -0,0 +1,72 @@
/***************************************************
This is an example for the SHT31-D Humidity & Temp Sensor
Designed specifically to work with the SHT31-D sensor from Adafruit
----> https://www.adafruit.com/products/2857
These sensors use I2C to communicate, 2 pins are required to
interface
****************************************************/
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
bool enableHeater = false;
uint8_t loopCnt = 0;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(9600);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("SHT31 test");
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}
Serial.print("Heater Enabled State: ");
if (sht31.isHeaterEnabled())
Serial.println("ENABLED");
else
Serial.println("DISABLED");
}
void loop() {
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (! isnan(t)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t");
} else {
Serial.println("Failed to read temperature");
}
if (! isnan(h)) { // check if 'is not a number'
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println("Failed to read humidity");
}
delay(1000);
// Toggle heater enabled state every 30 seconds
// An ~3.0 degC temperature increase can be noted when heater is enabled
if (loopCnt >= 30) {
enableHeater = !enableHeater;
sht31.heater(enableHeater);
Serial.print("Heater Enabled State: ");
if (sht31.isHeaterEnabled())
Serial.println("ENABLED");
else
Serial.println("DISABLED");
loopCnt = 0;
}
loopCnt++;
}
@@ -0,0 +1,10 @@
name=Adafruit SHT31 Library
version=2.2.2
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for SHT31 temperature & humidity sensor.
paragraph=Arduino library for SHT31 temperature & humidity sensor.
category=Sensors
url=https://github.com/adafruit/Adafruit_SHT31
architectures=*
depends=Adafruit BusIO
@@ -0,0 +1,26 @@
Software License Agreement (BSD License)
Copyright (c) 2012, Adafruit Industries
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.