MQTT (Message Queue Telemetry Transport)
Download all scripts: 9-mqtt.zip
91-adafruit_io_dotstar_w_urllib
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import standard python modules.
import json, time, urllib.request
import dotstar
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
ADAFRUIT_IO_USERNAME = 'peterknobloch' # See https://accounts.adafruit.com to find the username
ADAFRUIT_IO_KEY = '7d90915ebc154d02a881889b2afe7652' # See https://io.adafruit.com/settings
#
# Get last values from Adafruit IO via feed API
#
print('Requesting initial values from Adafruit IO...')
base_url = 'http://io.adafruit.com/api/v2/{}/feeds/{}'
response = urllib.request.urlopen(base_url.format(ADAFRUIT_IO_USERNAME, 'led-strip-control-toggle-button')) # Send the request
json_data = json.loads(response.read().decode('utf-8')) # Get the data in JSON format
is_on = json_data['last_value'] == 'ON'
print('led-strip-control-toggle-button = {}'.format(is_on))
response = urllib.request.urlopen(base_url.format(ADAFRUIT_IO_USERNAME, 'led-strip-control-color-picker')) # Send the request
json_data = json.loads(response.read().decode('utf-8')) # Get the data in JSON format
color = int('0x' + json_data['last_value'][1:], 16)
print('led-strip-control-color-picker = 0x{:x}'.format(color))
led_strip = dotstar.LED_Strip(10)
led_ids = None
if is_on:
led_strip.set_color(color, led_ids)
else:
led_strip.set_color(0, led_ids)
led_strip.display()
#
# Create an MQTT and connect it to Adafruit IO
#
print('Connecting to Adafruit IO...')
# Define callback functions which will be called when certain events happen
def connected(client):
client.subscribe('led-strip-control-toggle-button')
client.subscribe('led-strip-control-color-picker')
def disconnected(client):
sys.exit(1)
def message(client, feed_id, payload):
global color, is_on
print('Message received from feed_id \'{}\', payload \'{}\''.format(feed_id, payload))
if feed_id == 'led-strip-control-toggle-button':
is_on = (payload == 'ON')
if feed_id == 'led-strip-control-color-picker':
color = int('0x' + payload[1:], 16)
if is_on:
led_strip.set_color(color, led_ids)
else:
led_strip.set_color(0, led_ids)
led_strip.display()
# Create an MQTT client instance
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Setup the callback functions defined above
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
# Connect the client to the Adafruit IO MQTT server
client.connect()
client.loop_background()
try:
while True:
time.sleep(10)
except KeyboardInterrupt: # Control-C was pressed
pass
client.disconnect()
led_strip.cleanup()
print('\nBye, bye.')
"""
https://github.com/adafruit/io-client-python
"""
93-adafruit_io_dotstar_w_requests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import standard python modules.
import time, requests
import dotstar
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
ADAFRUIT_IO_USERNAME = 'peterknobloch' # See https://accounts.adafruit.com to find the username
ADAFRUIT_IO_KEY = '7d90915ebc154d02a881889b2afe7652' # See https://io.adafruit.com/settings
#
# Get last values from Adafruit IO via feed API
#
print('Requesting initial values from Adafruit IO...')
base_url = 'http://io.adafruit.com/api/v2/{}/feeds/{}'
response = requests.get(base_url.format(ADAFRUIT_IO_USERNAME, 'led-strip-control-toggle-button')) # Send the request
json_data = response.json() # Get the data in JSON format
is_on = json_data['last_value'] == 'ON'
print('led-strip-control-toggle-button = {}'.format(is_on))
response = requests.get(base_url.format(ADAFRUIT_IO_USERNAME, 'led-strip-control-color-picker')) # Send the request
json_data = response.json() # Get the data in JSON format
color = int('0x' + json_data['last_value'][1:], 16)
print('led-strip-control-color-picker = 0x{:x}'.format(color))
led_strip = dotstar.LED_Strip(10)
led_ids = None
if is_on:
led_strip.set_color(color, led_ids)
else:
led_strip.set_color(0, led_ids)
led_strip.display()
#
# Create an MQTT and connect it to Adafruit IO
#
print('Connecting to Adafruit IO...')
# Define callback functions which will be called when certain events happen
def connected(client):
client.subscribe('led-strip-control-toggle-button')
client.subscribe('led-strip-control-color-picker')
def disconnected(client):
sys.exit(1)
def message(client, feed_id, payload):
global color, is_on
print('Message received from feed_id \'{}\', payload \'{}\''.format(feed_id, payload))
if feed_id == 'led-strip-control-toggle-button':
is_on = (payload == 'ON')
if feed_id == 'led-strip-control-color-picker':
color = int('0x' + payload[1:], 16)
if is_on:
led_strip.set_color(color, led_ids)
else:
led_strip.set_color(0, led_ids)
led_strip.display()
# Create an MQTT client instance
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Setup the callback functions defined above
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
# Connect the client to the Adafruit IO MQTT server
client.connect()
client.loop_background()
try:
while True:
time.sleep(10)
except KeyboardInterrupt: # Control-C was pressed
pass
client.disconnect()
led_strip.cleanup()
print('\nBye, bye.')
"""
https://github.com/adafruit/io-client-python
"""