2019-06-14 14:51:00 +01:00
|
|
|
/*
|
|
|
|
xsns_46_MLX90614.ino - Support for MLX ir temperature sensor
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Gerhard Mutz and Theo Arends
|
2019-06-14 14:51:00 +01:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:43:23 +01:00
|
|
|
#ifdef USE_I2C
|
2019-06-14 14:51:00 +01:00
|
|
|
#ifdef USE_MLX90614
|
|
|
|
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XSNS_46 46
|
|
|
|
#define XI2C_32 32 // See I2CDEVICES.md
|
2019-06-14 14:51:00 +01:00
|
|
|
|
|
|
|
#define I2_ADR_IRT 0x5a
|
|
|
|
|
2019-11-10 16:02:02 +00:00
|
|
|
#define MLX90614_RAWIR1 0x04
|
|
|
|
#define MLX90614_RAWIR2 0x05
|
|
|
|
#define MLX90614_TA 0x06
|
|
|
|
#define MLX90614_TOBJ1 0x07
|
|
|
|
#define MLX90614_TOBJ2 0x08
|
|
|
|
|
2019-11-23 17:55:48 +00:00
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
uint16_t value;
|
|
|
|
uint32_t i2c_buf;
|
|
|
|
};
|
|
|
|
float obj_temp;
|
|
|
|
float amb_temp;
|
|
|
|
bool ready = false;
|
|
|
|
} mlx90614;
|
2019-06-14 14:51:00 +01:00
|
|
|
|
2019-11-07 15:56:05 +00:00
|
|
|
void MLX90614_Init(void)
|
|
|
|
{
|
|
|
|
if (!I2cSetDevice(I2_ADR_IRT)) { return; }
|
2019-11-10 16:02:02 +00:00
|
|
|
I2cSetActiveFound(I2_ADR_IRT, "MLX90614");
|
2019-11-23 17:55:48 +00:00
|
|
|
mlx90614.ready = true;
|
2019-06-14 14:51:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-10 16:02:02 +00:00
|
|
|
void MLX90614_Every_Second(void)
|
|
|
|
{
|
2021-03-30 07:37:50 +01:00
|
|
|
//mlx90614.i2c_buf = I2cRead24(I2_ADR_IRT, MLX90614_TOBJ1);
|
|
|
|
mlx90614.value = MLX90614_read16(I2_ADR_IRT, MLX90614_TOBJ1);
|
2019-11-23 17:55:48 +00:00
|
|
|
if (mlx90614.value & 0x8000) {
|
|
|
|
mlx90614.obj_temp = -999;
|
|
|
|
} else {
|
|
|
|
mlx90614.obj_temp = ((float)mlx90614.value * 0.02) - 273.15;
|
|
|
|
}
|
2021-03-30 07:37:50 +01:00
|
|
|
//mlx90614.i2c_buf = I2cRead24(I2_ADR_IRT,MLX90614_TA);
|
|
|
|
mlx90614.value = MLX90614_read16(I2_ADR_IRT, MLX90614_TA);
|
2019-11-23 17:55:48 +00:00
|
|
|
if (mlx90614.value & 0x8000) {
|
|
|
|
mlx90614.amb_temp = -999;
|
|
|
|
} else {
|
|
|
|
mlx90614.amb_temp = ((float)mlx90614.value * 0.02) - 273.15;
|
|
|
|
}
|
2019-06-14 14:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_IRTMP[] PROGMEM =
|
|
|
|
"{s}MXL90614 " "OBJ-" D_TEMPERATURE "{m}%s C" "{e}"
|
|
|
|
"{s}MXL90614 " "AMB-" D_TEMPERATURE "{m}%s C" "{e}";
|
2019-11-10 16:02:02 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2019-06-14 14:51:00 +01:00
|
|
|
|
2019-11-10 16:02:02 +00:00
|
|
|
void MLX90614_Show(uint8_t json)
|
|
|
|
{
|
2019-06-14 14:51:00 +01:00
|
|
|
char obj_tstr[16];
|
2019-11-23 17:55:48 +00:00
|
|
|
dtostrfd(mlx90614.obj_temp, Settings.flag2.temperature_resolution, obj_tstr);
|
2019-06-14 14:51:00 +01:00
|
|
|
char amb_tstr[16];
|
2019-11-23 17:55:48 +00:00
|
|
|
dtostrfd(mlx90614.amb_temp, Settings.flag2.temperature_resolution, amb_tstr);
|
2019-06-14 14:51:00 +01:00
|
|
|
|
|
|
|
if (json) {
|
2019-11-10 16:02:02 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"MLX90614\":{\"OBJTMP\":%s,\"AMBTMP\":%s}"), obj_tstr, amb_tstr);
|
2019-06-14 14:51:00 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-11-10 16:02:02 +00:00
|
|
|
WSContentSend_PD(HTTP_IRTMP, obj_tstr, amb_tstr);
|
2019-06-14 14:51:00 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 07:37:50 +01:00
|
|
|
uint16_t MLX90614_read16(uint8_t addr, uint8_t a) {
|
|
|
|
uint16_t ret;
|
|
|
|
|
|
|
|
Wire.beginTransmission(addr);
|
|
|
|
Wire.write(a);
|
|
|
|
Wire.endTransmission(false);
|
|
|
|
|
|
|
|
Wire.requestFrom(addr, (size_t)3);
|
|
|
|
uint8_t buff[5];
|
|
|
|
buff[0] = addr << 1;
|
|
|
|
buff[1] = a;
|
|
|
|
buff[2] = (addr << 1) | 1;
|
|
|
|
buff[3] = Wire.read();
|
|
|
|
buff[4] = Wire.read();
|
|
|
|
ret = buff[3] | (buff[4] << 8);
|
|
|
|
uint8_t pec = Wire.read();
|
|
|
|
uint8_t cpec = MLX90614_crc8(buff, sizeof(buff));
|
|
|
|
//AddLog(LOG_LEVEL_INFO,PSTR("%x - %x"),pec, cpec);
|
|
|
|
|
|
|
|
if (pec != cpec) {
|
|
|
|
AddLog(LOG_LEVEL_INFO,PSTR("mlx checksum error"));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t MLX90614_crc8(uint8_t *addr, uint8_t len)
|
|
|
|
// The PEC calculation includes all bits except the START, REPEATED START, STOP,
|
|
|
|
// ACK, and NACK bits. The PEC is a CRC-8 with polynomial X8+X2+X1+1.
|
|
|
|
{
|
|
|
|
uint8_t crc = 0;
|
|
|
|
while (len--) {
|
|
|
|
uint8_t inbyte = *addr++;
|
|
|
|
for (uint8_t i = 8; i; i--) {
|
|
|
|
uint8_t carry = (crc ^ inbyte) & 0x80;
|
|
|
|
crc <<= 1;
|
|
|
|
if (carry)
|
|
|
|
crc ^= 0x7;
|
|
|
|
inbyte <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-14 14:51:00 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xsns46(byte function)
|
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_32)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-06-14 14:51:00 +01:00
|
|
|
bool result = false;
|
|
|
|
|
2019-11-11 17:49:56 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
MLX90614_Init();
|
|
|
|
}
|
2019-11-23 17:55:48 +00:00
|
|
|
else if (mlx90614.ready) {
|
2019-11-11 17:49:56 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
MLX90614_Every_Second();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
MLX90614_Show(1);
|
|
|
|
break;
|
2019-06-14 14:51:00 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 17:49:56 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
MLX90614_Show(0);
|
|
|
|
break;
|
2019-06-14 14:51:00 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 17:49:56 +00:00
|
|
|
}
|
2019-06-14 14:51:00 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_MLX90614
|
2019-06-16 15:43:23 +01:00
|
|
|
#endif // USE_I2C
|