{"record":{"id":"7b5022e6b6650473","repo":"AlexxIT/go2rtc","slug":"failed-to-publish-wake-up-message-w","errorCode":null,"errorMessage":"failed to publish wake-up message: %w","messagePattern":"failed to publish wake-up message: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/tuya/mqtt.go","lineNumber":192,"sourceCode":"\n\t// Convert to hex string\n\thexStr := fmt.Sprintf(\"%08x\", crc)\n\n\t// Convert hex string to byte array (2 chars at a time)\n\tpayload := make([]byte, len(hexStr)/2)\n\tfor i := 0; i < len(hexStr); i += 2 {\n\t\tb, err := hex.DecodeString(hexStr[i : i+2])\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to decode hex: %w\", err)\n\t\t}\n\t\tpayload[i/2] = b[0]\n\t}\n\n\t// Publish to wake-up topic: m/w/{deviceId}\n\twakeUpTopic := fmt.Sprintf(\"m/w/%s\", c.deviceId)\n\ttoken := c.client.Publish(wakeUpTopic, 1, false, payload)\n\tif token.Wait() && token.Error() != nil {\n\t\treturn fmt.Errorf(\"failed to publish wake-up message: %w\", token.Error())\n\t}\n\n\t// Subscribe to lowPower topic to receive dps[149] status updates\n\t// (we don't wait for this signal - camera responds immediately)\n\tlowPowerTopic := fmt.Sprintf(\"smart/decrypt/in/%s\", c.deviceId)\n\tif token := c.client.Subscribe(lowPowerTopic, 1, c.onLowPowerMessage); token.Wait() && token.Error() != nil {\n\t\treturn fmt.Errorf(\"failed to subscribe to lowPower topic: %w\", token.Error())\n\t}\n\n\treturn nil\n}\n\nfunc (c *TuyaMqttClient) SendOffer(sdp string, streamResolution string, streamType int, isHEVC bool) error {\n\t// Map Skill StreamType to MQTT stream_type values\n\t// streamType comes from GetStreamType() and uses Skill StreamType values:\n\t// - mainStream = 2 (HD)\n\t// - substream = 4 (SD)\n\t//","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/tuya/mqtt.go#L174-L210","documentation":"After publishing the wake-up message to topic m/w/{deviceId} at QoS 1, WakeUp waits on the Paho publish token and wraps any token error in this message. It means the MQTT broker rejected or failed the PUBLISH — typically because the connection dropped or broker refused the publish. The wake-up command was not delivered.","triggerScenarios":"client.Publish(...).Wait() returns a non-nil token.Error(): broker disconnected mid-publish, keepalive expired, QoS 1 packet not acknowledged, or publish attempted while the client is disconnected/closed.","commonSituations":"Network drops between the device and broker, broker restarts, or calling WakeUp on a client whose MQTT connection already died; long-idle low-power camera clients often have stale connections.","solutions":["Check client.IsConnectionOpen() (or track the OnConnectionLost callback) before publishing","Reconnect the MQTT client (Reconnect/resubscribe) and retry WakeUp once","Verify broker reachability and TLS/auth settings used at Connect time","Ensure the client isn't closed; sending after Close() will fail similarly"],"exampleFix":"// before\nerr := client.WakeUp(deviceID)\n// after\nif !client.IsConnectionOpen() {\n    if cerr := reconnect(client); cerr != nil { return cerr }\n}\nerr := client.WakeUp(deviceID)\nif err != nil && strings.Contains(err.Error(), \"publish wake-up\") {\n    err = retryWakeUp(client, deviceID)\n}","handlingStrategy":"retry","validationCode":"if client == nil || !client.IsConnectionOpen() { return errors.New(\"mqtt not connected\") }","typeGuard":null,"tryCatchPattern":"err := client.WakeUp(deviceID)\nif err != nil && strings.Contains(err.Error(), \"publish wake-up\") {\n    if cerr := reconnect(client); cerr == nil {\n        err = client.WakeUp(deviceID) // one retry\n    }\n}","preventionTips":["Track OnConnectionLost and proactively reconnect","Add keepalive/ping settings appropriate to low-power links","Bound retries with backoff to avoid publishing into a dead broker"],"tags":["go","mqtt","publish","network","tuya"],"backgroundTag":"http-request-failed","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}