ds3231 temperature sensor (#17778)

This commit is contained in:
Barbudor 2023-01-24 08:52:43 +01:00 committed by GitHub
parent 4297fa3f3e
commit db0532de5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -718,6 +718,7 @@
// #define USE_RTC_CHIPS // Enable RTC chip support and NTP server - Select only one // #define USE_RTC_CHIPS // Enable RTC chip support and NTP server - Select only one
// #define USE_DS3231 // [I2cDriver26] Enable DS3231 RTC (I2C address 0x68) (+1k2 code) // #define USE_DS3231 // [I2cDriver26] Enable DS3231 RTC (I2C address 0x68) (+1k2 code)
// #define DS3231_ENABLE_TEMP // In DS3231 driver, enable the internal temperature sensor
// #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code) // #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code)
// #define USE_PCF85363 // [I2cDriver66] Enable PCF85363 RTC - found Shelly 3EM (I2C address 0x51) (+0k7 code) // #define USE_PCF85363 // [I2cDriver66] Enable PCF85363 RTC - found Shelly 3EM (I2C address 0x51) (+0k7 code)

View File

@ -23,6 +23,7 @@ struct {
void (* SetTime)(uint32_t); void (* SetTime)(uint32_t);
int32_t (* MemRead)(uint8_t *, uint32_t); int32_t (* MemRead)(uint8_t *, uint32_t);
int32_t (* MemWrite)(uint8_t *, uint32_t); int32_t (* MemWrite)(uint8_t *, uint32_t);
void (* ShowSensor)(bool);
bool detected; bool detected;
int8_t mem_size = -1; int8_t mem_size = -1;
uint8_t address; uint8_t address;
@ -51,6 +52,8 @@ struct {
#define DS3231_YEAR 0x06 #define DS3231_YEAR 0x06
#define DS3231_CONTROL 0x0E #define DS3231_CONTROL 0x0E
#define DS3231_STATUS 0x0F #define DS3231_STATUS 0x0F
#define DS3231_TEMP_MSB 0x11
#define DS3231_TEMP_LSB 0x12
// Control register bits // Control register bits
#define DS3231_OSF 7 #define DS3231_OSF 7
@ -81,6 +84,47 @@ uint32_t DS3231ReadTime(void) {
return MakeTime(tm); return MakeTime(tm);
} }
/*-------------------------------------------------------------------------------------------*\
* Read temperature from DS3231 internal sensor, return as float
\*-------------------------------------------------------------------------------------------*/
#ifdef DS3231_ENABLE_TEMP
float DS3231ReadTemp(void) {
int16_t temp_reg = I2cReadS16(RtcChip.address, DS3231_TEMP_MSB) >> 6;
float temp = temp_reg * 0.25;
//AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("RTC: DS3231 temp_reg=%d"), temp_reg);
return temp;
}
#endif // #ifdef DS3231_ENABLE_TEMP
/*-------------------------------------------------------------------------------------------*\
* Show temperature from DS3231 internal sensor, Web or SENSOR
\*-------------------------------------------------------------------------------------------*/
#ifdef DS3231_ENABLE_TEMP
void D3231ShowSensor(bool json) {
float f_temperature = ConvertTemp(DS3231ReadTemp());
if (json) {
ResponseAppend_P(PSTR(",\"DS3231\":{\"" D_JSON_TEMPERATURE "\":%*_f}"), Settings->flag2.temperature_resolution, &f_temperature);
#ifdef USE_DOMOTICZ
if (0 == TasmotaGlobal.tele_period) {
DomoticzFloatSensor(DZ_TEMP, f_temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
if (0 == TasmotaGlobal.tele_period) {
KnxSensor(KNX_TEMPERATURE, f_temperature);
}
#endif // USE_KNX
}
#ifdef USE_WEBSERVER
else {
WSContentSend_Temp("DS3231", f_temperature);
}
#endif // #ifdef USE_WEBSERVER
}
#endif // #ifdef DS3231_ENABLE_TEMP
/*-------------------------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------------------------*\
* Get time as TIME_T and set the DS3231 time to this value * Get time as TIME_T and set the DS3231 time to this value
\*-------------------------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------------------------*/
@ -109,6 +153,9 @@ void DS3231Detected(void) {
strcpy_P(RtcChip.name, PSTR("DS3231")); strcpy_P(RtcChip.name, PSTR("DS3231"));
RtcChip.ReadTime = &DS3231ReadTime; RtcChip.ReadTime = &DS3231ReadTime;
RtcChip.SetTime = &DS3231SetTime; RtcChip.SetTime = &DS3231SetTime;
#ifdef DS3231_ENABLE_TEMP
RtcChip.ShowSensor = &D3231ShowSensor;
#endif
RtcChip.mem_size = -1; RtcChip.mem_size = -1;
} }
} }
@ -456,6 +503,12 @@ bool Xdrv56(uint32_t function) {
case FUNC_TIME_SYNCED: case FUNC_TIME_SYNCED:
RtcChipTimeSynced(); RtcChipTimeSynced();
break; break;
case FUNC_WEB_SENSOR:
if (RtcChip.ShowSensor) RtcChip.ShowSensor(0);
break;
case FUNC_JSON_APPEND:
if (RtcChip.ShowSensor) RtcChip.ShowSensor(1);
break;
} }
} }