2017-04-10 16:25:31 +01:00
|
|
|
/*
|
2017-05-13 12:02:10 +01:00
|
|
|
xdrv_snfsc.ino - sonoff SC support for Sonoff-Tasmota
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
Copyright (C) 2017 Heiko Krupp and 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
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
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
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
uint16_t sc_value[5] = { 0 };
|
|
|
|
|
|
|
|
void sc_init()
|
|
|
|
{
|
|
|
|
// Serial.write("AT+DEVCONFIG=\"uploadFreq\":1800\e");
|
|
|
|
Serial.write("AT+START\e");
|
|
|
|
// Serial.write("AT+STATUS\e");
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_rcvstat(char *rcvstat)
|
|
|
|
{
|
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 };
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
if (!strncmp(rcvstat, "AT+UPDATE=", 10)) {
|
2017-04-10 16:25:31 +01:00
|
|
|
int8_t i = -1;
|
2017-04-25 17:24:42 +01:00
|
|
|
for (str = strtok_r(rcvstat, ":", &p); str && i < 5; str = strtok_r(NULL, ":", &p)) {
|
|
|
|
value[i++] = atoi(str);
|
|
|
|
}
|
2017-04-10 16:25:31 +01:00
|
|
|
if (value[0] > 0) {
|
2017-04-25 17:24:42 +01:00
|
|
|
for (byte i = 0; i < 5; i++) {
|
|
|
|
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-04-10 16:25:31 +01:00
|
|
|
Serial.write("AT+SEND=ok\e");
|
|
|
|
} else {
|
|
|
|
Serial.write("AT+SEND=fail\e");
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 13:40:53 +01:00
|
|
|
else if (!strcmp_P(rcvstat,PSTR("AT+STATUS?"))) {
|
2017-04-10 16:25:31 +01:00
|
|
|
Serial.write("AT+STATUS=4\e");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
float sc_convertCtoF(float c)
|
|
|
|
{
|
|
|
|
return c * 1.8 + 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sc_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
|
|
|
{
|
|
|
|
if (sc_value[0] > 0) {
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp1[10];
|
|
|
|
char stemp2[10];
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
float t = convertTemp(sc_value[1]);
|
|
|
|
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp1);
|
2017-04-10 16:25:31 +01:00
|
|
|
float h = sc_value[0];
|
2017-05-03 17:19:13 +01:00
|
|
|
dtostrf(h, 1, sysCfg.flag.humidity_resolution, stemp2);
|
2017-04-29 13:40:53 +01:00
|
|
|
// snprintf_P(svalue, ssvalue, PSTR("%s, \"SC\":{\"Temperature\":%s, \"Humidity\":%s, \"Light\":%d, \"Noise\":%d, \"AirQuality\":%d}"),
|
|
|
|
snprintf_P(svalue, ssvalue, PSTR("%s, \"Temperature\":%s, \"Humidity\":%s, \"Light\":%d, \"Noise\":%d, \"AirQuality\":%d"),
|
2017-04-10 16:25:31 +01:00
|
|
|
svalue, stemp1, stemp2, sc_value[2], sc_value[3], sc_value[4]);
|
|
|
|
*djson = 1;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
domoticz_sensor2(stemp1, stemp2);
|
|
|
|
domoticz_sensor5(sc_value[2]);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
String sc_webPresent()
|
|
|
|
{
|
|
|
|
String page = "";
|
|
|
|
|
|
|
|
if (sc_value[0] > 0) {
|
2017-04-25 17:24:42 +01:00
|
|
|
char stemp[10];
|
|
|
|
char sensor[80];
|
2017-04-29 13:40:53 +01:00
|
|
|
char scstype[] = "";
|
2017-04-10 16:25:31 +01:00
|
|
|
|
2017-05-03 17:19:13 +01:00
|
|
|
float t = convertTemp(sc_value[1]);
|
|
|
|
dtostrf(t, 1, sysCfg.flag.temperature_resolution, stemp);
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, scstype, stemp, tempUnit());
|
2017-04-10 16:25:31 +01:00
|
|
|
page += sensor;
|
|
|
|
float h = sc_value[0];
|
2017-05-03 17:19:13 +01:00
|
|
|
dtostrf(h, 1, sysCfg.flag.humidity_resolution, stemp);
|
2017-04-10 16:25:31 +01:00
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_HUM, scstype, stemp);
|
|
|
|
page += sensor;
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_LIGHT, scstype, sc_value[2]);
|
|
|
|
page += sensor;
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_NOISE, scstype, sc_value[3]);
|
|
|
|
page += sensor;
|
|
|
|
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_DUST, scstype, sc_value[4]);
|
|
|
|
page += sensor;
|
|
|
|
}
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|