Add JSON data handling over ESP-NOW in espNowReceiveCB
I'm building a custom remote and I want to control WLED with it. The WIZMote packet works great, but it isn’t easily customizable for my needs.
This PR adds support for sending JSON API commands over ESP-NOW. ESP-NOW limits packets to 255 bytes, but this is more than enough for remote commands.
For security, JSON parsing only happens when the sender (the remote) is present in the trusted device list, and only when the received payload starts with {
Summary by CodeRabbit
-
New Features
- ESP-NOW wireless protocol now supports receiving JSON-formatted data packets, allowing your devices to exchange structured state information and configuration updates in a standardized format. This enhancement enables improved remote device management capabilities, more flexible control options, and better synchronization across your entire network of connected devices.
✏️ Tip: You can customize this high-level summary in your review settings.
Walkthrough
The ESP-NOW receive callback in wled00/udp.cpp is enhanced to handle JSON data. When incoming ESP-NOW packets begin with '{', the code deserializes the data as JSON and applies it via the existing state deserialization mechanism, then returns early to skip further processing.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
ESP-NOW JSON handler wled00/udp.cpp |
Added conditional logic to ESP-NOW receive callback to detect and process JSON-formatted data. Acquires JSON buffer lock, deserializes incoming data as JSON document, applies deserialized state, releases lock, and returns early if successful. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
- Thread safety of JSON buffer lock acquisition and release pattern in the callback context
- JSON deserialization error handling and robustness when receiving malformed data
- Interaction between deserializeState and the existing ESP-NOW packet handling flow
- Potential edge cases where packets starting with '{' but not valid JSON could cause issues
Pre-merge checks and finishing touches
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The PR title accurately and specifically describes the main change: adding JSON data handling to the ESP-NOW receive callback function. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
✨ Finishing touches
- [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.