Hypfer / esp8266-vindriktning-particle-sensor

Wifi MQTT Data Logging via an esp8266 for the Ikea VINDRIKTNING PM2.5 air quality sensor
Apache License 2.0
1.18k stars 128 forks source link

Received message with invalid header. #41

Open CountOlaf87 opened 3 years ago

CountOlaf87 commented 3 years ago

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 commented 3 years ago

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

rublinetsky commented 2 years ago

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 https://github.com/Hypfer/esp8266-vindriktning-particle-sensor/blob/83fd7c5034cc9737b954c5625426563492d83a2c/src/SerialCom.h#L92-L94 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 https://github.com/Hypfer/esp8266-vindriktning-particle-sensor/blob/83fd7c5034cc9737b954c5625426563492d83a2c/src/SerialCom.h#L85-L95

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 commented 2 years ago

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: https://github.com/Hypfer/esp8266-vindriktning-particle-sensor/issues/20#issuecomment-950218909