2019-06-29 08:08:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include "TempSense.h"
|
|
|
|
#include "DebugPort.h"
|
2019-09-21 23:03:42 +00:00
|
|
|
#include "macros.h"
|
2019-10-18 01:55:16 +00:00
|
|
|
#include "NVStorage.h"
|
2019-06-29 08:08:37 +00:00
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
CSensor::CSensor()
|
|
|
|
{
|
|
|
|
_reading = -100;
|
|
|
|
_holdoff = 2;
|
|
|
|
_filter.setBounds(-50, 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CSensor::update(float val)
|
|
|
|
{
|
|
|
|
if(_holdoff) {
|
|
|
|
_holdoff--;
|
|
|
|
_reading = -100;
|
|
|
|
if(_holdoff == 0)
|
|
|
|
_filter.reset(val);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_filter.update(val);
|
|
|
|
_reading = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CSensor::reset()
|
|
|
|
{
|
|
|
|
_holdoff = 2;
|
|
|
|
_reading = -100;
|
|
|
|
_filter.reset(_reading);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CSensor::getTemperature(float& tempReading, bool filtered)
|
|
|
|
{
|
|
|
|
if(_holdoff) {
|
|
|
|
tempReading = -100;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(filtered)
|
|
|
|
tempReading = _filter.getValue();
|
|
|
|
else
|
|
|
|
tempReading = _reading;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _holdoff == 0;
|
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// DS18B20 probe support
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20probe::CDS18B20probe() : CSensor()
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
pSensorInfo = NULL;
|
|
|
|
init();
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
void
|
|
|
|
CDS18B20probe::init()
|
2019-08-07 08:59:30 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
release();
|
2019-10-18 22:58:19 +00:00
|
|
|
reset();
|
2019-10-18 01:55:16 +00:00
|
|
|
error = DS18B20_ERROR_UNKNOWN;
|
|
|
|
}
|
2019-08-07 08:59:30 +00:00
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
void
|
|
|
|
CDS18B20probe::release()
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
if(pSensorInfo)
|
|
|
|
ds18b20_free(&pSensorInfo);
|
|
|
|
pSensorInfo = NULL;
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
bool
|
2019-10-18 01:55:16 +00:00
|
|
|
CDS18B20probe::readSensor()
|
2019-08-07 08:59:30 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
bool retval = false;
|
2019-10-18 22:58:19 +00:00
|
|
|
float temperature;
|
2019-08-07 08:59:30 +00:00
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
error = ds18b20_read_temp(pSensorInfo, &temperature);
|
2019-10-18 01:55:16 +00:00
|
|
|
if(error == DS18B20_OK) {
|
2019-10-18 22:58:19 +00:00
|
|
|
retval = update(temperature);
|
|
|
|
if(!retval) {
|
|
|
|
error = DS18B20_ERROR_DEVICE; // holdoff still active, avoid allowing initial readings
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-10-18 22:58:19 +00:00
|
|
|
reset();
|
2019-08-07 08:59:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2019-08-07 08:59:30 +00:00
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
float
|
|
|
|
CDS18B20probe::getReading(bool filtered)
|
|
|
|
{
|
2019-10-18 22:58:19 +00:00
|
|
|
float temperature;
|
|
|
|
CSensor::getTemperature(temperature, filtered);
|
|
|
|
return temperature;
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
2019-08-07 08:59:30 +00:00
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
OneWireBus_ROMCode
|
|
|
|
CDS18B20probe::getROMcode() const
|
|
|
|
{
|
|
|
|
if(pSensorInfo)
|
|
|
|
return pSensorInfo->rom_code;
|
|
|
|
else {
|
|
|
|
OneWireBus_ROMCode nullROM = {0};
|
|
|
|
return nullROM;
|
2019-08-07 08:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
bool
|
|
|
|
CDS18B20probe::matchROMcode(uint8_t test[8])
|
|
|
|
{
|
|
|
|
if(pSensorInfo)
|
|
|
|
return memcmp(pSensorInfo->rom_code.bytes, test, 8) == 0;
|
2019-08-07 08:59:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CDS18B20SensorSet::CDS18B20SensorSet()
|
2019-10-18 01:55:16 +00:00
|
|
|
{
|
|
|
|
_owb = NULL;
|
|
|
|
_nNumSensors = 0;
|
|
|
|
for(int i=0; i< MAX_DS18B20_DEVICES; i++)
|
|
|
|
_Sensors[i].init();
|
|
|
|
|
|
|
|
for(int i=0; i<3; i++)
|
|
|
|
_sensorMap[i] = -1;
|
2019-10-27 03:25:40 +00:00
|
|
|
|
|
|
|
_bReportFind = true;
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::begin(int pin)
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
|
|
|
// initialise DS18B20 sensor interface
|
2019-10-18 01:55:16 +00:00
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
// create one wire bus interface, using RMT peripheral
|
|
|
|
_owb = owb_rmt_initialize(&_rmt_driver_info, pin, RMT_CHANNEL_1, RMT_CHANNEL_0);
|
|
|
|
owb_use_crc(_owb, true); // enable CRC check for ROM code
|
|
|
|
|
2019-10-27 03:25:40 +00:00
|
|
|
_bReportFind = true;
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
find();
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::readSensors()
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2019-09-21 23:03:42 +00:00
|
|
|
bool retval = false;
|
|
|
|
|
|
|
|
if(_nNumSensors == 0) {
|
2019-06-29 08:08:37 +00:00
|
|
|
|
|
|
|
bool found = find();
|
|
|
|
|
|
|
|
if(found) {
|
2019-09-21 23:03:42 +00:00
|
|
|
DebugPort.println("Found DS18B20 device(s)");
|
2019-06-29 08:08:37 +00:00
|
|
|
|
|
|
|
startConvert(); // request a new conversion,
|
|
|
|
waitConvertDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
if(_nNumSensors) {
|
|
|
|
for (int i = 0; i < MAX_DS18B20_DEVICES; ++i) {
|
2019-10-18 01:55:16 +00:00
|
|
|
_Sensors[i].setError(DS18B20_ERROR_UNKNOWN);
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
2019-06-29 08:08:37 +00:00
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
for (int i = 0; i < _nNumSensors; ++i) {
|
2019-10-18 01:55:16 +00:00
|
|
|
_Sensors[i].readSensor();
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
|
|
|
|
#ifdef REPORT_READINGS
|
|
|
|
DebugPort.println("\nTemperature readings (degrees C)");
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < _nNumSensors; ++i) {
|
2019-10-18 01:55:16 +00:00
|
|
|
if(_Sensors[i].OK()) {
|
2019-09-21 23:03:42 +00:00
|
|
|
#ifdef REPORT_READINGS
|
|
|
|
DebugPort.printf(" %d: %.1f OK\r\n", i, _Readings[i]);
|
|
|
|
#endif
|
|
|
|
retval = true; // at least one sensor read OK
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef REPORT_READINGS
|
|
|
|
DebugPort.printf("\007 %d: DS18B20 sensor removed?\r\n", i);
|
|
|
|
#endif
|
|
|
|
}
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
return retval;
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::find()
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
|
|
|
// Find all connected devices
|
2019-10-27 03:25:40 +00:00
|
|
|
if(_bReportFind)
|
|
|
|
DebugPort.println("Finding one wire bus devices...");
|
2019-10-18 01:55:16 +00:00
|
|
|
OneWireBus_ROMCode rom_codes[MAX_DS18B20_DEVICES];
|
|
|
|
memset(&rom_codes, 0, sizeof(rom_codes));
|
2019-06-29 08:08:37 +00:00
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
_nNumSensors = 0;
|
|
|
|
OneWireBus_SearchState search_state = {0};
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
owb_search_first(_owb, &search_state, &found);
|
|
|
|
while(found) {
|
|
|
|
char rom_code_s[17];
|
|
|
|
owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
|
2019-10-27 03:25:40 +00:00
|
|
|
if(_bReportFind)
|
|
|
|
DebugPort.printf(" %d : %s\r\n", _nNumSensors, rom_code_s);
|
2019-09-21 23:03:42 +00:00
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
rom_codes[_nNumSensors] = search_state.rom_code;
|
2019-09-21 23:03:42 +00:00
|
|
|
_nNumSensors++;
|
|
|
|
owb_search_next(_owb, &search_state, &found);
|
|
|
|
}
|
2019-10-27 03:25:40 +00:00
|
|
|
if(_bReportFind)
|
|
|
|
DebugPort.printf("Found %d DS18B20 device%s\r\n", _nNumSensors, _nNumSensors==1 ? "" : "s");
|
2019-09-21 23:03:42 +00:00
|
|
|
|
|
|
|
// Create DS18B20 devices on the 1-Wire bus
|
|
|
|
for (int i = 0; i < MAX_DS18B20_DEVICES; ++i) {
|
2019-10-18 01:55:16 +00:00
|
|
|
_Sensors[i].release();
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < _nNumSensors; ++i)
|
|
|
|
{
|
|
|
|
DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
|
2019-10-18 01:55:16 +00:00
|
|
|
_Sensors[i].assign(ds18b20_info);
|
2019-09-21 23:03:42 +00:00
|
|
|
|
|
|
|
if (_nNumSensors == 1)
|
|
|
|
{
|
2019-10-27 03:25:40 +00:00
|
|
|
DebugPort.print("DS18B20 Single device optimisations enabled\n");
|
2019-10-18 01:55:16 +00:00
|
|
|
ds18b20_init_solo(ds18b20_info, _owb); // only one device on bus
|
|
|
|
ds18b20_info->rom_code = rom_codes[0]; // added, for GUI setup!!
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
ds18b20_init(ds18b20_info, _owb, rom_codes[i]); // associate with bus and device
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings
|
|
|
|
ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION_12_BIT);
|
|
|
|
}
|
|
|
|
|
2019-10-27 03:25:40 +00:00
|
|
|
_bReportFind = false;
|
|
|
|
|
2019-09-21 23:03:42 +00:00
|
|
|
return found;
|
|
|
|
}
|
2019-06-29 08:08:37 +00:00
|
|
|
|
|
|
|
void
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::startConvert()
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
|
|
|
// kick off the initial temperature conversion
|
2019-10-18 01:55:16 +00:00
|
|
|
if(_Sensors[0].getSensorInfo())
|
2019-09-21 23:03:42 +00:00
|
|
|
ds18b20_convert_all(_owb);
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::waitConvertDone()
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
if(_Sensors[0].getSensorInfo())
|
|
|
|
ds18b20_wait_for_conversion(_Sensors[0].getSensorInfo());
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::checkNumSensors() const
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
|
|
|
long start = millis();
|
|
|
|
bool found = false;
|
|
|
|
int numSensors = 0;
|
|
|
|
OneWireBus_SearchState search_state = {0};
|
|
|
|
owb_search_first(_owb, &search_state, &found);
|
|
|
|
while(found) {
|
|
|
|
numSensors++;
|
|
|
|
owb_search_next(_owb, &search_state, &found);
|
|
|
|
}
|
|
|
|
DebugPort.printf("Found %d one-wire device%s\r\n", numSensors, numSensors==1 ? "" : "s");
|
|
|
|
long tDelta = millis() - start;
|
|
|
|
DebugPort.printf("checkNumSensors: %ldms\r\n", tDelta);
|
|
|
|
return numSensors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::mapSensor(int idx, OneWireBus_ROMCode romCode)
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
|
|
|
if(idx == -1) {
|
|
|
|
_sensorMap[0] = _sensorMap[1] = _sensorMap[2] = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(idx == -2) {
|
|
|
|
DebugPort.printf("Sensor Map: %d %d %d\r\n",
|
|
|
|
_sensorMap[0], _sensorMap[1], _sensorMap[2]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!INBOUNDS(idx, 0, 2))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(int i = 0; i < _nNumSensors; i++) {
|
2019-10-18 01:55:16 +00:00
|
|
|
if(_Sensors[i].matchROMcode(romCode.bytes)) {
|
2019-09-21 23:03:42 +00:00
|
|
|
_sensorMap[idx] = i;
|
|
|
|
DebugPort.printf("Mapped DS18B20 %02X:%02X:%02X:%02X:%02X:%02X as role %d\r\n",
|
2019-09-22 04:46:39 +00:00
|
|
|
romCode.fields.serial_number[5],
|
|
|
|
romCode.fields.serial_number[4],
|
|
|
|
romCode.fields.serial_number[3],
|
|
|
|
romCode.fields.serial_number[2],
|
|
|
|
romCode.fields.serial_number[1],
|
|
|
|
romCode.fields.serial_number[0],
|
|
|
|
idx
|
|
|
|
);
|
2019-09-21 23:03:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::getTemperature(int usrIdx, float& temperature, bool filtered)
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
int snsIdx = _sensorMap[usrIdx];
|
2019-09-22 04:46:39 +00:00
|
|
|
if(snsIdx < 0)
|
|
|
|
snsIdx = 0; // default to sensor 0 if not mapped
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
return getTemperatureIdx(snsIdx, temperature, filtered);
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-09-21 23:03:42 +00:00
|
|
|
|
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::getTemperatureIdx(int snsIdx, float& temperature, bool filtered)
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
2019-10-18 22:58:19 +00:00
|
|
|
return _Sensors[snsIdx].getTemperature(temperature, filtered);
|
2019-09-21 23:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-18 22:58:19 +00:00
|
|
|
CDS18B20SensorSet::getRomCodeIdx(int snsIdx, OneWireBus_ROMCode& romCode) const
|
2019-09-21 23:03:42 +00:00
|
|
|
{
|
2019-09-22 04:46:39 +00:00
|
|
|
if(snsIdx >= _nNumSensors)
|
2019-09-21 23:03:42 +00:00
|
|
|
return false;
|
2019-10-18 01:55:16 +00:00
|
|
|
romCode = _Sensors[snsIdx].getROMcode();
|
2019-09-21 23:03:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// BME-280 probe support
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
CBME280Sensor::CBME280Sensor() : CSensor()
|
2019-10-18 01:55:16 +00:00
|
|
|
{
|
|
|
|
_count = 0;
|
|
|
|
}
|
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
bool
|
2019-10-18 01:55:16 +00:00
|
|
|
CBME280Sensor::begin(int ID)
|
|
|
|
{
|
|
|
|
_count = 0;
|
|
|
|
bool status = _bme.begin(ID);
|
|
|
|
if (!status) {
|
|
|
|
DebugPort.println("Could not find a valid BME280 sensor, check wiring!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_count = 1;
|
|
|
|
_bme.setSampling(Adafruit_BME280::MODE_FORCED,
|
|
|
|
Adafruit_BME280::SAMPLING_X1,
|
|
|
|
Adafruit_BME280::SAMPLING_X1,
|
|
|
|
Adafruit_BME280::SAMPLING_X1,
|
|
|
|
Adafruit_BME280::FILTER_OFF,
|
|
|
|
Adafruit_BME280::STANDBY_MS_1000);
|
|
|
|
|
|
|
|
_lastSampleTime = millis();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CBME280Sensor::getTemperature(float& tempReading, bool filtered)
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
if(_count == 0) {
|
2019-10-18 22:58:19 +00:00
|
|
|
reset();
|
|
|
|
CSensor::getTemperature(tempReading, filtered);
|
2019-10-18 01:55:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-29 08:08:37 +00:00
|
|
|
|
2020-06-19 01:24:44 +00:00
|
|
|
/* long tDelta = millis() - _lastSampleTime;
|
2019-10-18 01:55:16 +00:00
|
|
|
if(tDelta >= 0) {
|
|
|
|
_bme.takeForcedMeasurement();
|
2019-10-18 22:58:19 +00:00
|
|
|
float temperature = _bme.readTemperature();
|
|
|
|
update(temperature);
|
2019-10-18 01:55:16 +00:00
|
|
|
_lastSampleTime = millis() + 1000;
|
2020-04-26 06:15:08 +00:00
|
|
|
DebugPort.println("Forced BME sensor reading");
|
2020-06-19 01:24:44 +00:00
|
|
|
}*/
|
2019-10-18 22:58:19 +00:00
|
|
|
|
|
|
|
CSensor::getTemperature(tempReading, filtered);
|
2020-03-23 05:54:15 +00:00
|
|
|
// tempReading += NVstore.getHeaterTuning().BME280probe.offset;;
|
2019-10-18 01:55:16 +00:00
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:48:32 +00:00
|
|
|
bool
|
2020-04-24 23:23:16 +00:00
|
|
|
CBME280Sensor::getAltitude(float& reading, bool fresh)
|
2020-01-13 08:48:32 +00:00
|
|
|
{
|
2020-04-24 23:23:16 +00:00
|
|
|
if(fresh) {
|
|
|
|
_fAltitude = _bme.readAltitude(1013.25); //use standard atmosphere as reference
|
|
|
|
}
|
|
|
|
reading = _fAltitude;
|
2020-01-13 08:48:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-24 23:23:16 +00:00
|
|
|
CBME280Sensor::getHumidity(float& reading, bool fresh)
|
2020-01-13 08:48:32 +00:00
|
|
|
{
|
2020-04-24 23:23:16 +00:00
|
|
|
if(fresh) {
|
|
|
|
_fHumidity = _bme.readHumidity();
|
|
|
|
}
|
|
|
|
reading = _fHumidity;
|
2020-01-13 08:48:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:15:08 +00:00
|
|
|
int
|
|
|
|
CBME280Sensor::getAllReadings(bme280_readings& readings)
|
|
|
|
{
|
2020-06-19 01:24:44 +00:00
|
|
|
_bme.takeForcedMeasurement();
|
2020-04-26 06:15:08 +00:00
|
|
|
int retval = _bme.readAll(readings);
|
|
|
|
_fAltitude = readings.altitude;
|
|
|
|
_fHumidity = readings.humidity;
|
|
|
|
update(readings.temperature);
|
|
|
|
|
2020-06-19 01:24:44 +00:00
|
|
|
/* _bme.takeForcedMeasurement();
|
|
|
|
readings.temperature = _bme.readTemperature();
|
|
|
|
update(readings.temperature);
|
|
|
|
_fAltitude = readings.altitude = _bme.readAltitude(1013.25);
|
|
|
|
_fHumidity =readings.humidity = _bme.readHumidity();
|
|
|
|
int retval = 0x07; // temperature read OK*/
|
|
|
|
|
2020-04-26 06:15:08 +00:00
|
|
|
_lastSampleTime = millis() + 1000;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
const char*
|
|
|
|
CBME280Sensor::getID()
|
|
|
|
{
|
|
|
|
return "BME-280";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CTempSense::CTempSense()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTempSense::begin(int oneWirePin, int I2CID)
|
|
|
|
{
|
|
|
|
DS18B20.begin(oneWirePin);
|
|
|
|
BME280.begin(I2CID);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CTempSense::getNumSensors() const
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
retval += DS18B20.getNumSensors();
|
|
|
|
retval += BME280.getCount();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-06-29 08:08:37 +00:00
|
|
|
void
|
|
|
|
CTempSense::startConvert()
|
|
|
|
{
|
2019-10-18 01:55:16 +00:00
|
|
|
DS18B20.startConvert();
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
bool
|
|
|
|
CTempSense::readSensors()
|
|
|
|
{
|
2020-04-26 06:15:08 +00:00
|
|
|
bme280_readings readings;
|
|
|
|
getBME280().getAllReadings(readings);
|
2020-04-24 23:23:16 +00:00
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
return DS18B20.readSensors();
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
CTempSense::getOffset(int usrIdx)
|
|
|
|
{
|
2019-10-18 22:58:19 +00:00
|
|
|
switch(getSensorType(usrIdx)) {
|
|
|
|
case 0: // BME280
|
2019-10-18 01:55:16 +00:00
|
|
|
return NVstore.getHeaterTuning().BME280probe.offset;
|
2019-10-18 22:58:19 +00:00
|
|
|
case 2: // DS18B20 - AFTER BME280 - bump index down
|
|
|
|
usrIdx--;
|
|
|
|
case 1: // DS18B20
|
|
|
|
if(INBOUNDS(usrIdx, 0, 2)) {
|
|
|
|
return NVstore.getHeaterTuning().DS18B20probe[usrIdx].offset;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
2019-10-18 22:58:19 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTempSense::setOffset(int usrIdx, float offset)
|
|
|
|
{
|
|
|
|
if(!INBOUNDS(offset, -10.0, +10.0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sHeaterTuning ht = NVstore.getHeaterTuning();
|
|
|
|
|
|
|
|
if(BME280.getCount() == 0) {
|
|
|
|
// no BME280 present - simply apply to DS18B20 list
|
|
|
|
if(INBOUNDS(usrIdx, 0, 2))
|
|
|
|
ht.DS18B20probe[usrIdx].offset = offset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// BME280 present
|
|
|
|
// need to change behvaiour depending if the BME is primary
|
|
|
|
if(NVstore.getHeaterTuning().BME280probe.bPrimary) {
|
|
|
|
// BME is primary - usrIdx 0 is BME, 1 is first DS18B20
|
|
|
|
if(usrIdx == 0) {
|
|
|
|
ht.BME280probe.offset = offset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
usrIdx--;
|
|
|
|
if(INBOUNDS(usrIdx, 0, 2))
|
|
|
|
ht.DS18B20probe[usrIdx].offset = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// BME is after DS18B20s
|
|
|
|
// note the index for the BME depends upon how many DS18B20s are connected!
|
|
|
|
if(usrIdx >= DS18B20.getNumSensors()) {
|
|
|
|
// assume any more than connected DS18B20 is the BME
|
|
|
|
ht.BME280probe.offset = offset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(INBOUNDS(usrIdx, 0, 2))
|
|
|
|
ht.DS18B20probe[usrIdx].offset = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NVstore.setHeaterTuning(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTempSense::getTemperature(int usrIdx, float& temperature, bool filtered)
|
2019-06-29 08:08:37 +00:00
|
|
|
{
|
2020-03-23 05:54:15 +00:00
|
|
|
bool bRetVal = false;
|
|
|
|
float offset = 0;
|
2019-10-18 22:58:19 +00:00
|
|
|
switch(getSensorType(usrIdx)) {
|
|
|
|
case 0:
|
2020-03-23 05:54:15 +00:00
|
|
|
bRetVal = BME280.getTemperature(temperature, filtered);
|
|
|
|
offset = getOffset(usrIdx);
|
|
|
|
break;
|
2019-10-18 22:58:19 +00:00
|
|
|
case 1:
|
2020-03-23 05:54:15 +00:00
|
|
|
bRetVal = DS18B20.getTemperature(usrIdx, temperature, filtered);
|
|
|
|
offset = getOffset(usrIdx);
|
|
|
|
break;
|
2019-10-18 22:58:19 +00:00
|
|
|
case 2:
|
2020-03-23 05:54:15 +00:00
|
|
|
bRetVal = DS18B20.getTemperature(usrIdx-1, temperature, filtered);
|
|
|
|
offset = getOffset(usrIdx-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(bRetVal) {
|
|
|
|
temperature += offset;
|
2019-10-18 22:58:19 +00:00
|
|
|
}
|
2020-03-23 05:54:15 +00:00
|
|
|
return bRetVal;
|
2019-10-18 22:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CTempSense::getSensorType(int usrIdx)
|
|
|
|
{
|
|
|
|
if(BME280.getCount() == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(NVstore.getHeaterTuning().BME280probe.bPrimary) {
|
|
|
|
if(usrIdx == 0)
|
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(usrIdx >= DS18B20.getNumSensors()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2019-10-18 01:55:16 +00:00
|
|
|
}
|
2019-06-29 08:08:37 +00:00
|
|
|
}
|
2019-10-18 01:55:16 +00:00
|
|
|
|
2019-10-18 22:58:19 +00:00
|
|
|
const char*
|
|
|
|
CTempSense::getID(int usrIdx)
|
|
|
|
{
|
|
|
|
if(getSensorType(usrIdx))
|
|
|
|
return "DS18B20";
|
|
|
|
else
|
|
|
|
return "BME280";
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTempSense::format(char* msg, float fTemp)
|
|
|
|
{
|
|
|
|
if(NVstore.getUserSettings().degF) {
|
|
|
|
fTemp = fTemp * 9 / 5 + 32;
|
|
|
|
sprintf(msg, "%.1f`F", fTemp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(msg, "%.1f`C", fTemp);
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 08:48:32 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
CTempSense::getTemperatureBME280(float& reading)
|
|
|
|
{
|
|
|
|
return BME280.getTemperature(reading, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-24 23:23:16 +00:00
|
|
|
CTempSense::getAltitude(float& reading, bool fresh)
|
2020-01-13 08:48:32 +00:00
|
|
|
{
|
2020-05-07 00:12:20 +00:00
|
|
|
if(BME280.getCount()) {
|
2020-04-24 23:23:16 +00:00
|
|
|
return BME280.getAltitude(reading, fresh);
|
2020-05-07 00:12:20 +00:00
|
|
|
}
|
2020-01-13 08:48:32 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-24 23:23:16 +00:00
|
|
|
CTempSense::getHumidity(float& reading, bool fresh)
|
2020-01-13 08:48:32 +00:00
|
|
|
{
|
|
|
|
if(BME280.getCount())
|
2020-04-24 23:23:16 +00:00
|
|
|
return BME280.getHumidity(reading, fresh);
|
2020-01-13 08:48:32 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|