Node-Red AIML Mosquitto

Hi guys I have a problem between Node-Red and Python communicating through the broker Mqtt mosquitto. The problem is that I cannot convert the message.payload sent by the broker to python to pass it as simple user input.

Code Example:

#!/usr/bin/python3
import os
import sys
import json
import io
import aiml

def on_message(client, userdata, message):
print(str(message.payload.decode("utf-8")))
#MQTT
from paho.mqtt.client import *
client = Client(client_id = "IA")

client.connect("localhost")

client.loop_start()
client.subscribe("domanda")
client.on_message=on_message

if os.path.exists(BRAIN_FILE):
print("Carico intelligenza artificiale: " + BRAIN_FILE)
k.loadBrain(BRAIN_FILE)
else:
print("Carico file AIML")
k.bootstrap(learnFiles="std-startup.aiml", commands="load aiml b")
print("Salvo file intelligenza Artificiale: " + BRAIN_FILE)
k.saveBrain(BRAIN_FILE)

Inserimento domanda

while True:

input_text = input("Dimmi... ")

#Non mi passa richiesta Broker
**response = k.respond(input_text)**

#Editare per escludere risposta a terminale
print(response)

#Invia Risposta a Broker
client.publish(topic = "risposta", payload = (response))

If I print on the screen "print (str (message.payload.decode (" utf-8 ")))" the message is displayed correctly, if I try to send it as input "response = k.respond (str (message.payload.decode ("utf-8")) "I'm getting errors
in summary I should send a string value with subscribe Mosquitto and send it to python as if it were user input. Thank you all for any help

I am not a programmer, but where did you import paho.mqtt.client?

In my code I have this at the start:

import time
import sys
import paho.mqtt.client as mqtt
import blinkt

Yours seems to be missing that line.

But I shall restate:
I am not a programmer

from paho.mqtt.client import *
client = Client(client_id = "IA")

if you look at the code it is imported after:
#MQTT
I do it to keep track. common libraries import them at the beginning

In node-red, add a debug node - set to display the complete msg object - to the node feeding the mqtt-out node so we can see what the data looks like.

Actually I already know how the data looks in fact convert them to utf8 via:
print (str (message.payload.decode ("utf-8")))

my problem is to convert print to input for python. As if given from terminal entries.

I'll explain better if I define: input_text = input ("Tell me ...")

and I set the answer:

response = k.respond (input_text)

then by entering the text from the keyboard the system responds correctly while if instead of "input_text" I insert:

response = k.respond (str (message.payload.decode ("utf-8")) the system only displays the payload but does not return python response to me as if the final send was missing or did not see the data

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.