0

I'm new to python and wrote this script for an IoT app. I want to use thread in while loop. Actually I want to use threading before waiting time (9.98). I want lines 34-38 (from PID = random.randint) to be executed by thread. Is this possible?



import paho.mqtt.client as mqtt
import time
import random
import thread
brokerAddress = "foobar.s2.eu.hivemq.cloud"
userName = "username"
passWord = "password"
topic = "Sensor"
#The callback for when the client recives a connack response from broker
def on_connect(client, userdata, flags, rc):
    
    if rc == 0:
        print("Connection Established")
    else:
        print("Coneect returened result code: " + str(rc))
def on_message(client, userdata, msg):
        print("Received message: " +msg.topic + " -> " + msg.payload.decode("utf-8"))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
client.username_pw_set(userName, passWord)
print("Connecting to broker::",brokerAddress)
client.connect(brokerAddress, 8883)
client.loop_start()
time.sleep(2)

while True:
    try:
        input("press enter to put the box on line:")
    except SyntaxError:
        pass
    PID = random.randint(0 , 2000000)
    print(PID)
    print("wait for the box to arrive to sensor:9.980")
    time.sleep(9.980)
    client.publish(topic, PID)
    print("..............................................................")

client.loop_stop()

Output from the above is:

output first (enter):
Connecting to broker:: d615ed48421a497f892c98c1b04cfb82.s2.eu.hivemq.cloud
Connection Established
press enter to put the box on line:
168488

Subscriber is:

import paho.mqtt.client as mqtt
import datetime
import time

brokerAddress = "foobar.s2.eu.hivemq.cloud"
userName = "user"
passWord = "password"
topic = "Sensor"
#the callback for when the client recieves a connack response from broker
def on_connect(client, userdata, flags, rc ):
    if rc == 0:
        print("Connection Established")
    else:
        print("Connect retured result code: " +str(rc))
        
#the calback from when a Publish message is received from the broker
def on_message(client, userdata, msg):
        print("Received message: " +msg.topic + " -> " + msg.payload.decode("utf-8"))
        #wait for actuactor
        time.sleep(.02)
        Prduction_time = datetime.datetime.now()
        print("production Time",Prduction_time)    
#create the client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
client.username_pw_set(userName, passWord)
print("Connecting to broker::",brokerAddress)
client.connect(brokerAddress, 8883)

client.subscribe(topic)

client.loop_forever()
Brits
  • 14,829
  • 2
  • 18
  • 31
  • 2
    Yes - it's possible. You are already starting the network loop in a separate thread (`client.loop_start()`) but then, for a reason I don't understand, stop it (`client.loop_stop()`)before entering your loop. Please edit your question and add a bit more information about your end goal (is there some other code you want to run concurrently with the loop?). – Brits Feb 09 '23 at 20:40
  • 1
    I really hope those are not valid username/passwords in the supplied code... – hardillb Feb 09 '23 at 21:34
  • I apologize for my lack of English. i edit the code. in publisher code every time i press enter i should wait 9.98 sec for the next. I want this delay to exist, but not between pressings. In other words, I want it to be passed to the thread the moment I hit enter, so that I can hit enter again and again – mohamad mohamady Feb 09 '23 at 23:19
  • OK - what have you tried? There are [other questions](https://stackoverflow.com/a/2906014/11810946) that provide examples you should be able to apply here. – Brits Feb 09 '23 at 23:28

0 Answers0