Last active
April 10, 2016 17:41
-
-
Save bachwehbi/898ec4941c5bdca03945fad21c971a36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Copyright (c) 2013-2016 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 json | |
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("test/onoff") | |
# Will be called for every received message | |
def on_message(client, data, msg): | |
# decodes the stringified MQTT message payload | |
bbt_message = json.loads(msg.payload) | |
# prints the data parameter - it is a boolean value | |
print(bbt_message.get("data")) | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
# Set the username to your channel token before calling connect | |
client.username_pw_set('token:YOUR_CHANNEL_TOKEN', None) | |
client.connect("mqtt.beebotte.com", 1883, 60) | |
client.loop_start() | |
# Stay up and running | |
while 1: | |
### To publish to the resource: | |
#client.publish("myChannel/onoff", json.dumps({'data': True}), 1) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment