MQTT client subscriber : How to break a loop by receiving a new message from a publisher?

Can someone help me on that ?
I try to break a timelapse (written as looping a function) on my RPI started with a message “StartScanning”, by receiving a “Quit” message.
I am sorry for my knowledge on programming, but I am really stuck at this last step!
Here the code block of the subscriber ( which is the RPI and its PiCamera )

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import time
import paho.mqtt.client as mqtt
from time import sleep
from datetime import datetime, timedelta
from picamera import PiCamera
import subprocess
import pathlib
import socket
camera = PiCamera()
Broker = "192.168.1.100"
Port = 1883
# topics
pub_topic = "DIC/scanning"
sub_topic = "DIC/keyboard"
# on connect
def on_connect(mqttc, obj, flags, reason_code):
print(f"rc :" + str(reason_code))
mqttc.subscribe(sub_topic)
# on message
def on_message(mqttc, obj, msg):
print(str(msg.topic) + " " + str(msg.qos) + " " + str(msg.payload))
if if msg.payload.decode() == "StartScanning":
while True:
timelapse()
elif msg.payload.decode() == "Quit" :
timelapse.hasbeencalled=False
#fonction scanning avec timelapse
def timelapse():
hostName = socket.gethostname()
pathlib.Path('/home/pi/Desktop/DIC_' + hostName).mkdir(parents=True,exist_ok=True)
fileName= datetime.now().strftime("%Y%m%d_%H-%M-%S-%f")+".jpg"
camera.resolution = (2592, 1944) #picture resolution
camera.capture('/home/pi/Desktop/DIC_' + hostName + '/' + fileName)
#camera.capture('/home/pi/Desktop/DIC_RPI1/RPI1_' + fileName)
sleep(1)
#on subscribe
def on_subscribe(mqttc, obj, mid, reason_code_list):
print("Subscribed: " + str(mid) + " " + str(reason_code_list))
#on log
def on_log(mqttc, obj, level, string):
print(string)
# on publish
def on_publish(mqttc, obj, mid):
print(f"risposta inviata con message id:" + str(mid))
# connect MQTT client
mqttc = mqtt.Client() #(mqtt.CallbackAPIVersion.VERSION1)
mqttc.on_subscribe = on_subscribe
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_publish = on_publish
mqttc.connect("192.168.1.100", 1883, 60)
#mqttc.subscribe(sub_topic)
mqttc.loop_forever()
</code>
<code>import time import paho.mqtt.client as mqtt from time import sleep from datetime import datetime, timedelta from picamera import PiCamera import subprocess import pathlib import socket camera = PiCamera() Broker = "192.168.1.100" Port = 1883 # topics pub_topic = "DIC/scanning" sub_topic = "DIC/keyboard" # on connect def on_connect(mqttc, obj, flags, reason_code): print(f"rc :" + str(reason_code)) mqttc.subscribe(sub_topic) # on message def on_message(mqttc, obj, msg): print(str(msg.topic) + " " + str(msg.qos) + " " + str(msg.payload)) if if msg.payload.decode() == "StartScanning": while True: timelapse() elif msg.payload.decode() == "Quit" : timelapse.hasbeencalled=False #fonction scanning avec timelapse def timelapse(): hostName = socket.gethostname() pathlib.Path('/home/pi/Desktop/DIC_' + hostName).mkdir(parents=True,exist_ok=True) fileName= datetime.now().strftime("%Y%m%d_%H-%M-%S-%f")+".jpg" camera.resolution = (2592, 1944) #picture resolution camera.capture('/home/pi/Desktop/DIC_' + hostName + '/' + fileName) #camera.capture('/home/pi/Desktop/DIC_RPI1/RPI1_' + fileName) sleep(1) #on subscribe def on_subscribe(mqttc, obj, mid, reason_code_list): print("Subscribed: " + str(mid) + " " + str(reason_code_list)) #on log def on_log(mqttc, obj, level, string): print(string) # on publish def on_publish(mqttc, obj, mid): print(f"risposta inviata con message id:" + str(mid)) # connect MQTT client mqttc = mqtt.Client() #(mqtt.CallbackAPIVersion.VERSION1) mqttc.on_subscribe = on_subscribe mqttc.on_connect = on_connect mqttc.on_message = on_message mqttc.on_publish = on_publish mqttc.connect("192.168.1.100", 1883, 60) #mqttc.subscribe(sub_topic) mqttc.loop_forever() </code>
import time
import paho.mqtt.client as mqtt

from time import sleep
from datetime import datetime, timedelta
from picamera import PiCamera
import subprocess
import pathlib


import socket

camera = PiCamera()

Broker = "192.168.1.100"
Port = 1883

# topics
pub_topic = "DIC/scanning"
sub_topic = "DIC/keyboard"



# on connect
def on_connect(mqttc, obj, flags, reason_code):
    print(f"rc :" + str(reason_code))
    mqttc.subscribe(sub_topic)
    
# on message
def on_message(mqttc, obj, msg):
    print(str(msg.topic) + " " +  str(msg.qos) + " " + str(msg.payload))
        
    if if msg.payload.decode() == "StartScanning":
        while True:
             timelapse()
    elif msg.payload.decode() == "Quit" :
        timelapse.hasbeencalled=False
            

#fonction scanning avec timelapse
def timelapse():
    hostName = socket.gethostname()
    pathlib.Path('/home/pi/Desktop/DIC_' + hostName).mkdir(parents=True,exist_ok=True)
    fileName= datetime.now().strftime("%Y%m%d_%H-%M-%S-%f")+".jpg"
    camera.resolution = (2592, 1944) #picture resolution
    camera.capture('/home/pi/Desktop/DIC_' + hostName + '/' + fileName)
    #camera.capture('/home/pi/Desktop/DIC_RPI1/RPI1_' + fileName)
    sleep(1)


#on subscribe
def on_subscribe(mqttc, obj, mid, reason_code_list):
    print("Subscribed: " + str(mid) + " " + str(reason_code_list))
    
    
#on log
def on_log(mqttc, obj, level, string):
    print(string)
    
# on publish
def on_publish(mqttc, obj, mid):
    print(f"risposta inviata con message  id:" + str(mid))
    
# connect MQTT client
mqttc = mqtt.Client() #(mqtt.CallbackAPIVersion.VERSION1)

mqttc.on_subscribe = on_subscribe
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_publish = on_publish

mqttc.connect("192.168.1.100", 1883, 60)
#mqttc.subscribe(sub_topic)

mqttc.loop_forever()

I tried to insert inside the function timelapse() a ‘ while msg.payload==”StartScanning”: ‘ but it didn’t work.
I also tried to publish the 2 messages “StartScanning” and “Quit” on two topics and subscribe o them, but since the loop function runs, I can’t receive other message from a publisher.

New contributor

Arthur Bohn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật