Commit graph

224 commits

Author SHA1 Message Date
cjkas
bd7ed17a97 feat: log AP signal levels and roam for a 6 dB gain
The roaming scan found two APs for the SSID every 10 minutes but the log
never showed their signal, so there was no way to tell whether the
device at -70 dBm had a better AP to move to. Log each AP on the
roaming scans, show roams at info level, and switch for a 6 dB gain
instead of 10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:50:46 +02:00
cjkas
f1eac31957 fix: report the real flipPosition for linked shades
serializeShadeRef sent flipCommands as flipPosition, so group linked
shades and the /shadeCommand reply showed the wrong setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:50:42 +02:00
cjkas
6b19d24eb4 fix: print each log line whole
The Arduino core's log_printf() has its UART lock commented out and
writes with ets_printf(), so lines logged at the same time from
async_tcp and loop() came out interleaved character by character. It
also formats short lines into one static buffer shared by every task.

Wrap log_printf at link time and hold a mutex around the core's
log_printfv(). It never blocks before the scheduler runs, in an ISR or
in a critical section, and waits at most 50ms before printing anyway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:50:41 +02:00
cjkas
dd47c04905 feat: log timings for slow work, socket clients and boot crashes
Add Timing.h and use it to log what holds up the main loop, how long a
new socket client's setup takes, slow socket sends, and how long each
web request took and from which client. Socket connects and disconnects
are logged with how long the client stayed, and late pings as warnings.
Routine sends and pings are not logged, as each line costs several ms
on a 115200 baud console.

Report a stored crash once with the crashed and running build IDs, then
erase it, since it was reported on every boot and its backtrace only
decodes against the build that crashed. Mount the file system once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:35:43 +02:00
cjkas
05c3d309a6 fix: send wifiStrength only for real signal changes
At -70 dBm the RSSI moves a dBm or two constantly, so the >1 dBm check
every 1.5s sent wifiStrength to every client about 270 times an hour.
That is extra traffic on an already lossy link and a state write in
Home Assistant for each one.

Broadcast on a channel change, or for a change of at least 3 dBm no
more than every 10s. A newly connected client still gets the current
value straight away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:35:42 +02:00
cjkas
3a8736f4d4 refactor: share the shade travel maths in ShadeMotion.cpp
checkMovement worked out the position four times, nearly copy-pasted,
for lift down, lift up, tilt down and tilt up.  All four now call
travelPosition(), which keeps the same arithmetic, including how the
elapsed time wraps with millis().  Tilt up with no tilt time still
stops the tilt direction in the caller.

transformPosition and transformCommand keep their behaviour through
the new displayPosition() and flipCommand() helpers.  The native env
builds ShadeMotion.cpp so all of this can be unit tested on the PC.
No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:37:48 +02:00
cjkas
8881289b1d fix: never write past a JSON buffer or send a broken socket event
When something didn't fit, the formatters skipped it silently, closing
brackets included, and clients got malformed JSON.  Two writes could
also go past the buffer:

- AsyncJsonResp flushed when full and then wrote the value without
  checking again, so a value bigger than the whole buffer overflowed.
  It is now streamed straight out instead.
- JsonSockEvent::closeEvent added the closing ] when only one byte was
  left, putting the terminator one byte past the buffer.

The formatters now set an overflowed() flag and log an error, and a
socket event that overflowed is logged and not sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:31:48 +02:00
cjkas
b2294ced34 fix: escape control characters and count JSON string lengths correctly
calcEscapedLength looped from strlen(raw) down to 1, counting the
terminator instead of the first character.  A value starting with a
character that needs escaping was counted one byte short, so the fit
check could let it be written one byte past the buffer.  Control
characters other than \b\f\n\r\t were copied raw, which isn't valid
JSON.

