Part 4 of Many DHT22 and memory leak

Hi All, back again. This time the culprit is memory overflow/leak. So basically I have stripped down my code back to where it only reads the DHT22 which works fine. However, the RAM on my Pi maxes out after about 24hours running the DHT22 script. This is been the base of all my issues.

I mean guys are sending Pi’s to the moon and I can’t get a script to run more than 24hours!! do I reload Rasbian or what? do we have an event logger to see why the ram filles up or do I clear variables I am stumped?

you can use top command to see what is causing the issue.

Thanks will have a look.


I had a look at “top” and the result was this after a couple of hours:

The “memory gauge” on cayenne shows mid 500mb used and then the script stops.

But i guess this is because of the DHT code polling too fast. what is the delay you have in the code.

#!/usr/bin/env python
import cayenne.client as cayenne
import time
import Adafruit_DHT
#import logging
#import math

time.sleep(60) #Sleep to allow wireless to connect before starting MQTT

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.

sensor = Adafruit_DHT.DHT22
pin = 17
leaftemp = 1.5

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID) #, loglevel=logging.INFO)

timestamp = 0

while True:
    if (time.time() > timestamp + 5):
        humidity22, temp22 = Adafruit_DHT.read_retry(sensor, pin)
        if temp22 is not None:
            client.virtualWrite(1, temp22, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS ) #change all channels 
            print("temp error")
        if humidity22 is not None:
            client.virtualWrite(2, humidity22, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
            print("humidity error")
        '''if temp22 is not None and humidity22 is not None:
            client.virtualWrite(3, vpd, "pressure", "kPa")
            print("vpd error")
        if temp22 is not None and humidity22 is not None:
            ltemp22 = temp22 - leaftemp
            asvp= 6.1078*math.exp(temp22/(temp22+238.3)*17.2694)
            lsvp= 6.1078*math.exp(ltemp22/(ltemp22+238.3)*17.2694)
            lvpd= (lsvp -(asvp*humidity22/100))/10
            client.virtualWrite(4, lvpd, "pressure", "kPa")
            print("lvpd error")     '''   
        timestamp = time.time()

change to:

if (time.time() > timestamp + 15) or maybe even higher value.

Will this not postpone the issue ? Surely there should be a reason why this is happening.

the issue can be anything dht raspbbery stops after sometime - Google Search

That looks like the Cayenne agent, not the script. Is the Cayenne agent installed? Also, i would just remove that block of if statements in the triple single quote if you’re not using it.

1 Like