Python Script Stopping after a while

Hi All, Currently tinkering to get the basics right before I dive deeper into the inner workings of Cayenne. I used the following as my “hello world” introduction to Cayenne:

(https://iotdesignpro.com/projects/humidity-and-temperature-monitoring-over-cloud-using-raspberry-pi-and-cayenne)

What I did was created the *.py file on the “desktop” location on my PI3. I then excecute the script via the terminal in putty, and it works great, both channels displays on my dash board in my devices in cayenne. However after about 15 - 20min the script stops running as if it times out or someting.

Have anyone experienced this before ?

Regards

you can replace paho-mqtt with cayenne python library https://github.com/myDevicesIoT/Cayenne-MQTT-Python. in this, you don’t have to create the MQTT topic manually, but use the client.virtualWrite to publish the data.

Hi Shramik,

Thanks for help I will definitely give this a try tonight. Another question is: where is the local library for Cayenne located on the PI ? The one that contains all the other sensor scripts? Assume my Rasbian / python knowledge is elementary as my background is C and microcontrollers MIcrochip and Atmel and the odd FPGA.

Thanks

are you referring to cayenne pi agent or MQTT python library?

Cayenne Pi Agent, basically where would I go to access or modify existing code related to sensors that are already included within the Cayenne API

you can get the location by running this commands in python interpreter:

 import myDevices.sensors.sensors
 print(myDevices.sensors.sensors.__file__)

So I tried doing what you suggested, but being a noob could not get it to work. I can see the code doing what it should as it pops up in my terminal when I execute the command. However, the sensor never goes active in Cayenne.

As a temporary solution, I edited the sudo crontab -e with:

#runs script when pi reboots
@reboot python /home/pi/python/Tomato/DHT11.py &

#reruns script every hour

  • 1 * * * python /home/pi/python/Tomato/DHT11.py &

Ill read up some more on what you suggested above with using a different library.

can you share the new code that you used with cayenne MQTT library.

To be honest its a question of not really knowing what I am doing, trying to read - implement - try - fail - read, you know the normal way of learning new skills:

import cayenne.client as cayenne
import time
import Adafruit_DHT


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

MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""

client = cayenne.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=1883)

timestamp = 0

while True:
    client.loop()
    if (time.time() > timestamp + 5):
        humidity11, temp11 = Adafruit_DHT.read_retry(11, 17) #11 is the sensor type, 17 is the GPIO pin number that DATA wire is connected to

        if temp11 is not None:
            client.virtualWrite(1, temp11, cayenne.TYPE_TEMPERATURE, cayenne.UNIT_CELSIUS)
        if humidity11 is not None:
           client.virtualWrite(2, humidity11, cayenne.TYPE_RELATIVE_HUMIDITY, cayenne.UNIT_PERCENT)
        timestamp = time.time()

Do I need to change anything on the API side ?

Sooo… I believe I never modified the crontab with the new script, it looks like its working now. I am receiving data from the PI to lthe API. I will monitor it and give feedback.

Thanks so much for the help and pointing me in the right direction

1 Like

So far so good !!

2 Likes