DHT (temperature and humidity) sensor is printing NaNs

Hello all,

I am doing a school project working with the Raspberry Pi, the GrovePi, Python 3, and the DHT11 (the blue) sensor. I have written my own software to read temperature data, serialize the data, and throw it on to a server. I am having trouble reading temperatures with my software that I wrote cause the software keeps on throwing type errors (throwing NaNs, or Not a Number, to be more specific).

So, I grabbed your sample file (Home Weather System file) off of GitHub as I might have done something wrong in my own software. That file also keeps on throwing NaNs as well. I have tried several ports on the GrovePi as well as well as changing the port number in the sample file.

Right now, I’m currently running the custom OS that Dexter Industries have made based off of Raspbian. Additionally, I have updated the firmware as well, just in case. I’m competent when it comes to Python, especially Python 3 as that’s the language I’m writing my software in.

I’m kind of at loss and I’m not so sure what to do except turn to the GrovePi community for assitance. Any help is greatly appreciated.

Many thanks in advance.

You are saying your code keeps throwing type errors - does that always happen or do you also get (accurate) values from the DHT?

Also, can you show me a part of your code? Maybe something’s missing.

Thank you!

Hello Robert, thank you for responding!

This type error exception always happens. I don’t get any readings from the DHT at all.

Here is a sample of my code. I copied from the sample file that Dexter Industries provided a bit but I made it fit my code for project purposes. For example, I created a temperature model and I return it.

    def read_temperature(self):
        '''
        Method to read temperature and humidity
        '''
        try:
            [temp, hum] = grovepi.dht(self.temp_sensor, self.sensor_type)

            # check if we have nans
            if math.isnan(temp) is True or math.isnan(hum) is True:
                raise TypeError('nan error')

            t = str(temp)
            h = str(hum)

            # temperature instance, store it in var, return that var
            result = temperature.Temperature(t, h)
            return result

        except (IOError, TypeError) as e:
            print(str(e))

        except KeyboardInterrupt as e:
            print(str(e))

Let me know if you find anything!