mirror of https://github.com/arendst/Tasmota.git
v5.0.0
5.0.0 20170425 * Memory status message update * Fix setting migration to better preserve settings during move (#382) * Best practice is first doing a Backup Configuration before installing version 5.0.0 * Reset save count after setting move * Start using new linker script without SPIFFS
This commit is contained in:
parent
f4ea594ef0
commit
ecc6bb5fdc
16
README.md
16
README.md
|
@ -1,7 +1,21 @@
|
|||
## Sonoff-Tasmota
|
||||
Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.
|
||||
|
||||
Current version is **4.2.0** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/master/sonoff/_releasenotes.ino) for change information.
|
||||
Current version is **5.0.0** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/master/sonoff/_releasenotes.ino) for change information.
|
||||
|
||||
### **** ATTENTION Version 5.0.0 specific information *******
|
||||
|
||||
This version uses a new linker script to free flash memory for future code additions. It moves the settings from Spiffs to Eeprom. If you compile your own download the new linker to your IDE or Platformio base folder.
|
||||
|
||||
Best practice to implement is:
|
||||
- Open the webpage to your device
|
||||
- Perform option ``Backup Configuration``
|
||||
- Upgrade new firmware using ``Firmware upgrade``
|
||||
- If configuration conversion failed keep the webpage open and perform ``Restore Configuration``
|
||||
|
||||
You should now have a device with 32k more code memory to play with.
|
||||
|
||||
### *********************************************************
|
||||
|
||||
- This version provides all (Sonoff) modules in one file and starts up with Sonoff Basic.
|
||||
- Once uploaded select module using the configuration webpage or the commands ```Modules``` and ```Module```.
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,11 @@
|
|||
/* 4.2.0 20170424
|
||||
/* 5.0.0 20170425
|
||||
* Memory status message update
|
||||
* Fix setting migration to better preserve settings during move (#382)
|
||||
* Best practice is first doing a Backup Configuration before installing version 5.0.0
|
||||
* Reset save count after setting move
|
||||
* Start using new linker script without SPIFFS
|
||||
*
|
||||
* 4.2.0 20170424
|
||||
* Prepare for SPIFFS removal by moving settings to EEPROM area
|
||||
* Fix compilation error when webserver is disabled (#378)
|
||||
*
|
||||
|
|
|
@ -36,7 +36,9 @@ uint32_t getRtcHash()
|
|||
uint32_t hash = 0;
|
||||
uint8_t *bytes = (uint8_t*)&rtcMem;
|
||||
|
||||
for (uint16_t i = 0; i < sizeof(RTCMEM); i++) hash += bytes[i]*(i+1);
|
||||
for (uint16_t i = 0; i < sizeof(RTCMEM); i++) {
|
||||
hash += bytes[i]*(i+1);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
@ -70,7 +72,7 @@ void RTC_Load()
|
|||
|
||||
boolean RTC_Valid()
|
||||
{
|
||||
return (rtcMem.valid == RTC_MEM_VALID);
|
||||
return (RTC_MEM_VALID == rtcMem.valid);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_THEO
|
||||
|
@ -79,7 +81,10 @@ void RTC_Dump()
|
|||
#define CFG_COLS 16
|
||||
|
||||
char log[LOGSZ];
|
||||
uint16_t idx, maxrow, row, col;
|
||||
uint16_t idx;
|
||||
uint16_t maxrow;
|
||||
uint16_t row;
|
||||
uint16_t col;
|
||||
|
||||
uint8_t *buffer = (uint8_t *) &rtcMem;
|
||||
maxrow = ((sizeof(RTCMEM)+CFG_COLS)/CFG_COLS);
|
||||
|
@ -88,12 +93,16 @@ void RTC_Dump()
|
|||
idx = row * CFG_COLS;
|
||||
snprintf_P(log, sizeof(log), PSTR("%04X:"), idx);
|
||||
for (col = 0; col < CFG_COLS; col++) {
|
||||
if (!(col%4)) snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
if (!(col%4)) {
|
||||
snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s %02X"), log, buffer[idx + col]);
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s |"), log);
|
||||
for (col = 0; col < CFG_COLS; col++) {
|
||||
// if (!(col%4)) snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
// if (!(col%4)) {
|
||||
// snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
// }
|
||||
snprintf_P(log, sizeof(log), PSTR("%s%c"), log, ((buffer[idx + col] > 0x20) && (buffer[idx + col] < 0x7F)) ? (char)buffer[idx + col] : ' ');
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s|"), log);
|
||||
|
@ -111,7 +120,6 @@ extern "C" {
|
|||
}
|
||||
#include "eboot_command.h"
|
||||
|
||||
//extern "C" uint32_t _SPIFFS_start;
|
||||
extern "C" uint32_t _SPIFFS_end;
|
||||
|
||||
#define SPIFFS_END ((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE
|
||||
|
@ -145,11 +153,11 @@ void setFlashMode(byte option, byte mode)
|
|||
address = 0;
|
||||
}
|
||||
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
|
||||
if (spi_flash_read(address, (uint32_t*)_buffer, FLASH_SECTOR_SIZE) == SPI_FLASH_RESULT_OK) {
|
||||
if (SPI_FLASH_RESULT_OK == spi_flash_read(address, (uint32_t*)_buffer, FLASH_SECTOR_SIZE)) {
|
||||
if (_buffer[2] != mode) {
|
||||
_buffer[2] = mode &3;
|
||||
noInterrupts();
|
||||
if (spi_flash_erase_sector(address / FLASH_SECTOR_SIZE) == SPI_FLASH_RESULT_OK) {
|
||||
if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(address / FLASH_SECTOR_SIZE)) {
|
||||
spi_flash_write(address, (uint32_t*)_buffer, FLASH_SECTOR_SIZE);
|
||||
}
|
||||
interrupts();
|
||||
|
@ -163,7 +171,9 @@ void setFlashMode(byte option, byte mode)
|
|||
void setModuleFlashMode(byte option)
|
||||
{
|
||||
uint8_t mode = 0; // QIO - ESP8266
|
||||
if ((sysCfg.module == SONOFF_TOUCH) || (sysCfg.module == SONOFF_4CH)) mode = 3; // DOUT - ESP8285
|
||||
if ((SONOFF_TOUCH == sysCfg.module) || (SONOFF_4CH == sysCfg.module)) {
|
||||
mode = 3; // DOUT - ESP8285
|
||||
}
|
||||
setFlashMode(option, mode);
|
||||
}
|
||||
|
||||
|
@ -172,7 +182,9 @@ uint32_t getHash()
|
|||
uint32_t hash = 0;
|
||||
uint8_t *bytes = (uint8_t*)&sysCfg;
|
||||
|
||||
for (uint16_t i = 0; i < sizeof(SYSCFG); i++) hash += bytes[i]*(i+1);
|
||||
for (uint16_t i = 0; i < sizeof(SYSCFG); i++) {
|
||||
hash += bytes[i]*(i+1);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
@ -215,7 +227,7 @@ void CFG_Load()
|
|||
addLog(LOG_LEVEL_DEBUG, log);
|
||||
|
||||
if (sysCfg.cfg_holder != CFG_HOLDER) {
|
||||
if ((sysCfg.version < 0x04020000) || (sysCfg.version > 0x73000000)) {
|
||||
if ((sysCfg.version < 0x04020000) || (sysCfg.version > 0x06000000)) {
|
||||
noInterrupts();
|
||||
spi_flash_read((CFG_LOCATION_3) * SPI_FLASH_SEC_SIZE, (uint32*)&sysCfg, sizeof(SYSCFG));
|
||||
spi_flash_read((CFG_LOCATION_3 + 1) * SPI_FLASH_SEC_SIZE, (uint32*)&_sysCfgH, sizeof(SYSCFGH));
|
||||
|
@ -224,6 +236,8 @@ void CFG_Load()
|
|||
interrupts();
|
||||
if (sysCfg.cfg_holder != CFG_HOLDER) {
|
||||
CFG_Default();
|
||||
} else {
|
||||
sysCfg.saveFlag = 0;
|
||||
}
|
||||
} else {
|
||||
CFG_Default();
|
||||
|
@ -253,7 +267,7 @@ void CFG_Erase()
|
|||
if (_serialoutput) {
|
||||
Serial.print(F("Flash: Erased sector "));
|
||||
Serial.print(_sector);
|
||||
if (result == SPI_FLASH_RESULT_OK) {
|
||||
if (SPI_FLASH_RESULT_OK == result) {
|
||||
Serial.println(F(" OK"));
|
||||
} else {
|
||||
Serial.println(F(" Error"));
|
||||
|
@ -268,7 +282,10 @@ void CFG_Dump()
|
|||
#define CFG_COLS 16
|
||||
|
||||
char log[LOGSZ];
|
||||
uint16_t idx, maxrow, row, col;
|
||||
uint16_t idx;
|
||||
uint16_t maxrow;
|
||||
uint16_t row;
|
||||
uint16_t col;
|
||||
|
||||
uint8_t *buffer = (uint8_t *) &sysCfg;
|
||||
maxrow = ((sizeof(SYSCFG)+CFG_COLS)/CFG_COLS);
|
||||
|
@ -277,12 +294,16 @@ void CFG_Dump()
|
|||
idx = row * CFG_COLS;
|
||||
snprintf_P(log, sizeof(log), PSTR("%04X:"), idx);
|
||||
for (col = 0; col < CFG_COLS; col++) {
|
||||
if (!(col%4)) snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
if (!(col%4)) {
|
||||
snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s %02X"), log, buffer[idx + col]);
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s |"), log);
|
||||
for (col = 0; col < CFG_COLS; col++) {
|
||||
// if (!(col%4)) snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
// if (!(col%4)) {
|
||||
// snprintf_P(log, sizeof(log), PSTR("%s "), log);
|
||||
// }
|
||||
snprintf_P(log, sizeof(log), PSTR("%s%c"), log, ((buffer[idx + col] > 0x20) && (buffer[idx + col] < 0x7F)) ? (char)buffer[idx + col] : ' ');
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("%s|"), log);
|
||||
|
@ -424,16 +445,26 @@ void CFG_DefaultSet_3_2_4()
|
|||
|
||||
void CFG_DefaultSet_3_9_3()
|
||||
{
|
||||
for (byte i = 0; i < 4; i++) sysCfg.domoticz_switch_idx[i] = 0;
|
||||
for (byte i = 0; i < 12; i++) sysCfg.domoticz_sensor_idx[i] = 0;
|
||||
for (byte i = 0; i < 4; i++) {
|
||||
sysCfg.domoticz_switch_idx[i] = 0;
|
||||
}
|
||||
for (byte i = 0; i < 12; i++) {
|
||||
sysCfg.domoticz_sensor_idx[i] = 0;
|
||||
}
|
||||
|
||||
sysCfg.module = MODULE;
|
||||
for (byte i = 0; i < MAX_GPIO_PIN; i++) sysCfg.my_module.gp.io[i] = 0;
|
||||
for (byte i = 0; i < MAX_GPIO_PIN; i++){
|
||||
sysCfg.my_module.gp.io[i] = 0;
|
||||
}
|
||||
|
||||
sysCfg.led_pixels = 0;
|
||||
for (byte i = 0; i < 5; i++) sysCfg.led_color[i] = 255;
|
||||
for (byte i = 0; i < 5; i++) {
|
||||
sysCfg.led_color[i] = 255;
|
||||
}
|
||||
sysCfg.led_table = 0;
|
||||
for (byte i = 0; i < 3; i++) sysCfg.led_dimmer[i] = 10;
|
||||
for (byte i = 0; i < 3; i++){
|
||||
sysCfg.led_dimmer[i] = 10;
|
||||
}
|
||||
sysCfg.led_fade = 0;
|
||||
sysCfg.led_speed = 0;
|
||||
sysCfg.led_scheme = 0;
|
||||
|
@ -446,11 +477,17 @@ void CFG_DefaultSet_4_0_4()
|
|||
strlcpy(sysCfg.ntp_server[0], NTP_SERVER1, sizeof(sysCfg.ntp_server[0]));
|
||||
strlcpy(sysCfg.ntp_server[1], NTP_SERVER2, sizeof(sysCfg.ntp_server[1]));
|
||||
strlcpy(sysCfg.ntp_server[2], NTP_SERVER3, sizeof(sysCfg.ntp_server[2]));
|
||||
for (byte j =0; j < 3; j++)
|
||||
for (byte i = 0; i < strlen(sysCfg.ntp_server[j]); i++)
|
||||
if (sysCfg.ntp_server[j][i] == ',') sysCfg.ntp_server[j][i] = '.';
|
||||
for (byte j =0; j < 3; j++) {
|
||||
for (byte i = 0; i < strlen(sysCfg.ntp_server[j]); i++) {
|
||||
if (sysCfg.ntp_server[j][i] == ',') {
|
||||
sysCfg.ntp_server[j][i] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
sysCfg.pulsetime[0] = sysCfg.ex_pulsetime;
|
||||
for (byte i = 1; i < MAX_PULSETIMERS; i++) sysCfg.pulsetime[i] = 0;
|
||||
for (byte i = 1; i < MAX_PULSETIMERS; i++) {
|
||||
sysCfg.pulsetime[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CFG_DefaultSet_4_0_9()
|
||||
|
@ -535,7 +572,9 @@ void CFG_Delta()
|
|||
sysCfg.my_module.gp.io[MAX_GPIO_PIN -1] = 0; // Clear ADC0
|
||||
}
|
||||
if (sysCfg.version < 0x04000700) {
|
||||
for (byte i = 0; i < 5; i++) sysCfg.pwmvalue[i] = 0;
|
||||
for (byte i = 0; i < 5; i++) {
|
||||
sysCfg.pwmvalue[i] = 0;
|
||||
}
|
||||
}
|
||||
if (sysCfg.version < 0x04000804) {
|
||||
CFG_DefaultSet_4_0_9();
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -131,7 +131,7 @@ Decoding 14 results
|
|||
|
||||
00:00:08 MQTT: tele/sonoff/INFO3 = {"Started":"Fatal exception:28 flag:2 (EXCEPTION) epc1:0x4000bf64 epc2:0x00000000 epc3:0x00000000 excvaddr:0x00000007 depc:0x00000000"}
|
||||
*/
|
||||
if (type == 1) {
|
||||
if (1 == type) {
|
||||
char svalue[10];
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("%s"), 7); // Exception 28 as number in string (7 in excvaddr)
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ Decoding 14 results
|
|||
14:51:42 osWatch: FreeRam 25360, rssi 62, last_run 38771
|
||||
14:51:42 osWatch: Warning, loop blocked. Restart now
|
||||
*/
|
||||
if (type == 2) {
|
||||
if (2 == type) {
|
||||
while(1) delay(1000); // this will trigger the os watch
|
||||
}
|
||||
}
|
||||
|
@ -155,23 +155,6 @@ Decoding 14 results
|
|||
* General
|
||||
\*********************************************************************************************/
|
||||
|
||||
boolean parseIPx(char* str, uint16_t len, uint32_t* addr)
|
||||
{
|
||||
uint8_t *part = (uint8_t *)addr;
|
||||
byte i;
|
||||
|
||||
if (!len) return false;
|
||||
*addr = 0;
|
||||
for (i = 0; i <= len; i++) if (*(str +i) == ',') *(str +i) = '.';
|
||||
for (i = 0; i < 4; i++) {
|
||||
part[i] = strtoul(str, NULL, 10); // Convert byte
|
||||
str = strchr(str, '.');
|
||||
if (str == NULL || *str == '\0') break; // No more separators, exit
|
||||
str++; // Point to next character after separator
|
||||
}
|
||||
return (i == 3);
|
||||
}
|
||||
|
||||
boolean parseIP(uint32_t* addr, const char* str)
|
||||
{
|
||||
uint8_t *part = (uint8_t*)addr;
|
||||
|
@ -181,10 +164,12 @@ boolean parseIP(uint32_t* addr, const char* str)
|
|||
for (i = 0; i < 4; i++) {
|
||||
part[i] = strtoul(str, NULL, 10); // Convert byte
|
||||
str = strchr(str, '.');
|
||||
if (str == NULL || *str == '\0') break; // No more separators, exit
|
||||
if (str == NULL || *str == '\0') {
|
||||
break; // No more separators, exit
|
||||
}
|
||||
str++; // Point to next character after separator
|
||||
}
|
||||
return (i == 3);
|
||||
return (3 == i);
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
|
@ -196,7 +181,12 @@ boolean parseIP(uint32_t* addr, const char* str)
|
|||
#define WIFI_CHECK_SEC 20 // seconds
|
||||
#define WIFI_RETRY_SEC 30 // seconds
|
||||
|
||||
uint8_t _wificounter, _wifiretry, _wifistatus, _wpsresult, _wificonfigflag = 0, _wifiConfigCounter = 0;
|
||||
uint8_t _wificounter;
|
||||
uint8_t _wifiretry;
|
||||
uint8_t _wifistatus;
|
||||
uint8_t _wpsresult;
|
||||
uint8_t _wificonfigflag = 0;
|
||||
uint8_t _wifiConfigCounter = 0;
|
||||
|
||||
int WIFI_getRSSIasQuality(int RSSI)
|
||||
{
|
||||
|
@ -214,7 +204,9 @@ int WIFI_getRSSIasQuality(int RSSI)
|
|||
|
||||
boolean WIFI_configCounter()
|
||||
{
|
||||
if (_wifiConfigCounter) _wifiConfigCounter = WIFI_MANAGER_SEC;
|
||||
if (_wifiConfigCounter) {
|
||||
_wifiConfigCounter = WIFI_MANAGER_SEC;
|
||||
}
|
||||
return (_wifiConfigCounter);
|
||||
}
|
||||
|
||||
|
@ -239,7 +231,7 @@ void WIFI_wps_status_cb(wps_cb_status status)
|
|||
*/
|
||||
|
||||
_wpsresult = status;
|
||||
if (_wpsresult == WPS_CB_ST_SUCCESS) {
|
||||
if (WPS_CB_ST_SUCCESS == _wpsresult) {
|
||||
wifi_wps_disable();
|
||||
} else {
|
||||
snprintf_P(log, sizeof(log), PSTR("WPSconfig: FAILED with status %d"), _wpsresult);
|
||||
|
@ -256,17 +248,27 @@ boolean WIFI_WPSConfigDone(void)
|
|||
boolean WIFI_beginWPSConfig(void)
|
||||
{
|
||||
_wpsresult = 99;
|
||||
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) &WIFI_wps_status_cb)) return false;
|
||||
if (!wifi_wps_start()) return false;
|
||||
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) &WIFI_wps_status_cb)) {
|
||||
return false;
|
||||
}
|
||||
if (!wifi_wps_start()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void WIFI_config(uint8_t type)
|
||||
{
|
||||
if (!_wificonfigflag) {
|
||||
if (type == WIFI_RETRY) return;
|
||||
if (WIFI_RETRY == type) {
|
||||
return;
|
||||
}
|
||||
#ifdef USE_EMULATION
|
||||
UDP_Disconnect();
|
||||
#endif // USE_EMULATION
|
||||
|
@ -275,14 +277,14 @@ void WIFI_config(uint8_t type)
|
|||
_wifiConfigCounter = WIFI_CONFIG_SEC; // Allow up to WIFI_CONFIG_SECS seconds for phone to provide ssid/pswd
|
||||
_wificounter = _wifiConfigCounter +5;
|
||||
blinks = 1999;
|
||||
if (_wificonfigflag == WIFI_RESTART) {
|
||||
if (WIFI_RESTART == _wificonfigflag) {
|
||||
restartflag = 2;
|
||||
}
|
||||
else if (_wificonfigflag == WIFI_SMARTCONFIG) {
|
||||
else if (WIFI_SMARTCONFIG == _wificonfigflag) {
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("Smartconfig: Active for 1 minute"));
|
||||
WiFi.beginSmartConfig();
|
||||
}
|
||||
else if (_wificonfigflag == WIFI_WPSCONFIG) {
|
||||
else if (WIFI_WPSCONFIG == _wificonfigflag) {
|
||||
if (WIFI_beginWPSConfig()) {
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("WPSconfig: Active for 1 minute"));
|
||||
} else {
|
||||
|
@ -291,7 +293,7 @@ void WIFI_config(uint8_t type)
|
|||
}
|
||||
}
|
||||
#ifdef USE_WEBSERVER
|
||||
else if (_wificonfigflag == WIFI_MANAGER) {
|
||||
else if (WIFI_MANAGER == _wificonfigflag) {
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("Wifimanager: Active for 1 minute"));
|
||||
beginWifiManager();
|
||||
}
|
||||
|
@ -313,11 +315,15 @@ void WIFI_begin(uint8_t flag)
|
|||
}
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_STA); // Disable AP mode
|
||||
// if (sysCfg.sleep) wifi_set_sleep_type(LIGHT_SLEEP_T); // Allow light sleep during idle times
|
||||
if (sysCfg.sleep) WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
|
||||
// WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
|
||||
// if (WiFi.getPhyMode() != WIFI_PHY_MODE_11N) WiFi.setPhyMode(WIFI_PHY_MODE_11N);
|
||||
if (!WiFi.getAutoConnect()) WiFi.setAutoConnect(true);
|
||||
if (sysCfg.sleep) {
|
||||
WiFi.setSleepMode(WIFI_LIGHT_SLEEP); // Allow light sleep during idle times
|
||||
}
|
||||
// if (WiFi.getPhyMode() != WIFI_PHY_MODE_11N) {
|
||||
// WiFi.setPhyMode(WIFI_PHY_MODE_11N);
|
||||
// }
|
||||
if (!WiFi.getAutoConnect()) {
|
||||
WiFi.setAutoConnect(true);
|
||||
}
|
||||
// WiFi.setAutoReconnect(true);
|
||||
switch (flag) {
|
||||
case 0: // AP1
|
||||
|
@ -327,9 +333,12 @@ void WIFI_begin(uint8_t flag)
|
|||
case 2: // Toggle
|
||||
sysCfg.sta_active ^= 1;
|
||||
} // 3: Current AP
|
||||
if (strlen(sysCfg.sta_ssid[1]) == 0) sysCfg.sta_active = 0;
|
||||
if (sysCfg.ip_address[0])
|
||||
if (0 == strlen(sysCfg.sta_ssid[1])) {
|
||||
sysCfg.sta_active = 0;
|
||||
}
|
||||
if (sysCfg.ip_address[0]) {
|
||||
WiFi.config(sysCfg.ip_address[0], sysCfg.ip_address[1], sysCfg.ip_address[2], sysCfg.ip_address[3]); // Set static IP
|
||||
}
|
||||
WiFi.hostname(Hostname);
|
||||
WiFi.begin(sysCfg.sta_ssid[sysCfg.sta_active], sysCfg.sta_pwd[sysCfg.sta_active]);
|
||||
snprintf_P(log, sizeof(log), PSTR("Wifi: Connecting to AP%d %s in mode 11%c as %s..."),
|
||||
|
@ -339,7 +348,7 @@ void WIFI_begin(uint8_t flag)
|
|||
|
||||
void WIFI_check_ip()
|
||||
{
|
||||
if ((WiFi.status() == WL_CONNECTED) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
||||
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
||||
_wificounter = WIFI_CHECK_SEC;
|
||||
_wifiretry = WIFI_RETRY_SEC;
|
||||
addLog_P((_wifistatus != WL_CONNECTED) ? LOG_LEVEL_INFO : LOG_LEVEL_DEBUG_MORE, PSTR("Wifi: Connected"));
|
||||
|
@ -360,24 +369,36 @@ void WIFI_check_ip()
|
|||
break;
|
||||
case WL_NO_SSID_AVAIL:
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("Wifi: Connect failed as AP cannot be reached"));
|
||||
if (_wifiretry > (WIFI_RETRY_SEC / 2)) _wifiretry = WIFI_RETRY_SEC / 2;
|
||||
else if (_wifiretry) _wifiretry = 0;
|
||||
if (_wifiretry > (WIFI_RETRY_SEC / 2)) {
|
||||
_wifiretry = WIFI_RETRY_SEC / 2;
|
||||
}
|
||||
else if (_wifiretry) {
|
||||
_wifiretry = 0;
|
||||
}
|
||||
break;
|
||||
case WL_CONNECT_FAILED:
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("Wifi: Connect failed with AP incorrect password"));
|
||||
if (_wifiretry > (WIFI_RETRY_SEC / 2)) _wifiretry = WIFI_RETRY_SEC / 2;
|
||||
else if (_wifiretry) _wifiretry = 0;
|
||||
if (_wifiretry > (WIFI_RETRY_SEC / 2)) {
|
||||
_wifiretry = WIFI_RETRY_SEC / 2;
|
||||
}
|
||||
else if (_wifiretry) {
|
||||
_wifiretry = 0;
|
||||
}
|
||||
break;
|
||||
default: // WL_IDLE_STATUS and WL_DISCONNECTED
|
||||
if (!_wifiretry || (_wifiretry == (WIFI_RETRY_SEC / 2))) {
|
||||
if (!_wifiretry || ((WIFI_RETRY_SEC / 2) == _wifiretry)) {
|
||||
addLog_P(LOG_LEVEL_INFO, PSTR("Wifi: Connect failed with AP timeout"));
|
||||
} else {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("Wifi: Attempting connection..."));
|
||||
}
|
||||
}
|
||||
if (_wifiretry) {
|
||||
if (_wifiretry == WIFI_RETRY_SEC) WIFI_begin(3); // Select default SSID
|
||||
if (_wifiretry == (WIFI_RETRY_SEC / 2)) WIFI_begin(2); // Select alternate SSID
|
||||
if (WIFI_RETRY_SEC == _wifiretry) {
|
||||
WIFI_begin(3); // Select default SSID
|
||||
}
|
||||
if ((WIFI_RETRY_SEC / 2) == _wifiretry) {
|
||||
WIFI_begin(2); // Select alternate SSID
|
||||
}
|
||||
_wificounter = 1;
|
||||
_wifiretry--;
|
||||
} else {
|
||||
|
@ -404,18 +425,28 @@ void WIFI_Check(uint8_t param)
|
|||
_wifiConfigCounter--;
|
||||
_wificounter = _wifiConfigCounter +5;
|
||||
if (_wifiConfigCounter) {
|
||||
if ((_wificonfigflag == WIFI_SMARTCONFIG) && WiFi.smartConfigDone()) _wifiConfigCounter = 0;
|
||||
if ((_wificonfigflag == WIFI_WPSCONFIG) && WIFI_WPSConfigDone()) _wifiConfigCounter = 0;
|
||||
if ((WIFI_SMARTCONFIG == _wificonfigflag) && WiFi.smartConfigDone()) {
|
||||
_wifiConfigCounter = 0;
|
||||
}
|
||||
if ((WIFI_WPSCONFIG == _wificonfigflag) && WIFI_WPSConfigDone()) {
|
||||
_wifiConfigCounter = 0;
|
||||
}
|
||||
if (!_wifiConfigCounter) {
|
||||
if (strlen(WiFi.SSID().c_str())) strlcpy(sysCfg.sta_ssid[0], WiFi.SSID().c_str(), sizeof(sysCfg.sta_ssid[0]));
|
||||
if (strlen(WiFi.psk().c_str())) strlcpy(sysCfg.sta_pwd[0], WiFi.psk().c_str(), sizeof(sysCfg.sta_pwd[0]));
|
||||
if (strlen(WiFi.SSID().c_str())) {
|
||||
strlcpy(sysCfg.sta_ssid[0], WiFi.SSID().c_str(), sizeof(sysCfg.sta_ssid[0]));
|
||||
}
|
||||
if (strlen(WiFi.psk().c_str())) {
|
||||
strlcpy(sysCfg.sta_pwd[0], WiFi.psk().c_str(), sizeof(sysCfg.sta_pwd[0]));
|
||||
}
|
||||
sysCfg.sta_active = 0;
|
||||
snprintf_P(log, sizeof(log), PSTR("Wificonfig: SSID1 %s and Password1 %s"), sysCfg.sta_ssid[0], sysCfg.sta_pwd[0]);
|
||||
addLog(LOG_LEVEL_INFO, log);
|
||||
}
|
||||
}
|
||||
if (!_wifiConfigCounter) {
|
||||
if (_wificonfigflag == WIFI_SMARTCONFIG) WiFi.stopSmartConfig();
|
||||
if (WIFI_SMARTCONFIG == _wificonfigflag) {
|
||||
WiFi.stopSmartConfig();
|
||||
}
|
||||
restartflag = 2;
|
||||
}
|
||||
} else {
|
||||
|
@ -424,7 +455,7 @@ void WIFI_Check(uint8_t param)
|
|||
_wificounter = WIFI_CHECK_SEC;
|
||||
WIFI_check_ip();
|
||||
}
|
||||
if ((WiFi.status() == WL_CONNECTED) && (static_cast<uint32_t>(WiFi.localIP()) != 0) && !_wificonfigflag) {
|
||||
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0) && !_wificonfigflag) {
|
||||
#ifdef USE_DISCOVERY
|
||||
if (!mDNSbegun) {
|
||||
mDNSbegun = MDNS.begin(Hostname);
|
||||
|
@ -444,7 +475,9 @@ void WIFI_Check(uint8_t param)
|
|||
stopWebserver();
|
||||
}
|
||||
#ifdef USE_EMULATION
|
||||
if (sysCfg.emulation) UDP_Connect();
|
||||
if (sysCfg.emulation) {
|
||||
UDP_Connect();
|
||||
}
|
||||
#endif // USE_EMULATION
|
||||
#endif // USE_WEBSERVER
|
||||
} else {
|
||||
|
@ -461,8 +494,12 @@ int WIFI_State()
|
|||
{
|
||||
int state;
|
||||
|
||||
if ((WiFi.status() == WL_CONNECTED) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) state = WIFI_RESTART;
|
||||
if (_wificonfigflag) state = _wificonfigflag;
|
||||
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
||||
state = WIFI_RESTART;
|
||||
}
|
||||
if (_wificonfigflag) {
|
||||
state = _wificonfigflag;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
|
@ -482,10 +519,13 @@ void WIFI_Connect()
|
|||
#ifdef MQTT_HOST_DISCOVERY
|
||||
boolean mdns_discoverMQTTServer()
|
||||
{
|
||||
char log[LOGSZ], ip_str[20];
|
||||
char log[LOGSZ];
|
||||
char ip_str[20];
|
||||
int n;
|
||||
|
||||
if (!mDNSbegun) return false;
|
||||
if (!mDNSbegun) {
|
||||
return false;
|
||||
}
|
||||
|
||||
n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service
|
||||
|
||||
|
@ -536,14 +576,15 @@ int32_t i2c_read(uint8_t addr, uint8_t reg, uint8_t size)
|
|||
do {
|
||||
Wire.beginTransmission(addr); // start transmission to device
|
||||
Wire.write(reg); // sends register address to read from
|
||||
if (Wire.endTransmission(false) == 0) { // Try to become I2C Master, send data and collect bytes, keep master status for next request...
|
||||
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
|
||||
if (Wire.available() == size)
|
||||
if (Wire.available() == size) {
|
||||
for(byte i = 0; i < size; i++) {
|
||||
data <<= 8;
|
||||
data |= Wire.read(); // receive DATA
|
||||
}
|
||||
}
|
||||
}
|
||||
x++;
|
||||
} while (Wire.endTransmission(true) != 0 && x <= I2C_RETRY_COUNTER); // end transmission
|
||||
return data;
|
||||
|
@ -594,19 +635,23 @@ void i2c_write8(uint8_t addr, uint8_t reg, uint8_t val)
|
|||
|
||||
void i2c_scan(char *devs, unsigned int devs_len)
|
||||
{
|
||||
byte error, address, any = 0;
|
||||
byte error;
|
||||
byte address;
|
||||
byte any = 0;
|
||||
char tstr[10];
|
||||
|
||||
snprintf_P(devs, devs_len, PSTR("{\"I2Cscan\":\"Device(s) found at"));
|
||||
for (address = 1; address <= 127; address++) {
|
||||
Wire.beginTransmission(address);
|
||||
error = Wire.endTransmission();
|
||||
if (error == 0) {
|
||||
if (0 == error) {
|
||||
snprintf_P(tstr, sizeof(tstr), PSTR(" 0x%2x"), address);
|
||||
strncat(devs, tstr, devs_len);
|
||||
any = 1;
|
||||
}
|
||||
else if (error == 4) snprintf_P(devs, devs_len, PSTR("{\"I2Cscan\":\"Unknown error at 0x%2x\"}"), address);
|
||||
else if (4 == error) {
|
||||
snprintf_P(devs, devs_len, PSTR("{\"I2Cscan\":\"Unknown error at 0x%2x\"}"), address);
|
||||
}
|
||||
}
|
||||
if (any) {
|
||||
strncat(devs, "\"}", devs_len);
|
||||
|
@ -637,15 +682,24 @@ Ticker tickerRTC;
|
|||
static const uint8_t monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // API starts months from 1, this array starts from 0
|
||||
static const char monthNames[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
||||
|
||||
uint32_t utctime = 0, loctime = 0, dsttime = 0, stdtime = 0, ntptime = 0, midnight = 1451602800;
|
||||
uint32_t utctime = 0;
|
||||
uint32_t loctime = 0;
|
||||
uint32_t dsttime = 0;
|
||||
uint32_t stdtime = 0;
|
||||
uint32_t ntptime = 0;
|
||||
uint32_t midnight = 1451602800;
|
||||
|
||||
String getBuildDateTime()
|
||||
{
|
||||
// "2017-03-07T11:08:02"
|
||||
char bdt[21];
|
||||
char *str, *p, *smonth;
|
||||
char *str;
|
||||
char *p;
|
||||
char *smonth;
|
||||
char mdate[] = __DATE__; // "Mar 7 2017"
|
||||
int month, day, year;
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
|
||||
// sscanf(mdate, "%s %d %d", bdt, &day, &year); // Not implemented in 2.3.0 and probably too many code
|
||||
byte i = 0;
|
||||
|
@ -682,7 +736,9 @@ void breakTime(uint32_t timeInput, TIME_T &tm)
|
|||
// this is a more compact version of the C library localtime function
|
||||
// note that year is offset from 1970 !!!
|
||||
|
||||
uint8_t year, month, monthLength;
|
||||
uint8_t year;
|
||||
uint8_t month;
|
||||
uint8_t monthLength;
|
||||
uint32_t time;
|
||||
unsigned long days;
|
||||
|
||||
|
@ -710,7 +766,7 @@ void breakTime(uint32_t timeInput, TIME_T &tm)
|
|||
month = 0;
|
||||
monthLength = 0;
|
||||
for (month = 0; month < 12; month++) {
|
||||
if (month == 1) { // february
|
||||
if (1 == month) { // february
|
||||
if (LEAP_YEAR(year)) {
|
||||
monthLength = 29;
|
||||
} else {
|
||||
|
@ -750,7 +806,7 @@ uint32_t makeTime(TIME_T &tm)
|
|||
|
||||
// add days for this year, months start from 1
|
||||
for (i = 1; i < tm.Month; i++) {
|
||||
if ((i == 2) && LEAP_YEAR(tm.Year)) {
|
||||
if ((2 == i) && LEAP_YEAR(tm.Year)) {
|
||||
seconds += SECS_PER_DAY * 29;
|
||||
} else {
|
||||
seconds += SECS_PER_DAY * monthDays[i-1]; // monthDay array starts from 0
|
||||
|
@ -767,11 +823,12 @@ uint32_t toTime_t(TimeChangeRule r, int yr)
|
|||
{
|
||||
TIME_T tm;
|
||||
uint32_t t;
|
||||
uint8_t m, w; // temp copies of r.month and r.week
|
||||
uint8_t m;
|
||||
uint8_t w; // temp copies of r.month and r.week
|
||||
|
||||
m = r.month;
|
||||
w = r.week;
|
||||
if (w == 0) { // Last week = 0
|
||||
if (0 == w) { // Last week = 0
|
||||
if (++m > 12) { // for "Last", go to the next month
|
||||
m = 1;
|
||||
yr++;
|
||||
|
@ -788,7 +845,9 @@ uint32_t toTime_t(TimeChangeRule r, int yr)
|
|||
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.Wday + 7) % 7) * SECS_PER_DAY;
|
||||
if (r.week == 0) t -= 7 * SECS_PER_DAY; //back up a week if this is a "Last" rule
|
||||
if (0 == r.week) {
|
||||
t -= 7 * SECS_PER_DAY; //back up a week if this is a "Last" rule
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
|
@ -797,9 +856,9 @@ String rtc_time(int type)
|
|||
char stime[25]; // Skip newline
|
||||
|
||||
uint32_t time = utctime;
|
||||
if (type == 1) time = loctime;
|
||||
if (type == 2) time = dsttime;
|
||||
if (type == 3) time = stdtime;
|
||||
if (1 == type) time = loctime;
|
||||
if (2 == type) time = dsttime;
|
||||
if (3 == type) time = stdtime;
|
||||
snprintf_P(stime, sizeof(stime), PSTR("%s"), sntp_get_real_time(time));
|
||||
return String(stime);
|
||||
}
|
||||
|
@ -818,16 +877,17 @@ void rtc_second()
|
|||
{
|
||||
char log[LOGSZ];
|
||||
byte ntpsync;
|
||||
uint32_t stdoffset, dstoffset;
|
||||
uint32_t stdoffset;
|
||||
uint32_t dstoffset;
|
||||
TIME_T tmpTime;
|
||||
|
||||
ntpsync = 0;
|
||||
if (rtcTime.Year < 2016) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (WL_CONNECTED == WiFi.status()) {
|
||||
ntpsync = 1; // Initial NTP sync
|
||||
}
|
||||
} else {
|
||||
if ((rtcTime.Minute == 1) && (rtcTime.Second == 1)) {
|
||||
if ((1 == rtcTime.Minute) && (1 == rtcTime.Second)) {
|
||||
ntpsync = 1; // Hourly NTP sync at xx:01:01
|
||||
}
|
||||
}
|
||||
|
@ -850,7 +910,7 @@ void rtc_second()
|
|||
utctime++;
|
||||
loctime = utctime;
|
||||
if (loctime > 1451602800) { // 2016-01-01
|
||||
if (sysCfg.timezone == 99) {
|
||||
if (99 == sysCfg.timezone) {
|
||||
dstoffset = myDST.offset * SECS_PER_MIN;
|
||||
stdoffset = mySTD.offset * SECS_PER_MIN;
|
||||
if ((utctime >= (dsttime - stdoffset)) && (utctime < (stdtime - dstoffset))) {
|
||||
|
@ -913,11 +973,14 @@ void addLog(byte loglevel, const char *line)
|
|||
if (sysCfg.webserver && (loglevel <= sysCfg.weblog_level)) {
|
||||
Log[logidx] = String(mxtime) + " " + String(line);
|
||||
logidx++;
|
||||
if (logidx > MAX_LOG_LINES -1) logidx = 0;
|
||||
if (logidx > MAX_LOG_LINES -1) {
|
||||
logidx = 0;
|
||||
}
|
||||
}
|
||||
#endif // USE_WEBSERVER
|
||||
// if (sysCfg.emulation) return; // Disable syslog (UDP) when emulation using UDP is selected
|
||||
if ((WiFi.status() == WL_CONNECTED) && (loglevel <= syslog_level)) syslog(line);
|
||||
if ((WL_CONNECTED == WiFi.status()) && (loglevel <= syslog_level)) {
|
||||
syslog(line);
|
||||
}
|
||||
}
|
||||
|
||||
void addLog_P(byte loglevel, const char *formatP)
|
||||
|
|
|
@ -286,7 +286,10 @@ ESP8266WebServer *webServer;
|
|||
|
||||
boolean _removeDuplicateAPs = true;
|
||||
int _minimumQuality = -1;
|
||||
uint8_t _httpflag = HTTP_OFF, _uploaderror = 0, _uploadfiletype, _colcount;
|
||||
uint8_t _httpflag = HTTP_OFF;
|
||||
uint8_t _uploaderror = 0;
|
||||
uint8_t _uploadfiletype;
|
||||
uint8_t _colcount;
|
||||
|
||||
void startWebserver(int type, IPAddress ipweb)
|
||||
{
|
||||
|
@ -294,7 +297,7 @@ void startWebserver(int type, IPAddress ipweb)
|
|||
|
||||
if (!_httpflag) {
|
||||
if (!webServer) {
|
||||
webServer = new ESP8266WebServer((type==HTTP_MANAGER)?80:WEB_PORT);
|
||||
webServer = new ESP8266WebServer((HTTP_MANAGER==type)?80:WEB_PORT);
|
||||
webServer->on("/", handleRoot);
|
||||
webServer->on("/cn", handleConfig);
|
||||
webServer->on("/md", handleModule);
|
||||
|
@ -323,12 +326,12 @@ void startWebserver(int type, IPAddress ipweb)
|
|||
webServer->on("/rb", handleRestart);
|
||||
webServer->on("/fwlink", handleRoot); // Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
|
||||
#ifdef USE_EMULATION
|
||||
if (sysCfg.emulation == EMUL_WEMO) {
|
||||
if (EMUL_WEMO == sysCfg.emulation) {
|
||||
webServer->on("/upnp/control/basicevent1", HTTP_POST, handleUPnPevent);
|
||||
webServer->on("/eventservice.xml", handleUPnPservice);
|
||||
webServer->on("/setup.xml", handleUPnPsetupWemo);
|
||||
}
|
||||
if (sysCfg.emulation == EMUL_HUE) {
|
||||
if (EMUL_HUE == sysCfg.emulation) {
|
||||
webServer->on("/description.xml", handleUPnPsetupHue);
|
||||
}
|
||||
#endif // USE_EMULATION
|
||||
|
@ -357,7 +360,7 @@ void stopWebserver()
|
|||
void beginWifiManager()
|
||||
{
|
||||
// setup AP
|
||||
if ((WiFi.status() == WL_CONNECTED) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
||||
if ((WL_CONNECTED == WiFi.status()) && (static_cast<uint32_t>(WiFi.localIP()) != 0)) {
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("Wifimanager: Set AccessPoint and keep Station"));
|
||||
} else {
|
||||
|
@ -379,18 +382,22 @@ void beginWifiManager()
|
|||
|
||||
void pollDnsWeb()
|
||||
{
|
||||
if (dnsServer) dnsServer->processNextRequest();
|
||||
if (webServer) webServer->handleClient();
|
||||
if (dnsServer) {
|
||||
dnsServer->processNextRequest();
|
||||
}
|
||||
if (webServer) {
|
||||
webServer->handleClient();
|
||||
}
|
||||
}
|
||||
|
||||
void showPage(String &page)
|
||||
{
|
||||
if((_httpflag == HTTP_ADMIN) && (sysCfg.web_password[0] != 0) && !webServer->authenticate(WEB_USERNAME, sysCfg.web_password)) {
|
||||
if((HTTP_ADMIN == _httpflag) && (sysCfg.web_password[0] != 0) && !webServer->authenticate(WEB_USERNAME, sysCfg.web_password)) {
|
||||
return webServer->requestAuthentication();
|
||||
}
|
||||
page.replace("{ha}", my_module.name);
|
||||
page.replace("{h}", sysCfg.friendlyname[0]);
|
||||
if (_httpflag == HTTP_MANAGER) {
|
||||
if (HTTP_MANAGER == _httpflag) {
|
||||
if (WIFI_configCounter()) {
|
||||
page.replace("<body>", "<body onload='u()'>");
|
||||
page += FPSTR(HTTP_COUNTER);
|
||||
|
@ -412,7 +419,7 @@ void handleRoot()
|
|||
return;
|
||||
}
|
||||
|
||||
if (_httpflag == HTTP_MANAGER) {
|
||||
if (HTTP_MANAGER == _httpflag) {
|
||||
handleWifi0();
|
||||
} else {
|
||||
char stemp[10], line[100];
|
||||
|
@ -422,7 +429,7 @@ void handleRoot()
|
|||
|
||||
page += F("<div id='l1' name='l1'></div>");
|
||||
if (Maxdevice) {
|
||||
if (sysCfg.module == SONOFF_LED) {
|
||||
if (SONOFF_LED == sysCfg.module) {
|
||||
snprintf_P(line, sizeof(line), PSTR("<input type='range' min='1' max='100' value='%d' onchange='lb(value)'>"),
|
||||
sysCfg.led_dimmer[0]);
|
||||
page += line;
|
||||
|
@ -438,7 +445,7 @@ void handleRoot()
|
|||
page += F("</tr></table>");
|
||||
}
|
||||
|
||||
if (_httpflag == HTTP_ADMIN) {
|
||||
if (HTTP_ADMIN == _httpflag) {
|
||||
page += FPSTR(HTTP_BTN_MENU1);
|
||||
page += FPSTR(HTTP_BTN_RSTRT);
|
||||
}
|
||||
|
@ -450,23 +457,35 @@ void handleAjax2()
|
|||
{
|
||||
char svalue[16];
|
||||
|
||||
if (strlen(webServer->arg("o").c_str())) do_cmnd_power(atoi(webServer->arg("o").c_str()), 2);
|
||||
if (strlen(webServer->arg("o").c_str())) {
|
||||
do_cmnd_power(atoi(webServer->arg("o").c_str()), 2);
|
||||
}
|
||||
if (strlen(webServer->arg("d").c_str())) {
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("dimmer %s"), webServer->arg("d").c_str());
|
||||
do_cmnd(svalue);
|
||||
}
|
||||
|
||||
String tpage = "";
|
||||
if (hlw_flg) tpage += hlw_webPresent();
|
||||
if (sysCfg.module == SONOFF_SC) tpage += sc_webPresent();
|
||||
if (hlw_flg) {
|
||||
tpage += hlw_webPresent();
|
||||
}
|
||||
if (SONOFF_SC == sysCfg.module) {
|
||||
tpage += sc_webPresent();
|
||||
}
|
||||
#ifdef USE_DS18B20
|
||||
if (pin[GPIO_DSB] < 99) tpage += dsb_webPresent();
|
||||
if (pin[GPIO_DSB] < 99) {
|
||||
tpage += dsb_webPresent();
|
||||
}
|
||||
#endif // USE_DS18B20
|
||||
#ifdef USE_DS18x20
|
||||
if (pin[GPIO_DSB] < 99) tpage += ds18x20_webPresent();
|
||||
if (pin[GPIO_DSB] < 99) {
|
||||
tpage += ds18x20_webPresent();
|
||||
}
|
||||
#endif // USE_DS18x20
|
||||
#ifdef USE_DHT
|
||||
if (dht_type) tpage += dht_webPresent();
|
||||
if (dht_type) {
|
||||
tpage += dht_webPresent();
|
||||
}
|
||||
#endif // USE_DHT
|
||||
#ifdef USE_I2C
|
||||
if (i2c_flg) {
|
||||
|
@ -504,7 +523,7 @@ void handleAjax2()
|
|||
}
|
||||
/*
|
||||
* Will interrupt user action when selected
|
||||
if (sysCfg.module == SONOFF_LED) {
|
||||
if (SONOFF_LED == sysCfg.module) {
|
||||
snprintf_P(line, sizeof(line), PSTR("<input type='range' min='1' max='100' value='%d' onchange='lb(value)'>"),
|
||||
sysCfg.led_dimmer[0]);
|
||||
page += line;
|
||||
|
@ -518,20 +537,26 @@ void handleAjax2()
|
|||
|
||||
boolean httpUser()
|
||||
{
|
||||
boolean status = (_httpflag == HTTP_USER);
|
||||
if (status) handleRoot();
|
||||
boolean status = (HTTP_USER == _httpflag);
|
||||
if (status) {
|
||||
handleRoot();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void handleConfig()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle config"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
page.replace("{v}", "Configuration");
|
||||
page += FPSTR(HTTP_BTN_MENU2);
|
||||
if (sysCfg.mqtt_enabled) page += FPSTR(HTTP_BTN_MENU3);
|
||||
if (sysCfg.mqtt_enabled) {
|
||||
page += FPSTR(HTTP_BTN_MENU3);
|
||||
}
|
||||
page += FPSTR(HTTP_BTN_MENU4);
|
||||
page += FPSTR(HTTP_BTN_MAIN);
|
||||
showPage(page);
|
||||
|
@ -541,29 +566,49 @@ boolean inModule(byte val, uint8_t *arr)
|
|||
{
|
||||
int offset = 0;
|
||||
|
||||
if (!val) return false; // None
|
||||
if (!val) {
|
||||
return false; // None
|
||||
}
|
||||
#ifndef USE_I2C
|
||||
if (val == GPIO_I2C_SCL) return true;
|
||||
if (val == GPIO_I2C_SDA) return true;
|
||||
if (GPIO_I2C_SCL == val) {
|
||||
return true;
|
||||
}
|
||||
if (GPIO_I2C_SDA == val) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifndef USE_WS2812
|
||||
if (val == GPIO_WS2812) return true;
|
||||
if (GPIO_WS2812 == val) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#ifndef USE_IR_REMOTE
|
||||
if (val == GPIO_IRSEND) return true;
|
||||
if (GPIO_IRSEND == val) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
if (((val >= GPIO_REL1) && (val <= GPIO_REL4)) || ((val >= GPIO_LED1) && (val <= GPIO_LED4))) offset = 4;
|
||||
if (((val >= GPIO_REL1_INV) && (val <= GPIO_REL4_INV)) || ((val >= GPIO_LED1_INV) && (val <= GPIO_LED4_INV))) offset = -4;
|
||||
if (((val >= GPIO_REL1) && (val <= GPIO_REL4)) || ((val >= GPIO_LED1) && (val <= GPIO_LED4))) {
|
||||
offset = 4;
|
||||
}
|
||||
if (((val >= GPIO_REL1_INV) && (val <= GPIO_REL4_INV)) || ((val >= GPIO_LED1_INV) && (val <= GPIO_LED4_INV))) {
|
||||
offset = -4;
|
||||
}
|
||||
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
||||
if (arr[i] == val) return true;
|
||||
if (arr[i] == val + offset) return true;
|
||||
if (arr[i] == val) {
|
||||
return true;
|
||||
}
|
||||
if (arr[i] == val + offset) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void handleModule()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
char stemp[20], line[128];
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle Module config"));
|
||||
|
@ -596,9 +641,9 @@ void handleModule()
|
|||
}
|
||||
func += F("\";os=o0.replace(/-1/g,\"<option value=\").replace(/-2/g,\"</option>\");");
|
||||
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
||||
if (cmodule.gp.io[i] == GPIO_USER) {
|
||||
if (GPIO_USER == cmodule.gp.io[i]) {
|
||||
snprintf_P(line, sizeof(line), PSTR("<br/><b>GPIO%d</b> %s<select id='g%d' name='g%d'></select></br>"),
|
||||
i, (i==0)?"Button1":(i==1)?"Serial Out":(i==3)?"Serial In":(i==12)?"Relay1":(i==13)?"Led1I":(i==14)?"Sensor":"", i, i);
|
||||
i, (0==i)?"Button1":(1==i)?"Serial Out":(3==i)?"Serial In":(12==i)?"Relay1":(13==i)?"Led1I":(14==i)?"Sensor":"", i, i);
|
||||
page += line;
|
||||
snprintf_P(line, sizeof(line), PSTR("sk(%d,%d);"), my_module.gp.io[i], i);
|
||||
func += line;
|
||||
|
@ -625,7 +670,9 @@ void handleWifi0()
|
|||
|
||||
void handleWifi(boolean scan)
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
char log[LOGSZ];
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle Wifi config"));
|
||||
|
@ -641,7 +688,7 @@ void handleWifi(boolean scan)
|
|||
int n = WiFi.scanNetworks();
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("Wifi: Scan done"));
|
||||
|
||||
if (n == 0) {
|
||||
if (0 == n) {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("Wifi: No networks found"));
|
||||
page += F("No networks found. Refresh to scan again.");
|
||||
} else {
|
||||
|
@ -664,7 +711,9 @@ void handleWifi(boolean scan)
|
|||
if (_removeDuplicateAPs) {
|
||||
String cssid;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (indices[i] == -1) continue;
|
||||
if (-1 == indices[i]) {
|
||||
continue;
|
||||
}
|
||||
cssid = WiFi.SSID(indices[i]);
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (cssid == WiFi.SSID(indices[j])) {
|
||||
|
@ -678,7 +727,9 @@ void handleWifi(boolean scan)
|
|||
|
||||
//display networks in page
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (indices[i] == -1) continue; // skip dups
|
||||
if (-1 == indices[i]) {
|
||||
continue; // skip dups
|
||||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("Wifi: SSID %s, RSSI %d"), WiFi.SSID(indices[i]).c_str(), WiFi.RSSI(indices[i]));
|
||||
addLog(LOG_LEVEL_DEBUG, log);
|
||||
int quality = WIFI_getRSSIasQuality(WiFi.RSSI(indices[i]));
|
||||
|
@ -714,7 +765,7 @@ void handleWifi(boolean scan)
|
|||
page.replace("{s2}", sysCfg.sta_ssid[1]);
|
||||
page.replace("{p2}", sysCfg.sta_pwd[1]);
|
||||
page += FPSTR(HTTP_FORM_END);
|
||||
if (_httpflag == HTTP_MANAGER) {
|
||||
if (HTTP_MANAGER == _httpflag) {
|
||||
page += FPSTR(HTTP_BTN_RSTRT);
|
||||
} else {
|
||||
page += FPSTR(HTTP_BTN_CONF);
|
||||
|
@ -724,7 +775,9 @@ void handleWifi(boolean scan)
|
|||
|
||||
void handleMqtt()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle MQTT config"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
|
@ -746,7 +799,9 @@ void handleMqtt()
|
|||
|
||||
void handleLog()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle Log config"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
|
@ -792,7 +847,9 @@ void handleLog()
|
|||
|
||||
void handleOther()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle other config"));
|
||||
char stemp[40];
|
||||
|
||||
|
@ -807,9 +864,9 @@ void handleOther()
|
|||
page.replace("{3", sysCfg.friendlyname[0]);
|
||||
#ifdef USE_EMULATION
|
||||
page += FPSTR(HTTP_FORM_OTHER3);
|
||||
page.replace("{r2}", (sysCfg.emulation == EMUL_NONE) ? " checked" : "");
|
||||
page.replace("{r3}", (sysCfg.emulation == EMUL_WEMO) ? " checked" : "");
|
||||
page.replace("{r4}", (sysCfg.emulation == EMUL_HUE) ? " checked" : "");
|
||||
page.replace("{r2}", (EMUL_NONE == sysCfg.emulation) ? " checked" : "");
|
||||
page.replace("{r3}", (EMUL_WEMO == sysCfg.emulation) ? " checked" : "");
|
||||
page.replace("{r4}", (EMUL_HUE == sysCfg.emulation) ? " checked" : "");
|
||||
for (int i = 1; i < Maxdevice; i++) {
|
||||
page += FPSTR(HTTP_FORM_OTHER2);
|
||||
page.replace("{1", String(i +1));
|
||||
|
@ -826,7 +883,9 @@ void handleOther()
|
|||
|
||||
void handleDownload()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle download config"));
|
||||
|
||||
uint8_t buffer[sizeof(sysCfg)];
|
||||
|
@ -842,25 +901,37 @@ void handleDownload()
|
|||
memcpy(buffer, &sysCfg, sizeof(sysCfg));
|
||||
buffer[0] = CONFIG_FILE_SIGN;
|
||||
buffer[1] = (!CONFIG_FILE_XOR)?0:1;
|
||||
if (buffer[1]) for (uint16_t i = 2; i < sizeof(buffer); i++) buffer[i] ^= (CONFIG_FILE_XOR +i);
|
||||
if (buffer[1]) {
|
||||
for (uint16_t i = 2; i < sizeof(buffer); i++) {
|
||||
buffer[i] ^= (CONFIG_FILE_XOR +i);
|
||||
}
|
||||
}
|
||||
myClient.write((const char*)buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
void handleSave()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
char log[LOGSZ +20], stemp[20];
|
||||
byte what = 0, restart;
|
||||
char log[LOGSZ +20];
|
||||
char stemp[20];
|
||||
byte what = 0;
|
||||
byte restart;
|
||||
String result = "";
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Parameter save"));
|
||||
|
||||
if (strlen(webServer->arg("w").c_str())) what = atoi(webServer->arg("w").c_str());
|
||||
if (strlen(webServer->arg("w").c_str())) {
|
||||
what = atoi(webServer->arg("w").c_str());
|
||||
}
|
||||
switch (what) {
|
||||
case 1:
|
||||
strlcpy(sysCfg.hostname, (!strlen(webServer->arg("h").c_str())) ? WIFI_HOSTNAME : webServer->arg("h").c_str(), sizeof(sysCfg.hostname));
|
||||
if (strstr(sysCfg.hostname,"%")) strlcpy(sysCfg.hostname, WIFI_HOSTNAME, sizeof(sysCfg.hostname));
|
||||
if (strstr(sysCfg.hostname,"%")) {
|
||||
strlcpy(sysCfg.hostname, WIFI_HOSTNAME, sizeof(sysCfg.hostname));
|
||||
}
|
||||
strlcpy(sysCfg.sta_ssid[0], (!strlen(webServer->arg("s1").c_str())) ? STA_SSID1 : webServer->arg("s1").c_str(), sizeof(sysCfg.sta_ssid[0]));
|
||||
strlcpy(sysCfg.sta_pwd[0], (!strlen(webServer->arg("p1").c_str())) ? STA_PASS1 : webServer->arg("p1").c_str(), sizeof(sysCfg.sta_pwd[0]));
|
||||
strlcpy(sysCfg.sta_ssid[1], (!strlen(webServer->arg("s2").c_str())) ? STA_SSID2 : webServer->arg("s2").c_str(), sizeof(sysCfg.sta_ssid[1]));
|
||||
|
@ -921,8 +992,10 @@ void handleSave()
|
|||
memcpy_P(&cmodule, &modules[sysCfg.module], sizeof(cmodule));
|
||||
String gpios = "";
|
||||
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
|
||||
if (new_modflg) sysCfg.my_module.gp.io[i] = 0;
|
||||
if (cmodule.gp.io[i] == GPIO_USER) {
|
||||
if (new_modflg) {
|
||||
sysCfg.my_module.gp.io[i] = 0;
|
||||
}
|
||||
if (GPIO_USER == cmodule.gp.io[i]) {
|
||||
snprintf_P(stemp, sizeof(stemp), PSTR("g%d"), i);
|
||||
sysCfg.my_module.gp.io[i] = (!strlen(webServer->arg(stemp).c_str())) ? 0 : atoi(webServer->arg(stemp).c_str());
|
||||
gpios += F(", GPIO"); gpios += String(i); gpios += F(" "); gpios += String(sysCfg.my_module.gp.io[i]);
|
||||
|
@ -943,7 +1016,7 @@ void handleSave()
|
|||
page += result;
|
||||
page += F("</div>");
|
||||
page += FPSTR(HTTP_MSG_RSTRT);
|
||||
if (_httpflag == HTTP_MANAGER) {
|
||||
if (HTTP_MANAGER == _httpflag) {
|
||||
_httpflag = HTTP_ADMIN;
|
||||
} else {
|
||||
page += FPSTR(HTTP_BTN_MAIN);
|
||||
|
@ -958,7 +1031,9 @@ void handleSave()
|
|||
|
||||
void handleReset()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
char svalue[16]; // was MESSZ
|
||||
|
||||
|
@ -977,7 +1052,9 @@ void handleReset()
|
|||
|
||||
void handleRestore()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle restore"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
|
@ -992,7 +1069,9 @@ void handleRestore()
|
|||
|
||||
void handleUpgrade()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle upgrade"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
|
@ -1008,7 +1087,9 @@ void handleUpgrade()
|
|||
|
||||
void handleUpgradeStart()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
char svalue[100]; // was MESSZ
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Firmware upgrade start"));
|
||||
|
@ -1032,10 +1113,14 @@ void handleUpgradeStart()
|
|||
|
||||
void handleUploadDone()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: File upload done"));
|
||||
|
||||
char error[80], log[LOGSZ];
|
||||
char error[80];
|
||||
char log[LOGSZ];
|
||||
|
||||
WIFI_configCounter();
|
||||
restartflag = 0;
|
||||
mqttcounter = 0;
|
||||
|
@ -1065,7 +1150,6 @@ void handleUploadDone()
|
|||
}
|
||||
snprintf_P(log, sizeof(log), PSTR("Upload: Error - %s"), error);
|
||||
addLog(LOG_LEVEL_DEBUG, log);
|
||||
sl_blank(0);
|
||||
} else {
|
||||
page += F("<font color='green'>successful</font></b><br/><br/>Device will restart in a few seconds");
|
||||
restartflag = 2;
|
||||
|
@ -1081,17 +1165,21 @@ void handleUploadLoop()
|
|||
char log[LOGSZ];
|
||||
boolean _serialoutput = (LOG_LEVEL_DEBUG <= seriallog_level);
|
||||
|
||||
if (_httpflag == HTTP_USER) return;
|
||||
if (HTTP_USER == _httpflag) {
|
||||
return;
|
||||
}
|
||||
if (_uploaderror) {
|
||||
if (!_uploadfiletype) Update.end();
|
||||
if (!_uploadfiletype) {
|
||||
Update.end();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
HTTPUpload& upload = webServer->upload();
|
||||
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
if (UPLOAD_FILE_START == upload.status) {
|
||||
restartflag = 60;
|
||||
if (upload.filename.c_str()[0] == 0) {
|
||||
if (0 == upload.filename.c_str()[0]) {
|
||||
_uploaderror = 1;
|
||||
return;
|
||||
}
|
||||
|
@ -1102,18 +1190,21 @@ void handleUploadLoop()
|
|||
#ifdef USE_EMULATION
|
||||
UDP_Disconnect();
|
||||
#endif // USE_EMULATION
|
||||
if (sysCfg.mqtt_enabled) mqttClient.disconnect();
|
||||
if (sysCfg.mqtt_enabled) {
|
||||
mqttClient.disconnect();
|
||||
}
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if (!Update.begin(maxSketchSpace)) { //start with max available size
|
||||
if (_serialoutput) Update.printError(Serial);
|
||||
if (_serialoutput) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
_uploaderror = 2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
sl_blank(1);
|
||||
_colcount = 0;
|
||||
} else if (!_uploaderror && (upload.status == UPLOAD_FILE_WRITE)) {
|
||||
if (upload.totalSize == 0)
|
||||
} else if (!_uploaderror && (UPLOAD_FILE_WRITE == upload.status)) {
|
||||
if (0 == upload.totalSize)
|
||||
{
|
||||
if (_uploadfiletype) {
|
||||
if (upload.buf[0] != CONFIG_FILE_SIGN) {
|
||||
|
@ -1134,7 +1225,7 @@ void handleUploadLoop()
|
|||
_uploaderror = 4;
|
||||
return;
|
||||
}
|
||||
if ((sysCfg.module == SONOFF_TOUCH) || (sysCfg.module == SONOFF_4CH)) {
|
||||
if ((SONOFF_TOUCH == sysCfg.module) || (SONOFF_4CH == sysCfg.module)) {
|
||||
upload.buf[2] = 3; // DOUT - ESP8285
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("FLSH: Set Flash Mode to 3"));
|
||||
}
|
||||
|
@ -1142,27 +1233,39 @@ void handleUploadLoop()
|
|||
}
|
||||
if (_uploadfiletype) { // config
|
||||
if (!_uploaderror) {
|
||||
if (upload.buf[1]) for (uint16_t i = 2; i < upload.currentSize; i++) upload.buf[i] ^= (CONFIG_FILE_XOR +i);
|
||||
if (upload.buf[1]) {
|
||||
for (uint16_t i = 2; i < upload.currentSize; i++) {
|
||||
upload.buf[i] ^= (CONFIG_FILE_XOR +i);
|
||||
}
|
||||
}
|
||||
CFG_DefaultSet2();
|
||||
memcpy((char*)&sysCfg +16, upload.buf +16, upload.currentSize -16);
|
||||
}
|
||||
} else { // firmware
|
||||
if (!_uploaderror && (Update.write(upload.buf, upload.currentSize) != upload.currentSize)) {
|
||||
if (_serialoutput) Update.printError(Serial);
|
||||
if (_serialoutput) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
_uploaderror = 5;
|
||||
return;
|
||||
}
|
||||
if (_serialoutput) {
|
||||
Serial.printf(".");
|
||||
_colcount++;
|
||||
if (!(_colcount % 80)) Serial.println();
|
||||
if (!(_colcount % 80)) {
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
} else if(!_uploaderror && (upload.status == UPLOAD_FILE_END)) {
|
||||
if (_serialoutput && (_colcount % 80)) Serial.println();
|
||||
}
|
||||
} else if(!_uploaderror && (UPLOAD_FILE_END == upload.status)) {
|
||||
if (_serialoutput && (_colcount % 80)) {
|
||||
Serial.println();
|
||||
}
|
||||
if (!_uploadfiletype) {
|
||||
if (!Update.end(true)) { // true to set the size to the current progress
|
||||
if (_serialoutput) Update.printError(Serial);
|
||||
if (_serialoutput) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
_uploaderror = 6;
|
||||
return;
|
||||
}
|
||||
|
@ -1171,18 +1274,22 @@ void handleUploadLoop()
|
|||
snprintf_P(log, sizeof(log), PSTR("Upload: Successful %u bytes. Restarting"), upload.totalSize);
|
||||
addLog(LOG_LEVEL_INFO, log);
|
||||
}
|
||||
} else if(upload.status == UPLOAD_FILE_ABORTED) {
|
||||
} else if (UPLOAD_FILE_ABORTED == upload.status) {
|
||||
restartflag = 0;
|
||||
mqttcounter = 0;
|
||||
_uploaderror = 7;
|
||||
if (!_uploadfiletype) Update.end();
|
||||
if (!_uploadfiletype) {
|
||||
Update.end();
|
||||
}
|
||||
}
|
||||
delay(0);
|
||||
}
|
||||
|
||||
void handleCmnd()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
char svalue[128]; // was MESSZ
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle cmnd"));
|
||||
|
@ -1210,7 +1317,9 @@ void handleCmnd()
|
|||
byte counter = curridx;
|
||||
do {
|
||||
if (Log[counter].length()) {
|
||||
if (message.length()) message += F("\n");
|
||||
if (message.length()) {
|
||||
message += F("\n");
|
||||
}
|
||||
if (sysCfg.mqtt_enabled) {
|
||||
// [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [RESULT = {"POWER":"OFF"}]
|
||||
// message += Log[counter].substring(17 + strlen(PUB_PREFIX) + strlen(sysCfg.mqtt_topic));
|
||||
|
@ -1221,7 +1330,9 @@ void handleCmnd()
|
|||
}
|
||||
}
|
||||
counter++;
|
||||
if (counter > MAX_LOG_LINES -1) counter = 0;
|
||||
if (counter > MAX_LOG_LINES -1) {
|
||||
counter = 0;
|
||||
}
|
||||
} while (counter != logidx);
|
||||
} else {
|
||||
message = F("Enable weblog 2 if response expected\n");
|
||||
|
@ -1238,7 +1349,9 @@ void handleCmnd()
|
|||
|
||||
void handleConsole()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle console"));
|
||||
|
||||
|
@ -1253,9 +1366,13 @@ void handleConsole()
|
|||
|
||||
void handleAjax()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
char log[LOGSZ], svalue[128]; // was MESSZ
|
||||
byte cflg = 1, counter = 99;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
char log[LOGSZ];
|
||||
char svalue[128]; // was MESSZ
|
||||
byte cflg = 1;
|
||||
byte counter = 99;
|
||||
|
||||
if (strlen(webServer->arg("c1").c_str())) {
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("%s"), webServer->arg("c1").c_str());
|
||||
|
@ -1267,7 +1384,9 @@ void handleAjax()
|
|||
syslog_level = syslog_now;
|
||||
}
|
||||
|
||||
if (strlen(webServer->arg("c2").c_str())) counter = atoi(webServer->arg("c2").c_str());
|
||||
if (strlen(webServer->arg("c2").c_str())) {
|
||||
counter = atoi(webServer->arg("c2").c_str());
|
||||
}
|
||||
|
||||
String message = F("<r><i>");
|
||||
message += String(logidx);
|
||||
|
@ -1279,17 +1398,23 @@ void handleAjax()
|
|||
}
|
||||
message += F("</j><l>");
|
||||
if (counter != logidx) {
|
||||
if (counter == 99) {
|
||||
if (99 == counter) {
|
||||
counter = logidx;
|
||||
cflg = 0;
|
||||
}
|
||||
do {
|
||||
if (Log[counter].length()) {
|
||||
if (cflg) message += F("\n"); else cflg = 1;
|
||||
if (cflg) {
|
||||
message += F("\n");
|
||||
} else {
|
||||
cflg = 1;
|
||||
}
|
||||
message += Log[counter];
|
||||
}
|
||||
counter++;
|
||||
if (counter > MAX_LOG_LINES -1) counter = 0;
|
||||
if (counter > MAX_LOG_LINES -1) {
|
||||
counter = 0;
|
||||
}
|
||||
} while (counter != logidx);
|
||||
}
|
||||
message += F("</l></r>");
|
||||
|
@ -1302,7 +1427,9 @@ void handleAjax()
|
|||
|
||||
void handleInfo()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle info"));
|
||||
|
||||
int freeMem = ESP.getFreeHeap();
|
||||
|
@ -1356,9 +1483,15 @@ void handleInfo()
|
|||
|
||||
page += F("<tr><th>Emulation</th><td>");
|
||||
#ifdef USE_EMULATION
|
||||
if (sysCfg.emulation == EMUL_WEMO) page += F("Belkin WeMo");
|
||||
else if (sysCfg.emulation == EMUL_HUE) page += F("Hue Bridge");
|
||||
else page += F("None");
|
||||
if (EMUL_WEMO == sysCfg.emulation) {
|
||||
page += F("Belkin WeMo");
|
||||
}
|
||||
else if (EMUL_HUE == sysCfg.emulation) {
|
||||
page += F("Hue Bridge");
|
||||
}
|
||||
else {
|
||||
page += F("None");
|
||||
}
|
||||
#else
|
||||
page += F("Disabled");
|
||||
#endif // USE_EMULATION
|
||||
|
@ -1395,13 +1528,15 @@ void handleInfo()
|
|||
|
||||
void handleRestart()
|
||||
{
|
||||
if (httpUser()) return;
|
||||
if (httpUser()) {
|
||||
return;
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Restarting"));
|
||||
|
||||
String page = FPSTR(HTTP_HEAD);
|
||||
page.replace("{v}", "Info");
|
||||
page += FPSTR(HTTP_MSG_RSTRT);
|
||||
if (_httpflag == HTTP_MANAGER) {
|
||||
if (HTTP_MANAGER == _httpflag) {
|
||||
_httpflag = HTTP_ADMIN;
|
||||
} else {
|
||||
page += FPSTR(HTTP_BTN_MAIN);
|
||||
|
@ -1421,7 +1556,7 @@ void handleNotFound()
|
|||
|
||||
#ifdef USE_EMULATION
|
||||
String path = webServer->uri();
|
||||
if ((sysCfg.emulation == EMUL_HUE) && (path.startsWith("/api"))) {
|
||||
if ((EMUL_HUE == sysCfg.emulation) && (path.startsWith("/api"))) {
|
||||
handle_hue_api(&path);
|
||||
} else
|
||||
#endif // USE_EMULATION
|
||||
|
@ -1448,7 +1583,7 @@ void handleNotFound()
|
|||
/* Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
|
||||
boolean captivePortal()
|
||||
{
|
||||
if ((_httpflag == HTTP_MANAGER) && !isIp(webServer->hostHeader())) {
|
||||
if ((HTTP_MANAGER == _httpflag) && !isIp(webServer->hostHeader())) {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Request redirected to captive portal"));
|
||||
|
||||
webServer->sendHeader("Location", String("http://") + webServer->client().localIP().toString(), true);
|
||||
|
|
|
@ -56,14 +56,16 @@ void mqtt_publishDomoticzPowerState(byte device)
|
|||
char svalue[64]; // was MESSZ
|
||||
|
||||
if (sysCfg.domoticz_relay_idx[device -1] && (strlen(sysCfg.domoticz_in_topic) != 0)) {
|
||||
if ((device < 1) || (device > Maxdevice)) device = 1;
|
||||
if ((device < 1) || (device > Maxdevice)) {
|
||||
device = 1;
|
||||
}
|
||||
|
||||
if (sysCfg.module == SONOFF_LED) {
|
||||
if (SONOFF_LED == sysCfg.module) {
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"idx\":%d,\"nvalue\":2,\"svalue\":\"%d\"}"),
|
||||
sysCfg.domoticz_relay_idx[device -1], sysCfg.led_dimmer[device -1]);
|
||||
mqtt_publish(sysCfg.domoticz_in_topic, svalue);
|
||||
}
|
||||
else if ((device == 1) && (pin[GPIO_WS2812] < 99)) {
|
||||
else if ((1 == device) && (pin[GPIO_WS2812] < 99)) {
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"idx\":%d,\"nvalue\":2,\"svalue\":\"%d\"}"),
|
||||
sysCfg.domoticz_relay_idx[device -1], sysCfg.ws_dimmer);
|
||||
mqtt_publish(sysCfg.domoticz_in_topic, svalue);
|
||||
|
@ -76,7 +78,9 @@ void mqtt_publishDomoticzPowerState(byte device)
|
|||
|
||||
void domoticz_updatePowerState(byte device)
|
||||
{
|
||||
if (domoticz_update_flag) mqtt_publishDomoticzPowerState(device);
|
||||
if (domoticz_update_flag) {
|
||||
mqtt_publishDomoticzPowerState(device);
|
||||
}
|
||||
domoticz_update_flag = 1;
|
||||
}
|
||||
|
||||
|
@ -86,7 +90,9 @@ void domoticz_mqttUpdate()
|
|||
domoticz_update_timer--;
|
||||
if (domoticz_update_timer <= 0) {
|
||||
domoticz_update_timer = sysCfg.domoticz_update_timer;
|
||||
for (byte i = 1; i <= Maxdevice; i++) mqtt_publishDomoticzPowerState(i);
|
||||
for (byte i = 1; i <= Maxdevice; i++) {
|
||||
mqtt_publishDomoticzPowerState(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -130,17 +136,25 @@ boolean domoticz_update()
|
|||
|
||||
boolean domoticz_mqttData(char *topicBuf, uint16_t stopicBuf, char *dataBuf, uint16_t sdataBuf)
|
||||
{
|
||||
char log[LOGSZ], stemp1[10];
|
||||
char log[LOGSZ];
|
||||
char stemp1[10];
|
||||
unsigned long idx = 0;
|
||||
int16_t nvalue, found = 0;
|
||||
int16_t nvalue;
|
||||
int16_t found = 0;
|
||||
|
||||
domoticz_update_flag = 1;
|
||||
if (!strncmp(topicBuf, sysCfg.domoticz_out_topic, strlen(sysCfg.domoticz_out_topic)) != 0) {
|
||||
if (sdataBuf < 20) return 1;
|
||||
if (sdataBuf < 20) {
|
||||
return 1;
|
||||
}
|
||||
StaticJsonBuffer<400> jsonBuf;
|
||||
JsonObject& domoticz = jsonBuf.parseObject(dataBuf);
|
||||
if (!domoticz.success()) return 1;
|
||||
// if (strcmp(domoticz["dtype"],"Light/Switch")) return 1;
|
||||
if (!domoticz.success()) {
|
||||
return 1;
|
||||
}
|
||||
// if (strcmp(domoticz["dtype"],"Light/Switch")) {
|
||||
// return 1;
|
||||
// }
|
||||
idx = domoticz["idx"];
|
||||
nvalue = domoticz["nvalue"];
|
||||
|
||||
|
@ -151,16 +165,22 @@ boolean domoticz_mqttData(char *topicBuf, uint16_t stopicBuf, char *dataBuf, uin
|
|||
for (byte i = 0; i < Maxdevice; i++) {
|
||||
if ((idx > 0) && (idx == sysCfg.domoticz_relay_idx[i])) {
|
||||
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), i +1);
|
||||
if (nvalue == 2) {
|
||||
if (2 == nvalue) {
|
||||
nvalue = domoticz["svalue1"];
|
||||
if ((pin[GPIO_WS2812] < 99) && (sysCfg.ws_dimmer == nvalue)) return 1;
|
||||
if ((sysCfg.module == SONOFF_LED) && (sysCfg.led_dimmer[i] == nvalue)) return 1;
|
||||
if ((pin[GPIO_WS2812] < 99) && (sysCfg.ws_dimmer == nvalue)) {
|
||||
return 1;
|
||||
}
|
||||
if ((SONOFF_LED == sysCfg.module) && (sysCfg.led_dimmer[i] == nvalue)) {
|
||||
return 1;
|
||||
}
|
||||
snprintf_P(topicBuf, stopicBuf, PSTR("%s/%s/DIMMER%s"),
|
||||
sysCfg.mqtt_prefix[0], sysCfg.mqtt_topic, (Maxdevice > 1) ? stemp1 : "");
|
||||
snprintf_P(dataBuf, sdataBuf, PSTR("%d"), nvalue);
|
||||
found = 1;
|
||||
} else {
|
||||
if (((power >> i) &1) == nvalue) return 1;
|
||||
if (((power >> i) &1) == nvalue) {
|
||||
return 1;
|
||||
}
|
||||
snprintf_P(topicBuf, stopicBuf, PSTR("%s/%s/POWER%s"),
|
||||
sysCfg.mqtt_prefix[0], sysCfg.mqtt_topic, (Maxdevice > 1) ? stemp1 : "");
|
||||
snprintf_P(dataBuf, sdataBuf, PSTR("%d"), nvalue);
|
||||
|
@ -170,7 +190,9 @@ boolean domoticz_mqttData(char *topicBuf, uint16_t stopicBuf, char *dataBuf, uin
|
|||
}
|
||||
}
|
||||
}
|
||||
if (!found) return 1;
|
||||
if (!found) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
snprintf_P(log, sizeof(log), PSTR("DMTZ: Receive topic %s, data %s"), topicBuf, dataBuf);
|
||||
addLog(LOG_LEVEL_DEBUG_MORE, log);
|
||||
|
@ -191,14 +213,14 @@ boolean domoticz_command(const char *type, uint16_t index, char *dataBuf, uint16
|
|||
if (!strncmp(type,"DOMOTICZ",8)) {
|
||||
if (!strcmp(type +8,"INTOPIC")) {
|
||||
if ((data_len > 0) && (data_len < sizeof(sysCfg.domoticz_in_topic))) {
|
||||
strlcpy(sysCfg.domoticz_in_topic, (payload == 1) ? DOMOTICZ_IN_TOPIC : dataBuf, sizeof(sysCfg.domoticz_in_topic));
|
||||
strlcpy(sysCfg.domoticz_in_topic, (1 == payload) ? DOMOTICZ_IN_TOPIC : dataBuf, sizeof(sysCfg.domoticz_in_topic));
|
||||
restartflag = 2;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"DomoticzInTopic\":\"%s\"}"), sysCfg.domoticz_in_topic);
|
||||
}
|
||||
else if (!strcmp(type +8,"OUTTOPIC")) {
|
||||
if ((data_len > 0) && (data_len < sizeof(sysCfg.domoticz_out_topic))) {
|
||||
strlcpy(sysCfg.domoticz_out_topic, (payload == 1) ? DOMOTICZ_OUT_TOPIC : dataBuf, sizeof(sysCfg.domoticz_out_topic));
|
||||
strlcpy(sysCfg.domoticz_out_topic, (1 == payload) ? DOMOTICZ_OUT_TOPIC : dataBuf, sizeof(sysCfg.domoticz_out_topic));
|
||||
restartflag = 2;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"DomoticzOutTopic\":\"%s\"}"), sysCfg.domoticz_out_topic);
|
||||
|
@ -246,7 +268,7 @@ boolean domoticz_button(byte key, byte device, byte state, byte svalflg)
|
|||
char svalue[80]; // was MESSZ
|
||||
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
|
||||
(key) ? sysCfg.domoticz_switch_idx[device -1] : sysCfg.domoticz_key_idx[device -1], (state) ? (state == 2) ? "Toggle" : "On" : "Off");
|
||||
(key) ? sysCfg.domoticz_switch_idx[device -1] : sysCfg.domoticz_key_idx[device -1], (state) ? (2 == state) ? "Toggle" : "On" : "Off");
|
||||
mqtt_publish(sysCfg.domoticz_in_topic, svalue);
|
||||
return 1;
|
||||
} else {
|
||||
|
@ -315,7 +337,7 @@ void domoticz_sensor5(uint16_t lux)
|
|||
#ifdef USE_WEBSERVER
|
||||
void handleDomoticz()
|
||||
{
|
||||
if (_httpflag == HTTP_USER) {
|
||||
if (HTTP_USER == _httpflag) {
|
||||
handleRoot();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -115,7 +115,9 @@ boolean ir_send_command(char *type, uint16_t index, char *dataBufUc, uint16_t da
|
|||
} else error = true;
|
||||
}
|
||||
} else error = true;
|
||||
if (error) snprintf_P(svalue, ssvalue, PSTR("{\"IRSend\":\"No protocol, bits or data\"}"));
|
||||
if (error) {
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"IRSend\":\"No protocol, bits or data\"}"));
|
||||
}
|
||||
}
|
||||
#ifdef USE_IR_HVAC
|
||||
else if (!strcmp(type,"IRHVAC")) {
|
||||
|
@ -145,7 +147,9 @@ boolean ir_send_command(char *type, uint16_t index, char *dataBufUc, uint16_t da
|
|||
else error = true;
|
||||
}
|
||||
} else error = true;
|
||||
if (error) snprintf_P(svalue, ssvalue, PSTR("{\"IRHVAC\":\"Wrong Vendor, Mode and/or FanSpeed\"}"));
|
||||
if (error) {
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"IRHVAC\":\"Wrong Vendor, Mode and/or FanSpeed\"}"));
|
||||
}
|
||||
}
|
||||
#endif // USE_IR_HVAC
|
||||
else {
|
||||
|
@ -160,7 +164,8 @@ boolean ir_hvac_toshiba(const char *HVAC_Mode, const char *HVAC_FanMode, boolean
|
|||
unsigned int rawdata[2 + 2*8*HVAC_TOSHIBA_DATALEN + 2];
|
||||
byte data[HVAC_TOSHIBA_DATALEN] = { 0xF2, 0x0D, 0x03, 0xFC, 0x01, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
char *p, *token;
|
||||
char *p;
|
||||
char *token;
|
||||
uint8_t mode;
|
||||
|
||||
if (HVAC_Mode == NULL) {
|
||||
|
@ -168,19 +173,27 @@ boolean ir_hvac_toshiba(const char *HVAC_Mode, const char *HVAC_FanMode, boolean
|
|||
} else {
|
||||
p = strchr(HVACMODE, HVAC_Mode[0]);
|
||||
}
|
||||
if (!p) return true;
|
||||
if (!p) {
|
||||
return true;
|
||||
}
|
||||
data[6] = (p - HVACMODE) ^ 0x03; // HOT = 0x03, DRY = 0x02, COOL = 0x01, AUTO = 0x00
|
||||
|
||||
if (!HVAC_Power) data[6] = (byte) 0x07; // Turn OFF HVAC
|
||||
if (!HVAC_Power) {
|
||||
data[6] = (byte) 0x07; // Turn OFF HVAC
|
||||
}
|
||||
|
||||
if (HVAC_FanMode == NULL) {
|
||||
p = (char*)FANSPEED; // default FAN_SPEED_AUTO
|
||||
} else {
|
||||
p = strchr(FANSPEED, HVAC_FanMode[0]);
|
||||
}
|
||||
if (!p) return true;
|
||||
if (!p) {
|
||||
return true;
|
||||
}
|
||||
mode = p - FANSPEED +1;
|
||||
if ((mode == 1) || (mode == 7)) mode = 0;
|
||||
if ((1 == mode) || (7 == mode)) {
|
||||
mode = 0;
|
||||
}
|
||||
mode = mode << 5; // AUTO = 0x00, SPEED = 0x40, 0x60, 0x80, 0xA0, 0xC0, SILENT = 0x00
|
||||
data[6] = data[6] | mode;
|
||||
|
||||
|
@ -191,7 +204,9 @@ boolean ir_hvac_toshiba(const char *HVAC_Mode, const char *HVAC_FanMode, boolean
|
|||
else if (HVAC_Temp < 17) {
|
||||
Temp = 17;
|
||||
}
|
||||
else Temp = HVAC_Temp;
|
||||
else {
|
||||
Temp = HVAC_Temp;
|
||||
}
|
||||
data[5] = (byte) Temp - 17 << 4;
|
||||
|
||||
data[HVAC_TOSHIBA_DATALEN-1] = 0;
|
||||
|
@ -234,7 +249,8 @@ boolean ir_hvac_toshiba(const char *HVAC_Mode, const char *HVAC_FanMode, boolean
|
|||
|
||||
boolean ir_hvac_mitsubishi(const char *HVAC_Mode,const char *HVAC_FanMode, boolean HVAC_Power, int HVAC_Temp)
|
||||
{
|
||||
char *p, *token;
|
||||
char *p;
|
||||
char *token;
|
||||
uint8_t mode;
|
||||
char log[LOGSZ];
|
||||
|
||||
|
@ -245,7 +261,9 @@ boolean ir_hvac_mitsubishi(const char *HVAC_Mode,const char *HVAC_FanMode, boole
|
|||
} else {
|
||||
p = strchr(HVACMODE, HVAC_Mode[0]);
|
||||
}
|
||||
if (!p) return true;
|
||||
if (!p) {
|
||||
return true;
|
||||
}
|
||||
mode = (p - HVACMODE +1) << 3; // HOT = 0x08, DRY = 0x10, COOL = 0x18, AUTO = 0x20
|
||||
mitsubir->setMode(mode);
|
||||
|
||||
|
@ -256,7 +274,9 @@ boolean ir_hvac_mitsubishi(const char *HVAC_Mode,const char *HVAC_FanMode, boole
|
|||
} else {
|
||||
p = strchr(FANSPEED, HVAC_FanMode[0]);
|
||||
}
|
||||
if (!p) return true;
|
||||
if (!p) {
|
||||
return true;
|
||||
}
|
||||
mode = p - FANSPEED; // AUTO = 0, SPEED = 1 .. 5, SILENT = 6
|
||||
mitsubir->setFan(mode);
|
||||
|
||||
|
|
|
@ -45,10 +45,11 @@ uint8_t ledTable[] = {
|
|||
184,186,189,191,193,195,197,199,201,204,206,208,210,212,215,217,
|
||||
219,221,224,226,228,231,233,235,238,240,243,245,248,250,253,255 };
|
||||
|
||||
uint8_t sl_dcolor[2], sl_tcolor[2], sl_lcolor[2];
|
||||
uint8_t sl_dcolor[2];
|
||||
uint8_t sl_tcolor[2];
|
||||
uint8_t sl_lcolor[2];
|
||||
|
||||
uint8_t sl_power;
|
||||
uint8_t sl_blankv;
|
||||
uint8_t sl_any;
|
||||
uint8_t sl_wakeupActive = 0;
|
||||
uint8_t sl_wakeupDimmer = 0;
|
||||
|
@ -56,17 +57,23 @@ uint16_t sl_wakeupCntr = 0;
|
|||
|
||||
uint32_t Atoh(char *s)
|
||||
{
|
||||
uint32_t value = 0, digit;
|
||||
uint32_t value = 0;
|
||||
uint32_t digit;
|
||||
int8_t c;
|
||||
|
||||
while((c = *s++)) {
|
||||
if('0' <= c && c <= '9')
|
||||
if ('0' <= c && c <= '9') {
|
||||
digit = c - '0';
|
||||
else if('A' <= c && c <= 'F')
|
||||
}
|
||||
else if ('A' <= c && c <= 'F') {
|
||||
digit = c - 'A' + 10;
|
||||
else if('a' <= c && c<= 'f')
|
||||
}
|
||||
else if ('a' <= c && c <= 'f') {
|
||||
digit = c - 'a' + 10;
|
||||
else break;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
value = (value << 4) | digit;
|
||||
}
|
||||
return value;
|
||||
|
@ -74,11 +81,9 @@ uint32_t Atoh(char *s)
|
|||
|
||||
void sl_setDim(uint8_t myDimmer)
|
||||
{
|
||||
float newDim, fmyCld, fmyWrm;
|
||||
|
||||
newDim = 100 / (float)myDimmer;
|
||||
fmyCld = (float)sysCfg.led_color[0] / newDim;
|
||||
fmyWrm = (float)sysCfg.led_color[1] / newDim;
|
||||
float newDim = 100 / (float)myDimmer;
|
||||
float fmyCld = (float)sysCfg.led_color[0] / newDim;
|
||||
float fmyWrm = (float)sysCfg.led_color[1] / newDim;
|
||||
sl_dcolor[0] = (uint8_t)fmyCld;
|
||||
sl_dcolor[1] = (uint8_t)fmyWrm;
|
||||
}
|
||||
|
@ -89,31 +94,13 @@ void sl_init(void)
|
|||
{
|
||||
sysCfg.pwmvalue[0] = 0; // We use led_color
|
||||
sysCfg.pwmvalue[1] = 0; // We use led_color
|
||||
sl_blankv = 0;
|
||||
sl_power = 0;
|
||||
sl_any = 0;
|
||||
sl_wakeupActive = 0;
|
||||
}
|
||||
|
||||
void sl_blank(byte state)
|
||||
{
|
||||
// Called by interrupt disabling routines like OTA or web upload
|
||||
// state = 0: No blank
|
||||
// 1: Blank led to solve flicker
|
||||
|
||||
/*
|
||||
* Disabled when used with latest arduino-esp8266 pwm files
|
||||
if (sysCfg.module == SONOFF_LED) {
|
||||
sl_blankv = state;
|
||||
sl_wakeupActive = 0;
|
||||
sl_animate();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void sl_setPower(uint8_t power)
|
||||
{
|
||||
sl_blankv = 0;
|
||||
sl_power = power &1;
|
||||
sl_wakeupActive = 0;
|
||||
sl_animate();
|
||||
|
@ -125,26 +112,34 @@ void sl_animate()
|
|||
char svalue[32]; // was MESSZ
|
||||
uint8_t fadeValue;
|
||||
|
||||
if ((sl_power == 0) || sl_blankv) { // Power Off
|
||||
if (0 == sl_power) { // Power Off
|
||||
sl_tcolor[0] = 0;
|
||||
sl_tcolor[1] = 0;
|
||||
}
|
||||
else {
|
||||
if (!sl_wakeupActive) { // Power On
|
||||
sl_setDim(sysCfg.led_dimmer[0]);
|
||||
if (sysCfg.led_fade == 0) {
|
||||
if (0 == sysCfg.led_fade) {
|
||||
sl_tcolor[0] = sl_dcolor[0];
|
||||
sl_tcolor[1] = sl_dcolor[1];
|
||||
} else {
|
||||
if ((sl_tcolor[0] != sl_dcolor[0]) || (sl_tcolor[1] != sl_dcolor[1])) {
|
||||
if (sl_tcolor[0] < sl_dcolor[0]) sl_tcolor[0] += ((sl_dcolor[0] - sl_tcolor[0]) >> sysCfg.led_speed) +1;
|
||||
if (sl_tcolor[1] < sl_dcolor[1]) sl_tcolor[1] += ((sl_dcolor[1] - sl_tcolor[1]) >> sysCfg.led_speed) +1;
|
||||
if (sl_tcolor[0] > sl_dcolor[0]) sl_tcolor[0] -= ((sl_tcolor[0] - sl_dcolor[0]) >> sysCfg.led_speed) +1;
|
||||
if (sl_tcolor[1] > sl_dcolor[1]) sl_tcolor[1] -= ((sl_tcolor[1] - sl_dcolor[1]) >> sysCfg.led_speed) +1;
|
||||
if (sl_tcolor[0] < sl_dcolor[0]) {
|
||||
sl_tcolor[0] += ((sl_dcolor[0] - sl_tcolor[0]) >> sysCfg.led_speed) +1;
|
||||
}
|
||||
if (sl_tcolor[1] < sl_dcolor[1]) {
|
||||
sl_tcolor[1] += ((sl_dcolor[1] - sl_tcolor[1]) >> sysCfg.led_speed) +1;
|
||||
}
|
||||
if (sl_tcolor[0] > sl_dcolor[0]) {
|
||||
sl_tcolor[0] -= ((sl_tcolor[0] - sl_dcolor[0]) >> sysCfg.led_speed) +1;
|
||||
}
|
||||
if (sl_tcolor[1] > sl_dcolor[1]) {
|
||||
sl_tcolor[1] -= ((sl_tcolor[1] - sl_dcolor[1]) >> sysCfg.led_speed) +1;
|
||||
}
|
||||
}
|
||||
} else { // Power On using wak up duration
|
||||
if (sl_wakeupActive == 1) {
|
||||
}
|
||||
} else { // Power On using wake up duration
|
||||
if (1 == sl_wakeupActive) {
|
||||
sl_wakeupActive = 2;
|
||||
sl_tcolor[0] = 0;
|
||||
sl_tcolor[1] = 0;
|
||||
|
@ -185,11 +180,12 @@ void sl_animate()
|
|||
|
||||
boolean sl_command(char *type, uint16_t index, char *dataBufUc, uint16_t data_len, int16_t payload, char *svalue, uint16_t ssvalue)
|
||||
{
|
||||
boolean serviced = true, coldim = false;
|
||||
boolean serviced = true;
|
||||
boolean coldim = false;
|
||||
|
||||
if (!strcmp(type,"COLOR")) {
|
||||
uint8_t my_color[5];
|
||||
if (data_len == 4) {
|
||||
if (4 == data_len) {
|
||||
char ccold[3], cwarm[3];
|
||||
memcpy(ccold, dataBufUc, 2);
|
||||
ccold[2] = '\0';
|
||||
|
@ -198,7 +194,9 @@ boolean sl_command(char *type, uint16_t index, char *dataBufUc, uint16_t data_le
|
|||
my_color[0] = Atoh(ccold);
|
||||
my_color[1] = Atoh(cwarm);
|
||||
uint16_t temp = my_color[0];
|
||||
if (temp < my_color[1]) temp = my_color[1];
|
||||
if (temp < my_color[1]) {
|
||||
temp = my_color[1];
|
||||
}
|
||||
float mDim = (float)temp / 2.55;
|
||||
sysCfg.led_dimmer[0] = (uint8_t)mDim;
|
||||
float newDim = 100 / mDim;
|
||||
|
@ -274,8 +272,12 @@ boolean sl_command(char *type, uint16_t index, char *dataBufUc, uint16_t data_le
|
|||
}
|
||||
if (coldim) {
|
||||
// do_cmnd_power(index, (sysCfg.led_dimmer[0]>0));
|
||||
if (sysCfg.led_dimmer[0] && !(power&1)) do_cmnd_power(1, 1);
|
||||
else if (!sysCfg.led_dimmer[0] && (power&1)) do_cmnd_power(1, 0);
|
||||
if (sysCfg.led_dimmer[0] && !(power&1)) {
|
||||
do_cmnd_power(1, 1);
|
||||
}
|
||||
else if (!sysCfg.led_dimmer[0] && (power&1)) {
|
||||
do_cmnd_power(1, 0);
|
||||
}
|
||||
#ifdef USE_DOMOTICZ
|
||||
mqtt_publishDomoticzPowerState(1);
|
||||
#endif // USE_DOMOTICZ
|
||||
|
|
|
@ -70,14 +70,19 @@ void sc_init()
|
|||
|
||||
void sc_rcvstat(char *rcvstat)
|
||||
{
|
||||
char *p, *str;
|
||||
char *p;
|
||||
char *str;
|
||||
uint16_t value[5] = { 0 };
|
||||
|
||||
if (!strncmp(rcvstat, "AT+UPDATE=", 10)) {
|
||||
int8_t i = -1;
|
||||
for (str = strtok_r(rcvstat, ":", &p); str && i < 5; str = strtok_r(NULL, ":", &p)) value[i++] = atoi(str);
|
||||
for (str = strtok_r(rcvstat, ":", &p); str && i < 5; str = strtok_r(NULL, ":", &p)) {
|
||||
value[i++] = atoi(str);
|
||||
}
|
||||
if (value[0] > 0) {
|
||||
for (byte i = 0; i < 5; i++) sc_value[i] = value[i];
|
||||
for (byte i = 0; i < 5; i++) {
|
||||
sc_value[i] = value[i];
|
||||
}
|
||||
sc_value[2] = (11 - sc_value[2]) * 10; // Invert light level
|
||||
sc_value[3] *= 10;
|
||||
sc_value[4] = (11 - sc_value[4]) * 10; // Invert dust level
|
||||
|
@ -103,10 +108,13 @@ float sc_convertCtoF(float c)
|
|||
void sc_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
if (sc_value[0] > 0) {
|
||||
char stemp1[10], stemp2[10];
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
|
||||
float t = sc_value[1];
|
||||
if (TEMP_CONVERSION) t = sc_convertCtoF(t);
|
||||
if (TEMP_CONVERSION) {
|
||||
t = sc_convertCtoF(t);
|
||||
}
|
||||
dtostrf(t, 1, TEMP_RESOLUTION &3, stemp1);
|
||||
float h = sc_value[0];
|
||||
dtostrf(h, 1, HUMIDITY_RESOLUTION &3, stemp2);
|
||||
|
@ -126,10 +134,14 @@ String sc_webPresent()
|
|||
String page = "";
|
||||
|
||||
if (sc_value[0] > 0) {
|
||||
char stemp[10], sensor[80], scstype[] = "SC";
|
||||
char stemp[10];
|
||||
char sensor[80];
|
||||
char scstype[] = "SC";
|
||||
|
||||
float t = sc_value[1];
|
||||
if (TEMP_CONVERSION) t = sc_convertCtoF(t);
|
||||
if (TEMP_CONVERSION) {
|
||||
t = sc_convertCtoF(t);
|
||||
}
|
||||
dtostrf(t, 1, TEMP_RESOLUTION &3, stemp);
|
||||
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, scstype, stemp, (TEMP_CONVERSION) ? 'F' : 'C');
|
||||
page += sensor;
|
||||
|
|
|
@ -53,6 +53,7 @@ const char WEMO_MSEARCH[] PROGMEM =
|
|||
String wemo_serial()
|
||||
{
|
||||
char serial[16];
|
||||
|
||||
snprintf_P(serial, sizeof(serial), PSTR("201612K%08X"), ESP.getChipId());
|
||||
return String(serial);
|
||||
}
|
||||
|
@ -60,13 +61,15 @@ String wemo_serial()
|
|||
String wemo_UUID()
|
||||
{
|
||||
char uuid[27];
|
||||
|
||||
snprintf_P(uuid, sizeof(uuid), PSTR("Socket-1_0-%s"), wemo_serial().c_str());
|
||||
return String(uuid);
|
||||
}
|
||||
|
||||
void wemo_respondToMSearch()
|
||||
{
|
||||
char message[TOPSZ], log[LOGSZ];
|
||||
char message[TOPSZ];
|
||||
char log[LOGSZ];
|
||||
|
||||
if (portUDP.beginPacket(portUDP.remoteIP(), portUDP.remotePort())) {
|
||||
String response = FPSTR(WEMO_MSEARCH);
|
||||
|
@ -113,6 +116,7 @@ const char HUE_ST3[] PROGMEM =
|
|||
String hue_bridgeid()
|
||||
{
|
||||
char bridgeid[16];
|
||||
|
||||
snprintf_P(bridgeid, sizeof(bridgeid), PSTR("5CCF7FFFFE%03X"), ESP.getChipId());
|
||||
return String(bridgeid);
|
||||
}
|
||||
|
@ -120,13 +124,15 @@ String hue_bridgeid()
|
|||
String hue_UUID()
|
||||
{
|
||||
char serial[36];
|
||||
|
||||
snprintf_P(serial, sizeof(serial), PSTR("f6543a06-da50-11ba-8d8f-5ccf7f%03x"), ESP.getChipId());
|
||||
return String(serial);
|
||||
}
|
||||
|
||||
void hue_respondToMSearch()
|
||||
{
|
||||
char message[TOPSZ], log[LOGSZ];
|
||||
char message[TOPSZ];
|
||||
char log[LOGSZ];
|
||||
|
||||
if (portUDP.beginPacket(portUDP.remoteIP(), portUDP.remotePort())) {
|
||||
String response = FPSTR(HUE_RESPONSE);
|
||||
|
@ -202,14 +208,16 @@ void pollUDP()
|
|||
if (udpConnected) {
|
||||
if (portUDP.parsePacket()) {
|
||||
int len = portUDP.read(packetBuffer, UDP_BUFFER_SIZE -1);
|
||||
if (len > 0) packetBuffer[len] = 0;
|
||||
if (len > 0) {
|
||||
packetBuffer[len] = 0;
|
||||
}
|
||||
String request = packetBuffer;
|
||||
// addLog_P(LOG_LEVEL_DEBUG_MORE, packetBuffer);
|
||||
if (request.indexOf("M-SEARCH") >= 0) {
|
||||
if ((sysCfg.emulation == EMUL_WEMO) &&(request.indexOf("urn:Belkin:device:**") > 0)) {
|
||||
if ((EMUL_WEMO == sysCfg.emulation) &&(request.indexOf("urn:Belkin:device:**") > 0)) {
|
||||
wemo_respondToMSearch();
|
||||
}
|
||||
else if ((sysCfg.emulation == EMUL_HUE) && ((request.indexOf("ST: urn:schemas-upnp-org:device:basic:1") > 0) || (request.indexOf("ST: upnp:rootdevice") > 0) || (request.indexOf("ST: ssdp:all") > 0))) {
|
||||
else if ((EMUL_HUE == sysCfg.emulation) && ((request.indexOf("ST: urn:schemas-upnp-org:device:basic:1") > 0) || (request.indexOf("ST: upnp:rootdevice") > 0) || (request.indexOf("ST: ssdp:all") > 0))) {
|
||||
hue_respondToMSearch();
|
||||
}
|
||||
}
|
||||
|
@ -340,8 +348,12 @@ void handleUPnPevent()
|
|||
addLog_P(LOG_LEVEL_DEBUG, PSTR("HTTP: Handle WeMo basic event"));
|
||||
|
||||
String request = webServer->arg(0);
|
||||
if (request.indexOf("State>1</Binary") > 0) do_cmnd_power(1, 1);
|
||||
if (request.indexOf("State>0</Binary") > 0) do_cmnd_power(1, 0);
|
||||
if (request.indexOf("State>1</Binary") > 0) {
|
||||
do_cmnd_power(1, 1);
|
||||
}
|
||||
if (request.indexOf("State>0</Binary") > 0) {
|
||||
do_cmnd_power(1, 0);
|
||||
}
|
||||
webServer->send(200, "text/plain", "");
|
||||
}
|
||||
|
||||
|
@ -408,12 +420,13 @@ void hue_global_cfg(String *path)
|
|||
|
||||
path->remove(0,1); // cut leading / to get <id>
|
||||
response = "{\"lights\":{\"";
|
||||
for (uint8_t i = 1; i <= Maxdevice; i++)
|
||||
{
|
||||
for (uint8_t i = 1; i <= Maxdevice; i++) {
|
||||
response += i;
|
||||
response += "\":";
|
||||
response += FPSTR(HUE_LIGHT_STATUS_JSON);
|
||||
if (i < Maxdevice) response += ",\"";
|
||||
if (i < Maxdevice) {
|
||||
response += ",\"";
|
||||
}
|
||||
response.replace("{state}", (power & (0x01 << (i-1))) ? "true" : "false");
|
||||
response.replace("{j1}", sysCfg.friendlyname[i-1]);
|
||||
response.replace("{j2}", hue_deviceId(i));
|
||||
|
@ -421,8 +434,7 @@ void hue_global_cfg(String *path)
|
|||
#ifdef USE_WS2812
|
||||
ws2812_replaceHSB(&response);
|
||||
#endif // USE_WS2812
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
response.replace("{h}", "0");
|
||||
response.replace("{s}", "0");
|
||||
response.replace("{b}", "0");
|
||||
|
@ -477,7 +489,9 @@ void hue_lights(String *path)
|
|||
response += i;
|
||||
response += "\":";
|
||||
response += FPSTR(HUE_LIGHT_STATUS_JSON);
|
||||
if (i < Maxdevice) response += ",\"";
|
||||
if (i < Maxdevice) {
|
||||
response += ",\"";
|
||||
}
|
||||
response.replace("{state}", (power & (0x01 << (i-1))) ? "true" : "false");
|
||||
response.replace("{j1}", sysCfg.friendlyname[i-1]);
|
||||
response.replace("{j2}", hue_deviceId(i));
|
||||
|
@ -498,13 +512,15 @@ void hue_lights(String *path)
|
|||
path->remove(0,8); // Remove /lights/
|
||||
path->remove(path->indexOf("/state")); // Remove /state
|
||||
device = atoi(path->c_str());
|
||||
if ((device < 1) || (device > Maxdevice)) device = 1;
|
||||
if ((device < 1) || (device > Maxdevice)) {
|
||||
device = 1;
|
||||
}
|
||||
response = "[";
|
||||
response += FPSTR(HUE_LIGHT_RESPONSE_JSON);
|
||||
response.replace("{api}", "/lights");
|
||||
response.replace("{id}", String(device));
|
||||
response.replace("{cmd}", "state/on");
|
||||
if (webServer->args() == 1) {
|
||||
if (1 == webServer->args()) {
|
||||
StaticJsonBuffer<400> jsonBuffer;
|
||||
JsonObject &hue_json = jsonBuffer.parseObject(webServer->arg(0));
|
||||
if (hue_json.containsKey("on")) {
|
||||
|
@ -572,7 +588,9 @@ void hue_lights(String *path)
|
|||
else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID
|
||||
path->remove(0,8); // Remove /lights/
|
||||
device = atoi(path->c_str());
|
||||
if ((device < 1) || (device > Maxdevice)) device = 1;
|
||||
if ((device < 1) || (device > Maxdevice)) {
|
||||
device = 1;
|
||||
}
|
||||
response = FPSTR(HUE_LIGHT_STATUS_JSON);
|
||||
response.replace("{state}", (power & (0x01 << (device -1))) ? "true" : "false");
|
||||
response.replace("{j1}", sysCfg.friendlyname[device -1]);
|
||||
|
|
|
@ -108,7 +108,9 @@ uint8_t ledTable[] = {
|
|||
219,221,224,226,228,231,233,235,238,240,243,245,248,250,253,255 };
|
||||
*/
|
||||
uint8_t lany = 0;
|
||||
RgbColor dcolor, tcolor, lcolor;
|
||||
RgbColor dcolor;
|
||||
RgbColor tcolor;
|
||||
RgbColor lcolor;
|
||||
|
||||
uint8_t wakeupDimmer = 0;
|
||||
uint16_t wakeupCntr = 0;
|
||||
|
@ -128,7 +130,8 @@ void ws2812_setDim(uint8_t myDimmer)
|
|||
void ws2812_setColor(uint16_t led, char* colstr)
|
||||
{
|
||||
HtmlColor hcolor;
|
||||
char log[LOGSZ], lcolstr[8];
|
||||
char log[LOGSZ];
|
||||
char lcolstr[8];
|
||||
|
||||
snprintf_P(lcolstr, sizeof(lcolstr), PSTR("#%s"), colstr);
|
||||
uint8_t result = hcolor.Parse<HtmlColorNames>((char *)lcolstr, 7);
|
||||
|
@ -143,8 +146,12 @@ void ws2812_setColor(uint16_t led, char* colstr)
|
|||
// addLog(LOG_LEVEL_DEBUG, log);
|
||||
|
||||
uint16_t temp = dcolor.R;
|
||||
if (temp < dcolor.G) temp = dcolor.G;
|
||||
if (temp < dcolor.B) temp = dcolor.B;
|
||||
if (temp < dcolor.G) {
|
||||
temp = dcolor.G;
|
||||
}
|
||||
if (temp < dcolor.B) {
|
||||
temp = dcolor.B;
|
||||
}
|
||||
float mDim = (float)temp / 2.55;
|
||||
sysCfg.ws_dimmer = (uint8_t)mDim;
|
||||
|
||||
|
@ -238,7 +245,9 @@ void ws2812_resetStripTimer()
|
|||
int mod(int a, int b)
|
||||
{
|
||||
int ret = a % b;
|
||||
if (ret < 0) ret += b;
|
||||
if (ret < 0) {
|
||||
ret += b;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -308,7 +317,9 @@ void ws2812_gradient()
|
|||
RgbColor c;
|
||||
|
||||
ColorScheme scheme = schemes[sysCfg.ws_scheme -3];
|
||||
if (scheme.count < 2) return;
|
||||
if (scheme.count < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t repeat = repeatValues[sysCfg.ws_width]; // number of scheme.count per ledcount
|
||||
uint8_t range = (uint8_t)ceil((float)sysCfg.ws_pixels / (float)repeat);
|
||||
|
@ -319,7 +330,9 @@ void ws2812_gradient()
|
|||
ws2812_gradientColor(&oldColor, range, gradRange, offset);
|
||||
currentColor = oldColor;
|
||||
for (uint16_t i = 0; i < sysCfg.ws_pixels; i++) {
|
||||
if (repeatValues[sysCfg.ws_width] > 1) ws2812_gradientColor(¤tColor, range, gradRange, i +offset);
|
||||
if (repeatValues[sysCfg.ws_width] > 1) {
|
||||
ws2812_gradientColor(¤tColor, range, gradRange, i +offset);
|
||||
}
|
||||
if (sysCfg.ws_speed > 0) {
|
||||
// Blend old and current color based on time for smooth movement.
|
||||
c.R = map(stripTimerCntr % speedValues[sysCfg.ws_speed], 0, speedValues[sysCfg.ws_speed], oldColor.red, currentColor.red);
|
||||
|
@ -351,7 +364,9 @@ void ws2812_bars()
|
|||
ColorScheme scheme = schemes[sysCfg.ws_scheme -3];
|
||||
|
||||
uint8_t maxSize = sysCfg.ws_pixels / scheme.count;
|
||||
if (widthValues[sysCfg.ws_width] > maxSize) maxSize = 0;
|
||||
if (widthValues[sysCfg.ws_width] > maxSize) {
|
||||
maxSize = 0;
|
||||
}
|
||||
|
||||
uint8_t offset = speedValues[sysCfg.ws_speed] > 0 ? stripTimerCntr / speedValues[sysCfg.ws_speed] : 0;
|
||||
|
||||
|
@ -368,8 +383,9 @@ void ws2812_bars()
|
|||
}
|
||||
uint8_t colorIndex = offset % scheme.count;
|
||||
for (i = 0; i < sysCfg.ws_pixels; i++) {
|
||||
if (maxSize)
|
||||
if (maxSize) {
|
||||
colorIndex = ((i + offset) % (scheme.count * widthValues[sysCfg.ws_width])) / widthValues[sysCfg.ws_width];
|
||||
}
|
||||
c.R = mcolor[colorIndex].red;
|
||||
c.G = mcolor[colorIndex].green;
|
||||
c.B = mcolor[colorIndex].blue;
|
||||
|
@ -384,7 +400,7 @@ void ws2812_animate()
|
|||
uint8_t fadeValue;
|
||||
|
||||
stripTimerCntr++;
|
||||
if (power == 0) { // Power Off
|
||||
if (0 == power) { // Power Off
|
||||
sleep = sysCfg.sleep;
|
||||
stripTimerCntr = 0;
|
||||
tcolor = 0;
|
||||
|
@ -394,22 +410,34 @@ void ws2812_animate()
|
|||
switch (sysCfg.ws_scheme) {
|
||||
case 0: // Power On
|
||||
ws2812_setDim(sysCfg.ws_dimmer);
|
||||
if (sysCfg.ws_fade == 0) {
|
||||
if (0 == sysCfg.ws_fade) {
|
||||
tcolor = dcolor;
|
||||
} else {
|
||||
if (tcolor != dcolor) {
|
||||
if (tcolor.R < dcolor.R) tcolor.R += ((dcolor.R - tcolor.R) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.G < dcolor.G) tcolor.G += ((dcolor.G - tcolor.G) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.B < dcolor.B) tcolor.B += ((dcolor.B - tcolor.B) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.R > dcolor.R) tcolor.R -= ((tcolor.R - dcolor.R) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.G > dcolor.G) tcolor.G -= ((tcolor.G - dcolor.G) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.B > dcolor.B) tcolor.B -= ((tcolor.B - dcolor.B) >> sysCfg.ws_speed) +1;
|
||||
if (tcolor.R < dcolor.R) {
|
||||
tcolor.R += ((dcolor.R - tcolor.R) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
if (tcolor.G < dcolor.G) {
|
||||
tcolor.G += ((dcolor.G - tcolor.G) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
if (tcolor.B < dcolor.B) {
|
||||
tcolor.B += ((dcolor.B - tcolor.B) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
if (tcolor.R > dcolor.R) {
|
||||
tcolor.R -= ((tcolor.R - dcolor.R) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
if (tcolor.G > dcolor.G) {
|
||||
tcolor.G -= ((tcolor.G - dcolor.G) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
if (tcolor.B > dcolor.B) {
|
||||
tcolor.B -= ((tcolor.B - dcolor.B) >> sysCfg.ws_speed) +1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1: // Wake up light
|
||||
wakeupCntr++;
|
||||
if (wakeupDimmer == 0) {
|
||||
if (0 == wakeupDimmer) {
|
||||
tcolor = 0;
|
||||
wakeupDimmer++;
|
||||
}
|
||||
|
@ -426,11 +454,17 @@ void ws2812_animate()
|
|||
}
|
||||
break;
|
||||
case 2: // Clock
|
||||
if ((state == (STATES/10)*2) || (lany != 2)) ws2812_clock();
|
||||
if (((STATES/10)*2 == state) || (lany != 2)) {
|
||||
ws2812_clock();
|
||||
}
|
||||
lany = 2;
|
||||
break;
|
||||
default:
|
||||
if (sysCfg.ws_fade == 1) ws2812_gradient(); else ws2812_bars();
|
||||
if (1 == sysCfg.ws_fade) {
|
||||
ws2812_gradient();
|
||||
} else {
|
||||
ws2812_bars();
|
||||
}
|
||||
lany = 1;
|
||||
break;
|
||||
}
|
||||
|
@ -445,9 +479,13 @@ void ws2812_animate()
|
|||
// addLog(LOG_LEVEL_DEBUG, log);
|
||||
|
||||
if (sysCfg.ws_ledtable) {
|
||||
for (uint16_t i = 0; i < sysCfg.ws_pixels; i++) strip->SetPixelColor(i, RgbColor(ledTable[lcolor.R],ledTable[lcolor.G],ledTable[lcolor.B]));
|
||||
for (uint16_t i = 0; i < sysCfg.ws_pixels; i++) {
|
||||
strip->SetPixelColor(i, RgbColor(ledTable[lcolor.R],ledTable[lcolor.G],ledTable[lcolor.B]));
|
||||
}
|
||||
} else {
|
||||
for (uint16_t i = 0; i < sysCfg.ws_pixels; i++) strip->SetPixelColor(i, lcolor);
|
||||
for (uint16_t i = 0; i < sysCfg.ws_pixels; i++) {
|
||||
strip->SetPixelColor(i, lcolor);
|
||||
}
|
||||
}
|
||||
strip->Show();
|
||||
}
|
||||
|
@ -502,14 +540,14 @@ boolean ws2812_command(char *type, uint16_t index, char *dataBuf, uint16_t data_
|
|||
snprintf_P(svalue, ssvalue, PSTR("{\"Pixels\":%d}"), sysCfg.ws_pixels);
|
||||
}
|
||||
else if (!strcmp(type,"LED") && (index > 0) && (index <= sysCfg.ws_pixels)) {
|
||||
if (data_len == 6) {
|
||||
if (6 == data_len) {
|
||||
// ws2812_setColor(index, dataBufUc);
|
||||
ws2812_setColor(index, dataBuf);
|
||||
}
|
||||
ws2812_getColor(index, svalue, ssvalue);
|
||||
}
|
||||
else if (!strcmp(type,"COLOR")) {
|
||||
if (data_len == 6) {
|
||||
if (6 == data_len) {
|
||||
// ws2812_setColor(0, dataBufUc);
|
||||
ws2812_setColor(0, dataBuf);
|
||||
power = 1;
|
||||
|
@ -570,14 +608,18 @@ boolean ws2812_command(char *type, uint16_t index, char *dataBuf, uint16_t data_
|
|||
else if (!strcmp(type,"WAKEUP")) {
|
||||
if ((data_len > 0) && (payload > 0) && (payload < 3601)) {
|
||||
sysCfg.ws_wakeup = payload;
|
||||
if (sysCfg.ws_scheme == 1) sysCfg.ws_scheme = 0;
|
||||
if (1 == sysCfg.ws_scheme) {
|
||||
sysCfg.ws_scheme = 0;
|
||||
}
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"WakeUp\":%d}"), sysCfg.ws_wakeup);
|
||||
}
|
||||
else if (!strcmp(type,"SCHEME")) {
|
||||
if ((data_len > 0) && (payload >= 0) && (payload <= 9)) {
|
||||
sysCfg.ws_scheme = payload;
|
||||
if (sysCfg.ws_scheme == 1) ws2812_resetWakupState();
|
||||
if (1 == sysCfg.ws_scheme) {
|
||||
ws2812_resetWakupState();
|
||||
}
|
||||
power = 1;
|
||||
ws2812_resetStripTimer();
|
||||
}
|
||||
|
|
|
@ -34,7 +34,8 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
|
||||
uint8_t bh1750addr, bh1750type = 0;
|
||||
uint8_t bh1750addr;
|
||||
uint8_t bh1750type = 0;
|
||||
char bh1750stype[7];
|
||||
|
||||
uint16_t bh1750_readLux(void)
|
||||
|
@ -48,7 +49,9 @@ uint16_t bh1750_readLux(void)
|
|||
|
||||
boolean bh1750_detect()
|
||||
{
|
||||
if (bh1750type) return true;
|
||||
if (bh1750type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
char log[LOGSZ];
|
||||
uint8_t status;
|
||||
|
@ -84,7 +87,9 @@ boolean bh1750_detect()
|
|||
|
||||
void bh1750_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
if (!bh1750type) return;
|
||||
if (!bh1750type) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t l = bh1750_readLux();
|
||||
snprintf_P(svalue, ssvalue, PSTR("%s, \"%s\":{\"Illuminance\":%d}"), svalue, bh1750stype, l);
|
||||
|
|
|
@ -39,7 +39,8 @@
|
|||
|
||||
#define BMP_REGISTER_CHIPID 0xD0
|
||||
|
||||
uint8_t bmpaddr, bmptype = 0;
|
||||
uint8_t bmpaddr;
|
||||
uint8_t bmptype = 0;
|
||||
char bmpstype[7];
|
||||
|
||||
/*********************************************************************************************\
|
||||
|
@ -67,8 +68,16 @@ char bmpstype[7];
|
|||
|
||||
#define BMP180_OSS 3
|
||||
|
||||
int16_t cal_ac1,cal_ac2,cal_ac3,cal_b1,cal_b2,cal_mc,cal_md;
|
||||
uint16_t cal_ac4,cal_ac5,cal_ac6;
|
||||
int16_t cal_ac1;
|
||||
int16_t cal_ac2;
|
||||
int16_t cal_ac3;
|
||||
int16_t cal_b1;
|
||||
int16_t cal_b2;
|
||||
int16_t cal_mc;
|
||||
int16_t cal_md;
|
||||
uint16_t cal_ac4;
|
||||
uint16_t cal_ac5;
|
||||
uint16_t cal_ac6;
|
||||
int32_t bmp180_b5 = 0;
|
||||
|
||||
boolean bmp180_calibration()
|
||||
|
@ -85,9 +94,9 @@ boolean bmp180_calibration()
|
|||
cal_md = i2c_read16(bmpaddr, BMP180_MD);
|
||||
|
||||
// Check for Errors in calibration data. Value never is 0x0000 or 0xFFFF
|
||||
if(!cal_ac1 | !cal_ac2 | !cal_ac3 | !cal_ac4 | !cal_ac5 |
|
||||
!cal_ac6 | !cal_b1 | !cal_b2 | !cal_mc | !cal_md)
|
||||
if (!cal_ac1 | !cal_ac2 | !cal_ac3 | !cal_ac4 | !cal_ac5 | !cal_ac6 | !cal_b1 | !cal_b2 | !cal_mc | !cal_md) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((cal_ac1 == 0xFFFF)|
|
||||
(cal_ac2 == 0xFFFF)|
|
||||
|
@ -98,8 +107,9 @@ boolean bmp180_calibration()
|
|||
(cal_b1 == 0xFFFF)|
|
||||
(cal_b2 == 0xFFFF)|
|
||||
(cal_mc == 0xFFFF)|
|
||||
(cal_md==0xFFFF))
|
||||
(cal_md == 0xFFFF)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -119,7 +129,9 @@ double bmp180_readTemperature()
|
|||
double bmp180_readPressure()
|
||||
{
|
||||
int32_t p;
|
||||
uint8_t msb,lsb,xlsb;
|
||||
uint8_t msb;
|
||||
uint8_t lsb;
|
||||
uint8_t xlsb;
|
||||
|
||||
i2c_write8(bmpaddr, BMP180_REG_CONTROL, BMP180_PRESSURE3); // Highest resolution
|
||||
delay(2 + (4 << BMP180_OSS)); // 26ms conversion time at ultra high resolution
|
||||
|
@ -268,7 +280,8 @@ boolean bme280_calibrate()
|
|||
|
||||
double bmp280_readTemperature(void)
|
||||
{
|
||||
int32_t var1, var2;
|
||||
int32_t var1;
|
||||
int32_t var2;
|
||||
|
||||
int32_t adc_T = i2c_read24(bmpaddr, BME280_REGISTER_TEMPDATA);
|
||||
adc_T >>= 4;
|
||||
|
@ -283,7 +296,9 @@ double bmp280_readTemperature(void)
|
|||
|
||||
double bmp280_readPressure(void)
|
||||
{
|
||||
int64_t var1, var2, p;
|
||||
int64_t var1;
|
||||
int64_t var2;
|
||||
int64_t p;
|
||||
|
||||
// Must be done first to get the t_fine variable set up
|
||||
// bmp280_readTemperature();
|
||||
|
@ -297,7 +312,7 @@ double bmp280_readPressure(void)
|
|||
var2 = var2 + (((int64_t)_bme280_calib.dig_P4) << 35);
|
||||
var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3) >> 8) + ((var1 * (int64_t)_bme280_calib.dig_P2) << 12);
|
||||
var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)_bme280_calib.dig_P1) >> 33;
|
||||
if (var1 == 0) {
|
||||
if (0 == var1) {
|
||||
return 0; // avoid exception caused by division by zero
|
||||
}
|
||||
p = 1048576 - adc_P;
|
||||
|
@ -354,7 +369,9 @@ double bmp_readTemperature(bool S)
|
|||
t = bmp280_readTemperature();
|
||||
}
|
||||
if (!isnan(t)) {
|
||||
if(S) t = bmp_convertCtoF(t);
|
||||
if (S) {
|
||||
t = bmp_convertCtoF(t);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
return 0;
|
||||
|
@ -386,7 +403,9 @@ double bmp_readHumidity(void)
|
|||
|
||||
boolean bmp_detect()
|
||||
{
|
||||
if (bmptype) return true;
|
||||
if (bmptype) {
|
||||
return true;
|
||||
}
|
||||
|
||||
char log[LOGSZ];
|
||||
boolean success = false;
|
||||
|
@ -426,9 +445,13 @@ boolean bmp_detect()
|
|||
|
||||
void bmp_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
if (!bmptype) return;
|
||||
if (!bmptype) {
|
||||
return;
|
||||
}
|
||||
|
||||
char stemp1[10], stemp2[10], stemp3[10];
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
char stemp3[10];
|
||||
|
||||
double t = bmp_readTemperature(TEMP_CONVERSION);
|
||||
double p = bmp_readPressure();
|
||||
|
@ -454,7 +477,8 @@ String bmp_webPresent()
|
|||
{
|
||||
String page = "";
|
||||
if (bmptype) {
|
||||
char stemp[10], sensor[80];
|
||||
char stemp[10];
|
||||
char sensor[80];
|
||||
|
||||
double t_bmp = bmp_readTemperature(TEMP_CONVERSION);
|
||||
double p_bmp = bmp_readPressure();
|
||||
|
|
|
@ -36,9 +36,11 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
uint8_t data[5];
|
||||
char dhtstype[7];
|
||||
uint32_t _lastreadtime, _maxcycles;
|
||||
uint32_t _lastreadtime;
|
||||
uint32_t _maxcycles;
|
||||
bool _lastresult;
|
||||
float mt, mh = 0;
|
||||
float mt;
|
||||
float mh = 0;
|
||||
|
||||
void dht_readPrep()
|
||||
{
|
||||
|
@ -49,8 +51,11 @@ uint32_t dht_expectPulse(bool level)
|
|||
{
|
||||
uint32_t count = 0;
|
||||
|
||||
while (digitalRead(pin[GPIO_DHT11]) == level)
|
||||
if (count++ >= _maxcycles) return 0;
|
||||
while (digitalRead(pin[GPIO_DHT11]) == level) {
|
||||
if (count++ >= _maxcycles) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
@ -79,12 +84,12 @@ boolean dht_read()
|
|||
delayMicroseconds(40);
|
||||
pinMode(pin[GPIO_DHT11], INPUT_PULLUP);
|
||||
delayMicroseconds(10);
|
||||
if (dht_expectPulse(LOW) == 0) {
|
||||
if (0 == dht_expectPulse(LOW)) {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for start signal low pulse"));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
if (dht_expectPulse(HIGH) == 0) {
|
||||
if (0 == dht_expectPulse(HIGH)) {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for start signal high pulse"));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
|
@ -98,13 +103,15 @@ boolean dht_read()
|
|||
for (int i=0; i<40; ++i) {
|
||||
uint32_t lowCycles = cycles[2*i];
|
||||
uint32_t highCycles = cycles[2*i+1];
|
||||
if ((lowCycles == 0) || (highCycles == 0)) {
|
||||
if ((0 == lowCycles) || (0 == highCycles)) {
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("DHT: Timeout waiting for pulse"));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
data[i/8] <<= 1;
|
||||
if (highCycles > lowCycles) data[i/8] |= 1;
|
||||
if (highCycles > lowCycles) {
|
||||
data[i/8] |= 1;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf_P(log, sizeof(log), PSTR("DHT: Received %02X, %02X, %02X, %02X, %02X =? %02X"),
|
||||
|
@ -141,7 +148,9 @@ boolean dht_readTempHum(bool S, float &t, float &h)
|
|||
case GPIO_DHT11:
|
||||
h = data[0];
|
||||
t = data[2];
|
||||
if(S) t = dht_convertCtoF(t);
|
||||
if (S) {
|
||||
t = dht_convertCtoF(t);
|
||||
}
|
||||
break;
|
||||
case GPIO_DHT22:
|
||||
case GPIO_DHT21:
|
||||
|
@ -153,12 +162,20 @@ boolean dht_readTempHum(bool S, float &t, float &h)
|
|||
t *= 256;
|
||||
t += data[3];
|
||||
t *= 0.1;
|
||||
if (data[2] & 0x80) t *= -1;
|
||||
if(S) t = dht_convertCtoF(t);
|
||||
if (data[2] & 0x80) {
|
||||
t *= -1;
|
||||
}
|
||||
if (S) {
|
||||
t = dht_convertCtoF(t);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!isnan(t)) mt = t;
|
||||
if (!isnan(h)) mh = h;
|
||||
if (!isnan(t)) {
|
||||
mt = t;
|
||||
}
|
||||
if (!isnan(h)) {
|
||||
mh = h;
|
||||
}
|
||||
}
|
||||
return (!isnan(t) && !isnan(h));
|
||||
}
|
||||
|
@ -192,8 +209,10 @@ void dht_init()
|
|||
|
||||
void dht_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
char stemp1[10], stemp2[10];
|
||||
float t, h;
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
float t;
|
||||
float h;
|
||||
|
||||
if (dht_readTempHum(TEMP_CONVERSION, t, h)) { // Read temperature
|
||||
dtostrf(t, 1, TEMP_RESOLUTION &3, stemp1);
|
||||
|
@ -212,10 +231,13 @@ void dht_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
|||
String dht_webPresent()
|
||||
{
|
||||
String page = "";
|
||||
float t, h;
|
||||
float t;
|
||||
float h;
|
||||
|
||||
if (dht_readTempHum(TEMP_CONVERSION, t, h)) { // Read temperature as Celsius (the default)
|
||||
char stemp[10], sensor[80];
|
||||
char stemp[10];
|
||||
char sensor[80];
|
||||
|
||||
dtostrf(t, 1, TEMP_RESOLUTION &3, stemp);
|
||||
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, dhtstype, stemp, (TEMP_CONVERSION) ? 'F' : 'C');
|
||||
page += sensor;
|
||||
|
|
|
@ -39,7 +39,9 @@ uint8_t dsb_reset()
|
|||
|
||||
pinMode(pin[GPIO_DSB], INPUT);
|
||||
do { // wait until the wire is high... just in case
|
||||
if (--retries == 0) return 0;
|
||||
if (--retries == 0) {
|
||||
return 0;
|
||||
}
|
||||
delayMicroseconds(2);
|
||||
} while (!digitalRead(pin[GPIO_DSB]));
|
||||
pinMode(pin[GPIO_DSB], OUTPUT);
|
||||
|
@ -71,8 +73,11 @@ uint8_t dsb_read(void)
|
|||
uint8_t bitMask;
|
||||
uint8_t r = 0;
|
||||
|
||||
for (bitMask = 0x01; bitMask; bitMask <<= 1)
|
||||
if (dsb_read_bit()) r |= bitMask;
|
||||
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
|
||||
if (dsb_read_bit()) {
|
||||
r |= bitMask;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
@ -97,9 +102,10 @@ void dsb_write(uint8_t ByteToWrite)
|
|||
{
|
||||
uint8_t bitMask;
|
||||
|
||||
for (bitMask = 0x01; bitMask; bitMask <<= 1)
|
||||
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
|
||||
dsb_write_bit((bitMask & ByteToWrite) ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
uint8 dsb_crc(uint8 inp, uint8 crc)
|
||||
{
|
||||
|
@ -173,7 +179,9 @@ boolean dsb_readTemp(bool S, float &t)
|
|||
sign = -1;
|
||||
}
|
||||
t = (float)sign * DSTemp * 0.0625;
|
||||
if(S) t = dsb_convertCtoF(t);
|
||||
if(S) {
|
||||
t = dsb_convertCtoF(t);
|
||||
}
|
||||
}
|
||||
if (!isnan(t)) dsb_mt = t;
|
||||
return !isnan(t);
|
||||
|
@ -206,7 +214,9 @@ String dsb_webPresent()
|
|||
float st;
|
||||
|
||||
if (dsb_readTemp(TEMP_CONVERSION, st)) { // Check if read failed
|
||||
char stemp[10], sensor[80];
|
||||
char stemp[10];
|
||||
char sensor[80];
|
||||
|
||||
dtostrf(st, 1, TEMP_RESOLUTION &3, stemp);
|
||||
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, "DS18B20", stemp, (TEMP_CONVERSION) ? 'F' : 'C');
|
||||
page += sensor;
|
||||
|
|
|
@ -59,18 +59,20 @@ void ds18x20_search()
|
|||
uint8_t i;
|
||||
|
||||
ds->reset_search();
|
||||
for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors)
|
||||
{
|
||||
for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors) {
|
||||
if (!ds->search(ds18x20_addr[num_sensors])) {
|
||||
ds->reset_search();
|
||||
break;
|
||||
}
|
||||
// If CRC Ok and Type DS18S20, DS18B20 or MAX31850
|
||||
if ((OneWire::crc8(ds18x20_addr[num_sensors], 7) == ds18x20_addr[num_sensors][7]) &&
|
||||
((ds18x20_addr[num_sensors][0]==DS18S20_CHIPID) || (ds18x20_addr[num_sensors][0]==DS18B20_CHIPID) || (ds18x20_addr[num_sensors][0]==MAX31850_CHIPID)))
|
||||
((ds18x20_addr[num_sensors][0]==DS18S20_CHIPID) || (ds18x20_addr[num_sensors][0]==DS18B20_CHIPID) || (ds18x20_addr[num_sensors][0]==MAX31850_CHIPID))) {
|
||||
num_sensors++;
|
||||
}
|
||||
for (int i = 0; i < num_sensors; i++) ds18x20_idx[i] = i;
|
||||
}
|
||||
for (int i = 0; i < num_sensors; i++) {
|
||||
ds18x20_idx[i] = i;
|
||||
}
|
||||
for (int i = 0; i < num_sensors; i++) {
|
||||
for (int j = i + 1; j < num_sensors; j++) {
|
||||
if (uint32_t(ds18x20_addr[ds18x20_idx[i]]) > uint32_t(ds18x20_addr[ds18x20_idx[j]])) {
|
||||
|
@ -91,7 +93,9 @@ String ds18x20_address(uint8_t sensor)
|
|||
char addrStr[20];
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < 8; i++) sprintf(addrStr+2*i, "%02X", ds18x20_addr[ds18x20_idx[sensor]][i]);
|
||||
for (i = 0; i < 8; i++) {
|
||||
sprintf(addrStr+2*i, "%02X", ds18x20_addr[ds18x20_idx[sensor]][i]);
|
||||
}
|
||||
return String(addrStr);
|
||||
}
|
||||
|
||||
|
@ -122,7 +126,9 @@ boolean ds18x20_read(uint8_t sensor, bool S, float &t)
|
|||
ds->select(ds18x20_addr[ds18x20_idx[sensor]]);
|
||||
ds->write(W1_READ_SCRATCHPAD); // Read Scratchpad
|
||||
|
||||
for (i = 0; i < 9; i++) data[i] = ds->read();
|
||||
for (i = 0; i < 9; i++) {
|
||||
data[i] = ds->read();
|
||||
}
|
||||
if (OneWire::crc8(data, 8) == data[8]) {
|
||||
switch(ds18x20_addr[ds18x20_idx[sensor]][0]) {
|
||||
case DS18S20_CHIPID: // DS18S20
|
||||
|
@ -150,7 +156,9 @@ boolean ds18x20_read(uint8_t sensor, bool S, float &t)
|
|||
temp9 = (data[0] >> 1) * sign;
|
||||
}
|
||||
t = (temp9 - 0.25) + ((16.0 - data[6]) / 16.0);
|
||||
if(S) t = ds18x20_convertCtoF(t);
|
||||
if(S) {
|
||||
t = ds18x20_convertCtoF(t);
|
||||
}
|
||||
break;
|
||||
case DS18B20_CHIPID: // DS18B20
|
||||
case MAX31850_CHIPID: // MAX31850
|
||||
|
@ -160,7 +168,9 @@ boolean ds18x20_read(uint8_t sensor, bool S, float &t)
|
|||
sign = -1;
|
||||
}
|
||||
t = sign * temp12 * 0.0625;
|
||||
if(S) t = ds18x20_convertCtoF(t);
|
||||
if(S) {
|
||||
t = ds18x20_convertCtoF(t);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -189,7 +199,8 @@ void ds18x20_type(uint8_t sensor)
|
|||
|
||||
void ds18x20_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
char stemp1[10], stemp2[10];
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
float t;
|
||||
|
||||
byte dsxflg = 0;
|
||||
|
@ -211,14 +222,18 @@ void ds18x20_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
|||
#endif // USE_DOMOTICZ
|
||||
}
|
||||
}
|
||||
if (dsxflg) snprintf_P(svalue, ssvalue, PSTR("%s}"), svalue);
|
||||
if (dsxflg) {
|
||||
snprintf_P(svalue, ssvalue, PSTR("%s}"), svalue);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
String ds18x20_webPresent()
|
||||
{
|
||||
String page = "";
|
||||
char stemp[10], stemp2[16], sensor[80];
|
||||
char stemp[10];
|
||||
char stemp2[16];
|
||||
char sensor[80];
|
||||
float t;
|
||||
|
||||
for (byte i = 0; i < ds18x20_sensors(); i++) {
|
||||
|
|
|
@ -53,13 +53,26 @@ byte hlw_mkwh_state = 0;
|
|||
uint16_t hlw_mplw_counter = 0;
|
||||
#endif // FEATURE_POWER_LIMIT
|
||||
|
||||
byte hlw_SELflag, hlw_cf_timer, hlw_cf1_timer, hlw_fifth_second, hlw_startup;
|
||||
unsigned long hlw_cf_plen, hlw_cf_last;
|
||||
unsigned long hlw_cf1_plen, hlw_cf1_last, hlw_cf1_ptot, hlw_cf1_pcnt, hlw_cf1u_plen, hlw_cf1i_plen;
|
||||
unsigned long hlw_Ecntr, hlw_EDcntr, hlw_kWhtoday;
|
||||
byte hlw_SELflag;
|
||||
byte hlw_cf_timer;
|
||||
byte hlw_cf1_timer;
|
||||
byte hlw_fifth_second;
|
||||
byte hlw_startup;
|
||||
unsigned long hlw_cf_plen;
|
||||
unsigned long hlw_cf_last;
|
||||
unsigned long hlw_cf1_plen;
|
||||
unsigned long hlw_cf1_last;
|
||||
unsigned long hlw_cf1_ptot;
|
||||
unsigned long hlw_cf1_pcnt;
|
||||
unsigned long hlw_cf1u_plen;
|
||||
unsigned long hlw_cf1i_plen;
|
||||
unsigned long hlw_Ecntr;
|
||||
unsigned long hlw_EDcntr;
|
||||
unsigned long hlw_kWhtoday;
|
||||
uint32_t hlw_lasttime;
|
||||
|
||||
unsigned long hlw_cf1u_pcntmax, hlw_cf1i_pcntmax;
|
||||
unsigned long hlw_cf1u_pcntmax;
|
||||
unsigned long hlw_cf1i_pcntmax;
|
||||
|
||||
Ticker tickerHLW;
|
||||
|
||||
|
@ -72,7 +85,9 @@ void hlw_cf_interrupt() // Service Power
|
|||
{
|
||||
hlw_cf_plen = micros() - hlw_cf_last;
|
||||
hlw_cf_last = micros();
|
||||
if (hlw_cf_plen > 4000000) hlw_cf_plen = 0; // Just powered on
|
||||
if (hlw_cf_plen > 4000000) {
|
||||
hlw_cf_plen = 0; // Just powered on
|
||||
}
|
||||
hlw_cf_timer = 15; // Support down to 4W which takes about 3 seconds
|
||||
hlw_EDcntr++;
|
||||
hlw_Ecntr++;
|
||||
|
@ -85,16 +100,19 @@ void hlw_cf1_interrupt() // Service Voltage and Current
|
|||
if ((hlw_cf1_timer > 2) && (hlw_cf1_timer < 8)) { // Allow for 300 mSec set-up time and measure for up to 1 second
|
||||
hlw_cf1_ptot += hlw_cf1_plen;
|
||||
hlw_cf1_pcnt++;
|
||||
if (hlw_cf1_pcnt == 10) hlw_cf1_timer = 8; // We need up to ten samples within 1 second (low current could take up to 0.3 second)
|
||||
if (10 == hlw_cf1_pcnt) {
|
||||
hlw_cf1_timer = 8; // We need up to ten samples within 1 second (low current could take up to 0.3 second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void hlw_200mS()
|
||||
{
|
||||
unsigned long hlw_len, hlw_temp;
|
||||
unsigned long hlw_len;
|
||||
unsigned long hlw_temp;
|
||||
|
||||
hlw_fifth_second++;
|
||||
if (hlw_fifth_second == 5) {
|
||||
if (5 == hlw_fifth_second) {
|
||||
hlw_fifth_second = 0;
|
||||
|
||||
if (hlw_EDcntr) {
|
||||
|
@ -111,7 +129,7 @@ void hlw_200mS()
|
|||
rtcMem.hlw_kWhtoday = hlw_kWhtoday;
|
||||
hlw_mkwh_state = 3;
|
||||
}
|
||||
if ((rtcTime.Hour == sysCfg.hlw_mkwhs) && (hlw_mkwh_state == 3)) {
|
||||
if ((rtcTime.Hour == sysCfg.hlw_mkwhs) && (3 == hlw_mkwh_state)) {
|
||||
hlw_mkwh_state = 0;
|
||||
}
|
||||
if (hlw_startup && (rtcTime.DayOfYear == sysCfg.hlw_kWhdoy)) {
|
||||
|
@ -124,7 +142,9 @@ void hlw_200mS()
|
|||
|
||||
if (hlw_cf_timer) {
|
||||
hlw_cf_timer--;
|
||||
if (!hlw_cf_timer) hlw_cf_plen = 0; // No load for over three seconds
|
||||
if (!hlw_cf_timer) {
|
||||
hlw_cf_plen = 0; // No load for over three seconds
|
||||
}
|
||||
}
|
||||
|
||||
hlw_cf1_timer++;
|
||||
|
@ -158,8 +178,13 @@ void hlw_savestate()
|
|||
|
||||
boolean hlw_readEnergy(byte option, float &ed, uint16_t &e, uint16_t &w, uint16_t &u, float &i, float &c)
|
||||
{
|
||||
unsigned long hlw_len, hlw_temp, hlw_w, hlw_u, hlw_i;
|
||||
int hlw_period, hlw_interval;
|
||||
unsigned long hlw_len;
|
||||
unsigned long hlw_temp;
|
||||
unsigned long hlw_w;
|
||||
unsigned long hlw_u;
|
||||
unsigned long hlw_i;
|
||||
int hlw_period;
|
||||
int hlw_interval;
|
||||
|
||||
//char log[LOGSZ];
|
||||
//snprintf_P(log, sizeof(log), PSTR("HLW: CF %d, CF1U %d (%d), CF1I %d (%d)"), hlw_cf_plen, hlw_cf1u_plen, hlw_cf1u_pcntmax, hlw_cf1i_plen, hlw_cf1i_pcntmax);
|
||||
|
@ -222,7 +247,7 @@ boolean hlw_readEnergy(byte option, float &ed, uint16_t &e, uint16_t &w, uint16_
|
|||
|
||||
void hlw_init()
|
||||
{
|
||||
if (!sysCfg.hlw_pcal || (sysCfg.hlw_pcal == 4975)) {
|
||||
if (!sysCfg.hlw_pcal || (4975 == sysCfg.hlw_pcal)) {
|
||||
sysCfg.hlw_pcal = HLW_PREF_PULSE;
|
||||
sysCfg.hlw_ucal = HLW_UREF_PULSE;
|
||||
sysCfg.hlw_ical = HLW_IREF_PULSE;
|
||||
|
@ -264,7 +289,9 @@ boolean hlw_margin(byte type, uint16_t margin, uint16_t value, byte &flag, byte
|
|||
{
|
||||
byte change;
|
||||
|
||||
if (!margin) return false;
|
||||
if (!margin) {
|
||||
return false;
|
||||
}
|
||||
change = saveflag;
|
||||
if (type) {
|
||||
flag = (value > margin);
|
||||
|
@ -282,10 +309,18 @@ void hlw_setPowerSteadyCounter(byte value)
|
|||
|
||||
void hlw_margin_chk()
|
||||
{
|
||||
char log[LOGSZ], svalue[200]; // was MESSZ
|
||||
float ped, pi, pc;
|
||||
uint16_t uped, piv, pe, pw, pu;
|
||||
boolean flag, jsonflg;
|
||||
char log[LOGSZ];
|
||||
char svalue[200]; // was MESSZ
|
||||
float ped;
|
||||
float pi;
|
||||
float pc;
|
||||
uint16_t uped;
|
||||
uint16_t piv;
|
||||
uint16_t pe;
|
||||
uint16_t pw;
|
||||
uint16_t pu;
|
||||
boolean flag;
|
||||
boolean jsonflg;
|
||||
|
||||
if (power_steady_cntr) {
|
||||
power_steady_cntr--;
|
||||
|
@ -343,7 +378,9 @@ void hlw_margin_chk()
|
|||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"MaxPowerReached\":\"%d%s\"}"), pw, (sysCfg.value_units) ? " W" : "");
|
||||
mqtt_publish_topic_P(0, PSTR("WARNING"), svalue);
|
||||
do_cmnd_power(1, 0);
|
||||
if (!hlw_mplr_counter) hlw_mplr_counter = MAX_POWER_RETRY +1;
|
||||
if (!hlw_mplr_counter) {
|
||||
hlw_mplr_counter = MAX_POWER_RETRY +1;
|
||||
}
|
||||
hlw_mplw_counter = sysCfg.hlw_mplw;
|
||||
}
|
||||
}
|
||||
|
@ -381,7 +418,7 @@ void hlw_margin_chk()
|
|||
mqtt_publish_topic_P(4, PSTR("ENERGYMONITOR"), svalue);
|
||||
do_cmnd_power(1, 1);
|
||||
}
|
||||
else if ((hlw_mkwh_state == 1) && (uped >= sysCfg.hlw_mkwh)) {
|
||||
else if ((1 == hlw_mkwh_state) && (uped >= sysCfg.hlw_mkwh)) {
|
||||
hlw_mkwh_state = 2;
|
||||
dtostrf(ped, 1, 3, svalue);
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"MaxEnergyReached\":\"%s%s\"}"), svalue, (sysCfg.value_units) ? " kWh" : "");
|
||||
|
@ -463,13 +500,13 @@ boolean hlw_command(char *type, uint16_t index, char *dataBuf, uint16_t data_len
|
|||
}
|
||||
else if (!strcmp(type,"MAXPOWERHOLD")) {
|
||||
if ((data_len > 0) && (payload >= 0) && (payload < 3601)) {
|
||||
sysCfg.hlw_mplh = (payload == 1) ? MAX_POWER_HOLD : payload;
|
||||
sysCfg.hlw_mplh = (1 == payload) ? MAX_POWER_HOLD : payload;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"MaxPowerHold\":\"%d%s\"}"), sysCfg.hlw_mplh, (sysCfg.value_units) ? " Sec" : "");
|
||||
}
|
||||
else if (!strcmp(type,"MAXPOWERWINDOW")) {
|
||||
if ((data_len > 0) && (payload >= 0) && (payload < 3601)) {
|
||||
sysCfg.hlw_mplw = (payload == 1) ? MAX_POWER_WINDOW : payload;
|
||||
sysCfg.hlw_mplw = (1 == payload) ? MAX_POWER_WINDOW : payload;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"MaxPowerWindow\":\"%d%s\"}"), sysCfg.hlw_mplw, (sysCfg.value_units) ? " Sec" : "");
|
||||
}
|
||||
|
@ -481,13 +518,13 @@ boolean hlw_command(char *type, uint16_t index, char *dataBuf, uint16_t data_len
|
|||
}
|
||||
else if (!strcmp(type,"SAFEPOWERHOLD")) {
|
||||
if ((data_len > 0) && (payload >= 0) && (payload < 3601)) {
|
||||
sysCfg.hlw_msplh = (payload == 1) ? SAFE_POWER_HOLD : payload;
|
||||
sysCfg.hlw_msplh = (1 == payload) ? SAFE_POWER_HOLD : payload;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"SafePowerHold\":\"%d%s\"}"), sysCfg.hlw_msplh, (sysCfg.value_units) ? " Sec" : "");
|
||||
}
|
||||
else if (!strcmp(type,"SAFEPOWERWINDOW")) {
|
||||
if ((data_len > 0) && (payload >= 0) && (payload < 1440)) {
|
||||
sysCfg.hlw_msplw = (payload == 1) ? SAFE_POWER_WINDOW : payload;
|
||||
sysCfg.hlw_msplw = (1 == payload) ? SAFE_POWER_WINDOW : payload;
|
||||
}
|
||||
snprintf_P(svalue, ssvalue, PSTR("{\"SafePowerWindow\":\"%d%s\"}"), sysCfg.hlw_msplw, (sysCfg.value_units) ? " Min" : "");
|
||||
}
|
||||
|
@ -517,9 +554,17 @@ boolean hlw_command(char *type, uint16_t index, char *dataBuf, uint16_t data_len
|
|||
|
||||
void hlw_mqttStat(byte option, char* svalue, uint16_t ssvalue)
|
||||
{
|
||||
char stemp0[10], stemp1[10], stemp2[10], stemp3[10], speriod[20];
|
||||
float ped, pi, pc;
|
||||
uint16_t pe, pw, pu;
|
||||
char stemp0[10];
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
char stemp3[10];
|
||||
char speriod[20];
|
||||
float ped;
|
||||
float pi;
|
||||
float pc;
|
||||
uint16_t pe;
|
||||
uint16_t pw;
|
||||
uint16_t pu;
|
||||
|
||||
hlw_readEnergy(option, ped, pe, pw, pu, pi, pc);
|
||||
dtostrf((float)sysCfg.hlw_kWhyesterday / 100000000, 1, ENERGY_RESOLUTION &7, stemp0);
|
||||
|
@ -542,10 +587,6 @@ void hlw_mqttPresent()
|
|||
|
||||
snprintf_P(svalue, sizeof(svalue), PSTR("{\"Time\":\"%s\", "), getDateTime().c_str());
|
||||
hlw_mqttStat(1, svalue, sizeof(svalue));
|
||||
|
||||
// snprintf_P(stopic, sizeof(stopic), PSTR("%s/%s/ENERGY"), sysCfg.mqtt_prefix[2], sysCfg.mqtt_topic);
|
||||
// mqtt_publish(stopic, svalue);
|
||||
|
||||
mqtt_publish_topic_P(1, PSTR("ENERGY"), svalue);
|
||||
}
|
||||
|
||||
|
@ -568,9 +609,17 @@ const char HTTP_ENERGY_SNS[] PROGMEM =
|
|||
String hlw_webPresent()
|
||||
{
|
||||
String page = "";
|
||||
char stemp[10], stemp2[10], stemp3[10], stemp4[10], sensor[300];
|
||||
float ped, pi, pc;
|
||||
uint16_t pe, pw, pu;
|
||||
char stemp[10];
|
||||
char stemp2[10];
|
||||
char stemp3[10];
|
||||
char stemp4[10];
|
||||
char sensor[300];
|
||||
float ped;
|
||||
float pi;
|
||||
float pc;
|
||||
uint16_t pe;
|
||||
uint16_t pw;
|
||||
uint16_t pu;
|
||||
|
||||
hlw_readEnergy(0, ped, pe, pw, pu, pi, pc);
|
||||
|
||||
|
|
|
@ -58,19 +58,21 @@
|
|||
|
||||
#define HTU21_CRC8_POLYNOM 0x13100
|
||||
|
||||
uint8_t htuaddr, htutype = 0;
|
||||
uint8_t delayT, delayH = 50;
|
||||
uint8_t htuaddr;
|
||||
uint8_t htutype = 0;
|
||||
uint8_t delayT;
|
||||
uint8_t delayH = 50;
|
||||
char htustype[7];
|
||||
|
||||
uint8_t check_crc8(uint16_t data)
|
||||
{
|
||||
for (uint8_t bit = 0; bit < 16; bit++)
|
||||
{
|
||||
if (data & 0x8000)
|
||||
for (uint8_t bit = 0; bit < 16; bit++) {
|
||||
if (data & 0x8000) {
|
||||
data = (data << 1) ^ HTU21_CRC8_POLYNOM;
|
||||
else
|
||||
} else {
|
||||
data <<= 1;
|
||||
}
|
||||
}
|
||||
return data >>= 8;
|
||||
}
|
||||
|
||||
|
@ -149,23 +151,30 @@ float htu21_readHumidity(void)
|
|||
|
||||
Wire.beginTransmission(HTU21_ADDR);
|
||||
Wire.write(HTU21_READHUM);
|
||||
if(Wire.endTransmission() != 0) return 0.0; // In case of error
|
||||
if (Wire.endTransmission() != 0) {
|
||||
return 0.0; // In case of error
|
||||
}
|
||||
delay(delayH); // Sensor time at max resolution
|
||||
|
||||
Wire.requestFrom(HTU21_ADDR, 3);
|
||||
if(3 <= Wire.available())
|
||||
{
|
||||
if (3 <= Wire.available()) {
|
||||
sensorval = Wire.read() << 8; // MSB
|
||||
sensorval |= Wire.read(); // LSB
|
||||
checksum = Wire.read();
|
||||
}
|
||||
if(check_crc8(sensorval) != checksum) return 0.0; // Checksum mismatch
|
||||
if (check_crc8(sensorval) != checksum) {
|
||||
return 0.0; // Checksum mismatch
|
||||
}
|
||||
|
||||
sensorval ^= 0x02; // clear status bits
|
||||
humidity = 0.001907 * (float)sensorval - 6;
|
||||
|
||||
if(humidity > 100) return 100.0;
|
||||
if(humidity < 0) return 0.01;
|
||||
if (humidity > 100) {
|
||||
return 100.0;
|
||||
}
|
||||
if (humidity < 0) {
|
||||
return 0.01;
|
||||
}
|
||||
|
||||
return humidity;
|
||||
}
|
||||
|
@ -178,33 +187,43 @@ float htu21_readTemperature(bool S)
|
|||
|
||||
Wire.beginTransmission(HTU21_ADDR);
|
||||
Wire.write(HTU21_READTEMP);
|
||||
if(Wire.endTransmission() != 0) return 0.0; // In case of error
|
||||
if (Wire.endTransmission() != 0) {
|
||||
return 0.0; // In case of error
|
||||
}
|
||||
delay(delayT); // Sensor time at max resolution
|
||||
|
||||
Wire.requestFrom(HTU21_ADDR, 3);
|
||||
if(3 == Wire.available())
|
||||
{
|
||||
if (3 == Wire.available()) {
|
||||
sensorval = Wire.read() << 8; // MSB
|
||||
sensorval |= Wire.read(); // LSB
|
||||
checksum = Wire.read();
|
||||
}
|
||||
if(check_crc8(sensorval) != checksum) return 0.0; // Checksum mismatch
|
||||
if (check_crc8(sensorval) != checksum) {
|
||||
return 0.0; // Checksum mismatch
|
||||
}
|
||||
|
||||
t = (0.002681 * (float)sensorval - 46.85);
|
||||
if(S) t = htu21_convertCtoF(t);
|
||||
if(S) {
|
||||
t = htu21_convertCtoF(t);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
float htu21_compensatedHumidity(float humidity, float temperature)
|
||||
{
|
||||
if(humidity == 0.00 && temperature == 0.00) return 0.0;
|
||||
if(temperature > 0.00 && temperature < 80.00)
|
||||
if(humidity == 0.00 && temperature == 0.00) {
|
||||
return 0.0;
|
||||
}
|
||||
if(temperature > 0.00 && temperature < 80.00) {
|
||||
return (-0.15)*(25-temperature)+humidity;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t htu_detect()
|
||||
{
|
||||
if (htutype) return true;
|
||||
if (htutype) {
|
||||
return true;
|
||||
}
|
||||
|
||||
char log[LOGSZ];
|
||||
boolean success = false;
|
||||
|
@ -253,9 +272,12 @@ uint8_t htu_detect()
|
|||
|
||||
void htu_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
if (!htutype) return;
|
||||
if (!htutype) {
|
||||
return;
|
||||
}
|
||||
|
||||
char stemp1[10], stemp2[10];
|
||||
char stemp1[10];
|
||||
char stemp2[10];
|
||||
|
||||
float t = htu21_readTemperature(TEMP_CONVERSION);
|
||||
float h = htu21_readHumidity();
|
||||
|
@ -274,7 +296,8 @@ String htu_webPresent()
|
|||
{
|
||||
String page = "";
|
||||
if (htutype) {
|
||||
char stemp[10], sensor[80];
|
||||
char stemp[10];
|
||||
char sensor[80];
|
||||
|
||||
float t_htu21 = htu21_readTemperature(TEMP_CONVERSION);
|
||||
float h_htu21 = htu21_readHumidity();
|
||||
|
|
|
@ -38,7 +38,9 @@ enum {
|
|||
SHT1X_CMD_SOFT_RESET = B00011110
|
||||
};
|
||||
|
||||
uint8_t sht_sda_pin, sht_scl_pin, shttype = 0;
|
||||
uint8_t sht_sda_pin;
|
||||
uint8_t sht_scl_pin;
|
||||
uint8_t shttype = 0;
|
||||
|
||||
boolean sht_reset()
|
||||
{
|
||||
|
@ -71,10 +73,14 @@ boolean sht_sendCommand(const byte cmd)
|
|||
boolean ackerror = false;
|
||||
digitalWrite(sht_scl_pin, HIGH);
|
||||
pinMode(sht_sda_pin, INPUT_PULLUP);
|
||||
if (digitalRead(sht_sda_pin) != LOW) ackerror = true;
|
||||
if (digitalRead(sht_sda_pin) != LOW) {
|
||||
ackerror = true;
|
||||
}
|
||||
digitalWrite(sht_scl_pin, LOW);
|
||||
delayMicroseconds(1); // Give the sensor time to release the data line
|
||||
if (digitalRead(sht_sda_pin) != HIGH) ackerror = true;
|
||||
if (digitalRead(sht_sda_pin) != HIGH) {
|
||||
ackerror = true;
|
||||
}
|
||||
if (ackerror) {
|
||||
shttype = 0;
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("SHT1X: Sensor did not ACK command"));
|
||||
|
@ -86,7 +92,9 @@ boolean sht_awaitResult()
|
|||
{
|
||||
// Maximum 320ms for 14 bit measurement
|
||||
for (byte i = 0; i < 16; i++) {
|
||||
if (digitalRead(sht_sda_pin) == LOW) return true;
|
||||
if (LOW == digitalRead(sht_sda_pin)) {
|
||||
return true;
|
||||
}
|
||||
delay(20);
|
||||
}
|
||||
addLog_P(LOG_LEVEL_DEBUG, PSTR("SHT1X: Data not ready"));
|
||||
|
@ -117,21 +125,33 @@ int sht_readData()
|
|||
|
||||
boolean sht_readTempHum(float &t, float &h)
|
||||
{
|
||||
float tempRaw, humRaw, rhLinear;
|
||||
float tempRaw;
|
||||
float humRaw;
|
||||
float rhLinear;
|
||||
|
||||
t = NAN;
|
||||
h = NAN;
|
||||
|
||||
if (!sht_reset()) return false;
|
||||
if (!sht_sendCommand(SHT1X_CMD_MEASURE_TEMP)) return false;
|
||||
if (!sht_awaitResult()) return false;
|
||||
if (!sht_reset()) {
|
||||
return false;
|
||||
}
|
||||
if (!sht_sendCommand(SHT1X_CMD_MEASURE_TEMP)) {
|
||||
return false;
|
||||
}
|
||||
if (!sht_awaitResult()) {
|
||||
return false;
|
||||
}
|
||||
tempRaw = sht_readData();
|
||||
// Temperature conversion coefficients from SHT1X datasheet for version 4
|
||||
const float d1 = -39.7; // 3.5V
|
||||
const float d2 = 0.01; // 14-bit
|
||||
t = d1 + (tempRaw * d2);
|
||||
if (!sht_sendCommand(SHT1X_CMD_MEASURE_RH)) return false;
|
||||
if (!sht_awaitResult()) return false;
|
||||
if (!sht_sendCommand(SHT1X_CMD_MEASURE_RH)) {
|
||||
return false;
|
||||
}
|
||||
if (!sht_awaitResult()) {
|
||||
return false;
|
||||
}
|
||||
humRaw = sht_readData();
|
||||
// Temperature conversion coefficients from SHT1X datasheet for version 4
|
||||
const float c1 = -2.0468;
|
||||
|
@ -141,13 +161,16 @@ boolean sht_readTempHum(float &t, float &h)
|
|||
const float t2 = 0.00008;
|
||||
rhLinear = c1 + c2 * humRaw + c3 * humRaw * humRaw;
|
||||
h = (t - 25) * (t1 + t2 * humRaw) + rhLinear;
|
||||
if(!isnan(t) && TEMP_CONVERSION) t = t * 1.8 + 32;
|
||||
if (!isnan(t) && TEMP_CONVERSION) {
|
||||
t = t * 1.8 + 32;
|
||||
}
|
||||
return (!isnan(t) && !isnan(h));
|
||||
}
|
||||
|
||||
boolean sht_readCharTempHum(char* temp, char* hum)
|
||||
{
|
||||
float t, h;
|
||||
float t;
|
||||
float h;
|
||||
|
||||
boolean success = sht_readTempHum(t, h);
|
||||
dtostrf(t, 1, TEMP_RESOLUTION &3, temp);
|
||||
|
@ -157,9 +180,12 @@ boolean sht_readCharTempHum(char* temp, char* hum)
|
|||
|
||||
boolean sht_detect()
|
||||
{
|
||||
if (shttype) return true;
|
||||
if (shttype) {
|
||||
return true;
|
||||
}
|
||||
|
||||
float t, h;
|
||||
float t;
|
||||
float h;
|
||||
|
||||
sht_sda_pin = pin[GPIO_I2C_SDA];
|
||||
sht_scl_pin = pin[GPIO_I2C_SCL];
|
||||
|
@ -179,9 +205,13 @@ boolean sht_detect()
|
|||
|
||||
void sht_mqttPresent(char* svalue, uint16_t ssvalue, uint8_t* djson)
|
||||
{
|
||||
if (!shttype) return;
|
||||
if (!shttype) {
|
||||
return;
|
||||
}
|
||||
|
||||
char stemp[10];
|
||||
char shum[10];
|
||||
|
||||
char stemp[10], shum[10];
|
||||
if (sht_readCharTempHum(stemp, shum)) {
|
||||
snprintf_P(svalue, ssvalue, JSON_SNS_TEMPHUM, svalue, "SHT1X", stemp, shum);
|
||||
*djson = 1;
|
||||
|
@ -196,7 +226,9 @@ String sht_webPresent()
|
|||
{
|
||||
String page = "";
|
||||
if (shttype) {
|
||||
char stemp[10], shum[10];
|
||||
char stemp[10];
|
||||
char shum[10];
|
||||
|
||||
if (sht_readCharTempHum(stemp, shum)) {
|
||||
char sensor[80];
|
||||
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, "SHT1X", stemp, (TEMP_CONVERSION) ? 'F' : 'C');
|
||||
|
|
Loading…
Reference in New Issue