Both now go through one escapeChar helper, which also escapes the
remaining control characters as \u00XX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:30:42 +02:00
cjkas
d723c1024d refactor: move JsonFormatter into JsonFormatter.cpp
JsonFormatter and BufferedJsonFormatter move verbatim out of
WResp.cpp/WResp.h, which now include JsonFormatter.h.  They don't need
the web server, so on their own they can be unit tested on the PC; the
native env builds the new file.  AsyncJsonResp and JsonSockEvent stay
in WResp.  No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:29:30 +02:00
cjkas
60419a37aa fix: parse the command names the firmware reports
shadeCommand events (and the Home Assistant events built from them)
name commands "Step Up", "Sun Flag", "My+Up" and so on, but parsing
those names fell through to the prefix shortcuts: "Step Up" became
SunFlag, "Sun Flag" became StepUp and "My+Up" became My.  Match the
reported names exactly before the shortcuts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:20:49 +02:00
cjkas
bb16c90a5a refactor: move the Somfy frame codec into SomfyFrame.cpp
The command and protocol enums, somfy_frame_t and its encode/decode
methods, and both translateSomfyCommand overloads move verbatim out of
Somfy.cpp/Somfy.h, which now include SomfyFrame.h.  The codec doesn't
depend on the radio, storage or network, so on its own it can be unit
tested on the PC.

decodeFrame(somfy_rx_t*) stays in Somfy.cpp since it reads the RSSI
from the CC1101.  No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 13:18:37 +02:00
cjkas
02df2daaab fix: report half-hour time zones correctly in ISO timestamps
calcTZOffset added the leftover seconds instead of minutes to the hhmm
offset, so +05:30 came out as 2300 and ISO timestamps showed +2300.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:54:56 +02:00
cjkas
1f394ca9df fix: back-pressure web commands and report radio failures
Web commands are answered once queued, but the main loop handed all of
them to the radio at once and anything past its free slots was dropped
silently.  Unpair also cleared paired even when the Prog wasn't sent.

- processQueue leaves commands queued until the radio has room.
- Commands that transmit get a 503 straight away during a frequency
  scan instead of being dropped later.
- Unpair restores paired when the Prog can't be sent.
- Failed web commands are logged.
- Note that /setPositions and /setSensor answer with the state before
  the command; the socket events carry the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:40 +02:00
cjkas
00a084048f fix: swap restored radio config under the radio lock
Restoring a backup wrote the transceiver config in place from the async
task while the radio task could be transmitting with it.

Read the record into a copy and swap it in through the new
Transceiver::setConfig, which Transceiver::fromJSON now uses as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:40 +02:00
cjkas
352f36e478 fix: commit favorite only once SETMY is fully on air
The favorite was persisted as soon as the long My press was queued.  A
flush or radio reconfiguration could drop or cut it short and the app
kept a favorite the motor never stored.

The radio task now reports finished/aborted for every job.  Setting a
favorite goes through sendSetMy, which commits it on finished and puts
the previous one back otherwise.  The new favorite is set before the
My is sent so the simulation no longer heads for the old one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:40 +02:00
cjkas
2a45dba37a fix: cap repeats per radio job to keep loop under the WDT
The radio task bit-bangs a whole job on loop()'s core at a higher
priority.  A job with 127+ repeats ran for 18-36s without loop() ever
running and the 15s task watchdog rebooted the device.

Limit a job to 50 repeats (about 7s) in encodeTxJob, which every send
path goes through, and advance the 80-bit repeat counter by the number
of frames actually queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:40 +02:00
cjkas
44227bfd87 fix: keep rolling codes in order when a stop is queued
A priority stop always went to the front of the radio queue, ahead of
an earlier frame of the same remote that was still waiting.  The older
rolling code then went out last and the motor rejected it.

Split "may use the reserved slots" (priority) from "goes to the front"
(front) and only send a stop to the front when its remote has nothing
queued at the back.  The radio task and flushes track which back jobs
have been taken so Transceiver::isQueued() can tell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:40 +02:00
cjkas
130b5b6b0a fix: hold simulation only for shades a command starts moving
markTxPending ran inside sendCommandAfter before callers set the new
target, so isAtTarget() kept idle shades from ever being held, while
shades already moving were frozen although their motor kept running.

