3 Commits

Author SHA1 Message Date
3c018ebba5 light info has unique id no 2025-08-30 21:38:24 +02:00
14041120cd add helper telnet 2025-08-30 12:09:20 +02:00
fdff1d369c add a serial tcp debug 2025-08-30 00:07:19 +02:00
5 changed files with 160 additions and 10 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json .vscode/c_cpp_properties.json
.vscode/launch.json .vscode/launch.json
.vscode/ipch .vscode/ipch
.venv/

125
helper/remote-serial.py Normal file
View File

@@ -0,0 +1,125 @@
#!/usr/bin/env python3
"""
Telnet-only tool to toggle DTR/RTS via ser2net admin console.
New sequences (physical levels):
- restart: RTS HI -> DTR LO -> DTR HI
- download: RTS LO -> DTR LO -> DTR HI -> RTS HI
Timing parameters tunable via args (ms). Use admin host/port and connection name (con).
Example:
python3 helper/remote-serial.py --host 192.168.1.100 --admin-port 7373 --con con1 --action restart -v
"""
import argparse
import socket
import errno
import time
import sys
def send_admin_cmd(host, port, cmd, timeout=1.0, verbose=False):
if verbose:
print(f"> {cmd}")
try:
with socket.create_connection((host, port), timeout=timeout) as s:
s.settimeout(timeout)
try:
s.sendall(cmd.encode('ascii') + b"\n")
except Exception as e:
print(f"ERROR sending command: {e}", file=sys.stderr)
return None
# small pause to let server respond
time.sleep(0.05)
chunks = []
while True:
try:
data = s.recv(4096)
if not data:
break
chunks.append(data)
# short sleep to allow more data to arrive
time.sleep(0.01)
except socket.timeout:
break
except OSError as e:
if getattr(e, 'errno', None) == errno.EWOULDBLOCK:
break
break
resp = b"".join(chunks)
if resp:
try:
txt = resp.decode(errors="ignore")
except Exception:
txt = str(resp)
if verbose:
print(txt.strip())
return txt
return ""
except Exception as e:
print(f"ERROR: cannot connect to {host}:{port} -> {e}", file=sys.stderr)
return None
def set_port_control(host, admin_port, con, line, hi_or_lo, timeout, verbose):
# hi_or_lo must be 'HI' or 'LO'
cmd = f"setportcontrol {con} {line}{hi_or_lo}"
return send_admin_cmd(host, admin_port, cmd, timeout=timeout, verbose=verbose)
def run_sequence(host, admin_port, con, seq, timeout, verbose):
# seq: list of (line, state, sleep_seconds)
for line, state, sleep_s in seq:
if verbose:
print(f"-> {line}{state} (wait {sleep_s:.3f}s)")
res = set_port_control(host, admin_port, con, line, state, timeout, verbose)
# continue even if response is None; user sees errors in stderr
if sleep_s and sleep_s > 0:
time.sleep(sleep_s)
def main():
p = argparse.ArgumentParser(description="ser2net admin: restart / download sequences (explicit HI/LO)")
p.add_argument("--host", required=True, help="ser2net admin host/ip")
p.add_argument("--admin-port", type=int, required=True, help="ser2net admin port")
p.add_argument("--con", required=True, help="ser2net connection name (e.g. con1)")
p.add_argument("--action", choices=["restart", "download", "bootloader"], default="restart",
help="restart or download (bootloader alias)")
p.add_argument("--pulse-ms", type=float, default=100.0, help="DTR pulse length in milliseconds")
p.add_argument("--step-delay-ms", type=float, default=20.0, help="short delay between steps in milliseconds")
p.add_argument("--tail-delay-ms", type=float, default=50.0, help="extra delay after sequence in milliseconds")
p.add_argument("--timeout", type=float, default=1.0, help="telnet connect/read timeout seconds")
p.add_argument("-v", "--verbose", action="store_true")
args = p.parse_args()
pulse_s = max(0.001, args.pulse_ms / 1000.0)
step_delay_s = max(0.0, args.step_delay_ms / 1000.0)
tail_delay_s = max(0.0, args.tail_delay_ms / 1000.0)
if args.verbose:
print(f"Admin {args.host}:{args.admin_port} con={args.con} action={args.action}")
# sequences use explicit physical HI/LO as requested
if args.action == "restart":
seq = [
("RTS", "HI", step_delay_s), # RTS HI
("DTR", "LO", pulse_s), # DTR LO (pulse start)
("DTR", "HI", tail_delay_s), # DTR HI (pulse end), then tail delay
]
else: # download / bootloader
# Ensure known starting state (RESET released), then enter download:
# 1) ensure DTR=HI (reset released)
# 2) RTS=LO (IO0 low)
# 3) DTR=LO -> wait pulse_ms
# 4) DTR=HI -> small delay
# 5) RTS=HI -> release IO0 (enter bootloader)
seq = [
("DTR", "HI", step_delay_s), # ensure reset released
("RTS", "LO", step_delay_s), # RTS LO (IO0 low)
("DTR", "LO", pulse_s), # DTR LO (reset asserted)
("DTR", "HI", step_delay_s), # DTR HI (reset released)
("RTS", "HI", tail_delay_s), # RTS HI (release IO0)
]
run_sequence(args.host, args.admin_port, args.con, seq, args.timeout, args.verbose)
if args.verbose:
print("Sequence finished.")
if __name__ == "__main__":
main()

