2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-05-13 12:02:10 +01:00
|
|
|
support.ino - support for Sonoff-Tasmota
|
|
|
|
|
2017-12-22 13:55:24 +00:00
|
|
|
Copyright (C) 2018 Theo Arends
|
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.
|
|
|
|
|
|
|
|
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-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-20 15:19:48 +00:00
|
|
|
IPAddress syslog_host_addr; // Syslog host IP address
|
|
|
|
uint32_t syslog_host_hash = 0; // Syslog host name hash
|
2017-09-26 16:50:39 +01:00
|
|
|
|
2017-02-28 15:01:48 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Watchdog extension (https://github.com/esp8266/Arduino/issues/1532)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
Ticker tickerOSWatch;
|
|
|
|
|
2018-01-13 14:53:02 +00:00
|
|
|
#define OSWATCH_RESET_TIME 120
|
2017-02-28 15:01:48 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
static unsigned long oswatch_last_loop_time;
|
|
|
|
byte oswatch_blocked_loop = 0;
|
2017-02-28 15:01:48 +00:00
|
|
|
|
2017-04-03 15:38:15 +01:00
|
|
|
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
|
2017-11-19 17:02:03 +00:00
|
|
|
//void OsWatchTicker() ICACHE_RAM_ATTR;
|
2017-04-03 15:38:15 +01:00
|
|
|
#endif // USE_WS2812_DMA
|
|
|
|
|
2018-05-11 14:00:41 +01:00
|
|
|
#ifdef USE_KNX
|
|
|
|
bool knx_started = false;
|
|
|
|
#endif // USE_KNX
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void OsWatchTicker()
|
2017-02-28 15:01:48 +00:00
|
|
|
{
|
|
|
|
unsigned long t = millis();
|
2017-10-18 17:22:34 +01:00
|
|
|
unsigned long last_run = abs(t - oswatch_last_loop_time);
|
2017-02-28 15:01:48 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_THEO
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_OSWATCH " FreeRam %d, rssi %d, last_run %d"), ESP.getFreeHeap(), WifiGetRssiAsQuality(WiFi.RSSI()), last_run);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-02-28 15:01:48 +00:00
|
|
|
#endif // DEBUG_THEO
|
2017-04-25 17:24:42 +01:00
|
|
|
if (last_run >= (OSWATCH_RESET_TIME * 1000)) {
|
2017-11-17 16:52:31 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_OSWATCH " " D_BLOCKED_LOOP ". " D_RESTARTING)); // Save iram space
|
2017-10-18 17:22:34 +01:00
|
|
|
RtcSettings.oswatch_blocked_loop = 1;
|
|
|
|
RtcSettingsSave();
|
2017-09-02 13:37:02 +01:00
|
|
|
// ESP.restart(); // normal reboot
|
2017-02-28 15:01:48 +00:00
|
|
|
ESP.reset(); // hard reset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void OsWatchInit()
|
2017-02-28 15:01:48 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
oswatch_blocked_loop = RtcSettings.oswatch_blocked_loop;
|
|
|
|
RtcSettings.oswatch_blocked_loop = 0;
|
|
|
|
oswatch_last_loop_time = millis();
|
|
|
|
tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), OsWatchTicker);
|
2017-02-28 15:01:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void OsWatchLoop()
|
2017-02-28 15:01:48 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
oswatch_last_loop_time = millis();
|
2017-02-28 15:01:48 +00:00
|
|
|
// while(1) delay(1000); // this will trigger the os watch
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
String GetResetReason()
|
2017-02-28 15:01:48 +00:00
|
|
|
{
|
|
|
|
char buff[32];
|
2017-10-18 17:22:34 +01:00
|
|
|
if (oswatch_blocked_loop) {
|
2018-01-06 16:34:42 +00:00
|
|
|
strncpy_P(buff, PSTR(D_JSON_BLOCKED_LOOP), sizeof(buff));
|
2017-02-28 15:01:48 +00:00
|
|
|
return String(buff);
|
|
|
|
} else {
|
|
|
|
return ESP.getResetReason();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:10:32 +01:00
|
|
|
boolean OsWatchBlockedLoop()
|
|
|
|
{
|
|
|
|
return oswatch_blocked_loop;
|
|
|
|
}
|
2017-03-25 16:24:11 +00:00
|
|
|
/*********************************************************************************************\
|
2018-01-30 13:14:55 +00:00
|
|
|
* Miscellaneous
|
2017-03-25 16:24:11 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
#ifdef ARDUINO_ESP8266_RELEASE_2_3_0
|
|
|
|
// Functions not available in 2.3.0
|
|
|
|
|
|
|
|
// http://clc-wiki.net/wiki/C_standard_library:string.h:memchr
|
|
|
|
void* memchr(const void* ptr, int value, size_t num)
|
|
|
|
{
|
|
|
|
unsigned char *p = (unsigned char*)ptr;
|
|
|
|
while (num--) {
|
|
|
|
if (*p != (unsigned char)value) {
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://clc-wiki.net/wiki/C_standard_library:string.h:strspn
|
2018-02-01 15:18:00 +00:00
|
|
|
// Get span until any character in string
|
2018-01-30 13:14:55 +00:00
|
|
|
size_t strcspn(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
while (*str1) {
|
2018-02-01 15:18:00 +00:00
|
|
|
if (strchr(str2, *str1)) { // Slow
|
2018-01-30 13:14:55 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
str1++;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-01 15:18:00 +00:00
|
|
|
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
2018-01-30 13:14:55 +00:00
|
|
|
|
2018-02-01 15:18:00 +00:00
|
|
|
// Get span until single character in string
|
|
|
|
size_t strchrspn(const char *str1, int character)
|
2018-01-30 13:14:55 +00:00
|
|
|
{
|
2018-02-01 15:18:00 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
char *start = (char*)str1;
|
|
|
|
char *end = strchr(str1, character);
|
|
|
|
if (end) ret = end - start;
|
|
|
|
return ret;
|
2018-01-30 13:14:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 05:32:54 +01:00
|
|
|
// Function to return a substring defined by a delimiter at an index
|
|
|
|
char* subStr(char* dest, char* str, const char *delim, int index)
|
|
|
|
{
|
|
|
|
char *act;
|
|
|
|
char *sub;
|
|
|
|
char *ptr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Since strtok consumes the first arg, make a copy
|
2018-09-06 18:21:52 +01:00
|
|
|
strncpy(dest, str, strlen(str)+1);
|
2018-07-23 05:32:54 +01:00
|
|
|
for (i = 1, act = dest; i <= index; i++, act = NULL) {
|
|
|
|
sub = strtok_r(act, delim, &ptr);
|
|
|
|
if (sub == NULL) break;
|
|
|
|
}
|
|
|
|
sub = Trim(sub);
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:01:43 +01:00
|
|
|
double CharToDouble(char *str)
|
2018-04-05 11:49:43 +01:00
|
|
|
{
|
|
|
|
// simple ascii to double, because atof or strtod are too large
|
|
|
|
char strbuf[24];
|
|
|
|
|
2018-09-02 10:52:24 +01:00
|
|
|
strlcpy(strbuf, str, sizeof(strbuf));
|
2018-04-05 11:49:43 +01:00
|
|
|
char *pt;
|
|
|
|
double left = atoi(strbuf);
|
|
|
|
double right = 0;
|
|
|
|
short len = 0;
|
|
|
|
pt = strtok (strbuf, ".");
|
|
|
|
if (pt) {
|
|
|
|
pt = strtok (NULL, ".");
|
|
|
|
if (pt) {
|
|
|
|
right = atoi(pt);
|
|
|
|
len = strlen(pt);
|
|
|
|
double fac = 1;
|
|
|
|
while (len) {
|
|
|
|
fac /= 10.0;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
// pow is also very large
|
|
|
|
//double fac=pow(10,-len);
|
|
|
|
right *= fac;
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 14:34:18 +01:00
|
|
|
double result = left + right;
|
|
|
|
if (left < 0) { result = left - right; }
|
|
|
|
return result;
|
2018-04-05 11:49:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:05:51 +01:00
|
|
|
int TextToInt(char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
uint8_t radix = 10;
|
|
|
|
if ('#' == str[0]) {
|
|
|
|
radix = 16;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return strtol(str, &p, radix);
|
|
|
|
}
|
|
|
|
|
2018-01-20 11:12:39 +00:00
|
|
|
char* dtostrfd(double number, unsigned char prec, char *s)
|
2017-09-02 13:37:02 +01:00
|
|
|
{
|
2018-10-25 13:03:34 +01:00
|
|
|
if ((isnan(number)) || (isinf(number))) { // Fix for JSON output (https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript)
|
2018-10-25 09:44:59 +01:00
|
|
|
strcpy(s, "null");
|
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
return dtostrf(number, 1, prec, s);
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 13:31:11 +00:00
|
|
|
char* Unescape(char* buffer, uint16_t* size)
|
|
|
|
{
|
|
|
|
uint8_t* read = (uint8_t*)buffer;
|
|
|
|
uint8_t* write = (uint8_t*)buffer;
|
|
|
|
uint16_t start_size = *size;
|
|
|
|
uint16_t end_size = *size;
|
|
|
|
uint8_t che = 0;
|
|
|
|
|
|
|
|
while (start_size > 0) {
|
|
|
|
uint8_t ch = *read++;
|
|
|
|
start_size--;
|
|
|
|
if (ch != '\\') {
|
|
|
|
*write++ = ch;
|
|
|
|
} else {
|
|
|
|
if (start_size > 0) {
|
|
|
|
uint8_t chi = *read++;
|
|
|
|
start_size--;
|
|
|
|
end_size--;
|
|
|
|
switch (chi) {
|
|
|
|
case '\\': che = '\\'; break; // 5C Backslash
|
|
|
|
case 'a': che = '\a'; break; // 07 Bell (Alert)
|
|
|
|
case 'b': che = '\b'; break; // 08 Backspace
|
|
|
|
case 'e': che = '\e'; break; // 1B Escape
|
|
|
|
case 'f': che = '\f'; break; // 0C Formfeed
|
|
|
|
case 'n': che = '\n'; break; // 0A Linefeed (Newline)
|
|
|
|
case 'r': che = '\r'; break; // 0D Carriage return
|
2018-03-20 15:28:18 +00:00
|
|
|
case 's': che = ' '; break; // 20 Space
|
2018-03-20 13:31:11 +00:00
|
|
|
case 't': che = '\t'; break; // 09 Horizontal tab
|
|
|
|
case 'v': che = '\v'; break; // 0B Vertical tab
|
|
|
|
// case '?': che = '\?'; break; // 3F Question mark
|
|
|
|
default : {
|
|
|
|
che = chi;
|
|
|
|
*write++ = ch;
|
|
|
|
end_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*write++ = che;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*size = end_size;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-10-14 11:53:11 +01:00
|
|
|
char* RemoveSpace(char* p)
|
|
|
|
{
|
|
|
|
char* write = p;
|
|
|
|
char* read = p;
|
|
|
|
char ch = '.';
|
|
|
|
|
|
|
|
while (ch != '\0') {
|
|
|
|
ch = *read++;
|
|
|
|
if (!isspace(ch)) {
|
|
|
|
*write++ = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*write = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:20:20 +00:00
|
|
|
char* UpperCase(char* dest, const char* source)
|
|
|
|
{
|
|
|
|
char* write = dest;
|
|
|
|
const char* read = source;
|
|
|
|
char ch = '.';
|
|
|
|
|
|
|
|
while (ch != '\0') {
|
|
|
|
ch = *read++;
|
|
|
|
*write++ = toupper(ch);
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* UpperCase_P(char* dest, const char* source)
|
|
|
|
{
|
|
|
|
char* write = dest;
|
|
|
|
const char* read = source;
|
|
|
|
char ch = '.';
|
|
|
|
|
|
|
|
while (ch != '\0') {
|
|
|
|
ch = pgm_read_byte(read++);
|
|
|
|
*write++ = toupper(ch);
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:32:53 +01:00
|
|
|
char* LTrim(char* p)
|
|
|
|
{
|
|
|
|
while ((*p != '\0') && (isblank(*p))) {
|
|
|
|
p++; // Trim leading spaces
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-05-29 16:24:42 +01:00
|
|
|
char* RTrim(char* p)
|
|
|
|
{
|
|
|
|
char* q = p + strlen(p) -1;
|
|
|
|
while ((q >= p) && (isblank(*q))) {
|
|
|
|
q--; // Trim trailing spaces
|
|
|
|
}
|
|
|
|
q++;
|
|
|
|
*q = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* Trim(char* p)
|
|
|
|
{
|
|
|
|
if (*p == '\0') { return p; }
|
|
|
|
while (isspace(*p)) { p++; } // Trim leading spaces
|
|
|
|
if (*p == '\0') { return p; }
|
|
|
|
char* q = p + strlen(p) -1;
|
|
|
|
while (isspace(*q) && q >= p) { q--; } // Trim trailing spaces
|
|
|
|
q++;
|
|
|
|
*q = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:43:20 +01:00
|
|
|
char* NoAlNumToUnderscore(char* dest, const char* source)
|
2018-04-19 20:41:59 +01:00
|
|
|
{
|
|
|
|
char* write = dest;
|
|
|
|
const char* read = source;
|
|
|
|
char ch = '.';
|
|
|
|
|
|
|
|
while (ch != '\0') {
|
|
|
|
ch = *read++;
|
2018-04-20 16:43:20 +01:00
|
|
|
*write++ = (isalnum(ch) || ('\0' == ch)) ? ch : '_';
|
2018-04-19 20:41:59 +01:00
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:53:09 +01:00
|
|
|
void SetShortcut(char* str, uint8_t action)
|
|
|
|
{
|
|
|
|
if ('\0' != str[0]) { // There must be at least one character in the buffer
|
|
|
|
str[0] = '0' + action; // SC_CLEAR, SC_DEFAULT, SC_USER
|
|
|
|
str[1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 12:06:22 +01:00
|
|
|
uint8_t Shortcut(const char* str)
|
|
|
|
{
|
|
|
|
uint8_t result = 10;
|
|
|
|
|
|
|
|
if ('\0' == str[1]) { // Only allow single character input for shortcut
|
|
|
|
if (('"' == str[0]) || ('0' == str[0])) {
|
2018-08-27 13:53:09 +01:00
|
|
|
result = SC_CLEAR;
|
2018-08-27 12:06:22 +01:00
|
|
|
} else {
|
2018-08-27 13:53:09 +01:00
|
|
|
result = atoi(str); // 1 = SC_DEFAULT, 2 = SC_USER
|
2018-08-27 12:06:22 +01:00
|
|
|
if (0 == result) {
|
|
|
|
result = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean ParseIp(uint32_t* addr, const char* str)
|
2017-03-25 16:24:11 +00:00
|
|
|
{
|
|
|
|
uint8_t *part = (uint8_t*)addr;
|
|
|
|
byte i;
|
|
|
|
|
|
|
|
*addr = 0;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
part[i] = strtoul(str, NULL, 10); // Convert byte
|
|
|
|
str = strchr(str, '.');
|
2017-04-25 17:24:42 +01:00
|
|
|
if (str == NULL || *str == '\0') {
|
|
|
|
break; // No more separators, exit
|
|
|
|
}
|
2017-03-25 16:24:11 +00:00
|
|
|
str++; // Point to next character after separator
|
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
return (3 == i);
|
2017-03-25 16:24:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void MakeValidMqtt(byte option, char* str)
|
2017-06-16 13:33:49 +01:00
|
|
|
{
|
|
|
|
// option 0 = replace by underscore
|
2017-09-02 13:37:02 +01:00
|
|
|
// option 1 = delete character
|
2017-06-16 13:33:49 +01:00
|
|
|
uint16_t i = 0;
|
|
|
|
while (str[i] > 0) {
|
|
|
|
// if ((str[i] == '/') || (str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
|
|
|
|
if ((str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
|
|
|
|
if (option) {
|
|
|
|
uint16_t j = i;
|
|
|
|
while (str[j] > 0) {
|
|
|
|
str[j] = str[j +1];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
} else {
|
|
|
|
str[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:54:19 +01:00
|
|
|
// Function to parse & check if version_str is newer than our currently installed version.
|
2017-10-18 17:22:34 +01:00
|
|
|
bool NewerVersion(char* version_str)
|
2017-06-30 16:54:19 +01:00
|
|
|
{
|
|
|
|
uint32_t version = 0;
|
|
|
|
uint8_t i = 0;
|
|
|
|
char *str_ptr;
|
|
|
|
char* version_dup = strdup(version_str); // Duplicate the version_str as strtok_r will modify it.
|
|
|
|
|
|
|
|
if (!version_dup) {
|
|
|
|
return false; // Bail if we can't duplicate. Assume bad.
|
|
|
|
}
|
|
|
|
// Loop through the version string, splitting on '.' seperators.
|
|
|
|
for (char *str = strtok_r(version_dup, ".", &str_ptr); str && i < sizeof(VERSION); str = strtok_r(NULL, ".", &str_ptr), i++) {
|
|
|
|
int field = atoi(str);
|
|
|
|
// The fields in a version string can only range from 0-255.
|
|
|
|
if ((field < 0) || (field > 255)) {
|
|
|
|
free(version_dup);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Shuffle the accumulated bytes across, and add the new byte.
|
|
|
|
version = (version << 8) + field;
|
|
|
|
// Check alpha delimiter after 1.2.3 only
|
|
|
|
if ((2 == i) && isalpha(str[strlen(str)-1])) {
|
|
|
|
field = str[strlen(str)-1] & 0x1f;
|
|
|
|
version = (version << 8) + field;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(version_dup); // We no longer need this.
|
|
|
|
// A version string should have 2-4 fields. e.g. 1.2, 1.2.3, or 1.2.3a (= 1.2.3.1).
|
|
|
|
// If not, then don't consider it a valid version string.
|
|
|
|
if ((i < 2) || (i > sizeof(VERSION))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Keep shifting the parsed version until we hit the maximum number of tokens.
|
|
|
|
// VERSION stores the major number of the version in the most significant byte of the uint32_t.
|
|
|
|
while (i < sizeof(VERSION)) {
|
|
|
|
version <<= 8;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// Now we should have a fully constructed version number in uint32_t form.
|
|
|
|
return (version > VERSION);
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
char* GetPowerDevice(char* dest, uint8_t idx, size_t size, uint8_t option)
|
2017-09-02 13:37:02 +01:00
|
|
|
{
|
|
|
|
char sidx[8];
|
|
|
|
|
2018-06-28 11:25:50 +01:00
|
|
|
strncpy_P(dest, S_RSLT_POWER, size); // POWER
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((devices_present + option) > 1) {
|
2018-06-28 11:25:50 +01:00
|
|
|
snprintf_P(sidx, sizeof(sidx), PSTR("%d"), idx); // x
|
|
|
|
strncat(dest, sidx, size); // POWERx
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
char* GetPowerDevice(char* dest, uint8_t idx, size_t size)
|
2017-09-02 13:37:02 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
return GetPowerDevice(dest, idx, size, 0);
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
float ConvertTemp(float c)
|
|
|
|
{
|
|
|
|
float result = c;
|
|
|
|
|
|
|
|
if (!isnan(c) && Settings.flag.temperature_conversion) {
|
|
|
|
result = c * 1.8 + 32; // Fahrenheit
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char TempUnit()
|
|
|
|
{
|
|
|
|
return (Settings.flag.temperature_conversion) ? 'F' : 'C';
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:55:12 +00:00
|
|
|
float ConvertPressure(float p)
|
2018-11-01 15:36:22 +00:00
|
|
|
{
|
|
|
|
float result = p;
|
|
|
|
|
2018-11-04 15:55:12 +00:00
|
|
|
if (!isnan(p) && Settings.flag.pressure_conversion) {
|
2018-11-01 15:36:22 +00:00
|
|
|
result = p * 0.75006375541921; // mmHg
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:55:12 +00:00
|
|
|
String PressureUnit()
|
|
|
|
{
|
|
|
|
return (Settings.flag.pressure_conversion) ? String(D_UNIT_MILLIMETER_MERCURY) : String(D_UNIT_PRESSURE);
|
|
|
|
}
|
|
|
|
|
2018-07-24 17:41:50 +01:00
|
|
|
void SetGlobalValues(float temperature, float humidity)
|
|
|
|
{
|
|
|
|
global_update = uptime;
|
|
|
|
global_temperature = temperature;
|
|
|
|
global_humidity = humidity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResetGlobalValues()
|
|
|
|
{
|
|
|
|
if ((uptime - global_update) > GLOBAL_VALUES_VALID) { // Reset after 5 minutes
|
|
|
|
global_update = 0;
|
|
|
|
global_temperature = 0;
|
|
|
|
global_humidity = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
double FastPrecisePow(double a, double b)
|
|
|
|
{
|
|
|
|
// https://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/
|
|
|
|
// calculate approximation with fraction of the exponent
|
|
|
|
int e = (int)b;
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
int x[2];
|
|
|
|
} u = { a };
|
|
|
|
u.x[1] = (int)((b - e) * (u.x[1] - 1072632447) + 1072632447);
|
|
|
|
u.x[0] = 0;
|
|
|
|
// exponentiation by squaring with the exponent's integer part
|
|
|
|
// double r = u.d makes everything much slower, not sure why
|
|
|
|
double r = 1.0;
|
|
|
|
while (e) {
|
|
|
|
if (e & 1) {
|
|
|
|
r *= a;
|
|
|
|
}
|
|
|
|
a *= a;
|
|
|
|
e >>= 1;
|
|
|
|
}
|
|
|
|
return r * u.d;
|
|
|
|
}
|
|
|
|
|
2018-09-28 14:48:42 +01:00
|
|
|
uint32_t SqrtInt(uint32_t num)
|
|
|
|
{
|
|
|
|
if (num <= 1) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t x = num / 2;
|
|
|
|
uint32_t y;
|
|
|
|
do {
|
|
|
|
y = (x + num / x) / 2;
|
|
|
|
if (y >= x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
x = y;
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RoundSqrtInt(uint32_t num)
|
|
|
|
{
|
|
|
|
uint32_t s = SqrtInt(4 * num);
|
|
|
|
if (s & 1) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return s / 2;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
char* GetTextIndexed(char* destination, size_t destination_size, uint16_t index, const char* haystack)
|
|
|
|
{
|
|
|
|
// Returns empty string if not found
|
|
|
|
// Returns text of found
|
|
|
|
char* write = destination;
|
|
|
|
const char* read = haystack;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
while (index--) {
|
|
|
|
size_t size = destination_size -1;
|
|
|
|
write = destination;
|
|
|
|
char ch = '.';
|
|
|
|
while ((ch != '\0') && (ch != '|')) {
|
|
|
|
ch = pgm_read_byte(read++);
|
|
|
|
if (size && (ch != '|')) {
|
|
|
|
*write++ = ch;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 == ch) {
|
|
|
|
if (index) {
|
|
|
|
write = destination;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*write = '\0';
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetCommandCode(char* destination, size_t destination_size, const char* needle, const char* haystack)
|
|
|
|
{
|
|
|
|
// Returns -1 of not found
|
|
|
|
// Returns index and command if found
|
|
|
|
int result = -1;
|
|
|
|
const char* read = haystack;
|
|
|
|
char* write = destination;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
result++;
|
|
|
|
size_t size = destination_size -1;
|
|
|
|
write = destination;
|
|
|
|
char ch = '.';
|
|
|
|
while ((ch != '\0') && (ch != '|')) {
|
|
|
|
ch = pgm_read_byte(read++);
|
|
|
|
if (size && (ch != '|')) {
|
|
|
|
*write++ = ch;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*write = '\0';
|
|
|
|
if (!strcasecmp(needle, destination)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (0 == ch) {
|
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-06 15:07:42 +01:00
|
|
|
int GetStateNumber(char *state_text)
|
|
|
|
{
|
|
|
|
char command[CMDSZ];
|
|
|
|
int state_number = -1;
|
|
|
|
|
2018-09-02 10:11:49 +01:00
|
|
|
if (GetCommandCode(command, sizeof(command), state_text, kOptionOff) >= 0) {
|
2018-05-06 15:07:42 +01:00
|
|
|
state_number = 0;
|
|
|
|
}
|
2018-09-02 10:11:49 +01:00
|
|
|
else if (GetCommandCode(command, sizeof(command), state_text, kOptionOn) >= 0) {
|
2018-05-06 15:07:42 +01:00
|
|
|
state_number = 1;
|
|
|
|
}
|
2018-09-02 10:11:49 +01:00
|
|
|
else if (GetCommandCode(command, sizeof(command), state_text, kOptionToggle) >= 0) {
|
2018-05-06 15:07:42 +01:00
|
|
|
state_number = 2;
|
|
|
|
}
|
|
|
|
else if (GetCommandCode(command, sizeof(command), state_text, kOptionBlink) >= 0) {
|
|
|
|
state_number = 3;
|
|
|
|
}
|
|
|
|
else if (GetCommandCode(command, sizeof(command), state_text, kOptionBlinkOff) >= 0) {
|
|
|
|
state_number = 4;
|
|
|
|
}
|
|
|
|
return state_number;
|
|
|
|
}
|
|
|
|
|
2018-05-24 10:24:03 +01:00
|
|
|
boolean GetUsedInModule(byte val, uint8_t *arr)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
if (!val) { return false; } // None
|
2018-11-05 16:27:02 +00:00
|
|
|
|
|
|
|
if ((val >= GPIO_KEY1) && (val < GPIO_KEY1 + MAX_KEYS)) {
|
|
|
|
offset = (GPIO_KEY1_NP - GPIO_KEY1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_KEY1_NP) && (val < GPIO_KEY1_NP + MAX_KEYS)) {
|
|
|
|
offset = -(GPIO_KEY1_NP - GPIO_KEY1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((val >= GPIO_SWT1) && (val < GPIO_SWT1 + MAX_SWITCHES)) {
|
|
|
|
offset = (GPIO_SWT1_NP - GPIO_SWT1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_SWT1_NP) && (val < GPIO_SWT1_NP + MAX_SWITCHES)) {
|
|
|
|
offset = -(GPIO_SWT1_NP - GPIO_SWT1);
|
|
|
|
}
|
2018-10-25 09:44:59 +01:00
|
|
|
|
2018-05-24 10:24:03 +01:00
|
|
|
if ((val >= GPIO_REL1) && (val < GPIO_REL1 + MAX_RELAYS)) {
|
|
|
|
offset = (GPIO_REL1_INV - GPIO_REL1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_REL1_INV) && (val < GPIO_REL1_INV + MAX_RELAYS)) {
|
|
|
|
offset = -(GPIO_REL1_INV - GPIO_REL1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((val >= GPIO_LED1) && (val < GPIO_LED1 + MAX_LEDS)) {
|
|
|
|
offset = (GPIO_LED1_INV - GPIO_LED1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_LED1_INV) && (val < GPIO_LED1_INV + MAX_LEDS)) {
|
|
|
|
offset = -(GPIO_LED1_INV - GPIO_LED1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((val >= GPIO_PWM1) && (val < GPIO_PWM1 + MAX_PWMS)) {
|
|
|
|
offset = (GPIO_PWM1_INV - GPIO_PWM1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_PWM1_INV) && (val < GPIO_PWM1_INV + MAX_PWMS)) {
|
|
|
|
offset = -(GPIO_PWM1_INV - GPIO_PWM1);
|
|
|
|
}
|
2018-11-05 16:27:02 +00:00
|
|
|
|
|
|
|
if ((val >= GPIO_CNTR1) && (val < GPIO_CNTR1 + MAX_COUNTERS)) {
|
|
|
|
offset = (GPIO_CNTR1_NP - GPIO_CNTR1);
|
|
|
|
}
|
|
|
|
if ((val >= GPIO_CNTR1_NP) && (val < GPIO_CNTR1_NP + MAX_COUNTERS)) {
|
|
|
|
offset = -(GPIO_CNTR1_NP - GPIO_CNTR1);
|
|
|
|
}
|
|
|
|
|
2018-05-24 10:24:03 +01:00
|
|
|
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
|
|
|
if (arr[i] == val) { return true; }
|
|
|
|
if (arr[i] == val + offset) { return true; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
void SetSerialBaudrate(int baudrate)
|
|
|
|
{
|
2018-03-18 12:47:30 +00:00
|
|
|
Settings.baudrate = baudrate / 1200;
|
2018-01-30 13:14:55 +00:00
|
|
|
if (Serial.baudRate() != baudrate) {
|
|
|
|
if (seriallog_level) {
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_SET_BAUDRATE_TO " %d"), baudrate);
|
|
|
|
AddLog(LOG_LEVEL_INFO);
|
|
|
|
}
|
|
|
|
delay(100);
|
|
|
|
Serial.flush();
|
2018-02-03 22:25:05 +00:00
|
|
|
Serial.begin(baudrate, serial_config);
|
2018-01-30 13:14:55 +00:00
|
|
|
delay(10);
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:21:26 +01:00
|
|
|
void ClaimSerial()
|
2018-05-09 12:43:22 +01:00
|
|
|
{
|
2018-05-10 16:21:26 +01:00
|
|
|
serial_local = 1;
|
|
|
|
AddLog_P(LOG_LEVEL_INFO, PSTR("SNS: Hardware Serial"));
|
|
|
|
SetSeriallog(LOG_LEVEL_NONE);
|
|
|
|
baudrate = Serial.baudRate();
|
|
|
|
Settings.baudrate = baudrate / 1200;
|
2018-05-09 12:43:22 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:21:44 +01:00
|
|
|
void SerialSendRaw(char *codes)
|
2018-09-16 15:09:00 +01:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char stemp[3];
|
|
|
|
uint8_t code;
|
2018-10-16 10:21:44 +01:00
|
|
|
|
|
|
|
int size = strlen(codes);
|
2018-09-16 15:09:00 +01:00
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
snprintf(stemp, sizeof(stemp), codes);
|
|
|
|
code = strtol(stemp, &p, 16);
|
|
|
|
Serial.write(code);
|
|
|
|
size -= 2;
|
|
|
|
codes += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:19:48 +00:00
|
|
|
uint32_t GetHash(const char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
uint32_t hash = 0;
|
|
|
|
for (uint16_t i = 0; i <= size; i++) {
|
|
|
|
hash += (uint8_t)*buffer++ * (i +1);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-05-28 14:52:42 +01:00
|
|
|
void ShowSource(int source)
|
|
|
|
{
|
|
|
|
if ((source > 0) && (source < SRC_MAX)) {
|
|
|
|
char stemp1[20];
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("SRC: %s"), GetTextIndexed(stemp1, sizeof(stemp1), source, kCommandSource));
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 13:27:33 +01:00
|
|
|
uint8_t ValidGPIO(uint8_t pin, uint8_t gpio)
|
|
|
|
{
|
|
|
|
uint8_t result = gpio;
|
|
|
|
if ((WEMOS == Settings.module) && (!Settings.flag3.user_esp8285_enable)) {
|
|
|
|
if ((pin == 9) || (pin == 10)) { result = GPIO_NONE; } // Disable possible flash GPIO9 and GPIO10
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:42:35 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Sleep aware time scheduler functions borrowed from ESPEasy
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
long TimeDifference(unsigned long prev, unsigned long next)
|
|
|
|
{
|
|
|
|
// Return the time difference as a signed value, taking into account the timers may overflow.
|
|
|
|
// Returned timediff is between -24.9 days and +24.9 days.
|
|
|
|
// Returned value is positive when "next" is after "prev"
|
|
|
|
long signed_diff = 0;
|
|
|
|
// To cast a value to a signed long, the difference may not exceed half 0xffffffffUL (= 4294967294)
|
|
|
|
const unsigned long half_max_unsigned_long = 2147483647u; // = 2^31 -1
|
|
|
|
if (next >= prev) {
|
|
|
|
const unsigned long diff = next - prev;
|
|
|
|
if (diff <= half_max_unsigned_long) { // Normal situation, just return the difference.
|
|
|
|
signed_diff = static_cast<long>(diff); // Difference is a positive value.
|
|
|
|
} else {
|
|
|
|
// prev has overflow, return a negative difference value
|
|
|
|
signed_diff = static_cast<long>((0xffffffffUL - next) + prev + 1u);
|
|
|
|
signed_diff = -1 * signed_diff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// next < prev
|
|
|
|
const unsigned long diff = prev - next;
|
|
|
|
if (diff <= half_max_unsigned_long) { // Normal situation, return a negative difference value
|
|
|
|
signed_diff = static_cast<long>(diff);
|
|
|
|
signed_diff = -1 * signed_diff;
|
|
|
|
} else {
|
|
|
|
// next has overflow, return a positive difference value
|
|
|
|
signed_diff = static_cast<long>((0xffffffffUL - prev) + next + 1u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signed_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
long TimePassedSince(unsigned long timestamp)
|
|
|
|
{
|
|
|
|
// Compute the number of milliSeconds passed since timestamp given.
|
|
|
|
// Note: value can be negative if the timestamp has not yet been reached.
|
|
|
|
return TimeDifference(timestamp, millis());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TimeReached(unsigned long timer)
|
|
|
|
{
|
|
|
|
// Check if a certain timeout has been reached.
|
|
|
|
const long passed = TimePassedSince(timer);
|
|
|
|
return (passed >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNextTimeInterval(unsigned long& timer, const unsigned long step)
|
|
|
|
{
|
|
|
|
timer += step;
|
|
|
|
const long passed = TimePassedSince(timer);
|
|
|
|
if (passed < 0) { return; } // Event has not yet happened, which is fine.
|
|
|
|
if (static_cast<unsigned long>(passed) > step) {
|
|
|
|
// No need to keep running behind, start again.
|
|
|
|
timer = millis() + step;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Try to get in sync again.
|
|
|
|
timer = millis() + (step - passed);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:36:45 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Fill feature list
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void GetFeatures()
|
|
|
|
{
|
2018-06-03 16:09:10 +01:00
|
|
|
feature_drv1 = 0x00000000; // xdrv_01_mqtt.ino, xdrv_01_light.ino, xdrv_04_snfbridge.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
|
|
|
|
// feature_drv1 |= 0x00000001;
|
|
|
|
// feature_drv1 |= 0x00000002;
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
feature_drv1 |= 0x00000004; // sonoff.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SPI
|
|
|
|
feature_drv1 |= 0x00000008; // sonoff.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISCOVERY
|
|
|
|
feature_drv1 |= 0x00000010; // sonoff.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ARDUINO_OTA
|
|
|
|
feature_drv1 |= 0x00000020; // sonoff.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MQTT_TLS
|
|
|
|
feature_drv1 |= 0x00000040; // sonoff.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00000080; // xdrv_02_webserver.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef WEBSERVER_ADVERTISE
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00000100; // xdrv_02_webserver.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_EMULATION
|
|
|
|
feature_drv1 |= 0x00000200; // xplg_wemohue.ino
|
|
|
|
#endif
|
|
|
|
#if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT)
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00000400; // xdrv_01_mqtt.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00000800; // xdrv_01_mqtt.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
2018-10-02 16:07:30 +01:00
|
|
|
#if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) // Obsolete since 6.2.1.11
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00001000; // xdrv_01_mqtt.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef MQTT_HOST_DISCOVERY
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00002000; // xdrv_01_mqtt.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_ARILUX_RF
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00004000; // xdrv_04_light.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_WS2812
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00008000; // xdrv_04_light.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_WS2812_DMA
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00010000; // xdrv_04_light.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_IR_REMOTE
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00020000; // xdrv_05_irremote.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_IR_HVAC
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00040000; // xdrv_05_irremote.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_IR_RECEIVE
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00080000; // xdrv_05_irremote.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_DOMOTICZ
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00100000; // xdrv_07_domoticz.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY
|
2018-08-25 11:26:36 +01:00
|
|
|
feature_drv1 |= 0x00200000; // xdrv_13_display.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2018-05-28 10:35:23 +01:00
|
|
|
feature_drv1 |= 0x00400000; // xdrv_12_home_assistant.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SERIAL_BRIDGE
|
|
|
|
feature_drv1 |= 0x00800000; // xdrv_08_serial_bridge.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TIMERS
|
|
|
|
feature_drv1 |= 0x01000000; // xdrv_09_timers.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SUNRISE
|
|
|
|
feature_drv1 |= 0x02000000; // xdrv_09_timers.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TIMERS_WEB
|
|
|
|
feature_drv1 |= 0x04000000; // xdrv_09_timers.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_RULES
|
|
|
|
feature_drv1 |= 0x08000000; // xdrv_10_rules.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_KNX
|
|
|
|
feature_drv1 |= 0x10000000; // xdrv_11_knx.ino
|
|
|
|
#endif
|
2018-07-15 14:21:48 +01:00
|
|
|
#ifdef USE_WPS
|
|
|
|
feature_drv1 |= 0x20000000; // support.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SMARTCONFIG
|
|
|
|
feature_drv1 |= 0x40000000; // support.ino
|
|
|
|
#endif
|
2018-10-02 16:07:30 +01:00
|
|
|
#if (MQTT_LIBRARY_TYPE == MQTT_ARDUINOMQTT)
|
|
|
|
feature_drv1 |= 0x80000000; // xdrv_01_mqtt.ino
|
|
|
|
#endif
|
2018-05-17 14:36:45 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
feature_drv2 = 0x00000000;
|
|
|
|
|
|
|
|
#ifdef USE_CONFIG_OVERRIDE
|
|
|
|
feature_drv2 |= 0x00000001; // user_config(_override).h
|
|
|
|
#endif
|
|
|
|
#ifdef BE_MINIMAL
|
|
|
|
feature_drv2 |= 0x00000002; // user_config(_override).h
|
|
|
|
#endif
|
2018-08-28 13:28:36 +01:00
|
|
|
#ifdef USE_SENSORS
|
2018-05-17 14:36:45 +01:00
|
|
|
feature_drv2 |= 0x00000004; // user_config(_override).h
|
|
|
|
#endif
|
|
|
|
#ifdef USE_CLASSIC
|
|
|
|
feature_drv2 |= 0x00000008; // user_config(_override).h
|
|
|
|
#endif
|
|
|
|
#ifdef USE_KNX_NO_EMULATION
|
|
|
|
feature_drv2 |= 0x00000010; // user_config(_override).h
|
|
|
|
#endif
|
2018-08-25 11:26:36 +01:00
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
feature_drv2 |= 0x00000020; // xdrv_13_display.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_GRAPH
|
|
|
|
feature_drv2 |= 0x00000040; // xdrv_13_display.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_LCD
|
|
|
|
feature_drv2 |= 0x00000080; // xdsp_01_lcd.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_SSD1306
|
|
|
|
feature_drv2 |= 0x00000100; // xdsp_02_ssd1306.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_MATRIX
|
|
|
|
feature_drv2 |= 0x00000200; // xdsp_03_matrix.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_ILI9341
|
|
|
|
feature_drv2 |= 0x00000400; // xdsp_04_ili9341.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_EPAPER
|
|
|
|
feature_drv2 |= 0x00000800; // xdsp_05_epaper.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DISPLAY_SH1106
|
|
|
|
feature_drv2 |= 0x00001000; // xdsp_06_sh1106.ino
|
|
|
|
#endif
|
2018-09-09 13:31:40 +01:00
|
|
|
#ifdef USE_MP3_PLAYER
|
|
|
|
feature_drv2 |= 0x00002000; // xdrv_14_mp3.ino
|
|
|
|
#endif
|
2018-10-21 11:44:45 +01:00
|
|
|
#ifdef USE_PCA9685
|
|
|
|
feature_drv2 |= 0x00004000; // xdrv_15_pca9685.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TUYA_DIMMER
|
|
|
|
feature_drv2 |= 0x00008000; // xdrv_16_tuyadimmer.ino
|
|
|
|
#endif
|
2018-10-25 17:17:58 +01:00
|
|
|
#ifdef USE_RC_SWITCH
|
|
|
|
feature_drv2 |= 0x00010000; // xdrv_17_rcswitch.ino
|
|
|
|
#endif
|
|
|
|
|
2018-05-17 14:36:45 +01:00
|
|
|
|
|
|
|
|
2018-09-09 13:31:40 +01:00
|
|
|
#ifdef NO_EXTRA_4K_HEAP
|
|
|
|
feature_drv2 |= 0x00800000; // sonoff_post.h
|
|
|
|
#endif
|
|
|
|
#ifdef VTABLES_IN_IRAM
|
|
|
|
feature_drv2 |= 0x01000000; // platformio.ini
|
|
|
|
#endif
|
|
|
|
#ifdef VTABLES_IN_DRAM
|
|
|
|
feature_drv2 |= 0x02000000; // platformio.ini
|
|
|
|
#endif
|
2018-05-17 14:36:45 +01:00
|
|
|
#ifdef VTABLES_IN_FLASH
|
|
|
|
feature_drv2 |= 0x04000000; // platformio.ini
|
|
|
|
#endif
|
|
|
|
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
|
|
|
|
feature_drv2 |= 0x08000000; // platformio.ini
|
|
|
|
#endif
|
|
|
|
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
|
|
|
|
feature_drv2 |= 0x10000000; // platformio.ini
|
|
|
|
#endif
|
|
|
|
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
|
|
|
|
feature_drv2 |= 0x20000000; // platformio.ini
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG_THEO
|
|
|
|
feature_drv2 |= 0x40000000; // xdrv_99_debug.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DEBUG_DRIVER
|
|
|
|
feature_drv2 |= 0x80000000; // xdrv_99_debug.ino
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
feature_sns1 = 0x00000000; // xsns_01_counter.ino, xsns_04_snfsc.ino
|
|
|
|
|
|
|
|
// feature_sns1 |= 0x00000001;
|
|
|
|
|
2018-05-19 16:31:55 +01:00
|
|
|
#ifdef USE_ADC_VCC
|
2018-05-17 14:36:45 +01:00
|
|
|
feature_sns1 |= 0x00000002; // support.ino (ADC)
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ENERGY_SENSOR
|
|
|
|
feature_sns1 |= 0x00000004; // xdrv_03_energy.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PZEM004T
|
2018-09-06 17:08:10 +01:00
|
|
|
feature_sns1 |= 0x00000008; // xnrg_03_pzem004t.ino
|
2018-05-17 14:36:45 +01:00
|
|
|
#endif
|
|
|
|
#ifdef USE_DS18B20
|
|
|
|
feature_sns1 |= 0x00000010; // xsns_05_ds18b20.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DS18x20_LEGACY
|
|
|
|
feature_sns1 |= 0x00000020; // xsns_05_ds18x20_legacy.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DS18x20
|
|
|
|
feature_sns1 |= 0x00000040; // xsns_05_ds18x20.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DHT
|
|
|
|
feature_sns1 |= 0x00000080; // xsns_06_dht.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SHT
|
|
|
|
feature_sns1 |= 0x00000100; // xsns_07_sht1x.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_HTU
|
|
|
|
feature_sns1 |= 0x00000200; // xsns_08_htu21.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BMP
|
|
|
|
feature_sns1 |= 0x00000400; // xsns_09_bmp.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BME680
|
|
|
|
feature_sns1 |= 0x00000800; // xsns_09_bmp.ino - BME680
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BH1750
|
|
|
|
feature_sns1 |= 0x00001000; // xsns_10_bh1750.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_VEML6070
|
|
|
|
feature_sns1 |= 0x00002000; // xsns_11_veml6070.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ADS1115_I2CDEV
|
|
|
|
feature_sns1 |= 0x00004000; // xsns_12_ads1115_i2cdev.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ADS1115
|
|
|
|
feature_sns1 |= 0x00008000; // xsns_12_ads1115.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_INA219
|
|
|
|
feature_sns1 |= 0x00010000; // xsns_13_ina219.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SHT3X
|
|
|
|
feature_sns1 |= 0x00020000; // xsns_14_sht3x.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MHZ19
|
|
|
|
feature_sns1 |= 0x00040000; // xsns_15_mhz19.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TSL2561
|
|
|
|
feature_sns1 |= 0x00080000; // xsns_16_tsl2561.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SENSEAIR
|
|
|
|
feature_sns1 |= 0x00100000; // xsns_17_senseair.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PMS5003
|
|
|
|
feature_sns1 |= 0x00200000; // xsns_18_pms5003.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MGS
|
|
|
|
feature_sns1 |= 0x00400000; // xsns_19_mgs.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_NOVA_SDS
|
|
|
|
feature_sns1 |= 0x00800000; // xsns_20_novasds.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SGP30
|
|
|
|
feature_sns1 |= 0x01000000; // xsns_21_sgp30.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SR04
|
|
|
|
feature_sns1 |= 0x02000000; // xsns_22_sr04.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SDM120
|
|
|
|
feature_sns1 |= 0x04000000; // xsns_23_sdm120.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SI1145
|
|
|
|
feature_sns1 |= 0x08000000; // xsns_24_si1145.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SDM630
|
|
|
|
feature_sns1 |= 0x10000000; // xsns_25_sdm630.ino
|
|
|
|
#endif
|
2018-06-24 16:50:42 +01:00
|
|
|
#ifdef USE_LM75AD
|
|
|
|
feature_sns1 |= 0x20000000; // xsns_26_lm75ad.ino
|
|
|
|
#endif
|
2018-06-29 10:15:27 +01:00
|
|
|
#ifdef USE_APDS9960
|
|
|
|
feature_sns1 |= 0x40000000; // xsns_27_apds9960.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_TM1638
|
|
|
|
feature_sns1 |= 0x80000000; // xsns_28_tm1638.ino
|
|
|
|
#endif
|
2018-05-17 14:36:45 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
feature_sns2 = 0x00000000;
|
2018-07-31 10:49:23 +01:00
|
|
|
|
|
|
|
#ifdef USE_MCP230xx
|
|
|
|
feature_sns2 |= 0x00000001; // xsns_29_mcp230xx.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MPR121
|
|
|
|
feature_sns2 |= 0x00000002; // xsns_30_mpr121.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_CCS811
|
|
|
|
feature_sns2 |= 0x00000004; // xsns_31_ccs811.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MPU6050
|
|
|
|
feature_sns2 |= 0x00000008; // xsns_32_mpu6050.ino
|
|
|
|
#endif
|
2018-08-25 11:26:36 +01:00
|
|
|
#ifdef USE_MCP230xx_OUTPUT
|
|
|
|
feature_sns2 |= 0x00000010; // xsns_29_mcp230xx.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MCP230xx_DISPLAYOUTPUT
|
|
|
|
feature_sns2 |= 0x00000020; // xsns_29_mcp230xx.ino
|
|
|
|
#endif
|
2018-09-06 17:08:10 +01:00
|
|
|
#ifdef USE_HLW8012
|
|
|
|
feature_sns2 |= 0x00000040; // xnrg_01_hlw8012.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_CSE7766
|
|
|
|
feature_sns2 |= 0x00000080; // xnrg_02_cse7766.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_MCP39F501
|
|
|
|
feature_sns2 |= 0x00000100; // xnrg_04_mcp39f501.ino
|
|
|
|
#endif
|
2018-10-17 11:27:05 +01:00
|
|
|
#ifdef USE_PZEM_AC
|
|
|
|
feature_sns2 |= 0x00000200; // xnrg_05_pzem_ac.ino
|
2018-09-06 17:08:10 +01:00
|
|
|
#endif
|
2018-10-11 16:33:07 +01:00
|
|
|
#ifdef USE_DS3231
|
|
|
|
feature_sns2 |= 0x00000400; // xsns_33_ds3231.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_HX711
|
2018-10-17 11:27:05 +01:00
|
|
|
feature_sns2 |= 0x00000800; // xsns_34_hx711.ino
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PZEM_DC
|
|
|
|
feature_sns2 |= 0x00001000; // xnrg_06_pzem_dc.ino
|
2018-10-11 16:33:07 +01:00
|
|
|
#endif
|
2018-10-21 11:44:45 +01:00
|
|
|
#ifdef USE_TX20_WIND_SENSOR
|
|
|
|
feature_sns2 |= 0x00002000; // xsns_35_tx20.ino
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-09-06 17:08:10 +01:00
|
|
|
|
2018-05-17 14:36:45 +01:00
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Wifi
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-03-11 14:23:17 +00:00
|
|
|
#define WIFI_CONFIG_SEC 180 // seconds before restart
|
|
|
|
#define WIFI_CHECK_SEC 20 // seconds
|
|
|
|
#define WIFI_RETRY_OFFSET_SEC 20 // seconds
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t wifi_counter;
|
2018-03-11 14:23:17 +00:00
|
|
|
uint8_t wifi_retry_init;
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t wifi_retry;
|
|
|
|
uint8_t wifi_status;
|
|
|
|
uint8_t wps_result;
|
|
|
|
uint8_t wifi_config_type = 0;
|
|
|
|
uint8_t wifi_config_counter = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
int WifiGetRssiAsQuality(int rssi)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
int quality = 0;
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (rssi <= -100) {
|
2017-01-28 13:41:01 +00:00
|
|
|
quality = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
} else if (rssi >= -50) {
|
2017-01-28 13:41:01 +00:00
|
|
|
quality = 100;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
quality = 2 * (rssi + 100);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
return quality;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean WifiConfigCounter()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if (wifi_config_counter) {
|
2018-01-18 15:19:28 +00:00
|
|
|
wifi_config_counter = WIFI_CONFIG_SEC;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
return (wifi_config_counter);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "user_interface.h"
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiWpsStatusCallback(wps_cb_status status);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiWpsStatusCallback(wps_cb_status status)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
/* from user_interface.h:
|
|
|
|
enum wps_cb_status {
|
|
|
|
WPS_CB_ST_SUCCESS = 0,
|
|
|
|
WPS_CB_ST_FAILED,
|
|
|
|
WPS_CB_ST_TIMEOUT,
|
|
|
|
WPS_CB_ST_WEP, // WPS failed because that WEP is not supported
|
|
|
|
WPS_CB_ST_SCAN_ERR, // can not find the target WPS AP
|
|
|
|
};
|
|
|
|
*/
|
2017-10-18 17:22:34 +01:00
|
|
|
wps_result = status;
|
|
|
|
if (WPS_CB_ST_SUCCESS == wps_result) {
|
2017-01-28 13:41:01 +00:00
|
|
|
wifi_wps_disable();
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_WIFI D_WPS_FAILED_WITH_STATUS " %d"), wps_result);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
wifi_config_counter = 2;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean WifiWpsConfigDone(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
return (!wps_result);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean WifiWpsConfigBegin(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
wps_result = 99;
|
2018-07-15 14:21:48 +01:00
|
|
|
if (!wifi_wps_disable()) { return false; }
|
|
|
|
if (!wifi_wps_enable(WPS_TYPE_PBC)) { return false; } // so far only WPS_TYPE_PBC is supported (SDK 2.0.0)
|
|
|
|
if (!wifi_set_wps_cb((wps_st_cb_t) &WifiWpsStatusCallback)) { return false; }
|
|
|
|
if (!wifi_wps_start()) { return false; }
|
2017-01-28 13:41:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiConfig(uint8_t type)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!wifi_config_type) {
|
2018-07-15 14:21:48 +01:00
|
|
|
if ((WIFI_RETRY == type) || (WIFI_WAIT == type)) { return; }
|
2018-04-10 10:45:53 +01:00
|
|
|
#if defined(USE_WEBSERVER) && defined(USE_EMULATION)
|
2017-10-18 17:22:34 +01:00
|
|
|
UdpDisconnect();
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2018-07-15 14:21:48 +01:00
|
|
|
WiFi.disconnect(); // Solve possible Wifi hangs
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_config_type = type;
|
2018-07-15 14:21:48 +01:00
|
|
|
|
2018-07-14 14:35:56 +01:00
|
|
|
#ifndef USE_WPS
|
2018-07-15 14:21:48 +01:00
|
|
|
if (WIFI_WPSCONFIG == wifi_config_type) { wifi_config_type = WIFI_MANAGER; }
|
2018-07-14 14:35:56 +01:00
|
|
|
#endif // USE_WPS
|
|
|
|
#ifndef USE_WEBSERVER
|
2018-07-15 14:21:48 +01:00
|
|
|
if (WIFI_MANAGER == wifi_config_type) { wifi_config_type = WIFI_SMARTCONFIG; }
|
2018-07-14 17:07:25 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2018-07-15 14:21:48 +01:00
|
|
|
#ifndef USE_SMARTCONFIG
|
|
|
|
if (WIFI_SMARTCONFIG == wifi_config_type) { wifi_config_type = WIFI_SERIAL; }
|
|
|
|
#endif // USE_SMARTCONFIG
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_config_counter = WIFI_CONFIG_SEC; // Allow up to WIFI_CONFIG_SECS seconds for phone to provide ssid/pswd
|
|
|
|
wifi_counter = wifi_config_counter +5;
|
2017-01-28 13:41:01 +00:00
|
|
|
blinks = 1999;
|
2017-10-18 17:22:34 +01:00
|
|
|
if (WIFI_RESTART == wifi_config_type) {
|
|
|
|
restart_flag = 2;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-07-15 14:21:48 +01:00
|
|
|
else if (WIFI_SERIAL == wifi_config_type) {
|
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_6_SERIAL " " D_ACTIVE_FOR_3_MINUTES));
|
|
|
|
}
|
|
|
|
#ifdef USE_SMARTCONFIG
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (WIFI_SMARTCONFIG == wifi_config_type) {
|
2018-02-10 14:13:27 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_1_SMARTCONFIG " " D_ACTIVE_FOR_3_MINUTES));
|
2017-01-28 13:41:01 +00:00
|
|
|
WiFi.beginSmartConfig();
|
|
|
|
}
|
2018-07-15 14:21:48 +01:00
|
|
|
#endif // USE_SMARTCONFIG
|
2018-07-14 14:35:56 +01:00
|
|
|
#ifdef USE_WPS
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (WIFI_WPSCONFIG == wifi_config_type) {
|
|
|
|
if (WifiWpsConfigBegin()) {
|
2018-02-10 14:13:27 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_3_WPSCONFIG " " D_ACTIVE_FOR_3_MINUTES));
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-02-10 14:13:27 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_3_WPSCONFIG " " D_FAILED_TO_START));
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_config_counter = 3;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 14:35:56 +01:00
|
|
|
#endif // USE_WPS
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (WIFI_MANAGER == wifi_config_type) {
|
2018-02-10 14:13:27 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_WCFG_2_WIFIMANAGER " " D_ACTIVE_FOR_3_MINUTES));
|
2017-10-18 17:22:34 +01:00
|
|
|
WifiManagerBegin();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:38:55 +01:00
|
|
|
void WiFiSetSleepMode()
|
|
|
|
{
|
|
|
|
/* Excerpt from the esp8266 non os sdk api reference (v2.2.1):
|
|
|
|
* Sets sleep type for power saving. Set WIFI_NONE_SLEEP to disable power saving.
|
|
|
|
* - Default mode: WIFI_MODEM_SLEEP.
|
|
|
|
* - In order to lower the power comsumption, ESP8266 changes the TCP timer
|
|
|
|
* tick from 250ms to 3s in WIFI_LIGHT_SLEEP mode, which leads to increased timeout for
|
|
|
|
* TCP timer. Therefore, the WIFI_MODEM_SLEEP or deep-sleep mode should be used
|
|
|
|
* where there is a requirement for the accurancy of the TCP timer.
|
2018-08-25 10:59:21 +01:00
|
|
|
*
|
|
|
|
* Sleep is disabled in core 2.4.1 and 2.4.2 as there are bugs in their SDKs
|
|
|
|
* See https://github.com/arendst/Sonoff-Tasmota/issues/2559
|
2018-08-24 15:38:55 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-08 21:25:42 +01:00
|
|
|
// Sleep explanation: https://github.com/esp8266/Arduino/blob/3f0c601cfe81439ce17e9bd5d28994a7ed144482/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp#L255
|
2018-08-25 10:59:21 +01:00
|
|
|
#if defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
|
|
|
|
#else // Enabled in 2.3.0, 2.4.0 and stage
|
2018-08-24 15:38:55 +01:00
|
|
|
if (sleep) {
|
|
|
|
WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
|
|
|
|
} else {
|
2018-10-08 21:25:42 +01:00
|
|
|
WiFi.setSleepMode(WIFI_MODEM_SLEEP); // Disable sleep (Esp8288/Arduino core and sdk default)
|
2018-08-24 15:38:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiBegin(uint8_t flag)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
const char kWifiPhyMode[] = " BGN";
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-04-10 10:45:53 +01:00
|
|
|
#if defined(USE_WEBSERVER) && defined(USE_EMULATION)
|
2017-10-18 17:22:34 +01:00
|
|
|
UdpDisconnect();
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2018-01-06 16:34:42 +00:00
|
|
|
|
|
|
|
#ifdef ARDUINO_ESP8266_RELEASE_2_3_0 // (!strncmp_P(ESP.getSdkVersion(),PSTR("1.5.3"),5))
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_WIFI, PSTR(D_PATCH_ISSUE_2186));
|
2018-02-24 15:37:33 +00:00
|
|
|
WiFi.mode(WIFI_OFF); // See https://github.com/esp8266/Arduino/issues/2186
|
2018-01-06 16:34:42 +00:00
|
|
|
#endif
|
2018-10-16 13:00:14 +01:00
|
|
|
|
|
|
|
WiFi.persistent(false); // Solve possible wifi init errors (re-add at 6.2.1.16 #4044, #4083)
|
2018-02-24 15:37:33 +00:00
|
|
|
WiFi.disconnect(true); // Delete SDK wifi config
|
|
|
|
delay(200);
|
2017-01-28 13:41:01 +00:00
|
|
|
WiFi.mode(WIFI_STA); // Disable AP mode
|
2018-08-24 15:38:55 +01:00
|
|
|
WiFiSetSleepMode();
|
2018-07-15 14:21:48 +01:00
|
|
|
// if (WiFi.getPhyMode() != WIFI_PHY_MODE_11N) { WiFi.setPhyMode(WIFI_PHY_MODE_11N); }
|
|
|
|
if (!WiFi.getAutoConnect()) { WiFi.setAutoConnect(true); }
|
2017-01-28 13:41:01 +00:00
|
|
|
// WiFi.setAutoReconnect(true);
|
|
|
|
switch (flag) {
|
|
|
|
case 0: // AP1
|
|
|
|
case 1: // AP2
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.sta_active = flag;
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.sta_active ^= 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
} // 3: Current AP
|
2018-07-15 14:21:48 +01:00
|
|
|
if ('\0' == Settings.sta_ssid[Settings.sta_active][0]) { Settings.sta_active ^= 1; } // Skip empty SSID
|
2017-10-18 17:22:34 +01:00
|
|
|
if (Settings.ip_address[0]) {
|
|
|
|
WiFi.config(Settings.ip_address[0], Settings.ip_address[1], Settings.ip_address[2], Settings.ip_address[3]); // Set static IP
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
WiFi.hostname(my_hostname);
|
|
|
|
WiFi.begin(Settings.sta_ssid[Settings.sta_active], Settings.sta_pwd[Settings.sta_active]);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_WIFI D_CONNECTING_TO_AP "%d %s " D_IN_MODE " 11%c " D_AS " %s..."),
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active], kWifiPhyMode[WiFi.getPhyMode() & 0x3], my_hostname);
|
|
|
|
AddLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:27:40 +00:00
|
|
|
void WifiSetState(uint8_t state)
|
2018-07-28 14:06:31 +01:00
|
|
|
{
|
|
|
|
if (state == global_state.wifi_down) {
|
|
|
|
if (state) {
|
|
|
|
rules_flag.wifi_connected = 1;
|
|
|
|
} else {
|
|
|
|
rules_flag.wifi_disconnected = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
global_state.wifi_down = state ^1;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiCheckIp()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
2018-10-31 10:27:40 +00:00
|
|
|
WifiSetState(1);
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_counter = WIFI_CHECK_SEC;
|
2018-03-11 14:23:17 +00:00
|
|
|
wifi_retry = wifi_retry_init;
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P((wifi_status != WL_CONNECTED) ? LOG_LEVEL_INFO : LOG_LEVEL_DEBUG_MORE, S_LOG_WIFI, PSTR(D_CONNECTED));
|
|
|
|
if (wifi_status != WL_CONNECTED) {
|
|
|
|
// AddLog_P(LOG_LEVEL_INFO, PSTR("Wifi: Set IP addresses"));
|
|
|
|
Settings.ip_address[1] = (uint32_t)WiFi.gatewayIP();
|
|
|
|
Settings.ip_address[2] = (uint32_t)WiFi.subnetMask();
|
|
|
|
Settings.ip_address[3] = (uint32_t)WiFi.dnsIP();
|
2017-03-25 16:24:11 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_status = WL_CONNECTED;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-10-31 10:27:40 +00:00
|
|
|
WifiSetState(0);
|
2018-07-15 14:21:48 +01:00
|
|
|
uint8_t wifi_config_tool = Settings.sta_config;
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_status = WiFi.status();
|
|
|
|
switch (wifi_status) {
|
2017-01-28 13:41:01 +00:00
|
|
|
case WL_CONNECTED:
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_CONNECT_FAILED_NO_IP_ADDRESS));
|
|
|
|
wifi_status = 0;
|
2018-03-11 14:23:17 +00:00
|
|
|
wifi_retry = wifi_retry_init;
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
case WL_NO_SSID_AVAIL:
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_CONNECT_FAILED_AP_NOT_REACHED));
|
|
|
|
if (WIFI_WAIT == Settings.sta_config) {
|
2018-03-11 14:23:17 +00:00
|
|
|
wifi_retry = wifi_retry_init;
|
2017-09-16 13:19:47 +01:00
|
|
|
} else {
|
2018-03-11 14:23:17 +00:00
|
|
|
if (wifi_retry > (wifi_retry_init / 2)) {
|
|
|
|
wifi_retry = wifi_retry_init / 2;
|
2017-09-16 13:19:47 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (wifi_retry) {
|
|
|
|
wifi_retry = 0;
|
2017-09-16 13:19:47 +01:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
case WL_CONNECT_FAILED:
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_CONNECT_FAILED_WRONG_PASSWORD));
|
2018-03-11 14:23:17 +00:00
|
|
|
if (wifi_retry > (wifi_retry_init / 2)) {
|
|
|
|
wifi_retry = wifi_retry_init / 2;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (wifi_retry) {
|
|
|
|
wifi_retry = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
default: // WL_IDLE_STATUS and WL_DISCONNECTED
|
2018-03-11 14:23:17 +00:00
|
|
|
if (!wifi_retry || ((wifi_retry_init / 2) == wifi_retry)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_INFO, S_LOG_WIFI, PSTR(D_CONNECT_FAILED_AP_TIMEOUT));
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-07-15 14:21:48 +01:00
|
|
|
if (('\0' == Settings.sta_ssid[0][0]) && ('\0' == Settings.sta_ssid[1][0])) {
|
|
|
|
wifi_config_tool = WIFI_CONFIG_NO_SSID; // Skip empty SSIDs and start Wifi config tool
|
|
|
|
wifi_retry = 0;
|
|
|
|
} else {
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_WIFI, PSTR(D_ATTEMPTING_CONNECTION));
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (wifi_retry) {
|
2018-03-11 14:23:17 +00:00
|
|
|
if (wifi_retry_init == wifi_retry) {
|
2017-10-18 17:22:34 +01:00
|
|
|
WifiBegin(3); // Select default SSID
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-03-11 14:23:17 +00:00
|
|
|
if ((Settings.sta_config != WIFI_WAIT) && ((wifi_retry_init / 2) == wifi_retry)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
WifiBegin(2); // Select alternate SSID
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_counter = 1;
|
|
|
|
wifi_retry--;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-07-15 14:21:48 +01:00
|
|
|
WifiConfig(wifi_config_tool);
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_counter = 1;
|
2018-03-11 14:23:17 +00:00
|
|
|
wifi_retry = wifi_retry_init;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiCheck(uint8_t param)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_counter--;
|
2017-01-28 13:41:01 +00:00
|
|
|
switch (param) {
|
2018-07-15 14:21:48 +01:00
|
|
|
case WIFI_SERIAL:
|
2017-01-28 13:41:01 +00:00
|
|
|
case WIFI_SMARTCONFIG:
|
|
|
|
case WIFI_MANAGER:
|
|
|
|
case WIFI_WPSCONFIG:
|
2017-10-18 17:22:34 +01:00
|
|
|
WifiConfig(param);
|
2017-01-28 13:41:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-10-18 17:22:34 +01:00
|
|
|
if (wifi_config_counter) {
|
|
|
|
wifi_config_counter--;
|
|
|
|
wifi_counter = wifi_config_counter +5;
|
|
|
|
if (wifi_config_counter) {
|
2018-07-15 14:21:48 +01:00
|
|
|
#ifdef USE_SMARTCONFIG
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((WIFI_SMARTCONFIG == wifi_config_type) && WiFi.smartConfigDone()) {
|
|
|
|
wifi_config_counter = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-07-15 14:21:48 +01:00
|
|
|
#endif // USE_SMARTCONFIG
|
2018-07-14 14:35:56 +01:00
|
|
|
#ifdef USE_WPS
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((WIFI_WPSCONFIG == wifi_config_type) && WifiWpsConfigDone()) {
|
|
|
|
wifi_config_counter = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-07-14 14:35:56 +01:00
|
|
|
#endif // USE_WPS
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!wifi_config_counter) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (strlen(WiFi.SSID().c_str())) {
|
2017-10-18 17:22:34 +01:00
|
|
|
strlcpy(Settings.sta_ssid[0], WiFi.SSID().c_str(), sizeof(Settings.sta_ssid[0]));
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
if (strlen(WiFi.psk().c_str())) {
|
2017-10-18 17:22:34 +01:00
|
|
|
strlcpy(Settings.sta_pwd[0], WiFi.psk().c_str(), sizeof(Settings.sta_pwd[0]));
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.sta_active = 0;
|
2018-04-21 17:17:24 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_WIFI D_WCFG_1_SMARTCONFIG D_CMND_SSID "1 %s"), Settings.sta_ssid[0]);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!wifi_config_counter) {
|
2018-07-15 14:21:48 +01:00
|
|
|
#ifdef USE_SMARTCONFIG
|
|
|
|
if (WIFI_SMARTCONFIG == wifi_config_type) { WiFi.stopSmartConfig(); }
|
|
|
|
#endif // USE_SMARTCONFIG
|
2018-07-14 14:35:56 +01:00
|
|
|
// SettingsSdkErase(); // Disabled v6.1.0b due to possible bad wifi connects
|
2017-10-18 17:22:34 +01:00
|
|
|
restart_flag = 2;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
if (wifi_counter <= 0) {
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE, S_LOG_WIFI, PSTR(D_CHECKING_CONNECTION));
|
|
|
|
wifi_counter = WIFI_CHECK_SEC;
|
|
|
|
WifiCheckIp();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0) && !wifi_config_type) {
|
2018-10-31 10:27:40 +00:00
|
|
|
WifiSetState(1);
|
2018-01-10 13:10:25 +00:00
|
|
|
#ifdef BE_MINIMAL
|
|
|
|
if (1 == RtcSettings.ota_loader) {
|
|
|
|
RtcSettings.ota_loader = 0;
|
|
|
|
ota_state_flag = 3;
|
|
|
|
}
|
|
|
|
#endif // BE_MINIMAL
|
2018-10-28 16:57:25 +00:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DISCOVERY
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!mdns_begun) {
|
2018-10-28 16:57:25 +00:00
|
|
|
if (mdns_delayed_start) {
|
|
|
|
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MDNS D_ATTEMPTING_CONNECTION));
|
|
|
|
mdns_delayed_start--;
|
|
|
|
} else {
|
|
|
|
mdns_delayed_start = Settings.param[P_MDNS_DELAYED_START];
|
|
|
|
mdns_begun = MDNS.begin(my_hostname);
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MDNS "%s"), (mdns_begun) ? D_INITIALIZED : D_FAILED);
|
|
|
|
AddLog(LOG_LEVEL_INFO);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_DISCOVERY
|
2018-10-28 16:57:25 +00:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-10-18 17:22:34 +01:00
|
|
|
if (Settings.webserver) {
|
|
|
|
StartWebserver(Settings.webserver, WiFi.localIP());
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DISCOVERY
|
|
|
|
#ifdef WEBSERVER_ADVERTISE
|
2018-10-28 16:57:25 +00:00
|
|
|
if (mdns_begun) {
|
|
|
|
MDNS.addService("http", "tcp", WEB_PORT);
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
#endif // WEBSERVER_ADVERTISE
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DISCOVERY
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
StopWebserver();
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-01-29 20:36:12 +00:00
|
|
|
#ifdef USE_EMULATION
|
2018-07-15 14:21:48 +01:00
|
|
|
if (Settings.flag2.emulation) { UdpConnect(); }
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2018-10-28 16:57:25 +00:00
|
|
|
|
2018-05-11 14:00:41 +01:00
|
|
|
#ifdef USE_KNX
|
|
|
|
if (!knx_started && Settings.flag.knx_enabled) {
|
|
|
|
KNXStart();
|
|
|
|
knx_started = true;
|
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
2018-10-28 16:57:25 +00:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-10-31 10:27:40 +00:00
|
|
|
WifiSetState(0);
|
2018-04-10 10:45:53 +01:00
|
|
|
#if defined(USE_WEBSERVER) && defined(USE_EMULATION)
|
2017-10-18 17:22:34 +01:00
|
|
|
UdpDisconnect();
|
2017-01-29 20:36:12 +00:00
|
|
|
#endif // USE_EMULATION
|
2017-10-18 17:22:34 +01:00
|
|
|
mdns_begun = false;
|
2018-05-11 14:00:41 +01:00
|
|
|
#ifdef USE_KNX
|
|
|
|
knx_started = false;
|
|
|
|
#endif // USE_KNX
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
int WifiState()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-10-30 14:34:31 +00:00
|
|
|
int state = -1;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-11-04 17:00:07 +00:00
|
|
|
if (!global_state.wifi_down) { state = WIFI_RESTART; }
|
2018-07-15 14:21:48 +01:00
|
|
|
if (wifi_config_type) { state = wifi_config_type; }
|
2017-01-28 13:41:01 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void WifiConnect()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-11-01 12:00:05 +00:00
|
|
|
WifiSetState(0);
|
2018-06-03 16:09:10 +01:00
|
|
|
WiFi.persistent(false); // Solve possible wifi init errors
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_status = 0;
|
2018-03-11 14:23:17 +00:00
|
|
|
wifi_retry_init = WIFI_RETRY_OFFSET_SEC + ((ESP.getChipId() & 0xF) * 2);
|
|
|
|
wifi_retry = wifi_retry_init;
|
2017-10-18 17:22:34 +01:00
|
|
|
wifi_counter = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 14:35:56 +01:00
|
|
|
/*
|
|
|
|
// Enable from 6.0.0a until 6.1.0a - disabled due to possible cause of bad wifi connect on core 2.3.0
|
2018-06-03 16:09:10 +01:00
|
|
|
void WifiDisconnect()
|
|
|
|
{
|
|
|
|
// Courtesy of EspEasy
|
|
|
|
WiFi.persistent(true); // use SDK storage of SSID/WPA parameters
|
|
|
|
ETS_UART_INTR_DISABLE();
|
|
|
|
wifi_station_disconnect(); // this will store empty ssid/wpa into sdk storage
|
|
|
|
ETS_UART_INTR_ENABLE();
|
|
|
|
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
void EspRestart()
|
|
|
|
{
|
2018-07-14 14:35:56 +01:00
|
|
|
// This results in exception 3 on restarts
|
|
|
|
delay(100); // Allow time for message xfer - disabled v6.1.0b
|
2018-06-03 16:09:10 +01:00
|
|
|
WifiDisconnect();
|
|
|
|
ESP.restart();
|
|
|
|
}
|
2018-07-14 14:35:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void EspRestart()
|
|
|
|
{
|
|
|
|
ESP.restart();
|
|
|
|
}
|
2018-06-03 16:09:10 +01:00
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Basic I2C routines
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#define I2C_RETRY_COUNTER 3
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
uint32_t i2c_buffer = 0;
|
|
|
|
|
|
|
|
bool I2cValidRead(uint8_t addr, uint8_t reg, uint8_t size)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
byte x = I2C_RETRY_COUNTER;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
i2c_buffer = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
do {
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
Wire.beginTransmission(addr); // start transmission to device
|
|
|
|
Wire.write(reg); // sends register address to read from
|
|
|
|
if (0 == Wire.endTransmission(false)) { // Try to become I2C Master, send data and collect bytes, keep master status for next request...
|
|
|
|
Wire.requestFrom((int)addr, (int)size); // send data n-bytes read
|
2017-04-25 17:24:42 +01:00
|
|
|
if (Wire.available() == size) {
|
2017-10-12 13:09:19 +01:00
|
|
|
for (byte i = 0; i < size; i++) {
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
i2c_buffer = i2c_buffer << 8 | Wire.read(); // receive DATA
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
x--;
|
|
|
|
} while (Wire.endTransmission(true) != 0 && x != 0); // end transmission
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidRead8(uint8_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
bool status = I2cValidRead(addr, reg, 1);
|
|
|
|
*data = (uint8_t)i2c_buffer;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidRead16(uint16_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
bool status = I2cValidRead(addr, reg, 2);
|
|
|
|
*data = (uint16_t)i2c_buffer;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidReadS16(int16_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
bool status = I2cValidRead(addr, reg, 2);
|
|
|
|
*data = (int16_t)i2c_buffer;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidRead16LE(uint16_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
uint16_t ldata;
|
|
|
|
bool status = I2cValidRead16(&ldata, addr, reg);
|
|
|
|
*data = (ldata >> 8) | (ldata << 8);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidReadS16_LE(int16_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
uint16_t ldata;
|
|
|
|
bool status = I2cValidRead16LE(&ldata, addr, reg);
|
|
|
|
*data = (int16_t)ldata;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cValidRead24(int32_t *data, uint8_t addr, uint8_t reg)
|
|
|
|
{
|
|
|
|
bool status = I2cValidRead(addr, reg, 3);
|
|
|
|
*data = i2c_buffer;
|
|
|
|
return status;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t I2cRead8(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
I2cValidRead(addr, reg, 1);
|
|
|
|
return (uint8_t)i2c_buffer;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t I2cRead16(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
I2cValidRead(addr, reg, 2);
|
|
|
|
return (uint16_t)i2c_buffer;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
int16_t I2cReadS16(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
I2cValidRead(addr, reg, 2);
|
|
|
|
return (int16_t)i2c_buffer;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t I2cRead16LE(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
I2cValidRead(addr, reg, 2);
|
|
|
|
uint16_t temp = (uint16_t)i2c_buffer;
|
2017-01-28 13:41:01 +00:00
|
|
|
return (temp >> 8) | (temp << 8);
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
int16_t I2cReadS16_LE(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
return (int16_t)I2cRead16LE(addr, reg);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
int32_t I2cRead24(uint8_t addr, uint8_t reg)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
I2cValidRead(addr, reg, 3);
|
|
|
|
return i2c_buffer;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-11-11 11:33:30 +00:00
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
bool I2cWrite(uint8_t addr, uint8_t reg, uint32_t val, uint8_t size)
|
2017-11-07 14:57:24 +00:00
|
|
|
{
|
|
|
|
byte x = I2C_RETRY_COUNTER;
|
|
|
|
|
|
|
|
do {
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
Wire.beginTransmission((uint8_t)addr); // start transmission to device
|
|
|
|
Wire.write(reg); // sends register address to write to
|
2017-11-28 20:57:35 +00:00
|
|
|
uint8_t bytes = size;
|
|
|
|
while (bytes--) {
|
|
|
|
Wire.write((val >> (8 * bytes)) & 0xFF); // write data
|
|
|
|
}
|
2017-11-07 14:57:24 +00:00
|
|
|
x--;
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
} while (Wire.endTransmission(true) != 0 && x != 0); // end transmission
|
|
|
|
return (x);
|
2017-11-07 14:57:24 +00:00
|
|
|
}
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
bool I2cWrite8(uint8_t addr, uint8_t reg, uint16_t val)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
return I2cWrite(addr, reg, val, 1);
|
2017-11-11 11:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool I2cWrite16(uint8_t addr, uint8_t reg, uint16_t val)
|
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
return I2cWrite(addr, reg, val, 2);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:41:00 +01:00
|
|
|
int8_t I2cReadBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission((uint8_t)addr);
|
|
|
|
Wire.write((uint8_t)reg);
|
|
|
|
Wire.endTransmission();
|
|
|
|
if (len != Wire.requestFrom((uint8_t)addr, (byte)len)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while (len--) {
|
|
|
|
*reg_data = (uint8_t)Wire.read();
|
|
|
|
reg_data++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t I2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission((uint8_t)addr);
|
|
|
|
Wire.write((uint8_t)reg);
|
|
|
|
while (len--) {
|
|
|
|
Wire.write(*reg_data);
|
|
|
|
reg_data++;
|
|
|
|
}
|
|
|
|
Wire.endTransmission();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void I2cScan(char *devs, unsigned int devs_len)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-09-29 11:34:24 +01:00
|
|
|
// Return error codes defined in twi.h and core_esp8266_si2c.c
|
|
|
|
// I2C_OK 0
|
|
|
|
// I2C_SCL_HELD_LOW 1 = SCL held low by another device, no procedure available to recover
|
|
|
|
// I2C_SCL_HELD_LOW_AFTER_READ 2 = I2C bus error. SCL held low beyond slave clock stretch time
|
|
|
|
// I2C_SDA_HELD_LOW 3 = I2C bus error. SDA line held low by slave/another_master after n bits
|
|
|
|
// I2C_SDA_HELD_LOW_AFTER_INIT 4 = line busy. SDA again held low by another device. 2nd master?
|
|
|
|
|
|
|
|
byte error = 0;
|
|
|
|
byte address = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
byte any = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-01-06 16:34:42 +00:00
|
|
|
snprintf_P(devs, devs_len, PSTR("{\"" D_CMND_I2CSCAN "\":\"" D_JSON_I2CSCAN_DEVICES_FOUND_AT));
|
2017-01-28 13:41:01 +00:00
|
|
|
for (address = 1; address <= 127; address++) {
|
|
|
|
Wire.beginTransmission(address);
|
|
|
|
error = Wire.endTransmission();
|
2017-04-25 17:24:42 +01:00
|
|
|
if (0 == error) {
|
2017-01-28 13:41:01 +00:00
|
|
|
any = 1;
|
2018-09-29 11:34:24 +01:00
|
|
|
snprintf_P(devs, devs_len, PSTR("%s 0x%02x"), devs, address);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-09-29 11:34:24 +01:00
|
|
|
else if (error != 2) { // Seems to happen anyway using this scan
|
|
|
|
any = 2;
|
|
|
|
snprintf_P(devs, devs_len, PSTR("{\"" D_CMND_I2CSCAN "\":\"Error %d at 0x%02x"), error, address);
|
|
|
|
break;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
if (any) {
|
|
|
|
strncat(devs, "\"}", devs_len);
|
2018-09-29 11:34:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-01-06 16:34:42 +00:00
|
|
|
snprintf_P(devs, devs_len, PSTR("{\"" D_CMND_I2CSCAN "\":\"" D_JSON_I2CSCAN_NO_DEVICES_FOUND "\"}"));
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 13:27:30 +01:00
|
|
|
|
|
|
|
boolean I2cDevice(byte addr)
|
|
|
|
{
|
|
|
|
for (byte address = 1; address <= 127; address++) {
|
|
|
|
Wire.beginTransmission(address);
|
|
|
|
if (!Wire.endTransmission() && (address == addr)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_I2C
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Real Time Clock
|
|
|
|
*
|
|
|
|
* Sources: Time by Michael Margolis and Paul Stoffregen (https://github.com/PaulStoffregen/Time)
|
|
|
|
* Timezone by Jack Christensen (https://github.com/JChristensen/Timezone)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "sntp.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SECS_PER_MIN ((uint32_t)(60UL))
|
|
|
|
#define SECS_PER_HOUR ((uint32_t)(3600UL))
|
|
|
|
#define SECS_PER_DAY ((uint32_t)(SECS_PER_HOUR * 24UL))
|
2018-11-01 12:00:05 +00:00
|
|
|
#define MINS_PER_HOUR ((uint32_t)(60UL))
|
2017-01-28 13:41:01 +00:00
|
|
|
#define LEAP_YEAR(Y) (((1970+Y)>0) && !((1970+Y)%4) && (((1970+Y)%100) || !((1970+Y)%400)))
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
Ticker TickerRtc;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
static const uint8_t kDaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // API starts months from 1, this array starts from 0
|
2017-12-29 13:35:05 +00:00
|
|
|
static const char kMonthNamesEnglish[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint32_t utc_time = 0;
|
|
|
|
uint32_t local_time = 0;
|
|
|
|
uint32_t daylight_saving_time = 0;
|
|
|
|
uint32_t standard_time = 0;
|
|
|
|
uint32_t ntp_time = 0;
|
2017-04-25 17:24:42 +01:00
|
|
|
uint32_t midnight = 1451602800;
|
2018-02-17 12:47:11 +00:00
|
|
|
uint32_t restart_time = 0;
|
2018-11-01 13:27:37 +00:00
|
|
|
int32_t time_timezone = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t midnight_now = 0;
|
2018-02-07 16:50:02 +00:00
|
|
|
uint8_t ntp_sync_minute = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
String GetBuildDateAndTime()
|
2017-03-08 15:20:45 +00:00
|
|
|
{
|
2017-07-15 14:07:30 +01:00
|
|
|
// "2017-03-07T11:08:02" - ISO8601:2004
|
2017-03-08 15:20:45 +00:00
|
|
|
char bdt[21];
|
2017-04-25 17:24:42 +01:00
|
|
|
char *p;
|
2017-03-08 15:20:45 +00:00
|
|
|
char mdate[] = __DATE__; // "Mar 7 2017"
|
2018-02-03 22:25:05 +00:00
|
|
|
char *smonth = mdate;
|
|
|
|
int day = 0;
|
|
|
|
int year = 0;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2018-02-03 22:25:05 +00:00
|
|
|
// sscanf(mdate, "%s %d %d", bdt, &day, &year); // Not implemented in 2.3.0 and probably too much code
|
2017-03-08 15:20:45 +00:00
|
|
|
byte i = 0;
|
2018-02-03 22:25:05 +00:00
|
|
|
for (char *str = strtok_r(mdate, " ", &p); str && i < 3; str = strtok_r(NULL, " ", &p)) {
|
2017-03-08 15:20:45 +00:00
|
|
|
switch (i++) {
|
|
|
|
case 0: // Month
|
|
|
|
smonth = str;
|
|
|
|
break;
|
|
|
|
case 1: // Day
|
|
|
|
day = atoi(str);
|
|
|
|
break;
|
|
|
|
case 2: // Year
|
|
|
|
year = atoi(str);
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 22:25:05 +00:00
|
|
|
int month = (strstr(kMonthNamesEnglish, smonth) -kMonthNamesEnglish) /3 +1;
|
2017-09-02 13:37:02 +01:00
|
|
|
snprintf_P(bdt, sizeof(bdt), PSTR("%d" D_YEAR_MONTH_SEPARATOR "%02d" D_MONTH_DAY_SEPARATOR "%02d" D_DATE_TIME_SEPARATOR "%s"), year, month, day, __TIME__);
|
2018-11-01 12:00:05 +00:00
|
|
|
return String(bdt); // 2017-03-07T11:08:02
|
|
|
|
}
|
|
|
|
|
|
|
|
String GetTimeZone()
|
|
|
|
{
|
|
|
|
char tz[7];
|
|
|
|
|
2018-11-01 13:27:37 +00:00
|
|
|
snprintf_P(tz, sizeof(tz), PSTR("%+03d:%02d"), time_timezone / 60, abs(time_timezone % 60));
|
2018-11-01 12:00:05 +00:00
|
|
|
|
|
|
|
return String(tz); // -03:45
|
2017-03-08 15:20:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 09:42:52 +01:00
|
|
|
/*
|
|
|
|
* timestamps in https://en.wikipedia.org/wiki/ISO_8601 format
|
2018-09-06 11:37:50 +01:00
|
|
|
*
|
2018-09-02 09:42:52 +01:00
|
|
|
* DT_UTC - current data and time in Greenwich, England (aka GMT)
|
|
|
|
* DT_LOCAL - current date and time taking timezone into account
|
|
|
|
* DT_RESTART - the date and time this device last started, in local timezone
|
2018-09-06 11:37:50 +01:00
|
|
|
*
|
2018-09-02 09:42:52 +01:00
|
|
|
* Format:
|
2018-09-06 11:37:50 +01:00
|
|
|
* "2017-03-07T11:08:02-07:00" - if DT_LOCAL and SetOption52 = 1
|
2018-09-02 09:42:52 +01:00
|
|
|
* "2017-03-07T11:08:02" - otherwise
|
|
|
|
*/
|
2018-02-17 12:47:11 +00:00
|
|
|
String GetDateAndTime(byte time_type)
|
2017-03-08 15:20:45 +00:00
|
|
|
{
|
2018-09-02 09:42:52 +01:00
|
|
|
// "2017-03-07T11:08:02-07:00" - ISO8601:2004
|
|
|
|
char dt[27];
|
2017-07-15 14:07:30 +01:00
|
|
|
TIME_T tmpTime;
|
|
|
|
|
2018-09-02 09:42:52 +01:00
|
|
|
switch (time_type) {
|
2018-10-30 16:04:19 +00:00
|
|
|
case DT_ENERGY:
|
|
|
|
BreakTime(Settings.energy_kWhtotal_time, tmpTime);
|
|
|
|
tmpTime.year += 1970;
|
|
|
|
break;
|
2018-09-02 09:42:52 +01:00
|
|
|
case DT_UTC:
|
|
|
|
BreakTime(utc_time, tmpTime);
|
|
|
|
tmpTime.year += 1970;
|
|
|
|
break;
|
|
|
|
case DT_RESTART:
|
|
|
|
if (restart_time == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
BreakTime(restart_time, tmpTime);
|
|
|
|
tmpTime.year += 1970;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tmpTime = RtcTime;
|
2018-02-17 12:47:11 +00:00
|
|
|
}
|
2018-09-02 09:42:52 +01:00
|
|
|
|
|
|
|
snprintf_P(dt, sizeof(dt), PSTR("%04d-%02d-%02dT%02d:%02d:%02d"),
|
|
|
|
tmpTime.year, tmpTime.month, tmpTime.day_of_month, tmpTime.hour, tmpTime.minute, tmpTime.second);
|
|
|
|
|
2018-10-30 16:04:19 +00:00
|
|
|
if (Settings.flag3.time_append_timezone && (DT_LOCAL == time_type)) {
|
|
|
|
// if (Settings.flag3.time_append_timezone && ((DT_LOCAL == time_type) || (DT_ENERGY == time_type))) {
|
2018-11-01 12:00:05 +00:00
|
|
|
strncat(dt, GetTimeZone().c_str(), sizeof(dt));
|
2018-09-02 09:42:52 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 12:00:05 +00:00
|
|
|
return String(dt); // 2017-03-07T11:08:02-07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
String GetTime(int type)
|
|
|
|
{
|
|
|
|
/* type 1 - Local time
|
|
|
|
* type 2 - Daylight Savings time
|
|
|
|
* type 3 - Standard time
|
|
|
|
*/
|
|
|
|
char stime[25]; // Skip newline
|
|
|
|
|
|
|
|
uint32_t time = utc_time;
|
|
|
|
if (1 == type) time = local_time;
|
|
|
|
if (2 == type) time = daylight_saving_time;
|
|
|
|
if (3 == type) time = standard_time;
|
|
|
|
snprintf_P(stime, sizeof(stime), sntp_get_real_time(time));
|
|
|
|
|
|
|
|
return String(stime); // Thu Nov 01 11:41:02 2018
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 16:50:02 +00:00
|
|
|
String GetUptime()
|
|
|
|
{
|
|
|
|
char dt[16];
|
|
|
|
|
|
|
|
TIME_T ut;
|
2018-02-17 12:47:11 +00:00
|
|
|
|
|
|
|
if (restart_time) {
|
|
|
|
BreakTime(utc_time - restart_time, ut);
|
|
|
|
} else {
|
|
|
|
BreakTime(uptime, ut);
|
|
|
|
}
|
2018-02-07 16:50:02 +00:00
|
|
|
|
|
|
|
// "P128DT14H35M44S" - ISO8601:2004 - https://en.wikipedia.org/wiki/ISO_8601 Durations
|
|
|
|
// snprintf_P(dt, sizeof(dt), PSTR("P%dDT%02dH%02dM%02dS"), ut.days, ut.hour, ut.minute, ut.second);
|
|
|
|
|
|
|
|
// "128 14:35:44" - OpenVMS
|
|
|
|
// "128T14:35:44" - Tasmota
|
2018-11-01 12:00:05 +00:00
|
|
|
snprintf_P(dt, sizeof(dt), PSTR("%dT%02d:%02d:%02d"), ut.days, ut.hour, ut.minute, ut.second);
|
|
|
|
|
|
|
|
return String(dt); // 128T14:35:44
|
2018-02-07 16:50:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:22:53 +01:00
|
|
|
uint32_t GetMinutesUptime()
|
|
|
|
{
|
|
|
|
TIME_T ut;
|
|
|
|
|
|
|
|
if (restart_time) {
|
|
|
|
BreakTime(utc_time - restart_time, ut);
|
|
|
|
} else {
|
|
|
|
BreakTime(uptime, ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ut.days *1440) + (ut.hour *60) + ut.minute;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetMinutesPastMidnight()
|
|
|
|
{
|
|
|
|
uint32_t minutes = 0;
|
|
|
|
|
|
|
|
if (RtcTime.valid) {
|
|
|
|
minutes = (RtcTime.hour *60) + RtcTime.minute;
|
|
|
|
}
|
|
|
|
return minutes;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void BreakTime(uint32_t time_input, TIME_T &tm)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
// break the given time_input into time components
|
2017-01-28 13:41:01 +00:00
|
|
|
// this is a more compact version of the C library localtime function
|
|
|
|
// note that year is offset from 1970 !!!
|
|
|
|
|
2017-04-25 17:24:42 +01:00
|
|
|
uint8_t year;
|
|
|
|
uint8_t month;
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t month_length;
|
2017-01-28 13:41:01 +00:00
|
|
|
uint32_t time;
|
|
|
|
unsigned long days;
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
time = time_input;
|
|
|
|
tm.second = time % 60;
|
2017-01-28 13:41:01 +00:00
|
|
|
time /= 60; // now it is minutes
|
2017-10-18 17:22:34 +01:00
|
|
|
tm.minute = time % 60;
|
2017-01-28 13:41:01 +00:00
|
|
|
time /= 60; // now it is hours
|
2017-10-18 17:22:34 +01:00
|
|
|
tm.hour = time % 24;
|
2017-01-28 13:41:01 +00:00
|
|
|
time /= 24; // now it is days
|
2018-02-07 16:50:02 +00:00
|
|
|
tm.days = time;
|
2017-10-18 17:22:34 +01:00
|
|
|
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
year = 0;
|
|
|
|
days = 0;
|
|
|
|
while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
|
|
|
|
year++;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
tm.year = year; // year is offset from 1970
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
days -= LEAP_YEAR(year) ? 366 : 365;
|
|
|
|
time -= days; // now it is days in this year, starting at 0
|
2017-10-18 17:22:34 +01:00
|
|
|
tm.day_of_year = time;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
|
|
|
days = 0;
|
|
|
|
month = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
month_length = 0;
|
2017-01-28 13:41:01 +00:00
|
|
|
for (month = 0; month < 12; month++) {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (1 == month) { // february
|
2017-01-28 13:41:01 +00:00
|
|
|
if (LEAP_YEAR(year)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
month_length = 29;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
month_length = 28;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
month_length = kDaysInMonth[month];
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (time >= month_length) {
|
|
|
|
time -= month_length;
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
strlcpy(tm.name_of_month, kMonthNames + (month *3), 4);
|
|
|
|
tm.month = month + 1; // jan is month 1
|
|
|
|
tm.day_of_month = time + 1; // day of month
|
|
|
|
tm.valid = (time_input > 1451602800); // 2016-01-01
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint32_t MakeTime(TIME_T &tm)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
// assemble time elements into time_t
|
|
|
|
// note year argument is offset from 1970
|
|
|
|
|
|
|
|
int i;
|
|
|
|
uint32_t seconds;
|
|
|
|
|
|
|
|
// seconds from 1970 till 1 jan 00:00:00 of the given year
|
2017-10-18 17:22:34 +01:00
|
|
|
seconds = tm.year * (SECS_PER_DAY * 365);
|
|
|
|
for (i = 0; i < tm.year; i++) {
|
2017-01-28 13:41:01 +00:00
|
|
|
if (LEAP_YEAR(i)) {
|
|
|
|
seconds += SECS_PER_DAY; // add extra days for leap years
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add days for this year, months start from 1
|
2017-10-18 17:22:34 +01:00
|
|
|
for (i = 1; i < tm.month; i++) {
|
|
|
|
if ((2 == i) && LEAP_YEAR(tm.year)) {
|
2017-01-28 13:41:01 +00:00
|
|
|
seconds += SECS_PER_DAY * 29;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
seconds += SECS_PER_DAY * kDaysInMonth[i-1]; // monthDay array starts from 0
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
seconds+= (tm.day_of_month - 1) * SECS_PER_DAY;
|
|
|
|
seconds+= tm.hour * SECS_PER_HOUR;
|
|
|
|
seconds+= tm.minute * SECS_PER_MIN;
|
|
|
|
seconds+= tm.second;
|
2017-01-28 13:41:01 +00:00
|
|
|
return seconds;
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:43:48 +01:00
|
|
|
uint32_t RuleToTime(TimeRule r, int yr)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-02-08 14:20:03 +00:00
|
|
|
TIME_T tm;
|
|
|
|
uint32_t t;
|
|
|
|
uint8_t m;
|
|
|
|
uint8_t w; // temp copies of r.month and r.week
|
|
|
|
|
|
|
|
m = r.month;
|
|
|
|
w = r.week;
|
|
|
|
if (0 == w) { // Last week = 0
|
|
|
|
if (++m > 12) { // for "Last", go to the next month
|
|
|
|
m = 1;
|
|
|
|
yr++;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2018-02-08 14:20:03 +00:00
|
|
|
w = 1; // and treat as first week of next month, subtract 7 days later
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-02-08 14:20:03 +00:00
|
|
|
tm.hour = r.hour;
|
|
|
|
tm.minute = 0;
|
|
|
|
tm.second = 0;
|
|
|
|
tm.day_of_month = 1;
|
|
|
|
tm.month = m;
|
|
|
|
tm.year = yr - 1970;
|
|
|
|
t = MakeTime(tm); // First day of the month, or first day of next month for "Last" rules
|
|
|
|
BreakTime(t, tm);
|
|
|
|
t += (7 * (w - 1) + (r.dow - tm.day_of_week + 7) % 7) * SECS_PER_DAY;
|
|
|
|
if (0 == r.week) {
|
|
|
|
t -= 7 * SECS_PER_DAY; // back up a week if this is a "Last" rule
|
|
|
|
}
|
|
|
|
return t;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint32_t LocalTime()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
return local_time;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint32_t Midnight()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
return midnight;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean MidnightNow()
|
2017-05-17 21:49:22 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean mnflg = midnight_now;
|
2018-02-08 14:20:03 +00:00
|
|
|
if (mnflg) midnight_now = 0;
|
2017-05-17 21:49:22 +01:00
|
|
|
return mnflg;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void RtcSecond()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
TIME_T tmpTime;
|
|
|
|
|
2018-02-20 15:19:48 +00:00
|
|
|
if ((ntp_sync_minute > 59) && (RtcTime.minute > 2)) ntp_sync_minute = 1; // If sync prepare for a new cycle
|
2018-02-07 16:50:02 +00:00
|
|
|
uint8_t offset = (uptime < 30) ? RtcTime.second : (((ESP.getChipId() & 0xF) * 3) + 3) ; // First try ASAP to sync. If fails try once every 60 seconds based on chip id
|
2018-11-04 17:00:07 +00:00
|
|
|
if (!global_state.wifi_down && (offset == RtcTime.second) && ((RtcTime.year < 2016) || (ntp_sync_minute == RtcTime.minute) || ntp_force_sync)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
ntp_time = sntp_get_current_timestamp();
|
2018-04-30 13:18:46 +01:00
|
|
|
if (ntp_time > 1451602800) { // Fix NTP bug in core 2.4.1/SDK 2.2.1 (returns Thu Jan 01 08:00:10 1970 after power on)
|
2018-05-14 22:22:29 +01:00
|
|
|
ntp_force_sync = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
utc_time = ntp_time;
|
2018-02-08 14:20:03 +00:00
|
|
|
ntp_sync_minute = 60; // Sync so block further requests
|
2018-02-17 12:47:11 +00:00
|
|
|
if (restart_time == 0) {
|
|
|
|
restart_time = utc_time - uptime; // save first ntp time as restart time
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
BreakTime(utc_time, tmpTime);
|
|
|
|
RtcTime.year = tmpTime.year + 1970;
|
2018-05-14 22:22:29 +01:00
|
|
|
daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
|
|
|
|
standard_time = RuleToTime(Settings.tflag[0], RtcTime.year);
|
2018-02-08 14:20:03 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "(" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
|
|
|
|
GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str());
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2018-04-12 13:01:43 +01:00
|
|
|
if (local_time < 1451602800) { // 2016-01-01
|
2018-06-25 17:00:20 +01:00
|
|
|
rules_flag.time_init = 1;
|
2018-04-11 09:11:20 +01:00
|
|
|
} else {
|
2018-06-25 17:00:20 +01:00
|
|
|
rules_flag.time_set = 1;
|
2018-04-11 09:11:20 +01:00
|
|
|
}
|
2018-02-07 16:50:02 +00:00
|
|
|
} else {
|
2018-02-08 14:20:03 +00:00
|
|
|
ntp_sync_minute++; // Try again in next minute
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
utc_time++;
|
|
|
|
local_time = utc_time;
|
|
|
|
if (local_time > 1451602800) { // 2016-01-01
|
2018-11-01 12:00:05 +00:00
|
|
|
int16_t timezone_minutes = Settings.timezone_minutes;
|
|
|
|
if (Settings.timezone < 0) { timezone_minutes *= -1; }
|
2018-11-01 13:27:37 +00:00
|
|
|
time_timezone = (Settings.timezone * SECS_PER_HOUR) + (timezone_minutes * SECS_PER_MIN);
|
2017-10-18 17:22:34 +01:00
|
|
|
if (99 == Settings.timezone) {
|
2018-11-01 12:00:05 +00:00
|
|
|
int32_t dstoffset = Settings.toffset[1] * SECS_PER_MIN;
|
|
|
|
int32_t stdoffset = Settings.toffset[0] * SECS_PER_MIN;
|
2018-05-14 22:22:29 +01:00
|
|
|
if (Settings.tflag[1].hemis) {
|
2018-05-13 04:12:02 +01:00
|
|
|
// Southern hemisphere
|
|
|
|
if ((utc_time >= (standard_time - dstoffset)) && (utc_time < (daylight_saving_time - stdoffset))) {
|
2018-11-01 13:27:37 +00:00
|
|
|
time_timezone = stdoffset; // Standard Time
|
2018-05-13 04:12:02 +01:00
|
|
|
} else {
|
2018-11-01 13:27:37 +00:00
|
|
|
time_timezone = dstoffset; // Daylight Saving Time
|
2018-05-13 18:43:48 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
2018-05-13 04:12:02 +01:00
|
|
|
// Northern hemisphere
|
|
|
|
if ((utc_time >= (daylight_saving_time - stdoffset)) && (utc_time < (standard_time - dstoffset))) {
|
2018-11-01 13:27:37 +00:00
|
|
|
time_timezone = dstoffset; // Daylight Saving Time
|
2018-05-13 04:12:02 +01:00
|
|
|
} else {
|
2018-11-01 13:27:37 +00:00
|
|
|
time_timezone = stdoffset; // Standard Time
|
2018-05-13 18:43:48 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 13:27:37 +00:00
|
|
|
local_time += time_timezone;
|
|
|
|
time_timezone /= 60;
|
2018-10-30 16:04:19 +00:00
|
|
|
if (!Settings.energy_kWhtotal_time) { Settings.energy_kWhtotal_time = local_time; }
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
BreakTime(local_time, RtcTime);
|
|
|
|
if (!RtcTime.hour && !RtcTime.minute && !RtcTime.second && RtcTime.valid) {
|
|
|
|
midnight = local_time;
|
|
|
|
midnight_now = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
RtcTime.year += 1970;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void RtcInit()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
sntp_setservername(0, Settings.ntp_server[0]);
|
|
|
|
sntp_setservername(1, Settings.ntp_server[1]);
|
|
|
|
sntp_setservername(2, Settings.ntp_server[2]);
|
2017-01-28 13:41:01 +00:00
|
|
|
sntp_stop();
|
|
|
|
sntp_set_timezone(0); // UTC time
|
|
|
|
sntp_init();
|
2017-10-18 17:22:34 +01:00
|
|
|
utc_time = 0;
|
|
|
|
BreakTime(utc_time, RtcTime);
|
|
|
|
TickerRtc.attach(1, RtcSecond);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifndef USE_ADC_VCC
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* ADC support
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-04-28 17:18:37 +01:00
|
|
|
uint16_t adc_last_value = 0;
|
|
|
|
|
|
|
|
uint16_t AdcRead()
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2017-11-04 15:36:51 +00:00
|
|
|
uint16_t analog = 0;
|
2017-11-03 17:07:25 +00:00
|
|
|
for (byte i = 0; i < 32; i++) {
|
2017-11-04 15:36:51 +00:00
|
|
|
analog += analogRead(A0);
|
2017-11-03 17:07:25 +00:00
|
|
|
delay(1);
|
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
analog >>= 5;
|
2018-04-28 17:18:37 +01:00
|
|
|
return analog;
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:21:45 +01:00
|
|
|
#ifdef USE_RULES
|
2018-08-26 16:10:18 +01:00
|
|
|
void AdcEvery250ms()
|
|
|
|
{
|
|
|
|
uint16_t new_value = AdcRead();
|
|
|
|
if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) {
|
|
|
|
adc_last_value = new_value;
|
|
|
|
uint16_t value = adc_last_value / 10;
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"ANALOG\":{\"A0div10\":%d}}"), (value > 99) ? 100 : value);
|
|
|
|
XdrvRulesProcess();
|
2018-04-28 17:18:37 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-03 14:21:45 +01:00
|
|
|
#endif // USE_RULES
|
2018-04-28 17:18:37 +01:00
|
|
|
|
|
|
|
void AdcShow(boolean json)
|
|
|
|
{
|
|
|
|
uint16_t analog = AdcRead();
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (json) {
|
2018-04-28 14:55:38 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"ANALOG\":{\"A0\":%d}"), mqtt_data, analog);
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-11-04 15:36:51 +00:00
|
|
|
} else {
|
2017-11-07 14:57:24 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ANALOG, mqtt_data, "", 0, analog);
|
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
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_02
|
|
|
|
|
|
|
|
boolean Xsns02(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (pin[GPIO_ADC0] < 99) {
|
|
|
|
switch (function) {
|
2018-05-03 14:21:45 +01:00
|
|
|
#ifdef USE_RULES
|
2018-08-26 16:10:18 +01:00
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
AdcEvery250ms();
|
2018-04-28 17:18:37 +01:00
|
|
|
break;
|
2018-05-03 14:21:45 +01:00
|
|
|
#endif // USE_RULES
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
AdcShow(1);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_WEB_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
AdcShow(0);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // USE_ADC_VCC
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Syslog
|
2017-11-04 15:36:51 +00:00
|
|
|
*
|
2017-11-03 17:07:25 +00:00
|
|
|
* Example:
|
|
|
|
* snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_LOG "Any value %d"), value);
|
|
|
|
* AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
*
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-03-18 12:47:30 +00:00
|
|
|
void SetSeriallog(byte loglevel)
|
|
|
|
{
|
|
|
|
Settings.seriallog_level = loglevel;
|
|
|
|
seriallog_level = loglevel;
|
|
|
|
seriallog_timer = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
void GetLog(byte idx, char** entry_pp, size_t* len_p)
|
|
|
|
{
|
|
|
|
char* entry_p = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (idx) {
|
|
|
|
char* it = web_log;
|
|
|
|
do {
|
|
|
|
byte cur_idx = *it;
|
|
|
|
it++;
|
2018-02-01 15:18:00 +00:00
|
|
|
size_t tmp = strchrspn(it, '\1');
|
2018-01-30 13:14:55 +00:00
|
|
|
tmp++; // Skip terminating '\1'
|
|
|
|
if (cur_idx == idx) { // Found the requested entry
|
|
|
|
len = tmp;
|
|
|
|
entry_p = it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it += tmp;
|
|
|
|
} while (it < web_log + WEB_LOG_SIZE && *it != '\0');
|
|
|
|
}
|
|
|
|
*entry_pp = entry_p;
|
|
|
|
*len_p = len;
|
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void Syslog()
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-09-14 13:20:27 +01:00
|
|
|
// Destroys log_data
|
|
|
|
char syslog_preamble[64]; // Hostname + Id
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-02-20 15:19:48 +00:00
|
|
|
if (syslog_host_hash != GetHash(Settings.syslog_host, strlen(Settings.syslog_host))) {
|
|
|
|
syslog_host_hash = GetHash(Settings.syslog_host, strlen(Settings.syslog_host));
|
|
|
|
WiFi.hostByName(Settings.syslog_host, syslog_host_addr); // If sleep enabled this might result in exception so try to do it once using hash
|
2017-09-26 16:50:39 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (PortUdp.beginPacket(syslog_host_addr, Settings.syslog_port)) {
|
|
|
|
snprintf_P(syslog_preamble, sizeof(syslog_preamble), PSTR("%s ESP-"), my_hostname);
|
2017-09-14 13:20:27 +01:00
|
|
|
memmove(log_data + strlen(syslog_preamble), log_data, sizeof(log_data) - strlen(syslog_preamble));
|
|
|
|
log_data[sizeof(log_data) -1] = '\0';
|
|
|
|
memcpy(log_data, syslog_preamble, strlen(syslog_preamble));
|
2017-10-18 17:22:34 +01:00
|
|
|
PortUdp.write(log_data);
|
|
|
|
PortUdp.endPacket();
|
2017-01-28 13:41:01 +00:00
|
|
|
} else {
|
|
|
|
syslog_level = 0;
|
|
|
|
syslog_timer = SYSLOG_TIMER;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_SYSLOG_HOST_NOT_FOUND ". " D_RETRY_IN " %d " D_UNIT_SECOND), SYSLOG_TIMER);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_INFO);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void AddLog(byte loglevel)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-01-30 13:14:55 +00:00
|
|
|
char mxtime[10]; // "13:45:21 "
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-01-30 13:14:55 +00:00
|
|
|
snprintf_P(mxtime, sizeof(mxtime), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d "), RtcTime.hour, RtcTime.minute, RtcTime.second);
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-09-14 13:20:27 +01:00
|
|
|
if (loglevel <= seriallog_level) {
|
2018-01-30 13:14:55 +00:00
|
|
|
Serial.printf("%s%s\n", mxtime, log_data);
|
2017-09-14 13:20:27 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2017-10-18 17:22:34 +01:00
|
|
|
if (Settings.webserver && (loglevel <= Settings.weblog_level)) {
|
2018-01-30 13:14:55 +00:00
|
|
|
// Delimited, zero-terminated buffer of log lines.
|
|
|
|
// Each entry has this format: [index][log data]['\1']
|
|
|
|
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
|
|
|
|
while (web_log_index == web_log[0] || // If log already holds the next index, remove it
|
|
|
|
strlen(web_log) + strlen(log_data) + 13 > WEB_LOG_SIZE) // 13 = web_log_index + mxtime + '\1' + '\0'
|
|
|
|
{
|
|
|
|
char* it = web_log;
|
|
|
|
it++; // Skip web_log_index
|
2018-02-01 15:18:00 +00:00
|
|
|
it += strchrspn(it, '\1'); // Skip log line
|
2018-01-30 13:14:55 +00:00
|
|
|
it++; // Skip delimiting "\1"
|
|
|
|
memmove(web_log, it, WEB_LOG_SIZE -(it-web_log)); // Move buffer forward to remove oldest log line
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2018-01-30 13:14:55 +00:00
|
|
|
snprintf_P(web_log, sizeof(web_log), PSTR("%s%c%s%s\1"), web_log, web_log_index++, mxtime, log_data);
|
2018-02-06 12:50:05 +00:00
|
|
|
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
2018-11-04 17:00:07 +00:00
|
|
|
if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); }
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void AddLog_P(byte loglevel, const char *formatP)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), formatP);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(loglevel);
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void AddLog_P(byte loglevel, const char *formatP, const char *formatP2)
|
2017-09-02 13:37:02 +01:00
|
|
|
{
|
2017-09-14 13:20:27 +01:00
|
|
|
char message[100];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), formatP);
|
2017-09-14 13:20:27 +01:00
|
|
|
snprintf_P(message, sizeof(message), formatP2);
|
|
|
|
strncat(log_data, message, sizeof(log_data));
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(loglevel);
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 13:39:16 +01:00
|
|
|
void AddLogSerial(byte loglevel, uint8_t *buffer, int count)
|
2018-02-04 17:09:09 +00:00
|
|
|
{
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SERIAL D_RECEIVED));
|
2018-04-14 13:39:16 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2018-02-13 13:30:30 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer++));
|
2018-02-04 17:09:09 +00:00
|
|
|
}
|
|
|
|
AddLog(loglevel);
|
|
|
|
}
|
|
|
|
|
2018-02-13 13:30:30 +00:00
|
|
|
void AddLogSerial(byte loglevel)
|
|
|
|
{
|
|
|
|
AddLogSerial(loglevel, (uint8_t*)serial_in_buffer, serial_in_byte_counter);
|
|
|
|
}
|
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
void AddLogMissed(char *sensor, uint8_t misses)
|
|
|
|
{
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("SNS: %s missed %d"), sensor, SENSOR_MAX_MISS - misses);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
*
|
|
|
|
\*********************************************************************************************/
|