2017-04-10 16:25:31 +01:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_04_snfsc.ino - sonoff SC support for Sonoff-Tasmota
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2019-03-13 17:00:15 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-05-13 12:02:10 +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.
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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/>.
|
2017-04-10 16:25:31 +01:00
|
|
|
*/
|
|
|
|
|
2019-10-23 12:11:53 +01:00
|
|
|
#ifdef USE_SONOFF_SC
|
2017-04-10 16:25:31 +01:00
|
|
|
/*********************************************************************************************\
|
2017-05-13 12:02:10 +01:00
|
|
|
Sonoff Sc
|
|
|
|
|
|
|
|
sc_value[0] DHT11 Humidity
|
|
|
|
sc_value[1] DHT11 Temperature in Celsius
|
|
|
|
sc_value[2] Light level from 1 (Dark) to 10 (Bright) - inverted from original
|
|
|
|
sc_value[3] Noise level from 1 (Quiet) to 10 (Loud)
|
|
|
|
sc_value[4] Air Quality level from 1 (Bad) to 10 (Good) - inverted from original
|
|
|
|
|
|
|
|
To ATMEGA328P:
|
|
|
|
AT+DEVCONFIG="uploadFreq":1800,"humiThreshold":2,"tempThreshold":1[1B]
|
|
|
|
AT+NOTIFY="uploadFreq":1800,"humiThreshold":2,"tempThreshold":1[1B]
|
|
|
|
response: AT+NOTIFY=ok[1B]
|
|
|
|
AT+SEND=fail[1B]
|
|
|
|
AT+SEND=ok[1B]
|
|
|
|
AT+STATUS=4[1B]
|
|
|
|
AT+STATUS[1B]
|
|
|
|
AT+START[1B]
|
|
|
|
|
|
|
|
From ATMEGA328P:
|
|
|
|
AT+UPDATE="humidity":42,"temperature":20,"light":7,"noise":3,"dusty":1[1B]
|
|
|
|
response: AT+SEND=ok[1B] or AT+SEND=fail[1B]
|
|
|
|
AT+STATUS?[1B]
|
|
|
|
response: AT+STATUS=4[1B]
|
|
|
|
|
|
|
|
Sequence:
|
|
|
|
SC sends: ATMEGA328P sends:
|
|
|
|
AT+START[1B]
|
|
|
|
AT+UPDATE="humidity":42,"temperature":20,"light":7,"noise":3,"dusty":1[1B]
|
|
|
|
AT+SEND=ok[1B]
|
|
|
|
|
|
|
|
AT+STATUS?[1B]
|
|
|
|
AT+STATUS=4[1B]
|
|
|
|
|
2017-04-10 16:25:31 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-12-15 14:55:51 +00:00
|
|
|
#define XSNS_04 4
|
|
|
|
|
2017-04-10 16:25:31 +01:00
|
|
|
uint16_t sc_value[5] = { 0 };
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffScSend(const char *data)
|
2017-09-14 13:20:27 +01:00
|
|
|
{
|
|
|
|
Serial.write(data);
|
|
|
|
Serial.write('\x1B');
|
2019-03-13 17:00:15 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_SERIAL D_TRANSMIT " %s"), data);
|
2017-09-14 13:20:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-15 14:55:51 +00:00
|
|
|
void SonoffScInit(void)
|
2017-04-10 16:25:31 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
// SonoffScSend("AT+DEVCONFIG=\"uploadFreq\":1800");
|
|
|
|
SonoffScSend("AT+START");
|
|
|
|
// SonoffScSend("AT+STATUS");
|
2017-04-10 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffScSerialInput(char *rcvstat)
|
2017-04-10 16:25:31 +01:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
char *p;
|
|
|
|
char *str;
|
2017-04-10 16:25:31 +01:00
|
|
|
uint16_t value[5] = { 0 };
|
|
|
|
|
2019-03-13 17:00:15 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_SERIAL D_RECEIVED " %s"), rcvstat);
|
2017-09-14 13:20:27 +01:00
|
|
|
|
|
|
|
if (!strncasecmp_P(rcvstat, PSTR("AT+UPDATE="), 10)) {
|
2017-04-10 16:25:31 +01:00
|
|
|
int8_t i = -1;
|
2019-07-05 12:44:24 +01:00
|
|
|
for (str = strtok_r(rcvstat, ":", &p); str && i < 5; str = strtok_r(nullptr, ":", &p)) {
|
2017-04-25 17:24:42 +01:00
|
|
|
value[i++] = atoi(str);
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
if (value[0] > 0) {
|
2019-07-05 12:44:24 +01:00
|
|
|
for (uint32_t i = 0; i < 5; i++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
sc_value[i] = value[i];
|
|
|
|
}
|
2017-04-10 16:33:57 +01:00
|
|
|
sc_value[2] = (11 - sc_value[2]) * 10; // Invert light level
|
|
|
|
sc_value[3] *= 10;
|
|
|
|
sc_value[4] = (11 - sc_value[4]) * 10; // Invert dust level
|
2017-10-18 17:22:34 +01:00
|
|
|
SonoffScSend("AT+SEND=ok");
|
2017-04-10 16:25:31 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
SonoffScSend("AT+SEND=fail");
|
2017-04-10 16:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-14 13:20:27 +01:00
|
|
|
else if (!strcasecmp_P(rcvstat, PSTR("AT+STATUS?"))) {
|
2017-10-18 17:22:34 +01:00
|
|
|
SonoffScSend("AT+STATUS=4");
|
2017-04-10 16:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_SCPLUS[] PROGMEM =
|
2019-07-05 12:44:24 +01:00
|
|
|
"{s}" D_LIGHT "{m}%d%%{e}{s}" D_NOISE "{m}%d%%{e}{s}" D_AIR_QUALITY "{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
|
|
|
|
2019-03-13 17:00:15 +00:00
|
|
|
void SonoffScShow(bool json)
|
2017-11-04 15:36:51 +00:00
|
|
|
{
|
2017-04-10 16:25:31 +01:00
|
|
|
if (sc_value[0] > 0) {
|
2017-10-18 17:22:34 +01:00
|
|
|
float t = ConvertTemp(sc_value[1]);
|
2019-07-05 12:44:24 +01:00
|
|
|
float h = ConvertHumidity(sc_value[0]);
|
2018-12-22 17:42:40 +00:00
|
|
|
|
|
|
|
char temperature[33];
|
2017-11-11 11:33:30 +00:00
|
|
|
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
|
2018-12-22 17:42:40 +00:00
|
|
|
char humidity[33];
|
2017-11-11 11:33:30 +00:00
|
|
|
dtostrfd(h, Settings.flag2.humidity_resolution, humidity);
|
2017-11-04 15:36:51 +00:00
|
|
|
|
|
|
|
if (json) {
|
2019-07-05 12:44:24 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"SonoffSC\":{\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s,\"" D_JSON_LIGHT "\":%d,\"" D_JSON_NOISE "\":%d,\"" D_JSON_AIRQUALITY "\":%d}"),
|
|
|
|
temperature, humidity, sc_value[2], sc_value[3], sc_value[4]);
|
2017-04-10 16:25:31 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzTempHumSensor(temperature, humidity);
|
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, sc_value[2]);
|
|
|
|
DomoticzSensor(DZ_COUNT, sc_value[3]);
|
|
|
|
DomoticzSensor(DZ_AIRQUALITY, 500 + ((100 - sc_value[4]) * 20));
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
2018-04-18 16:28:45 +01:00
|
|
|
|
|
|
|
#ifdef USE_KNX
|
|
|
|
if (0 == tele_period) {
|
|
|
|
KnxSensor(KNX_TEMPERATURE, t);
|
|
|
|
KnxSensor(KNX_HUMIDITY, h);
|
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
|
|
|
|
2017-04-10 16:25:31 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-11-04 15:36:51 +00:00
|
|
|
} else {
|
2019-07-05 12:44:24 +01:00
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, "", temperature, TempUnit());
|
|
|
|
WSContentSend_PD(HTTP_SNS_HUM, "", humidity);
|
|
|
|
WSContentSend_PD(HTTP_SNS_SCPLUS, sc_value[2], sc_value[3], sc_value[4]);
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:07:25 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-03-13 17:00:15 +00:00
|
|
|
bool Xsns04(uint8_t function)
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2019-03-13 17:00:15 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2019-03-13 17:00:15 +00:00
|
|
|
if (SONOFF_SC == my_module_type) {
|
2017-11-03 17:07:25 +00:00
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_INIT:
|
2017-11-08 10:57:16 +00:00
|
|
|
SonoffScInit();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
SonoffScShow(1);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-07-05 12:44:24 +01:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-11-04 15:36:51 +00:00
|
|
|
SonoffScShow(0);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-10-23 12:11:53 +01:00
|
|
|
|
|
|
|
#endif // USE_SONOFF_SC
|