Dim a LED or change PWM output of Raspberry Pi pin from anywhere with Cayenne IOT Dashboard.
This tutorial will show you how to control LED brigthness from Cayenne IOT Dashboard.
You will need the following parts for this tutorial:
You will need to connect the following pins to the LED and resistor:
To add Slider Controller widget, click on:
Then fill widget infos:
and click on Add Widget.
You can change widget infos later on widget.
Note: Channnel number must be same as "message.channel" in Python script.
You need to install this library for Python script. It is MQTT library to communicate RPİ and Cayenne IOT Broker.
This library can be installed using pip and pip3:
pip install cayenne-mqtt (for Python2)
pip3 install cayenne-mqtt (for Python3) >>>for this tutorial.
Note: In the script, you must modify your MQTT credentials and widget channel.
messsage.channel is channel of slider widget and it is a integer. message.value is value of received message from Cayenne and it is a string, we converted it to 2 digits float with round function and then divide by 100.
In gpiozero library, PWMLED.value changes between 0 and 1. 0 means Led is totally closed. 1 means Led is totally open.
import cayenne.client #Cayenne MQTT Client
from gpiozero import PWMLED led=PWMLED(17) #Declaring button pin 17# Cayenne authentication info. This should be obtained from the Cayenne Dashboard. MQTT_USERNAME = "YOUR MQTT USERNAME" MQTT_PASSWORD = "YOUR MQTT PASSWORD " MQTT_CLIENT_ID = "YOUR CLİENT ID"
# The callback for when a message is received from Cayenne. def on_message(message): print("message received: " + str(message)) if message.channel==2: #Dashboard Slider widget channel. They must be same. led.value=float(message.value)/100 #Setting led value to recevied slider value
client = cayenne.client.CayenneMQTTClient() client.on_message = on_message #When message recieved from Cayenne run on_message function client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
while True: client.loop()
This video shows final stage.