sml_ct002.tc¶
sml_ct002.tc — SML meter + Marstek CT002/CT003 emulator, no charts.
// @name: SML + Marstek CT002
// @info: https://ottelo9.github.io/tasmota-sml-script/ct002/
// sml_ct002.tc — SML meter + Marstek CT002/CT003 emulator, no charts.
// Meter setup and telemetry as in sml_simple.tc, plus the CT emulator on
// UDP 12345. For the charts variant use sml_chart_ct002.tc.
// Docs: https://ottelo9.github.io/tasmota-sml-script/ct002/
// Needs ESP32, TinyC in SLOT 0 (the /ctreg and /pwr endpoints only work
// there - Tasmota dispatches every webOn to slot 0), USE_SML.
#include "ui_common.tc"
#include "sml_descriptor.tc"
#include "sml_modbus_common.tc"
#include "ct002_common.tc"
char sml_vers[] = "28.09.2026";
// ─── Settings ──────────────────────────────────────────────────────────────
persist int sml_sndpwr; // 1 = publish power2 via MQTT
int sml_e_sndpwr; // edit buffer, applied by Save
// ─── Runtime ───────────────────────────────────────────────────────────────
int sml_pwr_last;
byte sml_mtopic[64];
byte sml_pub[128];
int do_reset;
// ─── Callbacks ─────────────────────────────────────────────────────────────
void Every100ms() {
ctPoll();
mbPoll();
}
void EverySecond() {
uiApplyBegin();
if (tc_opt_first == 1) {
sml_sndpwr = tcOptGet(10, sml_sndpwr);
tcOptSet(10, sml_sndpwr);
}
if (ui_reloading) sml_e_sndpwr = sml_sndpwr;
if (ui_applying) { sml_sndpwr = sml_e_sndpwr; tcOptSet(10, sml_sndpwr); }
sml_descriptor_apply();
ctTick();
mbTick();
if (do_reset) {
tasmCmd("Sensor53 r", sml_buf);
do_reset = 0;
}
if (tasm_uptime - sml_pwr_last >= 5) {
sml_pwr_last = tasm_uptime;
sml_power2 = (0.9 * sml_power2) + (0.1 * smlGet(1));
if (sml_sndpwr > 0) {
sprintf(sml_ftopic, "stat/%s/script/power2", sml_mtopic);
sprintf(sml_pub, "%.0f", sml_power2);
smlPublish(sml_ftopic, sml_pub);
sprintf(sml_ftopic, "stat/%s/script/cpwr", sml_mtopic);
sprintf(sml_pub, "%d", ct_ptot);
smlPublish(sml_ftopic, sml_pub);
}
}
}
void TaskLoop() {
ctTask();
delay(200);
}
void WebOn() {
int h = webHandler();
if (ctWebOn(h)) return; // /ctreg
if (h != 7) return; // /pwr
sml_cpwr = ct_ptot; sml_hascp = 1;
if (smlVars()) return; // /pwr?v=... single values
smlPwrJson(sml_power2);
sprintfAppend(sml_buf, ",\"cpwr\":%d}", ct_ptot);
smlSendJson(sml_buf);
}
void WebCall() {
if (!sml_activ) {
uiText("{s}SML{m}off (Rule1){e}", "{s}SML{m}aus (Rule1){e}");
return;
}
uiSep();
if (ui_de) {
sprintf(sml_buf, "{s}Leistung{m}%.0f W{e}", smlGet(1)); webSend(sml_buf);
sprintf(sml_buf, "{s}Bezug{m}%.3f kWh{e}", smlGet(2)); webSend(sml_buf);
sprintf(sml_buf, "{s}Einspeisung{m}%.3f kWh{e}", sml_export()); webSend(sml_buf);
} else {
sprintf(sml_buf, "{s}Power{m}%.0f W{e}", smlGet(1)); webSend(sml_buf);
sprintf(sml_buf, "{s}Import{m}%.3f kWh{e}", smlGet(2)); webSend(sml_buf);
sprintf(sml_buf, "{s}Export{m}%.3f kWh{e}", sml_export()); webSend(sml_buf);
}
sml_phase_rows();
uiSep();
ctRows();
mbRows();
}
void WebUI() {
// Page 1 exists only for its button in Tasmota's main menu: webPageLabel is
// the only way to get one, and it always points at /tc_ui - so hand over.
if (webPage() == 1) { webSend("<script>location.replace('/ctreg')</script>"); return; }
if (webPage() == 5) { webSend("<script>location.replace('/tc')</script>"); return; }
// Page 4 carries everything the CT emulator owns - it outgrew the main
// page when the control settings arrived. Rendered in ct002_common.tc.
if (webPage() == 4) { ctPage4("sml_ct002.tc", sml_vers); return; }
if (webPage() != 0) return;
sml_render_settings_panel(); // opens the panel div
ctPageLink();
uiHead("📡", "Telemetry", "Telemetrie", "sml/#telemetry");
if (ui_de) webCheckbox(sml_e_sndpwr, "power2 + cpwr senden (MQTT)");
else webCheckbox(sml_e_sndpwr, "Send power2 + cpwr (MQTT)");
sprintf(sml_buf, "<div class='n'>power2: <b>%.0f W</b></div>", sml_power2);
webSend(sml_buf);
sprintf(sml_buf, "<div class='n'>cpwr: <b>%d W</b></div>", ct_ptot);
webSend(sml_buf);
webSend("<b>HTTP API</b>");
webSend("<div class='n'>HTTP: <a href='/pwr' target='_blank'>/pwr</a> <a href='/pwr?v=all' target='_blank'>/pwr?v=all</a> <a href='/pwr?v=sml[1]' target='_blank'>/pwr?v=sml[1]</a></div>");
mbPanel();
webSend("<hr>");
uiSaveButton();
if (ui_de) webButton(do_reset, "SML Neustart|OK");
else webButton(do_reset, "Restart SML|OK");
uiUptime();
sprintf(sml_buf, "sml_ct002.tc · Version %s<br>", sml_vers);
webSend(sml_buf);
webSend("by <a href='https://ottelo.jimdofree.com' target='_blank'>ottelo.jimdofree.com</a> & gemu2015 (TinyC <span id='tcv'>...</span>)</div></div>");
webSend("<script>if(window.__tcv){document.getElementById('tcv').textContent=window.__tcv;}else{fetch('/tc_api?cmd=status').then(function(r){return r.json();}).then(function(j){window.__tcv=j.release;var e=document.getElementById('tcv');if(e)e.textContent=j.release;}).catch(function(){});}</script>");
}
void Command(char cmd[]) {
if (ctCmd(cmd)) return;
responseCmnd("CTSTAT | CTCLR | CTMAC <hex12> | CTMAIL <adr> | CTPASS <pw> | CTLOGIN | CTCREATE JA");
}
void JsonCall() {
ctJson();
}
int main() {
if (sml_rx_pin == 0 && sml_tx_pin == 0) {
sml_rx_pin = -1;
sml_tx_pin = -1;
}
if (sml_activ != tasm_rule) {
tasm_rule = sml_activ;
}
sml_e_sndpwr = sml_sndpwr;
tasmInfo(0, sml_mtopic);
sml_power2 = smlGet(1);
sml_pwr_last = 0;
ctInit(4); // /ctreg on handler 4
mbInit();
addCommand("CT");
webPageLabel(0, "⚙️ SML /🔋");
webPageLabel(1, "☁️ Marstek");
webPageLabel(4, "🔋 CT");
webPageLabel(5, "💻 TinyC"); // hands over to /tc - kept last so it stays
// last when a program adds a page
webOn(7, "/pwr");
smlScripterLoad("/sml_meter.def");
addLog("sml_ct002: %s started, %s, port %d, CT MAC '%s'",
sml_vers, ct_type, ct_port, ct_mac);
return 0;
}