Add SHT21 Support

This commit is contained in:
seiichiro 2020-12-04 18:24:13 +01:00
parent ae5410871a
commit 0da96c0dd1
5 changed files with 139 additions and 5 deletions

View file

@ -6,7 +6,7 @@ THIS IS STILL WORK IN PROGRESS!
## Configuration and Programming
This is the Work in Progress Repository for ATTNode v3 compatible firmware. At the moment it supports LoRa communication using OTAA and a BME280 sensor, as well as deep sleep between measurements.
This is the Work in Progress Repository for ATTNode v3 compatible firmware. At the moment it supports LoRa communication using OTAA and a BME280 or SHT21 sensor, as well as deep sleep between measurements.
As there is no PlatformIO Support for the ATTiny3216 yet, it is (for now) developed using Arduino IDE and the [MegaTinyCore](https://github.com/SpenceKonde/megaTinyCore). You also need to set the correct Settings for programming the ATTiny3216 in ArduionIDE. Here is a screenshot of the settings I use:

61
firmware/SHT21.cpp Normal file
View file

@ -0,0 +1,61 @@
/*
SHT21.cpp - SHT21 Sensor Library
Copyright (c) 2019-2020, Stefan Brand
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 holder 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 AND CONTRIBUTORS "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 OR CONTRIBUTORS 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.
*/
#include <Arduino.h>
#include <inttypes.h>
#include <Wire.h>
#include "SHT21.h"
SHT21::SHT21() {}
uint16_t SHT21::sensorRead(uint8_t command) {
uint8_t d = 85; // Delay after starting Measurement
if(command == SHT21_TEMPHOLD || command == SHT21_TEMPNOHOLD) d = 85;
if(command == SHT21_HUMIHOLD || command == SHT21_HUMINOHOLD) d = 30;
// Trigger Measurement
Wire.beginTransmission(SHT21_I2CADDR);
Wire.write(command);
Wire.endTransmission();
// Wait for Measurement
delay(d);
// Read and Return value
Wire.requestFrom(SHT21_I2CADDR, 2);
uint16_t result = (Wire.read() << 8) | Wire.read();
result &= ~0x0003; // Clear CRC Bits
return result;
}
float SHT21::getTemperature(void) {
return (-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)));
}
float SHT21::getHumidity(void) {
return (-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)));
}

52
firmware/SHT21.h Normal file
View file

@ -0,0 +1,52 @@
/*
SHT21.cpp - SHT21 Sensor Library
Copyright (c) 2019-2020, Stefan Brand
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 holder 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 AND CONTRIBUTORS "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 OR CONTRIBUTORS 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.
*/
#ifndef SHT21_H
#define SHT21_H
#include <inttypes.h>
#define SHT21_I2CADDR 0x40
#define SHT21_TEMPHOLD 0xE3
#define SHT21_TEMPNOHOLD 0xF3
#define SHT21_HUMIHOLD 0xE5
#define SHT21_HUMINOHOLD 0xF5
#define SHT21_SOFTRESET 0xFE
class SHT21
{
private:
uint16_t sensorRead(uint8_t command);
public:
SHT21(void);
float getTemperature(void);
float getHumidity(void);
};
#endif

View file

@ -4,6 +4,8 @@
// Define which Sensor is installed
#define HAS_BME280
// #define HAS_SHT21
// #define HAS_NO_SENSOR
// How many Minutes to sleep between Measuring/Sending
// Actual Sleep Time is SLEEP_TIME*2*32 Seconds due to the 32s sleep intervals of the ATTiny3216

View file

@ -16,6 +16,11 @@
BME280 sensor;
#endif
#ifdef HAS_SHT21
#include "SHT21.h"
SHT21 sensor;
#endif
// Define some LMIC Callbacks and Variables
void os_getArtEui (u1_t* buf) {
memcpy_P(buf, APPEUI, 8);
@ -111,7 +116,17 @@ void blink(uint8_t num) {
// All Sensor Code and Data Preparation goes here
void do_send(osjob_t* j) {
// Prepare LoRa Data Packet
#ifdef HAS_BME280
#ifdef HAS_NO_SENSOR
struct lora_data {
uint8_t bat;
} __attribute ((packed)) data;
#elif defined HAS_SHT21
struct lora_data {
uint8_t bat;
int32_t temperature;
int32_t humidity;
} __attribute__ ((packed)) data;
#elif defined HAS_BME280
struct lora_data {
uint8_t bat;
int32_t temperature;
@ -121,7 +136,7 @@ void do_send(osjob_t* j) {
#endif
if (LMIC.opmode & OP_TXRXPEND) {
delay(10);
delay(1);
} else {
// Add Battery Voltage (0.2V Accuracy stored in 1 byte)
uint32_t batv = readSupplyVoltage();
@ -129,7 +144,11 @@ void do_send(osjob_t* j) {
if (batv % 20 > 9)
data.bat += 1;
#ifdef HAS_BME280
// Take Measurements depending on Sensor
#ifdef HAS_SHT21
data.temperature = (int32_t)(sensor.getTemperature()*100);
data.humidity = (int32_t)(sensor.getHumidity()*100);
#elif defined HAS_BME280
sensor.getData(&data.temperature, &data.pressure, &data.humidity);
#endif