From 82ed5f0d06e817b373ee7acff24d4a3d066a69b2 Mon Sep 17 00:00:00 2001 From: gemu Date: Tue, 3 Jan 2023 17:41:37 +0100 Subject: [PATCH] Modbus section (#17583) * modbus section * Update xnrg_29_modbus.ino * Update xnrg_29_modbus.ino * Update xnrg_29_modbus.ino --- .../tasmota_xdrv_driver/xdrv_10_scripter.ino | 78 +++++++++++++++++-- .../tasmota_xnrg_energy/xnrg_29_modbus.ino | 7 ++ 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino index 1e60baf1f..bac7dd575 100755 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino @@ -2564,6 +2564,37 @@ chknext: } goto nfuncexit; } + if (!strncmp(lp, "ap(", 3)) { + //TasmotaGlobal.restart_flag = 216; + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, gv); + switch ((uint8_t)fvar) { + case 0: + { char ssid[SCRIPT_MAXSSIZE]; + lp = GetStringArgument(lp, OPER_EQU, ssid, 0); + char pw[SCRIPT_MAXSSIZE]; + lp = GetStringArgument(lp, OPER_EQU, pw, 0); + IPAddress local_IP(192,168,189,1); + IPAddress gateway(192,168,189,1); + IPAddress subnet(255,255,255,0); + // Begin Access Point + WiFi.softAPConfig(local_IP, gateway, subnet); + fvar = WiFi.softAP(ssid, pw); + } + break; + case 1: + fvar = WiFi.softAPdisconnect(true); + break; + case 2: + fvar = WiFi.disconnect(true); + break; + } + + //Web.state = HTTP_ADMIN; + // 192.168.4.1 + // WiFi.softAPIP(); + + goto nfuncexit; + } break; case 'b': @@ -10290,6 +10321,25 @@ void ScriptJsonAppend(void) { } #endif //USE_SCRIPT_JSON_EXPORT +// returns section as string +String ScriptLoadSection(const char *sect) { + uint16_t slen = strlen(sect); + if (Run_Scripter1(sect, -slen, 0) == 99) { + char *spo = glob_script_mem.section_ptr + slen; + char *sp = spo; + while (*sp) { + if (*sp == '#') { + *sp = 0; + String str = spo; + *sp = '#'; + return str; + } + sp++; + } + } + return ""; +} + bool RulesProcessEvent(const char *json_event) { if (bitRead(Settings->rule_enabled, 0)) { @@ -10737,7 +10787,7 @@ void cpy2lf(char *dst, uint32_t dstlen, char *src) { #ifdef USE_SCRIPT_I2C uint8_t script_i2c_addr; TwoWire *script_i2c_wire; -uint32_t script_i2c(uint8_t sel, uint32_t val, uint32_t val1) { +uint32_t script_i2c(uint8_t sel, uint16_t val, uint32_t val1) { uint32_t rval = 0; uint8_t bytes = 1; @@ -10755,9 +10805,11 @@ uint32_t script_i2c(uint8_t sel, uint32_t val, uint32_t val1) { break; case 2: // read 1..4 bytes - script_i2c_wire->beginTransmission(script_i2c_addr); - script_i2c_wire->write(val); - script_i2c_wire->endTransmission(); + if ((val & 0x8000) == 0) { + script_i2c_wire->beginTransmission(script_i2c_addr); + script_i2c_wire->write(val); + script_i2c_wire->endTransmission(); + } script_i2c_wire->requestFrom((int)script_i2c_addr, (int)val1); for (uint8_t cnt = 0; cnt < val1; cnt++) { @@ -10773,10 +10825,20 @@ uint32_t script_i2c(uint8_t sel, uint32_t val, uint32_t val1) { // write 1 .. 4 bytes bytes = sel - 9; script_i2c_wire->beginTransmission(script_i2c_addr); - script_i2c_wire->write(val); - for (uint8_t cnt = 0; cnt < bytes; cnt++) { - script_i2c_wire->write(val1); - val1 >>= 8; + if ((val & 0x8000) == 0) { + script_i2c_wire->write(val); + } + if ((val & 0x4000) == 0) { + for (uint8_t cnt = 0; cnt < bytes; cnt++) { + script_i2c_wire->write(val1); + val1 >>= 8; + } + } else { + uint32_t wval = 0; + for (uint8_t cnt = 0; cnt < bytes; cnt++) { + wval = val1 >> ((bytes - 1 - cnt) * 8); + script_i2c_wire->write(wval); + } } script_i2c_wire->endTransmission(); break; diff --git a/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino b/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino index 074a31801..2d98c29e4 100644 --- a/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino +++ b/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino @@ -501,6 +501,13 @@ bool EnergyModbusReadRegisters(void) { } #endif // USE_RULES +#ifdef USE_SCRIPT + if (!modbus.length()) { + modbus = ScriptLoadSection(">y"); + AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Loaded from script")); + } +#endif // USE_SCRIPT + if (!modbus.length()) { return false; } // File not found // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: File '%s'"), modbus.c_str());