Hi guys. I have huge problem with my code. Try to publish data by paho-mqtt (localhost, port=1883).
Blender is not frozen but I don’t receive anything on:
mosquitto_sub -v -t ‘#’ -h localhost -p 1883.
My code looks like that:
explain to me why you need an infinite loop to send three values and sleep for five seconds.
just run the script periodically and be done with it. put the sensor on repeat and have it wait between positive pulses. if you need the reference to the client assign it to a property. if you need to make sure the cleanup runs then bind it to the escape key.
so run the update method through this setup. initialization through the always like you already do. then terminate the client when exit key is pressed.
as for saving a reference to the client, simple as
Are you getting any errors in the console? Firstly, I don’t believe paho is an included module in blender. Are you using a custom build or install it into blender some other way? Secondly, your code should error on line 13 - ‘bge’ is not defined as you are importing ‘logic’ from the ‘bge’ module and not ‘bge’ itself. so you can delete the ‘bge.’ from that and simply use logic.getCurrentScene()
Further:
As Liebranca pointed out, you can run your script periodically by enabling pulse mode and setting the skip parameter equal to your desired period - 1.5 seconds = 90 ticks. So, no need for threading unless you have another use for it.
To create your client and hold the reference you would need to add an initialization to your ‘main’ function, such as:
# Get reference to our game object - use 'owner' to refer to self
owner = logic.getCurrentController().owner
def main():
if not 'client' in owner:
# This runs only once
owner['client'] = mqtt.Client("relayr")
# This runs every time main is called
owner['client'].connect("localhost", 1883)
...
owner['client'].disconnect()
If this run on the cube itself, you can use ‘owner’ when referring to the position instead of ‘cube’ (or you can call the owner ‘cube’ if you wish)
Finally, I will mention that ‘position’ is deprecated and would recommend using ‘worldPosition’ or ‘localPosition’ instead - owner.worldPosition.x
Guys, thanks everyone for help. For now i combine solution from Liebranca and sodsm_live and it works for me!! In near future I wanna set some service for this and just for training will try to do this threading using WKnight02 solution. When I will finish it will share with you Thanks once again