2017-10-25 13:27:30 +01:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_11_veml6070.ino - VEML6070 ultra violet light sensor support for Sonoff-Tasmota
|
2017-10-25 13:27:30 +01:00
|
|
|
|
2017-12-22 13:55:24 +00:00
|
|
|
Copyright (C) 2018 Theo Arends
|
2017-10-25 13:27:30 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_VEML6070
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* VEML6070 - Ultra Violet Light Intensity
|
2017-11-11 11:33:30 +00:00
|
|
|
*
|
|
|
|
* I2C Address: 0x38 and 0x39
|
2017-10-25 13:27:30 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define VEML6070_ADDR_H 0x39
|
|
|
|
#define VEML6070_ADDR_L 0x38
|
|
|
|
|
|
|
|
#define VEML6070_INTEGRATION_TIME 3 // 500msec integration time
|
|
|
|
|
|
|
|
uint8_t veml6070_address;
|
|
|
|
uint8_t veml6070_type = 0;
|
|
|
|
|
|
|
|
uint16_t Veml6070ReadUv()
|
|
|
|
{
|
|
|
|
if (Wire.requestFrom(VEML6070_ADDR_H, 1) != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint16_t uvi = Wire.read();
|
|
|
|
uvi <<= 8;
|
|
|
|
if (Wire.requestFrom(VEML6070_ADDR_L, 1) != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uvi |= Wire.read();
|
|
|
|
|
2017-11-03 17:07:25 +00:00
|
|
|
return uvi;
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-12-06 16:14:41 +00:00
|
|
|
void Veml6070Detect()
|
2017-10-25 13:27:30 +01:00
|
|
|
{
|
|
|
|
if (veml6070_type) {
|
2017-12-06 16:14:41 +00:00
|
|
|
return;
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t itime = VEML6070_INTEGRATION_TIME;
|
|
|
|
|
|
|
|
veml6070_address = VEML6070_ADDR_L;
|
|
|
|
Wire.beginTransmission(veml6070_address);
|
|
|
|
Wire.write((itime << 2) | 0x02);
|
2017-12-06 16:14:41 +00:00
|
|
|
uint8_t status = Wire.endTransmission();
|
2017-10-25 13:27:30 +01:00
|
|
|
if (!status) {
|
|
|
|
veml6070_type = 1;
|
2017-12-06 16:14:41 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "VEML6070", veml6070_address);
|
2017-10-25 13:27:30 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_ULTRAVIOLET[] PROGMEM =
|
2017-11-03 17:07:25 +00:00
|
|
|
"%s{s}VEML6070 " D_UV_LEVEL "{m}%d{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
void Veml6070Show(boolean json)
|
2017-10-25 13:27:30 +01:00
|
|
|
{
|
|
|
|
if (veml6070_type) {
|
2017-11-04 15:36:51 +00:00
|
|
|
uint16_t uvlevel = Veml6070ReadUv();
|
2017-11-07 14:57:24 +00:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (json) {
|
2017-12-29 13:35:05 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"VEML6070\":{\"" D_JSON_UV_LEVEL "\":%d}"), mqtt_data, uvlevel);
|
2017-11-04 15:36:51 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, uvlevel);
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ULTRAVIOLET, mqtt_data, uvlevel);
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_11
|
|
|
|
|
|
|
|
boolean Xsns11(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_PREP_BEFORE_TELEPERIOD:
|
2017-11-04 15:36:51 +00:00
|
|
|
Veml6070Detect();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
Veml6070Show(1);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_WEB_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
Veml6070Show(0);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-11-04 15:36:51 +00:00
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-25 13:27:30 +01:00
|
|
|
#endif // USE_VEML6070
|
|
|
|
#endif // USE_I2C
|
|
|
|
|