codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help me fix a problem


Hi 

I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test.

embedded.py

import paho.mqtt.client as mqtt
from mqtt import *

client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)

client.on_connect = on_connect
client.subscribe("topic/test")
client.on_subscribe = on_subscribe
print("Subscribing to topic", "topic/test")
client.on_message = on_message

client.loop_forever()

gui.py

import paho.mqtt.client as mqtt
from mqtt import *
import json

# This is the Publisher

client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
print("Publishing message (name: Hello world!) to topic", "topic/test")
client.publish("topic/test",json.dumps({"name": "Hello world!"}));
client.loop_forever();

mqtt.py

import logging
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
	print("Connecting to broker")
	# client.subscribe("topic/test")

def on_subscribe(client, userdata, mid, granted_qos):
	print("I've subscribed to topic")

def on_message(client, userdata, msg):
	print("message recieved= " + msg.payload.decode())
	# print("File which you want to import(with .py extension)")
	print("message topic=", msg.topic)
	print("message qos=", msg.qos)
	print("message retain flag=", msg.retain)
	
	if msg.payload[name] == "Hello world!":
		print("Yes!")

Regards 
Spencer