DHT22/AM2302 piettetech goes stale

I’m pretty much using the example code from the library but after a short to medium period of time, the AM2302 is just returning the same measurement.

I’ve changed from a 10k resistor to a 4.7k one. I have replaced the AM2302 as well.

I have a AM2302 on a RPi that is not having similar issues.

Thoughts?

Thank you

Hi @martingehrke

It looks like the code has some nice sprintf published messages that should help debug this problem. What are you seeing in the published STATUS?

This code in loop():

       tfs = String(tf,2);
       //String tcs(tc,2);
       hs = String(h,2);

will eventual cause heap problem since you are allocating new memory every time you go through this section. The old memory does get reclaimed but can get fragmented over time. These are pretty small strings so I don’t think that is your main problem.

I’m not sure this is related to your problem, but the Particle.variable function isn’t being used properly. You should call Particle.variable once during setup(). For a String, the String object should be a global variable. You don’t notify the system firmware when the value changes; it automatically queries the current value of the underlying variable when it’s requested.

2 Likes

Wow! Do I need more coffee this morning! Thanks @rickkas7.

@martingehrke your code cannot work in this way. The message to cloud describing variables and functions is currently only sent during the setup() function.

1 Like

Thank you, I think I had that at some point but was confused.

I will fix. THank you.

How could this be better accomplished?

Hi @martingehrke

I am assuming you mean the String part of my comments. It is possible to use String object without using more memory by setting them to empty and then appending what you want into the String, but it is hard to get it completely correct.

That said I would just declare a global char array and sprintf into it in the exact same way your code does for the STATUS published messages, except the char array should be global in scope (i.e. above all functions where you have the String objects declared right now).

1 Like