Particle.publish() data attribute is sometimes zero

The photon reads temperature and humidity using a DHT11 sensor.
I measure a sensor 5 times and publish the mean (+100 for debugging) of the readings using Particle.publish().

I would then expect that if the computations or the sensor readings would be zero, then the published value would be 100, but instead I see that the published value is 0;

Here is a graph of the published values:

How do I fix this problem?

Put the measured values in the array and then make the elimination of the minimum value of the series. Then the resulting new array divide it by the number of members of the array.

The mean of non-negative values are only zero iff all values are zero thus it can not be the computation of mean. Also I have added 100 after computing mean. This is neither visible in the published data.

How are you looking at the data? Are you subscribing to the event on another device, using the console, or the CLI?

The event is propagated to a backend using hooks. The backend also produces the graphs. The problem seems to occur before reaching the hook, as I see 0 values in the web particle console.

maybe something happening in your code… overflowing an array or some integer division causing unexpected results.

can we have a look at that part of your code?

Do you publish PRIVATE and subscribe MY_DEVICES?
What is your event name?
What do the “raw” console logs look like for the erronous readings?

Most likely problem is a library for reading the sensor.
See through a serial connection if the problem is the reading of the sensor.
The problem initiates a problem with the initialization of the array, if anywhere using memset or using a timer. We need to see the code.

I currently add 100 to all values published for debug.

I don’t assume that the sensors or library is the problem, but here you have the associated code (and some of it dependencies).

Here are the dependencies:

I don’t think this has anything to do with your problem, but your mean function has a problem. The sizeof function within that function will give you the size of the pointer to the array, which will be 4. You need to pass in the array count to that function to have it behave as you expect.

3 Likes