Jun-21-2018, 07:42 PM
(This post was last modified: Jun-21-2018, 07:42 PM by hwa_rang098tkd.)
I am trying to establish a mqtt client on my pc, So I start writing a script in python. When I run the script from Python IDLE it works fine and stable. I downloaded AlwaysUp and created a service. When I run the service it stops after 5s.
Error by event viewer are 3 at a try . Two with ID "2" and one with ID "7023".
Also I tried with runassvc from pirmasoft and failed: error msg : "the mqtt_commands1 service on local computer started and then stopped. Some services stop aytomatically if the are not in use by other services or programs"
help plz!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import paho.mqtt.client as mqtt #import the client1 import time import os strshtdwn = 0 def on_log(clint, userdata,level, buf): print ( "log: " + buf) def on_connect(client,userdata, flags ,rc): if rc = = 0 : print ( "Connected OK" ) else : print ( "Bad connection Returned code=" ,rc) def on_disconnect(client, userdata, flags, rc = 9 ): print ( "DisConnected result code " + str (rc)) def on_message(clinet, userdata, msg): topic = msg.topic m_decode = str (msg.payload.decode( "utf-8" , "ignore" )) if m_decode = = "SHUTDOWN" : os.system( "shutdown /p" ) client.publish( "stat/hwa_rang_pc/POWER" , "Shuting Down" ) elif m_decode = = "RESTART" : os.system( "shutdown /r" ) client.publish( "stat/hwa_rang_pc/POWER" , "Restarting" ) elif m_decode = = "LOGOFF" : os.system( "shutdown /l" ) client.publish( "stat/hwa_rang_pc/POWER" , "Logging OFF" ) print ( "message received: " ,m_decode) print ( "strshtdwn" ,strshtdwn) broker = "192.168.1.3" client = mqtt.Client( "Hwa_Rang_PC" ) #create new instance client.on_connect = on_connect client.on_disconnect = on_disconnect #client.on_log=on_log client.on_message = on_message print ( "Connecting to broker " ,broker) client.connect(broker) #connect to broker client.loop_start() client.subscribe( "cmnd/hwa_rang_pc/power" ) #client.loop_stop() #client.disconnect() #disconnect |
Also I tried with runassvc from pirmasoft and failed: error msg : "the mqtt_commands1 service on local computer started and then stopped. Some services stop aytomatically if the are not in use by other services or programs"
help plz!!!