Hold a shade only when it was idle before the command and release the
hold when the command didn't set it moving.  The single mark in
sendCommandAfter now covers group members too, so the second pass in
SomfyGroup::sendCommand is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:46:39 +02:00
cjkas
2a54740f52 fix: never leave radio TX suspended after a scan or aborted OTA
- endFrequencyScan always resumes TX; applying radio settings ends a scan.
- Frequency scans time out after 5 minutes.
- Reboot when an OTA upload is abandoned mid-transfer.
- /sendRemoteCommand returns 503 when the radio can't take the frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:23:20 +02:00
cjkas
e55d9e48f7 fix: keep shade simulation in step with what goes on air
- Hold a shade's simulated movement until its command is on air.
- Send stops at the target as priority jobs.
- If a stop can't be sent or is dropped, simulate the run to the limit.
- Roll back targets when a movement command is dropped.
- Only store a favorite position when SETMY was queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:21:25 +02:00
cjkas
ad15ecacd4 fix: propagate radio TX result to callers instead of pre-checking
- Send functions return whether the radio took the command.
- Shade, group and tilt state only change when the command was queued.
- Rolling code is committed only after the frame is queued.
- repeatFrame advances the repeat counter only on success.
- Remove canTransmit/canQueueTx; queueTx is the single authority.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:14:29 +02:00
cjkas
a426efe53f fix: make radio TX queue flush-safe and report job outcomes
- Serialize queueTx and flushTx so a queued job can't be lost to a flush.
- Settings changes preempt only the job in flight; the queue survives.
- Flush drains the queue and reports each job as dropped.
- Track suspend reasons (scan, OTA) separately.
- Report started/dropped for every job on a single event queue.
- Add priority jobs that go to the front and may use reserved slots.
- Remove the MAX_TX_REPEATS clamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 12:08:48 +02:00
cjkas
9d16d6cc6d fix: address radio TX task review findings
- Abort in-flight transmissions on flush so config save, frequency scan
  and OTA never wait on the radio lock for a whole job
- Suspend TX during frequency scan and OTA
- Refuse commands the radio queue cannot take before consuming a rolling
  code or moving simulated shade state; shift movement start when a job
  goes on air late
- Apply radio config changes under the radio lock
- Re-check receiver idle before repeater transmissions
- Return 503 from web handlers when the command queue is full
- Restore 200 ms settle delay before the set-My command
- Report RSSI 0 instead of a stale value when the radio is busy
- Encode each frame once and centralise the repeat clamp

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-10 20:39:18 +02:00
cjkas
9a9afa5e86 fix: move radio TX to a dedicated task so HA WebSocket stays connected
Web command handlers ran on the async_tcp task and blocked up to 3s
waiting for loop() to bit-bang radio frames. async_tcp also services the
WebSocket on port 8080, so pongs stalled and Home Assistant
(ping_timeout=15) disconnected when several covers were commanded.

- Add a radio_tx FreeRTOS task with a job queue; sendFrame/repeatFrame
  and repeater frames enqueue instead of transmitting inline.
- Serialize CC1101 access with a recursive mutex (TX task, apply, end,
  frequency scan, RSSI read).
