diff --git a/sonoff/user_config.h b/sonoff/user_config.h index 792780545..269e21a24 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -277,7 +277,12 @@ // #define USE_MGS // Add I2C code for Xadow and Grove Mutichannel Gas sensor using library Multichannel_Gas_Sensor (+10k code) #define MGS_SENSOR_ADDR 0x04 // Default Mutichannel Gas sensor i2c address // #define USE_APDS9960 // Add I2C code for APDS9960 Proximity Sensor. Disables SHT and VEML6070 (+4k7 code) -// #define USE_MCP230xx // Add I2C code for MCP23008/MCP23017 for GP INPUT ONLY + + // If only USE_MCP230xx then device can be configured using sensor29 command +// #define USE_MCP230xx // Add I2C code for MCP23008/MCP23017 for GP INPUT ONLY (+2260 bytes) +// #define USE_MCP230xx_displaymain // Display pin status on Tasmota main page (+188 bytes) +// #define USE_MCP230xx_webconfig // Enable web config button and form to Tasmota web interface (+2444) + #endif // USE_I2C // -- SPI sensors --------------------------------- diff --git a/sonoff/xdrv_02_webserver.ino b/sonoff/xdrv_02_webserver.ino index a6cd2d4fc..ec6423e6b 100644 --- a/sonoff/xdrv_02_webserver.ino +++ b/sonoff/xdrv_02_webserver.ino @@ -204,8 +204,10 @@ const char HTTP_BTN_MENU_MQTT[] PROGMEM = ""; #ifdef USE_I2C #ifdef USE_MCP230xx +#ifdef USE_MCP230xx_webconfig const char HTTP_BTN_MCP230XX[] PROGMEM = "
"; +#endif // USE_MCP230xx_webconfig #endif // USE_MCP230xx #endif // USE_I2C const char HTTP_BTN_MENU4[] PROGMEM = @@ -384,7 +386,9 @@ void StartWebserver(int type, IPAddress ipweb) WebServer->on("/rb", HandleRestart); #ifdef USE_I2C #ifdef USE_MCP230xx +#ifdef USE_MCP230xx_webconfig WebServer->on("/mcp230xx", handleMCP230xx); +#endif // USE_MCP230xx_webconfig #endif // USE_MCP230xx #endif // USE_I2C #ifndef BE_MINIMAL @@ -708,9 +712,11 @@ void HandleConfiguration() if (Settings.flag.mqtt_enabled) { page += FPSTR(HTTP_BTN_MENU_MQTT); } #ifdef USE_I2C #ifdef USE_MCP230xx +#ifdef USE_MCP230xx_webconfig if (MCP230xx_Type()) { // Configuration button will only show if MCP23008/MCP23017 was detected on I2C page += FPSTR(HTTP_BTN_MCP230XX); } +#endif // USE_MCP230xx_webconfig #endif // USE_MCP230xx #endif // USE_I2C page += FPSTR(HTTP_BTN_MENU4); @@ -1144,9 +1150,11 @@ void HandleSaveSettings() break; #ifdef USE_I2C #ifdef USE_MCP230xx +#ifdef USE_MCP230xx_webconfig case 8: // MCP230xx_SaveSettings MCP230xx_SaveSettings(); break; +#endif // USE_MCP230xx_webconfig #endif // USE_MCP230xx #endif // USE_I2C case 6: diff --git a/sonoff/xsns_29_mcp230xx.ino b/sonoff/xsns_29_mcp230xx.ino index 872a447c3..1fdb8a863 100644 --- a/sonoff/xsns_29_mcp230xx.ino +++ b/sonoff/xsns_29_mcp230xx.ino @@ -29,6 +29,8 @@ I2C Address: 0x20 - 0x27 \*********************************************************************************************/ +#define XSNS_29 29 + #define MCP230xx_ADDRESS1 0x20 #define MCP230xx_ADDRESS2 0x21 #define MCP230xx_ADDRESS3 0x22 @@ -56,8 +58,10 @@ uint8_t mcp230xx_addresses[] = { MCP230xx_ADDRESS1, MCP230xx_ADDRESS2, MCP230xx_ uint8_t mcp280xx_pincount = 0; #ifdef USE_WEBSERVER +#ifdef USE_MCP230xx_displaymain const char HTTP_SNS_MCP230xx_GPIO[] PROGMEM = "%s{s}%s MCP230XX D%d{m}%d{e}"; // {s} = , {m} = , {e} = - +#endif // USE_MCP230xx_displaymain +#ifdef USE_MCP230xx_webconfig const char HTTP_FORM_I2C_MCP230XX_T[] PROGMEM = ""; const char HTTP_FORM_I2C_MCP230XX_TE[] PROGMEM = "
"; @@ -186,6 +190,8 @@ void MCP230xx_SaveSettings() MCP230xx_ApplySettings(); } +#endif // USE_MCP230xx_webconfig + #endif // USE_WEBSERVER uint8_t MCP230xx_Type(void) { @@ -339,6 +345,7 @@ void MCP230xx_Show(boolean json) } #ifdef USE_WEBSERVER +#ifdef USE_MCP230xx_displaymain } else { uint8_t gpio1 = MCP230xx_readGPIO(0); uint8_t gpio2 = 0; @@ -352,17 +359,74 @@ void MCP230xx_Show(boolean json) snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_MCP230xx_GPIO, mqtt_data, "", pin, (gpio>>pin)&1); } } +#endif // USE_MCP230xx_displaymain #endif // USE_WEBSERVER } } } +bool MCP230xx_Command(void) { + boolean serviced = true; + uint8_t _a, _b = 0; + uint8_t pin, pinmode, pullup = 0; + String data = XdrvMailbox.data; + _a = data.indexOf(","); + _b = data.indexOf(",", _a + 1); + if (_a < XdrvMailbox.data_len) { + if (_b < XdrvMailbox.data_len) { + pin = data.substring(0, _a).toInt(); + pinmode = data.substring(_a+1, _b).toInt(); + pullup = data.substring(_b+1, XdrvMailbox.data_len).toInt(); + data = "MCP D" + String(pin) + " mode=" + String(pinmode) + " pullup=" + String(pullup); + if (pinmode) { + Settings.mcp230xx_config[pin].enable = 1; + if (pinmode >= 2) { + Settings.mcp230xx_config[pin].inten = 1; + if (pinmode >= 3) { + Settings.mcp230xx_config[pin].intmode = 1; + if (pinmode >= 4) { + Settings.mcp230xx_config[pin].intcomp = 1; + } else { + Settings.mcp230xx_config[pin].intcomp = 0; + } + } else { + Settings.mcp230xx_config[pin].intmode = 0; + Settings.mcp230xx_config[pin].intcomp = 0; + } + } else { + Settings.mcp230xx_config[pin].inten = 0; + Settings.mcp230xx_config[pin].intmode = 0; + Settings.mcp230xx_config[pin].intcomp = 0; + } + } else { + Settings.mcp230xx_config[pin].enable = 0; + Settings.mcp230xx_config[pin].inten = 0; + Settings.mcp230xx_config[pin].intmode = 0; + Settings.mcp230xx_config[pin].intcomp = 0; + } + Settings.mcp230xx_config[pin].b5 = 0; + Settings.mcp230xx_config[pin].b6 = 0; + Settings.mcp230xx_config[pin].b7 = 0; + if (Settings.mcp230xx_config[pin].enable) { + Settings.mcp230xx_config[pin].pullup = pullup; + } else { + Settings.mcp230xx_config[pin].pullup = 0; + } + MCP230xx_ApplySettings(); + snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_29, data.c_str()); + } else { + serviced = false; + } + } else { + serviced = false; + } + return serviced; +} + /*********************************************************************************************\ Interface \*********************************************************************************************/ -#define XSNS_29 - boolean Xsns29(byte function) { boolean result = false; @@ -378,10 +442,15 @@ boolean Xsns29(byte function) case FUNC_JSON_APPEND: MCP230xx_Show(1); break; + case FUNC_COMMAND: + result = MCP230xx_Command(); + break; #ifdef USE_WEBSERVER +#ifdef USE_MCP230xx_displaymain case FUNC_WEB_APPEND: MCP230xx_Show(0); break; +#endif // USE_MCP230xx_displaymain #endif // USE_WEBSERVER } }