Versions Compared

Key

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

...



add interpreter module in pycharm

basic python code

http://www.steves-internet-guide.com/into-mqtt-python-client/


Code Block
titlebasic mqtt code
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, 1883broker_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



run the python script




...