Skip to content

Instantly share code, notes, and snippets.

@malefs
Forked from bachwehbi/bbt_mqtt.py
Created January 10, 2017 14:33
Show Gist options
  • Select an option

  • Save malefs/0e3aa59cf0ffdb1a517e87341fb6bbbb to your computer and use it in GitHub Desktop.

Select an option

Save malefs/0e3aa59cf0ffdb1a517e87341fb6bbbb to your computer and use it in GitHub Desktop.
Simple example showing how to use MQTT in Beebotte. This code uses the Paho.mqtt Python client library.
#!/usr/bin/python
# Copyright (c) 2013-2014 Beebotte <[email protected]>
# This program is published under the MIT License (http://opensource.org/licenses/MIT).
############################################################
# This code uses the Beebotte API, you must have an account.
# You can register here: http://beebotte.com/register
#############################################################
import time
import paho.mqtt.client as mqtt
# Will be called upon reception of CONNACK response from the server.
def on_connect(client, data, rc):
client.subscribe("mychannel/myresource", 1)
def on_message(client, data, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# Set the username to 'token:CHANNEL_TOKEN' before calling connect
client.username_pw_set("token:YOUR_CHANNEL_TOKEN")
# Alternatively, set the username to your SECRET KEY
#client.username_pw_set('YOUR_SECRET_KEY')
client.connect("mqtt.beebotte.com", 1883, 60)
client.loop_start()
while 1:
# Publish a message every second
client.publish("mychannel/myresource", "Hello World", 1)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment