mardi 29 janvier 2019

Timer jumps randomly from 2 seconds to 50

I want to connect my raspberry pi with my computer. The connection works fine but now I want to check if the computer sends messages and after a couple of minutes without a message the raspi should shutdown. If I start the program manually it works fine but i want to start it on autostart. If I do so the timer jumps from (for example) 2 seconds to 50 and I dont know why.

My Code looks like this:

<!-- language: lang-python -->
import threading
import time
import paho.mqtt.client as mqtt

shutdown = 0

def on_connect(client, userdata, flags, rc):
   print("Connected with result code "+str(rc))
   client.subscribe("CoreElectronics/test")
   client.subscribe("CoreElectronics/topic")

def on_message(client, userdata, msg):
   global shutdown
   shutdown = time.time()
   print(msg.topic+" "+str(msg.payload))

def timer_count():
    global shutdown
    shutdown = time.time()
    elapsed = 0
    while elapsed < 10:
         elapsed = time.time()-shutdown
         print("no Message")
         time.sleep(2)

t1 = threading.Thread(target = timer_count)
t1.start()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.xx.xx", 1883, 60)


client.loop_forever()


I printed out the time.time() Funktion and there it randomly jumps 50 seconds ahead. Does somebody knows an answer to this ? Thank you for the help!




Aucun commentaire:

Enregistrer un commentaire