Skip to content

Instantly share code, notes, and snippets.

@bachwehbi
Last active July 12, 2022 12:17
Show Gist options
  • Save bachwehbi/2a3fec727c8b879970b0 to your computer and use it in GitHub Desktop.
Save bachwehbi/2a3fec727c8b879970b0 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 <contact@beebotte.com>
# 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, flags, 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