-
Notifications
You must be signed in to change notification settings - Fork 53
Example: MQTT
MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.
Install MQTT server:
sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa && sudo apt-get update && sudo apt-get install mosquitto
Install MQTT and BlinkStick Python packages
sudo pip install mosquitto blinkstick
Create file listen.py and paste the following text:
#!/usr/bin/env python
from blinkstick import blinkstick
import sys
import mosquitto
def on_connect(mosq, obj, rc):
print("rc: "+str(rc))
def on_message(mosq, obj, msg):
sticks[0].set_color(hex="#"+str(msg.payload))
print(msg.topic+" "+str(msg.payload))
def on_publish(mosq, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mosq, obj, level, string):
print(string)
def main():
global sticks
print "BlinkStick MQTT script"
print ""
sticks = blinkstick.find_all()
if len(sticks) == 0:
print "BlinkStick not found..."
return 64
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("localhost", 1883, 60)
mqttc.subscribe("blinkstick/color", 0)
rc = 0
while rc == 0:
rc = mqttc.loop()
return 0
if __name__ == "__main__":
sys.exit(main())
Create file send.py and paste the following content:
#!/usr/bin/env python
import mosquitto
mqttc = mosquitto.Mosquitto()
mqttc.connect("localhost", 1883, 60)
mqttc.publish("blinkstick/color", "00ff00")
Make sure you have BlinkStick connected to the computer and start the listener:
python listen.py
Open up another terminal window and run the following command to set the color of BlinkStick to green (#00FF00):
python send.py
Alternatively you can set the color with the mosquitto_pub command line tool:
mosquitto_pub -h localhost -t blinkstick/colour -m 00ff00
Thanks to Andrew D Lindsay @andrewdlindsay for coming up with this idea!
This solution gives you a local version of remote control for BlinkStick. If you want to control your BlinkStick with your mobile device or share it with somebody else via Internet, you can find all information how to connect it to BlinkStick.com in the tutorials section.