Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



test


add interpreter module in pycharm

Image Modified

basic python code
here
import time
import paho.mqtt.client as paho

#broker="broker.hivemq.com"
#broker="iot.eclipse.org"
broker="test.mosquitto.org"
broker_port = 1883

#define callback
def on_message(client, userdata, message):
    time.sleep(1)
    print("received message =",str(message.payload.decode("utf-8")))

client= paho.Client("client-001") #create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on")
######Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",broker)
client.connect(broker, broker_port)#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("jlk/test/led1")#subscribe
time.sleep(2)
print("publishing ")
client.publish("jlk/test/led1","on")#publish
time.sleep(4)
client.disconnect() #disconnect
client.loop_stop() #stop loop


test: publish from the script

1- connect and subscribe on the mobile App 

2- run the script


Mobile App:

1- connect

2- subscribe ( to receive)

https://itunes.apple.com/us/app/mqttool/id1085976398?mt=8


Click on the word "Connect"  which will change to "Disconnect" 

Image Added


Click on "Subscribe"  which will change to "Unsubscribe"


Image Added



run the python script