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

Received message with invalid header. #41

Open
CountOlaf87 opened this issue Nov 22, 2021 · 3 comments
Open

Received message with invalid header. #41

CountOlaf87 opened this issue Nov 22, 2021 · 3 comments

Comments

@CountOlaf87
Copy link

Hi,

I have uploaded to a D1 Mini, it connects to my wifi, I get the status Online message in MQTT, together with the air filter and wifi message, however, it doesn't show any values in MQTT and I can't see it in Home Assistant. When I look at the Serial output of the arduino IDE, I get this messag: "Received message with invalid header."

Occasionally it does show a reading:

Received PM 2.5 reading: 42
Current measurements: 42, 39, 39, 40, 40

Thanks in advance

@wvtbg
Copy link

wvtbg commented Nov 23, 2021

This is shown when the data received from the particle meter is not as expected. Is your soldering correct and reliable?

@rublinetsky
Copy link

I had the same problem using ESP32 and the Hardware Serial2.

The problem is caused by the fact that, depending on how often you poll the sensor library (SerialCom::handleUart(state)), serialRxBufreceives multiple messages at a time (20 bytes each) and their combined length is often over 64 bytes. For example, I've just received 16 11 0B 00 00 00 31 00 00 03 9A 00 00 00 2F 01 00 00 00 D0 16 11 0B 00 00 00 32 00 00 03 9B 00 00 00 30 01 00 00 00 CD 16 11 0B 00 00 00 33 00 00 03 9E 00 00 00 31 01 00 00 00 C8 16 11 0B 00 00 00 33 00 00 03 9F 00 00 00 31 01 00 00 00 C7 16 11 0B 00 00 00 33 00 00 03 9F 00 00 00 31 01 00 00 00 C7 , which is 5 messages in a row.

The code

if (rxBufIdx >= 64) {
clearRxBuf();
}

clears the buffer after 64 bytes thus deleting three whole messages (3 x 20 bytes) and the 4 bytes of the next message. What is left is not a valid message because the header was chopped off.

Anyway, the fix seems to be replacing the loop

while (sensorSerial.available()) {
serialRxBuf[rxBufIdx++] = sensorSerial.read();
Serial.print(".");
// Without this delay, receiving data breaks for reasons that are beyond me
delay(15);
if (rxBufIdx >= 64) {
clearRxBuf();
}
}

with

while (sensorSerial.available() && rxBufIdx < sizeof(serialRxBuf))
{
    serialRxBuf[rxBufIdx++] = sensorSerial.read();
    Serial.print(".");
}

Works fine for me after this change. I've not created a pull request because I am not sure why the author used this clearing of the buffer in reading process - probably there is a good reason that I don't see.

@Volcano-28
Copy link

I had the same problem. I tried to reupload sketch several times, but the problem was in incorrect configuration to the Home Assistant (mqtt). After reuploading the sensor didn't create AP again (allowing to reconfigure settings).
As I saw the mqtt configuration is stored in the flash and used even if I reupload the sketch.
The solution is to erase the flash, like described here:
#20 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants