Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update remote.cpp to allow parsing JSON over ESP NOW #4084

Open
wants to merge 3 commits into
base: 0_15
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions wled00/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void handleRemote(){}
// This is kind of an esoteric strucure because it's pulled from the "Wizmote"
// product spec. That remote is used as the baseline for behavior and availability
// since it's broadly commercially available and works out of the box as a drop-in

typedef struct message_structure {
uint8_t program; // 0x91 for ON button, 0x81 for all others
uint8_t seq[4]; // Incremental sequence number 32 bit unsigned integer LSB first
Expand Down Expand Up @@ -114,7 +115,18 @@ static void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t palet
unloadPlaylist();
applyPresetWithFallback(presetID, CALL_MODE_BUTTON_PRESET, effectID, paletteID);
}


bool parsePayload(const char* jsonStr) {
// Use WLED's built-in JSON handling functions
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, jsonStr);
if (error) {
return false;
}
JsonObject root = doc.as<JsonObject>();
return deserializeState(root, CALL_MODE_BUTTON);
}

// Callback function that will be executed when data is received
#ifdef ESP8266
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
Expand All @@ -134,6 +146,17 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
if (len != sizeof(incoming)) {
DEBUG_PRINT(F("Unknown incoming ESP Now message received of length "));
DEBUG_PRINTLN(len);

char jsonStr[len + 1];
memcpy(jsonStr, incomingData, len);
jsonStr[len] = '\0';

if (!parsePayload(jsonStr)) {
DEBUG_PRINTLN(F("Failed to parse JSON"));
return;
}

stateUpdated(CALL_MODE_BUTTON);
return;
}

Expand All @@ -144,7 +167,6 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
return;
}


DEBUG_PRINT(F("Incoming ESP Now Packet["));
DEBUG_PRINT(cur_seq);
DEBUG_PRINT(F("] from sender["));
Expand All @@ -162,7 +184,6 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
case WIZMOTE_BUTTON_BRIGHT_UP : brightnessUp(); stateUpdated(CALL_MODE_BUTTON); break;
case WIZMOTE_BUTTON_BRIGHT_DOWN : brightnessDown(); stateUpdated(CALL_MODE_BUTTON); break;
default: break;

}

last_seq = cur_seq;
Expand Down
Loading