Try this...
Here's the Node-RED flow to send a directive...
[{"id":"332f5e2948396c68","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"0f465be5ce4f96e5","type":"mqtt out","z":"332f5e2948396c68","name":"","topic":"topic/sub","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7002d750.c4462","x":700,"y":260,"wires":[]},{"id":"ca67fabdf76b653b","type":"inject","z":"332f5e2948396c68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":260,"wires":[["5310c8f218f69d9f"]]},{"id":"5310c8f218f69d9f","type":"function","z":"332f5e2948396c68","name":"prepare directive","func":"msg.payload = { \"request_report\": \"anything\" };\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":260,"wires":[["0f465be5ce4f96e5"]]},{"id":"7002d750.c4462","type":"mqtt-broker","name":"","broker":"192.168.1.156","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
And here's the modified Micro-Python script to detect the directive sent from Node-RED.
Note: You need to import ujson.
import utime
import network
import ujson
from umqtt.simple import MQTTClient
# Wi-Fi configuration
WIFI_SSID = "***"
WIFI_PASSWORD = "***"
# MQTT configuration
MQTT_BROKER = "**************"
MQTT_CLIENT_ID = "ESP32"
MQTT_TOPIC_SUBSCRIBE = "topic/sub"
MQTT_TOPIC_PUBLISH = "topic/pub"
# Connect to Wi-Fi
def connect_to_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("Connecting to Wi-Fi...")
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
pass
print("Wi-Fi connected:", wlan.ifconfig())
# MQTT message callback
def mqtt_callback(topic, msg):
print("Received message on topic {}: {}".format(topic, msg.decode()))
mqtt_directives = dict(ujson.loads(msg.decode("utf-8")))
print("Recd directive is:", mqtt_directives)
#Then decode the directive that has been sent
# Connect to MQTT broker
def connect_to_mqtt():
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(MQTT_TOPIC_SUBSCRIBE)
print("Connected to MQTT broker")
return client
# Main function
def main():
connect_to_wifi()
mqtt_client = connect_to_mqtt()
# Publish data
last_publish_time = utime.time()
publish_interval = 5 # Publish every 5 seconds
while True:
current_time = utime.time()
# Non-blocking delay
if current_time - last_publish_time >= publish_interval:
data = "Hello, MQTT!"
mqtt_client.publish(MQTT_TOPIC_PUBLISH, data)
print("Published data:", data)
last_publish_time = current_time
# Check for incoming messages
mqtt_client.check_msg()
utime.sleep_ms(100) # Sleep for 100 milliseconds
if __name__ == "__main__":
main()