- Make Web::queueCommand fire-and-forget; drop the done semaphore.
- Route /setMyPosition and /unpairShade through the command queue.
- Clamp repeats to MAX_TX_REPEATS (40).
- Pin AsyncTCP to core 0 on dual-core targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:13:31 +02:00
github-actions[bot]
b117c34cb8 chore: bump version to v3.0.14 2026-10-10 17:01:25 +00:00
Ropheb
9f1f8f2efe Fix: remove via_device self-reference (breaks HA 2026.9.x MQTT discovery)
HA 2026.9.2 added strict device registry validation rejecting discovery
messages where via_device equals identifiers[0] ('A device can not be
its own via device'). ESPSomfy sets via_device = identifiers[0], causing
all cover entities to go unavailable on every ESP32 reboot.

via_device is for proxy/gateway relationships. ESPSomfy shades ARE the
ESP32 device — there is no proxy. Removing the field is semantically
correct and has no functional impact.

Fixes #32
2026-09-19 15:29:11 +01:00
Dennis
77f97498b0 Fix OTA update URLs to point to cjkas fork 2026-06-18 11:46:44 +02:00
github-actions[bot]
3ba27ad967 chore: bump version to v3.0.13 2026-04-22 12:21:50 +00:00
cjkas
6365f16850 Memory-bounded streaming builder for /controller. Produces the JSON payload one small unit at a time into a fixed 3KB buffer, so a chunked HTTP response can drain it as the TCP send window allows — no growing cbuf in the async response stream. 2026-04-21 14:56:20 +02:00
github-actions[bot]
f51e90bb7c chore: bump version to v3.0.12 2026-04-20 16:32:44 +00:00
cjkas
26400c534f Fix off-by-one bug in RTW protocol decoder.
The RTW decoder formula encKey - 133 was off by one versus the
RTW encoder (which maps command N to encKey N + 132). This caused
every non-My RTW command to decode as the wrong button (e.g. Up
decoded as My, Down as MyUp). Also adds rawCmd diagnostic field to
received frames for protocol debugging.
2026-04-19 11:07:53 +02:00
github-actions[bot]
3b6e1d1e67 chore: bump version to v3.0.11 2026-04-11 21:53:01 +00:00
Shailen Sobhee
b2e402e20e Fixed task_wdt: esp_task_wdt_reset(707): task not found 2026-04-11 23:48:19 +02:00
Shailen Sobhee
41270fffb8 tidier .gitignore 2026-04-11 12:26:22 +02:00
Shailen Sobhee
174f9a2287 cleaned code, as per inputs from cjkas 2026-04-11 12:08:55 +02:00
cjkas
7e6b65bc8d
Update ConfigSettings.h 2026-04-06 20:30:26 +02:00
cjkas
bb3ec52e89
Merge branch 'main' into main 2026-04-06 20:27:20 +02:00
github-actions[bot]
2ca1b8bd61 chore: bump version to v3.0.11 2026-04-06 09:02:01 +00:00
Shailen Sobhee
25f6195bbc build pass for esp32c6, added dependencies.lock 2026-04-05 21:06:48 +02:00
Shailen Sobhee
7524ce1d60 updated platformio.ini + add -D CHIP_ESP32C6 2026-04-05 20:19:23 +02:00
Shailen Sobhee
51109a16c5 updated gitignore file 2026-04-05 19:55:09 +02:00
Shailen Sobhee
3271327208 merge conflicts from 05.04 2026-04-05 19:08:01 +02:00
cjkas
3152002354 Merge branch 'main' into cjkas/wifi-crash-fix 2026-04-04 20:23:31 +02:00
cjkas
675971340e Fix wifi crash on freq scan 2026-04-04 20:21:40 +02:00
cjkas
bf0c3d37f5
Merge pull request #20 from cjkas/cjkas/fix-bkp
Update platform to espressif32 @ 6.13.0. SSID scan interval increased to 10 minutes to offload ESP.
2026-04-04 20:04:49 +02:00
cjkas
3a3ce2ae6a Update platform to espressif32 @ 6.13.0. SSID scan interval increased to 10 minutes to offload ESP. 2026-04-04 19:58:38 +02:00
github-actions[bot]
e513f94a37 chore: bump version to v3.0.10 2026-04-04 10:57:03 +00:00
cjkas
5a167e3b11 Fix bkp bug 2026-04-03 20:07:40 +02:00
Shailen Sobhee
fe4aa83ed2 updated code logic to pic FW/App version from appversion instead of hard-coding in js/html/cpp files 2026-03-30 22:41:08 +02:00
Shailen Sobhee
bb61a1703d fixed esp32-c6 platform detection issue 2026-03-30 21:04:59 +02:00