View File

@@ -16,8 +16,8 @@ const struct {
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId) Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId)
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr) { : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), lightInfo(uniqueId) {
lightInfo.uniqueId = uniqueId;
lightType = LightType::rgb; lightType = LightType::rgb;
uint8_t bits = pinR->getLedResolutionBits(); uint8_t bits = pinR->getLedResolutionBits();
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u; maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;
@@ -25,8 +25,7 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId)
} }
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId) Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId)
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW) { : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), lightInfo(uniqueId) {
lightInfo.uniqueId = uniqueId;
lightType = LightType::rgbww; lightType = LightType::rgbww;
uint8_t bits = pinR->getLedResolutionBits(); uint8_t bits = pinR->getLedResolutionBits();
maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u; maxPwm = (bits >= 1 && bits <= 31) ? ((1u << bits) - 1u) : 255u;

View File

@@ -4,14 +4,32 @@
#include "pin.h" #include "pin.h"
struct LightInfo { struct LightInfo {
LightInfo() = default;
LightInfo(const std::string& id) {
uniqueId = id;
updateTopics();
}
void setUniqueId(const std::string& id) {
uniqueId = id;
updateTopics();
}
void updateTopics() {
discoveryTopic = discoveryTopicBase + uniqueId + "/config";
availabilityTopic = topicBase + uniqueId + "/availability";
stateTopic = topicBase + uniqueId + "/state";
jsonAttributesTopic = topicBase + uniqueId + "/attributes";
commandTopic = topicBase + uniqueId + "/state/set";
}
std::string uniqueId; std::string uniqueId;
const std::string name = "Smart RGB Light"; const std::string name = "Smart RGB Light";
const std::string discoveryTopic = "homeassistant/light/smart_rgb_light/light/config"; const std::string discoveryTopicBase = "homeassistant/light/";
const std::string availabilityTopic = "studiotj/smart-rgb/light/status"; std::string discoveryTopic = discoveryTopicBase + uniqueId + "/config";
const std::string stateTopic = "studiotj/smart-rgb/light/state"; const std::string topicBase = "studiotj/";
const std::string jsonAttributesTopic = "studiotj/smart-rgb/light/attributes"; std::string availabilityTopic = topicBase + uniqueId + "/availability";
const std::string stateValueTemplate = "{{ value_json.state }}"; std::string stateTopic = topicBase + uniqueId + "/state";
const std::string commandTopic = "studiotj/smart-rgb/light/state/set"; std::string jsonAttributesTopic = topicBase + uniqueId + "/attributes";
std::string stateValueTemplate = "{{ value_json.state }}";
std::string commandTopic = topicBase + uniqueId + "/state/set";
const std::string supportedColorModesValue = "['rgb', 'brightness']"; const std::string supportedColorModesValue = "['rgb', 'brightness']";
const std::string availabilityTemplate = "{{ value_json.availability }}"; const std::string availabilityTemplate = "{{ value_json.availability }}";
}; };

View File

@@ -29,6 +29,13 @@ build_flags =
${env.build_flags} ${env.build_flags}
-D ENABLE_SERIAL_DEBUG=1 -D ENABLE_SERIAL_DEBUG=1
[env:esp32dev-serial-tcp]
build_flags =
${env.build_flags}
-D ENABLE_SERIAL_DEBUG=1
monitor_port = rfc2217://10.238.75.163:6000
upload_port = rfc2217://10.238.75.163:6000
[env:esp32dev-ota] [env:esp32dev-ota]
upload_protocol = espota upload_protocol = espota
upload_port = smart-rgb.local upload_port = smart-rgb.local